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
--- /dev/null
+#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;
+}
--- /dev/null
+#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)
#include <map>
#include <set>
+#include "acceptor.h"
#include "markpool.h"
#include "metacube.h"
#include "parse.h"
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;