]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Fix an error where clients in asleep state would hang after a HUP.
[cubemap] / server.cpp
index 54e6b5e804d9959632b1ef70df6a53fee7f52fac..eeab64bb64fbf7bb790d89b14150e4eb97f42251 100644 (file)
@@ -201,13 +201,15 @@ void Server::add_client(int sock)
 
        // Start listening on data from this socket.
        epoll_event ev;
-       ev.events = EPOLLIN | EPOLLRDHUP;
+       ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
        ev.data.u64 = 0;  // Keep Valgrind happy.
        ev.data.fd = sock;
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
                perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
        }
+
+       process_client(&clients[sock]);
 }
 
 void Server::add_client_from_serialized(const ClientProto &client)
@@ -215,6 +217,7 @@ void Server::add_client_from_serialized(const ClientProto &client)
        MutexLock lock(&mutex);
        Stream *stream = find_stream(client.stream_id());
        clients.insert(make_pair(client.sock(), Client(client, stream)));
+       Client *client_ptr = &clients[client.sock()];
 
        // Start listening on data from this socket.
        epoll_event ev;
@@ -232,7 +235,12 @@ void Server::add_client_from_serialized(const ClientProto &client)
                exit(1);
        }
 
-       process_client(&clients[client.sock()]);
+       if (client_ptr->state == Client::SENDING_DATA && 
+           client_ptr->bytes_sent == client_ptr->stream->data_size) {
+               put_client_to_sleep(client_ptr);
+       } else {
+               process_client(client_ptr);
+       }
 }
 
 void Server::add_stream(const string &stream_id)
@@ -404,7 +412,7 @@ sending_header_or_error_again:
                // This is postcondition #3.
                client->state = Client::SENDING_DATA;
                client->bytes_sent = client->stream->data_size;
-               sleeping_clients.push_back(client);
+               put_client_to_sleep(client);
                return;
        }
        case Client::SENDING_DATA: {
@@ -412,6 +420,9 @@ sending_header_or_error_again:
                // but resync will be the mux's problem.
                const Stream *stream = client->stream;
                size_t bytes_to_send = stream->data_size - client->bytes_sent;
+               if (bytes_to_send == 0) {
+                       return;
+               }
                if (bytes_to_send > BACKLOG_SIZE) {
                        fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
                                client->sock,