void Server::addConnectionSockets(SocketSet &sockSet) { SocketServer::addConnectionSockets(sockSet); for (iterator it = begin(); it != end(); it++) { Connection &con = *it->castPtr<Connection>(); switch (con.getState()) { case Connection::READING_HEADER: case Connection::READING_DATA: sockSet.add(con.getSocket(), SocketSet::READ); break; case Connection::WRITING_HEADER: case Connection::WRITING_DATA: if (!con.getContext() || con.getContext()->isReady()) sockSet.add(con.getSocket(), SocketSet::WRITE); break; default: break; } } }
void SocketServer::service() { // Add listeners SocketSet sockSet; for (unsigned i = 0; i < ports.size(); i++) sockSet.add(ports[i]->socket, SocketSet::READ); // Add others addConnectionSockets(sockSet); if (sockSet.select(0.1) || connectionsReady()) { // Process connections processConnections(sockSet); // Accept new connections for (unsigned i = 0; i < ports.size() && !shouldShutdown(); i++) { try { Socket &socket = ports[i]->socket; if (!sockSet.isSet(socket, SocketSet::READ)) continue; IPAddress clientIP; while (true) { SmartPointer<Socket> client = socket.accept(&clientIP); if (client.isNull()) break; // Check access if (!allow(clientIP.getIP())) { LOG_INFO(3, "Server access denied for " << clientIP); continue; } SocketConnectionPtr con = createConnection(client, clientIP); client = 0; // Release socket pointer if (con.isNull()) LOG_INFO(3, "Dropped server connection on " << ports[i]->ip << " from " << clientIP); else { connections.push_back(con); LOG_INFO(3, "Server connection id=" << con->getID() << " on " << ports[i]->ip << " from " << IPAddress(clientIP.getIP())); } } } CBANG_CATCH_ERROR; } }
void Waitable::wait(int timeout) { SocketSet set; set.add(this); set.wait(timeout); }