| 19 | namespace server3 { |
| 20 | |
| 21 | server::server(const std::string& address, const std::string& port, |
| 22 | const std::string& doc_root, std::size_t thread_pool_size) |
| 23 | : thread_pool_size_(thread_pool_size), |
| 24 | signals_(io_context_), |
| 25 | acceptor_(io_context_), |
| 26 | request_handler_(doc_root) |
| 27 | { |
| 28 | // Register to handle the signals that indicate when the server should exit. |
| 29 | // It is safe to register for the same signal multiple times in a program, |
| 30 | // provided all registration for the specified signal is made through Asio. |
| 31 | signals_.add(SIGINT); |
| 32 | signals_.add(SIGTERM); |
| 33 | #if defined(SIGQUIT) |
| 34 | signals_.add(SIGQUIT); |
| 35 | #endif // defined(SIGQUIT) |
| 36 | |
| 37 | do_await_stop(); |
| 38 | |
| 39 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). |
| 40 | boost::asio::ip::tcp::resolver resolver(io_context_); |
| 41 | boost::asio::ip::tcp::endpoint endpoint = |
| 42 | *resolver.resolve(address, port).begin(); |
| 43 | acceptor_.open(endpoint.protocol()); |
| 44 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); |
| 45 | acceptor_.bind(endpoint); |
| 46 | acceptor_.listen(); |
| 47 | |
| 48 | do_accept(); |
| 49 | } |
| 50 | |
| 51 | void server::run() |
| 52 | { |