]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Separate out the acceptor stuff into its own file.
[cubemap] / acceptor.cpp
diff --git a/acceptor.cpp b/acceptor.cpp
new file mode 100644 (file)
index 0000000..0b8de50
--- /dev/null
@@ -0,0 +1,108 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <arpa/inet.h>
+#include <sys/ioctl.h>
+#include <sys/poll.h>
+#include <sys/socket.h>
+
+#include "acceptor.h"
+#include "serverpool.h"
+
+using namespace std;
+
+extern ServerPool *servers;
+extern volatile bool hupped;
+
+int create_server_socket(int port)
+{
+       int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
+       if (server_sock == -1) {
+               perror("socket");
+               exit(1);
+       }
+
+       int one = 1;
+       if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
+               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)");
+               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;
+       addr.sin6_port = htons(port);
+
+       if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
+               perror("bind");
+               exit(1);
+       }
+
+       if (listen(server_sock, 128) == -1) {
+               perror("listen");
+               exit(1);
+       }
+
+       return server_sock;
+}
+
+void *acceptor_thread_run(void *arg)
+{
+       int server_sock = int(intptr_t(arg));
+       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);
+                       continue;
+               }
+
+               sockaddr_in6 addr;
+               socklen_t addrlen = sizeof(addr);
+
+               // Get a new socket.
+               int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
+               if (sock == -1 && errno == EINTR) {
+                       continue;
+               }
+               if (sock == -1) {
+                       perror("accept");
+                       usleep(100000);
+                       continue;
+               }
+
+               // Set the socket as nonblocking.
+               int one = 1;
+               if (ioctl(sock, FIONBIO, &one) == -1) {
+                       perror("FIONBIO");
+                       exit(1);
+               }
+
+               // Pick a server, round-robin, and hand over the socket to it.
+               servers->add_client(sock);
+       }
+       return NULL;
+}