]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // server.cpp | |
3 | // ~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #include "server.hpp" | |
12 | #include <signal.h> | |
13 | #include <utility> | |
14 | ||
15 | namespace http { | |
16 | namespace server { | |
17 | ||
18 | server::server(const std::string& address, const std::string& port, | |
19 | const std::string& doc_root) | |
20 | : io_service_(), | |
21 | signals_(io_service_), | |
22 | acceptor_(io_service_), | |
23 | connection_manager_(), | |
24 | socket_(io_service_), | |
25 | request_handler_(doc_root) | |
26 | { | |
27 | // Register to handle the signals that indicate when the server should exit. | |
28 | // It is safe to register for the same signal multiple times in a program, | |
29 | // provided all registration for the specified signal is made through Asio. | |
30 | signals_.add(SIGINT); | |
31 | signals_.add(SIGTERM); | |
32 | #if defined(SIGQUIT) | |
33 | signals_.add(SIGQUIT); | |
34 | #endif // defined(SIGQUIT) | |
35 | ||
36 | do_await_stop(); | |
37 | ||
38 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). | |
39 | boost::asio::ip::tcp::resolver resolver(io_service_); | |
40 | boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve({address, port}); | |
41 | acceptor_.open(endpoint.protocol()); | |
42 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); | |
43 | acceptor_.bind(endpoint); | |
44 | acceptor_.listen(); | |
45 | ||
46 | do_accept(); | |
47 | } | |
48 | ||
49 | void server::run() | |
50 | { | |
51 | // The io_service::run() call will block until all asynchronous operations | |
52 | // have finished. While the server is running, there is always at least one | |
53 | // asynchronous operation outstanding: the asynchronous accept call waiting | |
54 | // for new incoming connections. | |
55 | io_service_.run(); | |
56 | } | |
57 | ||
58 | void server::do_accept() | |
59 | { | |
60 | acceptor_.async_accept(socket_, | |
61 | [this](boost::system::error_code ec) | |
62 | { | |
63 | // Check whether the server was stopped by a signal before this | |
64 | // completion handler had a chance to run. | |
65 | if (!acceptor_.is_open()) | |
66 | { | |
67 | return; | |
68 | } | |
69 | ||
70 | if (!ec) | |
71 | { | |
72 | connection_manager_.start(std::make_shared<connection>( | |
73 | std::move(socket_), connection_manager_, request_handler_)); | |
74 | } | |
75 | ||
76 | do_accept(); | |
77 | }); | |
78 | } | |
79 | ||
80 | void server::do_await_stop() | |
81 | { | |
82 | signals_.async_wait( | |
83 | [this](boost::system::error_code /*ec*/, int /*signo*/) | |
84 | { | |
85 | // The server is stopped by cancelling all outstanding asynchronous | |
86 | // operations. Once all operations have finished the io_service::run() | |
87 | // call will exit. | |
88 | acceptor_.close(); | |
89 | connection_manager_.stop_all(); | |
90 | }); | |
91 | } | |
92 | ||
93 | } // namespace server | |
94 | } // namespace http |