]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Enable TCP_CORK if available.
[cubemap] / acceptor.cpp
index 87bf47b83ea1b1c09616fbd19c0410c6834bfc20..32a89fa7670d7221b54e7c98ffa0f8ce7e255abd 100644 (file)
@@ -1,46 +1,55 @@
-#include <stdio.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 <netinet/tcp.h>
+#include <poll.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
-#include <sys/poll.h>
 #include <sys/socket.h>
+#include <sys/types.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)
+int create_server_socket(int port, SocketType socket_type)
 {
-       int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
+       int server_sock;
+       if (socket_type == TCP_SOCKET) {
+               server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
+       } else {
+               assert(socket_type == UDP_SOCKET);
+               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);
        }
 
@@ -50,13 +59,15 @@ int create_server_socket(int port)
        addr.sin6_port = htons(port);
 
        if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
-               perror("bind");
+               log_perror("bind");
                exit(1);
        }
 
-       if (listen(server_sock, 128) == -1) {
-               perror("listen");
-               exit(1);
+       if (socket_type == TCP_SOCKET) {
+               if (listen(server_sock, 128) == -1) {
+                       log_perror("listen");
+                       exit(1);
+               }
        }
 
        return server_sock;
@@ -84,32 +95,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;
                }
 
@@ -122,7 +114,7 @@ void Acceptor::do_work()
                        continue;
                }
                if (sock == -1) {
-                       perror("accept");
+                       log_perror("accept");
                        usleep(100000);
                        continue;
                }
@@ -130,10 +122,18 @@ void Acceptor::do_work()
                // Set the socket as nonblocking.
                int one = 1;
                if (ioctl(sock, FIONBIO, &one) == -1) {
-                       perror("FIONBIO");
+                       log_perror("ioctl(FIONBIO)");
                        exit(1);
                }
 
+               // Enable TCP_CORK for maximum throughput. In the rare case that the
+               // stream stops entirely, this will cause a small delay (~200 ms)
+               // before the last part is sent out, but that should be fine.
+               if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
+                       log_perror("setsockopt(TCP_CORK)");
+                       // Can still continue.
+               }
+
                // Pick a server, round-robin, and hand over the socket to it.
                servers->add_client(sock);
        }