3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
15 #include "serverpool.h"
21 extern ServerPool *servers;
23 int create_server_socket(int port, SocketType socket_type)
26 if (socket_type == TCP_SOCKET) {
27 server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
29 assert(socket_type == UDP_SOCKET);
30 server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
32 if (server_sock == -1) {
38 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
39 log_perror("setsockopt(SO_REUSEADDR)");
43 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
45 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
46 log_perror("setsockopt(IPV6_V6ONLY)");
50 // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
51 if (ioctl(server_sock, FIONBIO, &one) == -1) {
52 log_perror("ioctl(FIONBIO)");
57 memset(&addr, 0, sizeof(addr));
58 addr.sin6_family = AF_INET6;
59 addr.sin6_port = htons(port);
61 if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
66 if (socket_type == TCP_SOCKET) {
67 if (listen(server_sock, 128) == -1) {
76 Acceptor::Acceptor(int server_sock, int port)
77 : server_sock(server_sock),
82 Acceptor::Acceptor(const AcceptorProto &serialized)
83 : server_sock(serialized.server_sock()),
84 port(serialized.port())
88 AcceptorProto Acceptor::serialize() const
90 AcceptorProto serialized;
91 serialized.set_server_sock(server_sock);
92 serialized.set_port(port);
96 void Acceptor::close_socket()
98 safe_close(server_sock);
101 void Acceptor::do_work()
103 while (!should_stop()) {
104 if (!wait_for_activity(server_sock, POLLIN, NULL)) {
109 socklen_t addrlen = sizeof(addr);
112 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
113 if (sock == -1 && errno == EINTR) {
117 log_perror("accept");
122 // Set the socket as nonblocking.
124 if (ioctl(sock, FIONBIO, &one) == -1) {
125 log_perror("ioctl(FIONBIO)");
129 // Enable TCP_CORK for maximum throughput. In the rare case that the
130 // stream stops entirely, this will cause a small delay (~200 ms)
131 // before the last part is sent out, but that should be fine.
132 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
133 log_perror("setsockopt(TCP_CORK)");
134 // Can still continue.
137 // Pick a server, round-robin, and hand over the socket to it.
138 servers->add_client(sock);