X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=cubemap.cpp;h=b471afb254adf483175d4ff631cc171275726797;hp=d5cfa010a1443782c37614903ae8fdeed905d7e6;hb=fbfb955ee7233030357ec32c0d613f9279700d70;hpb=1db0474e2a914bfc31014c067d9af24f87037784 diff --git a/cubemap.cpp b/cubemap.cpp index d5cfa01..b471afb 100644 --- a/cubemap.cpp +++ b/cubemap.cpp @@ -3,12 +3,11 @@ #include #include #include -#include #include #include #include #include -#include +#include #include #include #include @@ -58,6 +57,12 @@ int create_server_socket(int port) exit(1); } + // Set as non-blocking, so the acceptor thread can notice that we want to shut it down. + if (ioctl(server_sock, FIONBIO, &one) == -1) { + perror("ioctl(FIONBIO)"); + exit(1); + } + sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; @@ -79,8 +84,23 @@ int create_server_socket(int port) void *acceptor_thread_run(void *arg) { int server_sock = int(intptr_t(arg)); - int num_accepted = 0; - for ( ;; ) { + while (!hupped) { + // Since we are non-blocking, we need to wait for the right state first. + // Wait up to 50 ms, then check hupped. + pollfd pfd; + pfd.fd = server_sock; + pfd.events = POLLIN; + + int nfds = poll(&pfd, 1, 50); + if (nfds == 0 || (nfds == -1 && errno == EAGAIN)) { + continue; + } + if (nfds == -1) { + perror("poll"); + usleep(100000); + continue; + } + sockaddr_in6 addr; socklen_t addrlen = sizeof(addr); @@ -91,7 +111,8 @@ void *acceptor_thread_run(void *arg) } if (sock == -1) { perror("accept"); - exit(1); + usleep(100000); + continue; } // Set the socket as nonblocking. @@ -103,8 +124,8 @@ void *acceptor_thread_run(void *arg) // Pick a server, round-robin, and hand over the socket to it. servers->add_client(sock); - ++num_accepted; } + return NULL; } // Serialize the given state to a file descriptor, and return the (still open) @@ -283,7 +304,12 @@ int main(int argc, char **argv) // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec. for (size_t i = 0; i < inputs.size(); ++i) { inputs[i]->stop(); - delete inputs[i]; // TODO: serialize instead of using libcurl. + delete inputs[i]; // TODO: Serialize. + } + + if (pthread_join(acceptor_thread, NULL) == -1) { + perror("pthread_join"); + exit(1); } CubemapStateProto state;