]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Support UDP packets larger than 4 kB.
[cubemap] / acceptor.cpp
index 6f7ffa240a140584afd3ea620bae7c52cbc09893..2abfa5220064ec47efb8d40c41d3ad9ceed6a79d 100644 (file)
@@ -1,22 +1,22 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <signal.h>
+#include <assert.h>
 #include <errno.h>
-#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <poll.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
-#include <sys/poll.h>
 #include <sys/socket.h>
+#include <unistd.h>
 
 #include "acceptor.h"
+#include "log.h"
 #include "serverpool.h"
 #include "state.pb.h"
+#include "util.h"
 
 using namespace std;
 
 extern ServerPool *servers;
-extern volatile bool hupped;
 
 int create_server_socket(int port, SocketType socket_type)
 {
@@ -28,26 +28,26 @@ int create_server_socket(int port, SocketType socket_type)
                server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        }
        if (server_sock == -1) {
-               perror("socket");
+               log_perror("socket");
                exit(1);
        }
 
        int one = 1;
        if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
-               perror("setsockopt(SO_REUSEADDR)");
+               log_perror("setsockopt(SO_REUSEADDR)");
                exit(1);
        }
 
        // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
        int zero = 0;
        if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
-               perror("setsockopt(IPV6_V6ONLY)");
+               log_perror("setsockopt(IPV6_V6ONLY)");
                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)");
+               log_perror("ioctl(FIONBIO)");
                exit(1);
        }
 
@@ -57,13 +57,13 @@ int create_server_socket(int port, SocketType socket_type)
        addr.sin6_port = htons(port);
 
        if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
-               perror("bind");
+               log_perror("bind");
                exit(1);
        }
 
        if (socket_type == TCP_SOCKET) {
                if (listen(server_sock, 128) == -1) {
-                       perror("listen");
+                       log_perror("listen");
                        exit(1);
                }
        }
@@ -93,32 +93,13 @@ AcceptorProto Acceptor::serialize() const
 
 void Acceptor::close_socket()
 {
-       int ret;
-       do {
-               ret = close(server_sock);
-       } while (ret == -1 && errno == EINTR);
-
-       if (ret == -1) {
-               perror("close");
-       }
+       safe_close(server_sock);
 }
 
 void Acceptor::do_work()
 {
-       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 == EINTR)) {
-                       continue;
-               }
-               if (nfds == -1) {
-                       perror("poll");
-                       usleep(100000);
+       while (!should_stop()) {
+               if (!wait_for_activity(server_sock, POLLIN, NULL)) {
                        continue;
                }
 
@@ -131,7 +112,7 @@ void Acceptor::do_work()
                        continue;
                }
                if (sock == -1) {
-                       perror("accept");
+                       log_perror("accept");
                        usleep(100000);
                        continue;
                }
@@ -139,7 +120,7 @@ void Acceptor::do_work()
                // Set the socket as nonblocking.
                int one = 1;
                if (ioctl(sock, FIONBIO, &one) == -1) {
-                       perror("FIONBIO");
+                       log_perror("FIONBIO");
                        exit(1);
                }