+
+ // Process each client where its stream has new data,
+ // even if there was no socket activity.
+ for (size_t i = 0; i < streams.size(); ++i) {
+ vector<Client *> to_process;
+ swap(streams[i]->to_process, to_process);
+ for (size_t i = 0; i < to_process.size(); ++i) {
+ process_client(to_process[i]);
+ }
+ }
+
+ // Finally, go through each client to see if it's timed out
+ // in the READING_REQUEST state. (Seemingly there are clients
+ // that can hold sockets up for days at a time without sending
+ // anything at all.)
+ timespec timeout_time;
+ if (clock_gettime(CLOCK_MONOTONIC_COARSE, &timeout_time) == -1) {
+ log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
+ continue;
+ }
+ timeout_time.tv_sec -= REQUEST_READ_TIMEOUT_SEC;
+ while (!clients_ordered_by_connect_time.empty()) {
+ pair<timespec, int> &connect_time_and_fd = clients_ordered_by_connect_time.front();
+
+ // See if we have reached the end of clients to process.
+ if (is_earlier(timeout_time, connect_time_and_fd.first)) {
+ break;
+ }
+
+ // If this client doesn't exist anymore, just ignore it
+ // (it was deleted earlier).
+ std::map<int, Client>::iterator client_it = clients.find(connect_time_and_fd.second);
+ if (client_it == clients.end()) {
+ clients_ordered_by_connect_time.pop();
+ continue;
+ }
+ Client *client = &client_it->second;
+ if (!is_equal(client->connect_time, connect_time_and_fd.first)) {
+ // Another client has taken this fd in the meantime.
+ clients_ordered_by_connect_time.pop();
+ continue;
+ }
+
+ if (client->state != Client::READING_REQUEST) {
+ // Only READING_REQUEST can time out.
+ clients_ordered_by_connect_time.pop();
+ continue;
+ }
+
+ // OK, it timed out.
+ close_client(client);
+ clients_ordered_by_connect_time.pop();
+ }