]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Tweak the MutexLock implementation slightly, so as to confuse Coverity less.
[cubemap] / acceptor.cpp
index ecebf7e5b65d74359e6ce6cf857c55fa4920c19b..32a89fa7670d7221b54e7c98ffa0f8ce7e255abd 100644 (file)
@@ -1,22 +1,24 @@
 #include <assert.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <poll.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.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, SocketType socket_type)
 {
@@ -28,26 +30,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 +59,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 +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;
                }
 
@@ -131,7 +114,7 @@ void Acceptor::do_work()
                        continue;
                }
                if (sock == -1) {
-                       perror("accept");
+                       log_perror("accept");
                        usleep(100000);
                        continue;
                }
@@ -139,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);
        }