Separate out the acceptor stuff into its own file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 20:40:00 +0000 (22:40 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Apr 2013 20:40:00 +0000 (22:40 +0200)
Makefile
acceptor.cpp [new file with mode: 0644]
acceptor.h [new file with mode: 0644]
main.cpp

index a9333f0..11adc8b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ PROTOC=protoc
 CXXFLAGS=-Wall -O2 -g
 LDLIBS=-lpthread -lprotobuf
 
-OBJS=main.o server.o serverpool.o mutexlock.o input.o parse.o markpool.o state.pb.o
+OBJS=main.o server.o serverpool.o mutexlock.o input.o parse.o markpool.o acceptor.o state.pb.o
 
 all: cubemap
 
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;
+}
diff --git a/acceptor.h b/acceptor.h
new file mode 100644 (file)
index 0000000..f173e5b
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef _ACCEPTOR_H
+#define _ACCEPTOR_H
+
+int create_server_socket(int port);
+
+// A thread that accepts new connections on a given socket,
+// and hands them off to the server pool.
+void *acceptor_thread_run(void *arg);
+
+#endif  // !defined(_ACCEPTOR_H)
index 1300508..a4a4dbe 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -18,6 +18,7 @@
 #include <map>
 #include <set>
 
+#include "acceptor.h"
 #include "markpool.h"
 #include "metacube.h"
 #include "parse.h"
@@ -36,98 +37,6 @@ void hup(int ignored)
        hupped = true;
 }
 
-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;
-}
-
 struct StatsThreadParameters {
        string stats_file;
        int stats_interval;