X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=acceptor.cpp;h=2abfa5220064ec47efb8d40c41d3ad9ceed6a79d;hp=7620b7991104d98d19364559b46d6778d4c7919e;hb=845934ca50eee40884e8cc85ea81eb310efa5ca3;hpb=e1722a5c0341fd541ce57f1eed4dc76cbd3efe07 diff --git a/acceptor.cpp b/acceptor.cpp index 7620b79..2abfa52 100644 --- a/acceptor.cpp +++ b/acceptor.cpp @@ -1,45 +1,53 @@ -#include -#include -#include -#include +#include #include -#include +#include +#include +#include +#include #include -#include #include +#include #include "acceptor.h" +#include "log.h" #include "serverpool.h" +#include "state.pb.h" +#include "util.h" using namespace std; extern ServerPool *servers; -extern volatile bool hupped; -int create_server_socket(int port) +int create_server_socket(int port, SocketType socket_type) { - int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); + int server_sock; + if (socket_type == TCP_SOCKET) { + server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); + } else { + assert(socket_type == UDP_SOCKET); + server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); + } if (server_sock == -1) { - perror("socket"); + log_perror("socket"); exit(1); } int one = 1; if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { - perror("setsockopt(SO_REUSEADDR)"); + log_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)"); + log_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)"); + log_perror("ioctl(FIONBIO)"); exit(1); } @@ -49,39 +57,49 @@ int create_server_socket(int port) addr.sin6_port = htons(port); if (bind(server_sock, reinterpret_cast(&addr), sizeof(addr)) == -1) { - perror("bind"); + log_perror("bind"); exit(1); } - if (listen(server_sock, 128) == -1) { - perror("listen"); - exit(1); + if (socket_type == TCP_SOCKET) { + if (listen(server_sock, 128) == -1) { + log_perror("listen"); + exit(1); + } } return server_sock; } -AcceptorThread::AcceptorThread(int server_sock) - : server_sock(server_sock) +Acceptor::Acceptor(int server_sock, int port) + : server_sock(server_sock), + port(port) { } -void AcceptorThread::do_work() +Acceptor::Acceptor(const AcceptorProto &serialized) + : server_sock(serialized.server_sock()), + port(serialized.port()) { - 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); +} + +AcceptorProto Acceptor::serialize() const +{ + AcceptorProto serialized; + serialized.set_server_sock(server_sock); + serialized.set_port(port); + return serialized; +} + +void Acceptor::close_socket() +{ + safe_close(server_sock); +} + +void Acceptor::do_work() +{ + while (!should_stop()) { + if (!wait_for_activity(server_sock, POLLIN, NULL)) { continue; } @@ -94,7 +112,7 @@ void AcceptorThread::do_work() continue; } if (sock == -1) { - perror("accept"); + log_perror("accept"); usleep(100000); continue; } @@ -102,7 +120,7 @@ void AcceptorThread::do_work() // Set the socket as nonblocking. int one = 1; if (ioctl(sock, FIONBIO, &one) == -1) { - perror("FIONBIO"); + log_perror("FIONBIO"); exit(1); }