| 64 | } |
| 65 | |
| 66 | void ODSocketServer::doTask(int timeoutMs) |
| 67 | { |
| 68 | mClockMainTask.restart(); |
| 69 | while((timeoutMs == 0) || |
| 70 | (timeoutMs > mClockMainTask.getElapsedTime().asMilliseconds())) |
| 71 | { |
| 72 | bool isSockReady; |
| 73 | if(timeoutMs != 0) |
| 74 | { |
| 75 | // We adapt the timeout so that the function returns after timeoutMs |
| 76 | // even if events occurred |
| 77 | int timeoutMsAdjusted = std::max(1, timeoutMs - mClockMainTask.getElapsedTime().asMilliseconds()); |
| 78 | isSockReady = mSockSelector.wait(sf::milliseconds(timeoutMsAdjusted)); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | isSockReady = mSockSelector.wait(sf::Time::Zero); |
| 83 | } |
| 84 | |
| 85 | // Check if a client tries to connect or to communicate |
| 86 | if(!isSockReady) |
| 87 | continue; |
| 88 | |
| 89 | if(mSockSelector.isReady(mSockListener)) |
| 90 | { |
| 91 | // New connection |
| 92 | ODSocketClient* newClient = notifyNewConnection(mSockListener); |
| 93 | if (newClient != nullptr) |
| 94 | { |
| 95 | // New connection |
| 96 | OD_LOG_INF("New client connected."); |
| 97 | // The server wants to keep the client |
| 98 | newClient->setSource(ODSocketClient::ODSource::network); |
| 99 | mSockSelector.add(newClient->getSockClient()); |
| 100 | mSockClients.push_back(newClient); |
| 101 | } |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | |
| 106 | for(std::vector<ODSocketClient*>::iterator it = mSockClients.begin(); it != mSockClients.end();) |
| 107 | { |
| 108 | ODSocketClient* client = *it; |
| 109 | if((mSockSelector.isReady(client->getSockClient())) && |
| 110 | (!notifyClientMessage(client))) |
| 111 | { |
| 112 | // The server wants to remove the client |
| 113 | it = mSockClients.erase(it); |
| 114 | mSockSelector.remove(client->getSockClient()); |
| 115 | client->disconnect(); |
| 116 | delete client; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | ++it; |
| 121 | } |
| 122 | } |
| 123 | } |
nothing calls this directly
no test coverage detected