Ejemplo n.º 1
0
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;
    }
  }