]> git.sesse.net Git - cubemap/blobdiff - acceptor.cpp
Support multiple listening sockets. Actually mostly because it makes the code somewha...
[cubemap] / acceptor.cpp
index 0b8de507af4d0b5576cb2f9aa309ba6e68519a62..87bf47b83ea1b1c09616fbd19c0410c6834bfc20 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <signal.h>
 #include <errno.h>
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
@@ -9,6 +10,7 @@
 
 #include "acceptor.h"
 #include "serverpool.h"
+#include "state.pb.h"
 
 using namespace std;
 
@@ -59,10 +61,41 @@ int create_server_socket(int port)
 
        return server_sock;
 }
+       
+Acceptor::Acceptor(int server_sock, int port)
+       : server_sock(server_sock),
+         port(port)
+{
+}
+
+Acceptor::Acceptor(const AcceptorProto &serialized)
+       : server_sock(serialized.server_sock()),
+         port(serialized.port())
+{
+}
+
+AcceptorProto Acceptor::serialize() const
+{
+       AcceptorProto serialized;
+       serialized.set_server_sock(server_sock);
+       serialized.set_port(port);
+       return serialized;
+}
+
+void Acceptor::close_socket()
+{
+       int ret;
+       do {
+               ret = close(server_sock);
+       } while (ret == -1 && errno == EINTR);
+
+       if (ret == -1) {
+               perror("close");
+       }
+}
 
-void *acceptor_thread_run(void *arg)
+void Acceptor::do_work()
 {
-       int server_sock = int(intptr_t(arg));
        while (!hupped) {
                // Since we are non-blocking, we need to wait for the right state first.
                // Wait up to 50 ms, then check hupped.
@@ -104,5 +137,4 @@ void *acceptor_thread_run(void *arg)
                // Pick a server, round-robin, and hand over the socket to it.
                servers->add_client(sock);
        }
-       return NULL;
 }