void SocketReactor::run() { _pThread = Thread::current(); Socket::SocketList readable; Socket::SocketList writable; Socket::SocketList except; while (!_stop) { try { readable.clear(); writable.clear(); except.clear(); int nSockets = 0; { FastMutex::ScopedLock lock(_mutex); for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it) { if (it->second->accepts(_pReadableNotification)) { readable.push_back(it->first); nSockets++; } if (it->second->accepts(_pWritableNotification)) { writable.push_back(it->first); nSockets++; } if (it->second->accepts(_pErrorNotification)) { except.push_back(it->first); nSockets++; } } } if (nSockets == 0) { onIdle(); Thread::trySleep(static_cast<long>(_timeout.totalMilliseconds())); } else if (Socket::select(readable, writable, except, _timeout)) { onBusy(); for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it) dispatch(*it, _pReadableNotification); for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it) dispatch(*it, _pWritableNotification); for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it) dispatch(*it, _pErrorNotification); } else onTimeout(); } catch (Exception& exc) { ErrorHandler::handle(exc); } catch (std::exception& exc) { ErrorHandler::handle(exc); } catch (...) { ErrorHandler::handle(); } } onShutdown(); }
//---------------------------------------------------------------------------- void ServerPoll::_ReadWriteRun() { Socket::SocketList readable; Socket::SocketList writable; Socket::SocketList except; std::map<px2_socket_t, ClientContext*> contextMap; int nSockets = 0; { ScopedCS cs(&mContextMapMutex); std::map<unsigned int, ClientContext *>::iterator it = mClientMap.begin(); for (; it != mClientMap.end(); it++) { contextMap[it->second->TheSocket.GetSocket()] = it->second; readable.push_back(it->second->TheSocket); writable.push_back(it->second->TheSocket); except.push_back(it->second->TheSocket); nSockets++; } } Timespan timeout(250000); if (nSockets == 0) { // odle /*_*/ } else if (Socket::Select(readable, writable, except, timeout)) { // busy for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it) { ClientContext *context = contextMap[(*it).GetSocket()]; if (_OnRead(context) < 0) { DisconnectClient(context->ClientID); } } for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it) { ClientContext *context = contextMap[(*it).GetSocket()]; if (_OnWrite(context) < 0) { DisconnectClient(context->ClientID); } } for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it) { } } else { // time out /*_*/ } }