]> git.sesse.net Git - cubemap/blobdiff - cubemap.cpp
Fix a few issues in the to_process() handling.
[cubemap] / cubemap.cpp
index d5cfa010a1443782c37614903ae8fdeed905d7e6..b471afb254adf483175d4ff631cc171275726797 100644 (file)
@@ -3,12 +3,11 @@
 #include <stdint.h>
 #include <assert.h>
 #include <arpa/inet.h>
-#include <curl/curl.h>
 #include <sys/socket.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
-#include <sys/epoll.h>
+#include <sys/poll.h>
 #include <signal.h>
 #include <errno.h>
 #include <ctype.h>
@@ -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;