]> git.sesse.net Git - cubemap/blob - acceptor.cpp
Update the man page with our non-alphaness as well.
[cubemap] / acceptor.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
5 #include <poll.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/ioctl.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "acceptor.h"
13 #include "log.h"
14 #include "serverpool.h"
15 #include "state.pb.h"
16 #include "util.h"
17
18 using namespace std;
19
20 extern ServerPool *servers;
21
22 int create_server_socket(int port, SocketType socket_type)
23 {
24         int server_sock;
25         if (socket_type == TCP_SOCKET) {
26                 server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
27         } else {
28                 assert(socket_type == UDP_SOCKET);
29                 server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
30         }
31         if (server_sock == -1) {
32                 log_perror("socket");
33                 exit(1);
34         }
35
36         int one = 1;
37         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
38                 log_perror("setsockopt(SO_REUSEADDR)");
39                 exit(1);
40         }
41
42         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
43         int zero = 0;
44         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
45                 log_perror("setsockopt(IPV6_V6ONLY)");
46                 exit(1);
47         }
48
49         // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
50         if (ioctl(server_sock, FIONBIO, &one) == -1) {
51                 log_perror("ioctl(FIONBIO)");
52                 exit(1);
53         }
54
55         sockaddr_in6 addr;
56         memset(&addr, 0, sizeof(addr));
57         addr.sin6_family = AF_INET6;
58         addr.sin6_port = htons(port);
59
60         if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
61                 log_perror("bind");
62                 exit(1);
63         }
64
65         if (socket_type == TCP_SOCKET) {
66                 if (listen(server_sock, 128) == -1) {
67                         log_perror("listen");
68                         exit(1);
69                 }
70         }
71
72         return server_sock;
73 }
74         
75 Acceptor::Acceptor(int server_sock, int port)
76         : server_sock(server_sock),
77           port(port)
78 {
79 }
80
81 Acceptor::Acceptor(const AcceptorProto &serialized)
82         : server_sock(serialized.server_sock()),
83           port(serialized.port())
84 {
85 }
86
87 AcceptorProto Acceptor::serialize() const
88 {
89         AcceptorProto serialized;
90         serialized.set_server_sock(server_sock);
91         serialized.set_port(port);
92         return serialized;
93 }
94
95 void Acceptor::close_socket()
96 {
97         safe_close(server_sock);
98 }
99
100 void Acceptor::do_work()
101 {
102         while (!should_stop()) {
103                 if (!wait_for_activity(server_sock, POLLIN, NULL)) {
104                         continue;
105                 }
106
107                 sockaddr_in6 addr;
108                 socklen_t addrlen = sizeof(addr);
109
110                 // Get a new socket.
111                 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
112                 if (sock == -1 && errno == EINTR) {
113                         continue;
114                 }
115                 if (sock == -1) {
116                         log_perror("accept");
117                         usleep(100000);
118                         continue;
119                 }
120
121                 // Set the socket as nonblocking.
122                 int one = 1;
123                 if (ioctl(sock, FIONBIO, &one) == -1) {
124                         log_perror("ioctl(FIONBIO)");
125                         exit(1);
126                 }
127
128                 // Enable TCP_CORK for maximum throughput. In the rare case that the
129                 // stream stops entirely, this will cause a small delay (~200 ms)
130                 // before the last part is sent out, but that should be fine.
131                 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
132                         log_perror("setsockopt(TCP_CORK)");
133                         // Can still continue.
134                 }
135
136                 // Pick a server, round-robin, and hand over the socket to it.
137                 servers->add_client(sock);
138         }
139 }