/************************************************************************* Agent lord working thread *************************************************************************/ unsigned int System::lordWorkingThread(void) { while(true) { //---------------------------------------- // wait cmd sign ::WaitForSingleObject(m_hLordCmdSign, INFINITE); // enter critical section ::EnterCriticalSection(&m_csLordCmdQueue); if(m_qLordCmd.empty()) { // leave critical section ::LeaveCriticalSection(&m_csLordCmdQueue); continue; } // get cmd LordCmd theCmd = m_qLordCmd.front(); m_qLordCmd.pop(); if(m_qLordCmd.empty()) ::ResetEvent(m_hLordCmdSign); // leave critical section ::LeaveCriticalSection(&m_csLordCmdQueue); //---------------------------------------- //Process cmd switch(theCmd.type) { // create agents... case LCT_CreateAgent: { bool bStartNow = LOWORD(theCmd.nParam2)==1; int nCounts = (int)HIWORD(theCmd.nParam2); AgentCreateParam* pCreateparam = (AgentCreateParam*)(INT_PTR)theCmd.nParam1; // 保存登录用户名 char buf[128]; int iParam = -1; std::string sParam; for( int j=0; j<(int)pCreateparam->vParams.size(); j++ ) { if( pCreateparam->vParams[j].first == "AccountName" ) { iParam = j; sParam = pCreateparam->vParams[j].second; break; } } // 形如000X时,特殊处理 int iNum = atoi( sParam.substr( sParam.length()-4 ).c_str() ); for( int i=0; i<nCounts; i++ ) { if( i == 1 ) { sParam = sParam.substr( 0, sParam.length()-4 ); } // 一组多用户命名规则 if( i > 0 && iParam >= 0 ) { sprintf( buf, "*****@*****.**", i + iNum ); pCreateparam->vParams[iParam].second = sParam + buf; } else { sprintf( buf, "@game.hopecool.com" ); pCreateparam->vParams[iParam].second = sParam + buf; } Agent* pAgent = m_pAgentManager->createAgent(*pCreateparam); if(bStartNow) { if( pAgent->start() == false ) m_hUnStartAgents.push_back( pAgent->getID() ); ::Sleep(AGENT_AROUSE_TIME_INTERVAL); } ::SendMessage(m_hNotifyWnd, WMA_AGENT_NUM, m_pAgentManager->getAgentNum(), 0); } //delete org createparam delete pCreateparam; pCreateparam=0; if( m_hUnStartAgents.size() > 0 ) pushLordCommand( LCT_ArouseStartFail ); } break; // arouse agent... case LCT_ArouseAll: { //Start all agent AgentManager::AgentIterator agent = m_pAgentManager->getAgentIterator(); while (!agent.isAtEnd()) { agent.getCurrentValue()->start(); ++agent; ::Sleep(AGENT_AROUSE_TIME_INTERVAL); } if( m_hUnStartAgents.size() > 0 ) pushLordCommand( LCT_ArouseStartFail ); } break; // stop all agent... case LCT_StopAll: { // resume all first ::SetEvent(m_hResumeHandle); // send stop sign int nAgentNum = m_pAgentManager->getAgentNum(); int nAgentTeam = nAgentNum / MAXIMUM_WAIT_OBJECTS + 1; HANDLE (*hAllWorkThread)[MAXIMUM_WAIT_OBJECTS] = new HANDLE[nAgentTeam][MAXIMUM_WAIT_OBJECTS]; AgentManager::AgentIterator agent = m_pAgentManager->getAgentIterator(); int nIndex = 0; int nOffset = 0; while (!agent.isAtEnd()) { hAllWorkThread[nOffset][nIndex++] = agent.getCurrentValue()->getWorkThread(); if( nIndex == MAXIMUM_WAIT_OBJECTS ) { nIndex = 0; nOffset ++; } agent.getCurrentValue()->stop(); ++agent; } //wait for all thread stop(10 sec)... for( int i = 0; i < nAgentTeam; i ++ ) { int nWait = i == nAgentTeam - 1 ? nIndex : MAXIMUM_WAIT_OBJECTS; if( nWait == 0 ) break; if(WAIT_TIMEOUT == ::WaitForMultipleObjects(nWait, hAllWorkThread[i], TRUE, 10*1000)) { //TODO ... // Terminate all thread! int a=0; } } delete [] hAllWorkThread; setState(Ready); break; } break; case LCT_ArouseStartFail: { for( std::list<int>::iterator it = m_hUnStartAgents.begin(); it != m_hUnStartAgents.end(); ) { Agent* pAgent = m_pAgentManager->findAgent( *it ); if( pAgent ) { if( pAgent->start() == true ) { m_hUnStartAgents.pop_front(); } else { ++ it; } Sleep( 100 ); } else { m_hUnStartAgents.pop_front(); } } if( m_hUnStartAgents.size() > 0 ) pushLordCommand( LCT_ArouseStartFail ); } break; default: break; } }; return 0; }
bool Agent::operator==(Agent &other) const { return (this->ID == other.getID()); }