void CNet::DealAcceptEvent(struct iocp_event * pEvent) { ASSERT(m_szCallAddress[CALL_REMOTE_CONNECTED]); if (m_szCallAddress[CALL_REMOTE_CONNECTED] != NULL) { if (ERROR_SUCCESS == pEvent->nerron) { s32 nConnectID = m_ConnectPool.CreateID(); m_ConnectPool[nConnectID]->s = pEvent->s; SafeSprintf(m_ConnectPool[nConnectID]->szIP, sizeof(m_ConnectPool[nConnectID]->szIP), "%s", inet_ntoa(pEvent->remote.sin_addr)); m_ConnectPool[nConnectID]->nPort = ntohs(pEvent->remote.sin_port); m_szCallAddress[CALL_REMOTE_CONNECTED](nConnectID, pEvent->p, 0); pEvent->event = EVENT_ASYNC_RECV; pEvent->wbuf.buf = pEvent->buff; pEvent->wbuf.len = sizeof(pEvent->buff); m_ConnectPool[nConnectID]->pContext = pEvent->p; pEvent->p = m_ConnectPool[nConnectID]; s32 err; if (ERROR_NO_ERROR != async_recv(pEvent, &err, pEvent->p)) { NET_ERROR("async_recv error %d code %d", err, pEvent->nerron); SafeShutdwon(pEvent, CSD_BOTH); return; } } else { safe_close(pEvent); } } }
void CNet::DealRecvEvent(struct iocp_event * pEvent) { ASSERT(m_szCallAddress[CALL_CONNECTION_BREAK]); ASSERT(m_szCallAddress[CALL_RECV_DATA]); CConnection * pCConnection = (CConnection *) (pEvent->p); if (ERROR_SUCCESS == pEvent->nerron) { if (0 == pEvent->ioBytes) { m_szCallAddress[CALL_CONNECTION_BREAK](m_ConnectPool.QueryID(pCConnection), pCConnection->pContext, 0); SafeShutdwon(pEvent, CSD_RECV); return; } else { m_szCallAddress[CALL_RECV_DATA](m_ConnectPool.QueryID(pCConnection), pEvent->wbuf.buf, pEvent->ioBytes); pEvent->event = EVENT_ASYNC_RECV; pEvent->wbuf.buf = pEvent->buff; pEvent->wbuf.len = sizeof(pEvent->buff); s32 err; if (ERROR_NO_ERROR != async_recv(pEvent, &err, pEvent->p)) { m_szCallAddress[CALL_CONNECTION_BREAK](m_ConnectPool.QueryID(pCConnection), pCConnection->pContext, 0); SafeShutdwon(pEvent, CSD_RECV); return; } } } else { m_szCallAddress[CALL_CONNECTION_BREAK](m_ConnectPool.QueryID(pCConnection), pCConnection->pContext, 0); SafeShutdwon(pEvent, CSD_RECV); } }
//!Main MPI loop-only one can exist within a program. void mpi_controller::send_recv(){ static int ms_sleep_for=100; //don't combine into one big statement to ensure compiler doesn't optimize away calls bool work_done = async_recv(); work_done = handle_events() | work_done; work_done = async_send() | work_done; //If there was nothing to send or recieve, sleep for a few seconds and try again //This frees up the core for something else if(!work_done){ ms_sleep_for = std::min(int(ms_sleep_for*1.5), 5000); std::this_thread::sleep_for(std::chrono::milliseconds(ms_sleep_for)); } else{ ms_sleep_for=100; } }