]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Enable TCP_CORK if available.
[cubemap] / acceptor.cpp
index 3172c73b80a1821e0973e08c267f6f8ccae74c10..32a89fa7670d7221b54e7c98ffa0f8ce7e255abd 100644 (file)
@@ -1,22 +1,24 @@
 #include <assert.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <assert.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <poll.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <poll.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 <unistd.h>
 
 #include "acceptor.h"
 #include "log.h"
 #include "serverpool.h"
 #include "state.pb.h"
+#include "util.h"
 
 using namespace std;
 
 extern ServerPool *servers;
 
 using namespace std;
 
 extern ServerPool *servers;
-extern volatile bool hupped;
 
 int create_server_socket(int port, SocketType socket_type)
 {
 
 int create_server_socket(int port, SocketType socket_type)
 {
@@ -93,32 +95,13 @@ AcceptorProto Acceptor::serialize() const
 
 void Acceptor::close_socket()
 {
 
 void Acceptor::close_socket()
 {
-       int ret;
-       do {
-               ret = close(server_sock);
-       } while (ret == -1 && errno == EINTR);
-
-       if (ret == -1) {
-               log_perror("close");
-       }
+       safe_close(server_sock);
 }
 
 void Acceptor::do_work()
 {
 }
 
 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) {
-                       log_perror("poll");
-                       usleep(100000);
+       while (!should_stop()) {
+               if (!wait_for_activity(server_sock, POLLIN, NULL)) {
                        continue;
                }
 
                        continue;
                }
 
@@ -139,10 +122,18 @@ void Acceptor::do_work()
                // Set the socket as nonblocking.
                int one = 1;
                if (ioctl(sock, FIONBIO, &one) == -1) {
                // Set the socket as nonblocking.
                int one = 1;
                if (ioctl(sock, FIONBIO, &one) == -1) {
-                       log_perror("FIONBIO");
+                       log_perror("ioctl(FIONBIO)");
                        exit(1);
                }
 
                        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);
        }
                // Pick a server, round-robin, and hand over the socket to it.
                servers->add_client(sock);
        }