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