#ifndef _ACCEPTOR_H
#define _ACCEPTOR_H
-int create_server_socket(int port);
+#include <netinet/in.h>
+
+#include <string>
+
+#include "thread.h"
+
+enum SocketType {
+ TCP_SOCKET,
+ UDP_SOCKET,
+};
+int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type);
+
+class AcceptorProto;
+
+sockaddr_in6 create_any_address(int port);
+sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto);
// A thread that accepts new connections on a given socket,
// and hands them off to the server pool.
-class AcceptorThread {
+class Acceptor : public Thread {
public:
- AcceptorThread(int server_sock);
- void run();
- void stop();
+ Acceptor(int server_sock, const sockaddr_in6 &addr,
+ const std::string &certificate_chain, const std::string &private_key);
-private:
- // Recovers the this pointer, and hands over control to do_work().
- static void *do_work_thunk(void *arg);
+ // Serialization/deserialization.
+ Acceptor(const AcceptorProto &serialized);
+ AcceptorProto serialize() const;
- void do_work();
+ bool is_tls() const { return !certificate_chain.empty(); }
- int server_sock;
+ std::string get_certificate_chain() const {
+ assert(is_tls());
+ return certificate_chain;
+ }
+
+ std::string get_private_key() const {
+ assert(is_tls());
+ return private_key;
+ }
+
+ void close_socket();
- pthread_t worker_thread;
- volatile bool should_stop;
+private:
+ virtual void do_work();
+
+ int server_sock;
+ sockaddr_in6 addr;
+ std::string certificate_chain, private_key; // Both empty for no TLS.
};
#endif // !defined(_ACCEPTOR_H)