MysqlClientHandle* getHandleByID(connHandleID handleID) { mlock.lock(); if (!idmaps.empty()) { handlesIDMap::iterator it = idmaps.find(handleID); if (it != idmaps.end()) { mlock.unlock(); return (*it).second; } } /* for(handlesPool::iterator it = handles.begin(); it != handles.end(); ++it) { MysqlClientHandle *tempHandle=(*it).second; if(tempHandle->getID()==handleID) { mlock.unlock(); return tempHandle; } }*/ mlock.unlock(); return NULL; }
~zMysqlDBConnPool() { mlock.lock(); if (!handles.empty()) { for(handlesPool::iterator it = handles.begin(); it != handles.end(); ++it) { MysqlClientHandle *tempHandle=(*it).second; SAFE_DELETE(tempHandle); } } handles.clear(); urls.clear(); hashCode=defaultHashCode; mlock.unlock(); }
bool putURL(unsigned int hashcode,const char *url,bool supportTransactions) { UrlInfo ui(hashcode,url,supportTransactions); MysqlClientHandle *handle=new MysqlClientHandle(ui); if (handle==NULL) return false; if(handle->initHandle()) { mlock.lock(); handles.insert(handlesPool::value_type(hashcode, handle)); urls.insert(urlsPool::value_type(hashcode, ui)); idmaps.insert(handlesIDMap::value_type(handle->getID(), handle)); mlock.unlock(); return true; } else { SAFE_DELETE(handle); return false; } }
/** * \brief 初始化网络服务器程序 * * 实现了虚函数<code>zService::init</code> * * \return 是否成功 */ bool SessionService::init() { //NFilterModuleArray::const_iterator pIterator; if (!country_info.init()) { Zebra::logger->error("加载界域信息失败!"); } for(int i=0; i<10; i++) countryLevel[i]=0; uncheckCountryProcess=false; dbConnPool = zDBConnPool::newInstance(NULL); if (NULL == dbConnPool || !dbConnPool->putURL(0,Zebra::global["mysql"].c_str(),false)) { return false; } metaData = DBMetaData::newInstance(""); if (NULL == metaData || !metaData->init(Zebra::global["mysql"])) { return false; } //初始化连接线程池 int state = state_none; Zebra::to_lower(Zebra::global["threadPoolState"]); if ("repair" == Zebra::global["threadPoolState"] || "maintain" == Zebra::global["threadPoolState"]) state = state_maintain; taskPool = new zTCPTaskPool(2048, state); if (NULL == taskPool || !taskPool->init()) return false; strncpy(pstrIP,zSocket::getIPByIfName(Zebra::global["ifname"].c_str()),MAX_IP_LENGTH - 1); //Zebra::logger->debug("%s",pstrIP); // 初始化对战系统相关参数 dare_active_time = atoi(Zebra::global["dare_active_time"].c_str()); if (dare_active_time<=0) { dare_active_time = 3600; // 默认3600秒 } dare_ready_time = atoi(Zebra::global["dare_ready_time"].c_str()); if (dare_ready_time<=0) { dare_ready_time = 300; // 默认300秒 } dare_need_gold = atoi(Zebra::global["dare_need_gold"].c_str()); if (dare_need_gold<=0) { dare_need_gold = 5000; // 五十两 } dare_winner_gold = atoi(Zebra::global["dare_winner_gold"].c_str()); if (dare_winner_gold<=0) { dare_winner_gold = 10000; // 奖励一百两 } dare_need_gold_sept = atoi(Zebra::global["dare_need_gold_sept"].c_str()); if (dare_need_gold_sept<=0) { //dare_need_gold_sept = 1000; // 十两 dare_need_gold_sept = 0; } dare_winner_gold_sept = atoi(Zebra::global["dare_winner_gold_sept"].c_str()); if (dare_winner_gold_sept<=0) { //dare_winner_gold_sept = 2000; // 奖励二十两 dare_winner_gold_sept = 0; } // 初始化竞赛系统相关参数 quiz_active_time = atoi(Zebra::global["quiz_active_time"].c_str()); if (quiz_active_time<=0) { quiz_active_time = 60; // 默认60题 } quiz_ready_time = atoi(Zebra::global["quiz_ready_time"].c_str()); if (quiz_ready_time<=0) { quiz_ready_time = 10; // 默认10秒 } if (!zSubNetService::init()) { return false; } const Cmd::Super::ServerEntry *serverEntry = NULL; //连接档案服务器 serverEntry = getServerEntryByType(RECORDSERVER); if (NULL == serverEntry) { Zebra::logger->error("can not find recordserver !!!"); return false; } recordClient = new RecordClient("RecordServer",serverEntry->pstrIP,serverEntry->wdPort); if (NULL == recordClient) { Zebra::logger->error("new RecordClient ERROR!!!"); return false; } if (!recordClient->connectToRecordServer()) { Zebra::logger->error("SessionServer connect record ERROR!!!"); return false; } if (recordClient->start()) { Zebra::logger->info("初始化档案服务器模块成功"); //Zebra::logger->info("初始化档案服务器模块(%s:%d)成功",serverEntry->pstrIP,serverEntry->wdPort); } if (!UserSessionManager::getInstance()->init()) return false; if (!SceneSessionManager::getInstance()->init()) return false; #if 0 if (!CSeptM::getMe().init()) return false; if (!CUnionM::getMe().init()) return false; if (!CSchoolM::getMe().init()) return false; if (!COfflineMessage::init()) return false; if (!CDareM::getMe().init()) return false; if (!CSubjectM::getMe().init()) { Zebra::logger->error("初始化题库数据失败"); return false; } if (!CQuizM::getMe().init()) return false; if (!CNpcDareM::getMe().init()) return false; if (!CCityM::getMe().init()) { Zebra::logger->error("初始化CITY数据失败"); return false; } if (!CCountryM::getMe().init()) { Zebra::logger->error("初始化COUNTRY数据失败"); return false; } if (!CDareRecordM::getMe().init()) { Zebra::logger->error("初始化DARERECORD数据失败"); return false; } #endif if (!CSortM::getMe().init()) { Zebra::logger->error("初始化角色排名系统失败"); return false; } #if 0 if (!CVoteM::getMe().init()) { Zebra::logger->error("初始化投票系统失败"); return false; } if (!CArmyM::getMe().init()) { Zebra::logger->error("初始化军队数据失败"); return false; } if (!CGemM::getMe().init()) { Zebra::logger->error("初始化护宝数据失败"); return false; } Gift::getMe().init(); if (!CAllyM::getMe().init()) { Zebra::logger->error("初始化国家联盟数据失败"); return false; } MailService::getMe().loadNewMail(); EmperorForbid::getMe(); #endif xml::Configs::load_session(); //aotu parse xml if (SessionTimeTick::getInstance().start()) Zebra::logger->info("init SessionTimeTick OK!!!"); #if 0 loadFilter(g_nFMA,"SessionServer_*.dll"); //init for(pIterator=g_nFMA.begin(); pIterator != g_nFMA.end();pIterator++) { if (NULL != pIterator->filter_init) { pIterator->filter_init(); } } //sky 把跳场景队员临时列表放在这里初始化下 g_MoveSceneMemberMapLock.lock(); MoveSceneMemberMap.clear(); g_MoveSceneMemberMapLock.unlock(); #endif return true; }
MysqlClientHandle* getHandleByHashcode(unsigned int hashcode) { while(true) { MysqlClientHandle* invalidHandle=NULL; mlock.lock(); __gnu_cxx::pair<handlesPool::iterator,handlesPool::iterator> hps = handles.equal_range(hashcode); for(handlesPool::iterator it = hps.first; it != hps.second; ++it) { MysqlClientHandle* tempHandle=(*it).second; switch(tempHandle->state) { case MYSQLCLIENT_HANDLE_INVALID: //handle无效,如果没有找到可用的,需要初始化为可用 if(invalidHandle==NULL) invalidHandle=tempHandle; break; case MYSQLCLIENT_HANDLE_VALID: //handle可用 if(tempHandle->setHandle()) { mlock.unlock(); return tempHandle; } break; case MYSQLCLIENT_HANDLE_USED: //handle正在使用中 if(tempHandle->useTime.elapse()>10) { //使用时间过长,是否程序存在问题 logger->warn("The handle(%u) timeout %lus by thread %u", tempHandle->getID(),tempHandle->useTime.elapse(),tempHandle->getedThread); logger->warn("The handle sql is : %s" , tempHandle->my_sql.c_str()); } break; } } if(urls.find(hashcode)==urls.end() || urls[hashcode].url.size()==0) { mlock.unlock(); return NULL; } if(invalidHandle!=NULL) { if(invalidHandle->initHandle()) { if(invalidHandle->setHandle()) { mlock.unlock(); return invalidHandle; } } } else if(handles.count(hashcode) < maxHandleBuf) { MysqlClientHandle *handle=new MysqlClientHandle(urls[hashcode]); if (NULL==handle) { mlock.unlock(); logger->fatal("not enough memory to allocate handle"); return handle; } if(handle->initHandle()) { handles.insert(handlesPool::value_type(hashcode, handle)); idmaps.insert(handlesIDMap::value_type(handle->getID(), handle)); if(handle->setHandle()) { mlock.unlock(); return handle; } } } mlock.unlock(); logger->debug("usleep(10000) with getHandleByHashcode"); usleep(10000); } }