int main(int argc, char *argv[]) { signal(SIGCHLD, sigchld_hander); CListener listener = CListener(CHandler::Handler); int ret = listener.Listen(9999); if( ret < 0 ) { return 1; } listener.Run(); return 0; }
TInt32 CConnectionMgr::Run(TUInt32 runCnt) { TInt32 usedCnt = 0; CListener *pList = m_pListeners; m_nTimeNow = time(NULL); while (pList) { usedCnt += pList->Run(runCnt-usedCnt); if (usedCnt >= runCnt) { return usedCnt; } pList = pList->GetNext(); } usedCnt += m_connector.Run((runCnt-usedCnt)); TIOEvent *pEvent = m_netEventQueues.GetNetEvent(); while(pEvent) { TIOEvent event = *pEvent; m_netEventQueues.ConfirmHandleNetEvent(pEvent); CConnection *pConnection = m_conncectionPool.GetConectionByIdx(pEvent->m_connectionIdx); if (pConnection) { //先确认处理了消息,这样网络层如果有新事件,也会发事件通知. pConnection->OnAppRecved(); //由应用层调用,如果返回-1,则表示需要释放连接,把链接close ,并且放回connectionPool TInt32 ret = pConnection->AppRoutine(m_pBuff,m_buffSize); //不能先处理,再确认,因为,可能在处理完了,即上句执行完了,网络层又有了新事件,就会丢失. //pConnection->OnAppRecved(); pConnection->OnAppHandled(); if (ret < SUCCESS) { //pConnection->CloseConnection(); m_conncectionPool.ReleaseItem(pConnection); } } ++ usedCnt; if(usedCnt > runCnt) { break; } pEvent = m_netEventQueues.GetNetEvent(); } return usedCnt; }