]> git.sesse.net Git - cubemap/blob - acceptor.cpp
0b8de507af4d0b5576cb2f9aa309ba6e68519a62
[cubemap] / acceptor.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <arpa/inet.h>
6 #include <sys/ioctl.h>
7 #include <sys/poll.h>
8 #include <sys/socket.h>
9
10 #include "acceptor.h"
11 #include "serverpool.h"
12
13 using namespace std;
14
15 extern ServerPool *servers;
16 extern volatile bool hupped;
17
18 int create_server_socket(int port)
19 {
20         int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
21         if (server_sock == -1) {
22                 perror("socket");
23                 exit(1);
24         }
25
26         int one = 1;
27         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
28                 perror("setsockopt(SO_REUSEADDR)");
29                 exit(1);
30         }
31
32         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
33         int zero = 0;
34         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
35                 perror("setsockopt(IPV6_V6ONLY)");
36                 exit(1);
37         }
38
39         // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
40         if (ioctl(server_sock, FIONBIO, &one) == -1) {
41                 perror("ioctl(FIONBIO)");
42                 exit(1);
43         }
44
45         sockaddr_in6 addr;
46         memset(&addr, 0, sizeof(addr));
47         addr.sin6_family = AF_INET6;
48         addr.sin6_port = htons(port);
49
50         if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
51                 perror("bind");
52                 exit(1);
53         }
54
55         if (listen(server_sock, 128) == -1) {
56                 perror("listen");
57                 exit(1);
58         }
59
60         return server_sock;
61 }
62
63 void *acceptor_thread_run(void *arg)
64 {
65         int server_sock = int(intptr_t(arg));
66         while (!hupped) {
67                 // Since we are non-blocking, we need to wait for the right state first.
68                 // Wait up to 50 ms, then check hupped.
69                 pollfd pfd;
70                 pfd.fd = server_sock;
71                 pfd.events = POLLIN;
72
73                 int nfds = poll(&pfd, 1, 50);
74                 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
75                         continue;
76                 }
77                 if (nfds == -1) {
78                         perror("poll");
79                         usleep(100000);
80                         continue;
81                 }
82
83                 sockaddr_in6 addr;
84                 socklen_t addrlen = sizeof(addr);
85
86                 // Get a new socket.
87                 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
88                 if (sock == -1 && errno == EINTR) {
89                         continue;
90                 }
91                 if (sock == -1) {
92                         perror("accept");
93                         usleep(100000);
94                         continue;
95                 }
96
97                 // Set the socket as nonblocking.
98                 int one = 1;
99                 if (ioctl(sock, FIONBIO, &one) == -1) {
100                         perror("FIONBIO");
101                         exit(1);
102                 }
103
104                 // Pick a server, round-robin, and hand over the socket to it.
105                 servers->add_client(sock);
106         }
107         return NULL;
108 }