| 63 | |
| 64 | |
| 65 | void SocketReactor::run() |
| 66 | { |
| 67 | _pThread = Thread::current(); |
| 68 | |
| 69 | Socket::SocketList readable; |
| 70 | Socket::SocketList writable; |
| 71 | Socket::SocketList except; |
| 72 | |
| 73 | while (!_stop) |
| 74 | { |
| 75 | try |
| 76 | { |
| 77 | readable.clear(); |
| 78 | writable.clear(); |
| 79 | except.clear(); |
| 80 | int nSockets = 0; |
| 81 | { |
| 82 | FastMutex::ScopedLock lock(_mutex); |
| 83 | for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it) |
| 84 | { |
| 85 | if (it->second->accepts(_pReadableNotification)) |
| 86 | { |
| 87 | readable.push_back(it->first); |
| 88 | nSockets++; |
| 89 | } |
| 90 | if (it->second->accepts(_pWritableNotification)) |
| 91 | { |
| 92 | writable.push_back(it->first); |
| 93 | nSockets++; |
| 94 | } |
| 95 | if (it->second->accepts(_pErrorNotification)) |
| 96 | { |
| 97 | except.push_back(it->first); |
| 98 | nSockets++; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | if (nSockets == 0) |
| 103 | { |
| 104 | onIdle(); |
| 105 | Thread::trySleep(static_cast<long>(_timeout.totalMilliseconds())); |
| 106 | } |
| 107 | else if (Socket::select(readable, writable, except, _timeout)) |
| 108 | { |
| 109 | onBusy(); |
| 110 | |
| 111 | for (Socket::SocketList::iterator it = readable.begin(); it != readable.end(); ++it) |
| 112 | dispatch(*it, _pReadableNotification); |
| 113 | for (Socket::SocketList::iterator it = writable.begin(); it != writable.end(); ++it) |
| 114 | dispatch(*it, _pWritableNotification); |
| 115 | for (Socket::SocketList::iterator it = except.begin(); it != except.end(); ++it) |
| 116 | dispatch(*it, _pErrorNotification); |
| 117 | } |
| 118 | else onTimeout(); |
| 119 | } |
| 120 | catch (Exception& exc) |
| 121 | { |
| 122 | ErrorHandler::handle(exc); |