]> git.sesse.net Git - cubemap/blob - acceptor.h
Capitalize HTTP header names after dashes.
[cubemap] / acceptor.h
1 #ifndef _ACCEPTOR_H
2 #define _ACCEPTOR_H
3
4 #include <netinet/in.h>
5
6 #include <string>
7
8 #include "thread.h"
9
10 enum SocketType {
11         TCP_SOCKET,
12         UDP_SOCKET,
13 };
14 int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type);
15
16 class AcceptorProto;
17
18 sockaddr_in6 create_any_address(int port);
19 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto);
20
21 // A thread that accepts new connections on a given socket,
22 // and hands them off to the server pool.
23 class Acceptor : public Thread {
24 public:
25         Acceptor(int server_sock, const sockaddr_in6 &addr,
26                  const std::string &certificate_chain, const std::string &private_key);
27
28         // Serialization/deserialization.
29         Acceptor(const AcceptorProto &serialized);
30         AcceptorProto serialize() const;
31
32         bool is_tls() const { return !certificate_chain.empty(); }
33
34         std::string get_certificate_chain() const {
35                 assert(is_tls());
36                 return certificate_chain;
37         }
38
39         std::string get_private_key() const {
40                 assert(is_tls());
41                 return private_key;
42         }
43
44         void close_socket();
45
46 private:
47         virtual void do_work();
48
49         int server_sock;
50         sockaddr_in6 addr;
51         std::string certificate_chain, private_key;  // Both empty for no TLS.
52 };
53
54 #endif  // !defined(_ACCEPTOR_H)