int XSocketMgr::Query() { int nRetCode = 0; time_t nTimeNow = time(NULL); int nIOCount = 0; XListenNode* pListenNode = m_ListenHeadNode.pNext; while (pListenNode) { if (!pListenNode->bUsrClose) { nRetCode = ProcessNewSocket(pListenNode, nTimeNow); if (nRetCode > 0) nIOCount++; } pListenNode = pListenNode->pNext; } XStreamNode* pStreamNode = m_StreamHeadNode.pNext; while (pStreamNode) { if (pStreamNode->bComplete && !pStreamNode->bUsrClose) { ProcessReceive(pStreamNode, nTimeNow); nIOCount++; } pStreamNode = pStreamNode->pNext; } if (nTimeNow > m_uNextCycleProcess) { ProcessCycle(nTimeNow); m_uNextCycleProcess = nTimeNow; } // Windows由于IOCP机制问题,这里只是近似的模拟epoll,kqueue的阻塞 if (nIOCount == 0) { ThreadSleep(m_nQueryTimeout); } return nIOCount; }
void CALLBACK CMainFrame::NotifyProc(LPVOID lpParam, ClientContext *pContext, UINT nCode) { try { CMainFrame* pFrame = (CMainFrame*) lpParam; CString str; // 对g_pConnectView 进行初始化 g_pConnectView = (CPcView *)((CGh0stApp *)AfxGetApp())->m_pConnectView; // g_pConnectView还没创建,这情况不会发生 if (((CGh0stApp *)AfxGetApp())->m_pConnectView == NULL) return; g_pConnectView->m_iocpServer = m_iocpServer; //str.Format("S: %.2f kb/s R: %.2f kb/s", (float)m_iocpServer->m_nSendKbps / 1024, (float)m_iocpServer->m_nRecvKbps / 1024); str.Format("发送: %.2f kb/s 接收: %.2f kb/s", (float)m_iocpServer->m_nSendKbps / 1024, (float)m_iocpServer->m_nRecvKbps / 1024); g_pFrame->m_wndStatusBar.SetPaneText(1, str); switch (nCode) { case NC_CLIENT_CONNECT: break; case NC_CLIENT_DISCONNECT: g_pConnectView->PostMessage(WM_REMOVEFROMLIST, 0, (LPARAM)pContext); break; case NC_TRANSMIT: break; case NC_RECEIVE: ProcessReceive(pContext); break; case NC_RECEIVE_COMPLETE: ProcessReceiveComplete(pContext); break; } } catch(...) {} }