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 Server::processConnections(SocketSet &sockSet) { if (!initialized) THROW("HTTP::Server not initialized"); // Save current thread ID and restore it before leaving this scope struct SmartThreadID { unsigned threadID; SmartThreadID() : threadID(Logger::instance().getThreadID()) {} ~SmartThreadID() {Logger::instance().setThreadID(threadID);} } smartThreadID; for (iterator it = begin(); it != end() && !shouldShutdown(); it++) { // Set thread ID = connection ID Logger::instance().setThreadID((*it)->getID()); processConnection(*it, sockSet.isSet((*it)->getSocket())); } }