From 3f82e5f8ede16a712aa0bcd0913d2d8e3689742c Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 10 Apr 2013 22:40:00 +0200 Subject: [PATCH] Separate out the acceptor stuff into its own file. --- Makefile | 2 +- acceptor.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ acceptor.h | 10 +++++ main.cpp | 93 +------------------------------------------- 4 files changed, 120 insertions(+), 93 deletions(-) create mode 100644 acceptor.cpp create mode 100644 acceptor.h diff --git a/Makefile b/Makefile index a9333f0..11adc8b 100644 --- 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 index 0000000..0b8de50 --- /dev/null +++ b/acceptor.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#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(&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(&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 index 0000000..f173e5b --- /dev/null +++ b/acceptor.h @@ -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) diff --git a/main.cpp b/main.cpp index 1300508..a4a4dbe 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,7 @@ #include #include +#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(&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(&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; -- 2.39.2