]> git.sesse.net Git - cubemap/blob - acceptor.cpp
87bf47b83ea1b1c09616fbd19c0410c6834bfc20
[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 #include "state.pb.h"
14
15 using namespace std;
16
17 extern ServerPool *servers;
18 extern volatile bool hupped;
19
20 int create_server_socket(int port)
21 {
22         int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
23         if (server_sock == -1) {
24                 perror("socket");
25                 exit(1);
26         }
27
28         int one = 1;
29         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
30                 perror("setsockopt(SO_REUSEADDR)");
31                 exit(1);
32         }
33
34         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
35         int zero = 0;
36         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
37                 perror("setsockopt(IPV6_V6ONLY)");
38                 exit(1);
39         }
40
41         // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
42         if (ioctl(server_sock, FIONBIO, &one) == -1) {
43                 perror("ioctl(FIONBIO)");
44                 exit(1);
45         }
46
47         sockaddr_in6 addr;
48         memset(&addr, 0, sizeof(addr));
49         addr.sin6_family = AF_INET6;
50         addr.sin6_port = htons(port);
51
52         if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
53                 perror("bind");
54                 exit(1);
55         }
56
57         if (listen(server_sock, 128) == -1) {
58                 perror("listen");
59                 exit(1);
60         }
61
62         return server_sock;
63 }
64         
65 Acceptor::Acceptor(int server_sock, int port)
66         : server_sock(server_sock),
67           port(port)
68 {
69 }
70
71 Acceptor::Acceptor(const AcceptorProto &serialized)
72         : server_sock(serialized.server_sock()),
73           port(serialized.port())
74 {
75 }
76
77 AcceptorProto Acceptor::serialize() const
78 {
79         AcceptorProto serialized;
80         serialized.set_server_sock(server_sock);
81         serialized.set_port(port);
82         return serialized;
83 }
84
85 void Acceptor::close_socket()
86 {
87         int ret;
88         do {
89                 ret = close(server_sock);
90         } while (ret == -1 && errno == EINTR);
91
92         if (ret == -1) {
93                 perror("close");
94         }
95 }
96
97 void Acceptor::do_work()
98 {
99         while (!hupped) {
100                 // Since we are non-blocking, we need to wait for the right state first.
101                 // Wait up to 50 ms, then check hupped.
102                 pollfd pfd;
103                 pfd.fd = server_sock;
104                 pfd.events = POLLIN;
105
106                 int nfds = poll(&pfd, 1, 50);
107                 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
108                         continue;
109                 }
110                 if (nfds == -1) {
111                         perror("poll");
112                         usleep(100000);
113                         continue;
114                 }
115
116                 sockaddr_in6 addr;
117                 socklen_t addrlen = sizeof(addr);
118
119                 // Get a new socket.
120                 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
121                 if (sock == -1 && errno == EINTR) {
122                         continue;
123                 }
124                 if (sock == -1) {
125                         perror("accept");
126                         usleep(100000);
127                         continue;
128                 }
129
130                 // Set the socket as nonblocking.
131                 int one = 1;
132                 if (ioctl(sock, FIONBIO, &one) == -1) {
133                         perror("FIONBIO");
134                         exit(1);
135                 }
136
137                 // Pick a server, round-robin, and hand over the socket to it.
138                 servers->add_client(sock);
139         }
140 }