]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // server.cpp | |
3 | // ~~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 <boost/thread/thread.hpp> | |
13 | #include <boost/bind.hpp> | |
14 | #include <boost/shared_ptr.hpp> | |
15 | #include <vector> | |
16 | ||
17 | namespace http { | |
18 | namespace server3 { | |
19 | ||
20 | server::server(const std::string& address, const std::string& port, | |
21 | const std::string& doc_root, std::size_t thread_pool_size) | |
22 | : thread_pool_size_(thread_pool_size), | |
b32b8144 FG |
23 | signals_(io_context_), |
24 | acceptor_(io_context_), | |
7c673cae FG |
25 | new_connection_(), |
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 | signals_.async_wait(boost::bind(&server::handle_stop, this)); | |
37 | ||
38 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). | |
b32b8144 FG |
39 | boost::asio::ip::tcp::resolver resolver(io_context_); |
40 | boost::asio::ip::tcp::endpoint endpoint = | |
41 | *resolver.resolve(address, port).begin(); | |
7c673cae FG |
42 | acceptor_.open(endpoint.protocol()); |
43 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); | |
44 | acceptor_.bind(endpoint); | |
45 | acceptor_.listen(); | |
46 | ||
47 | start_accept(); | |
48 | } | |
49 | ||
50 | void server::run() | |
51 | { | |
b32b8144 | 52 | // Create a pool of threads to run all of the io_contexts. |
7c673cae FG |
53 | std::vector<boost::shared_ptr<boost::thread> > threads; |
54 | for (std::size_t i = 0; i < thread_pool_size_; ++i) | |
55 | { | |
56 | boost::shared_ptr<boost::thread> thread(new boost::thread( | |
b32b8144 | 57 | boost::bind(&boost::asio::io_context::run, &io_context_))); |
7c673cae FG |
58 | threads.push_back(thread); |
59 | } | |
60 | ||
61 | // Wait for all threads in the pool to exit. | |
62 | for (std::size_t i = 0; i < threads.size(); ++i) | |
63 | threads[i]->join(); | |
64 | } | |
65 | ||
66 | void server::start_accept() | |
67 | { | |
b32b8144 | 68 | new_connection_.reset(new connection(io_context_, request_handler_)); |
7c673cae FG |
69 | acceptor_.async_accept(new_connection_->socket(), |
70 | boost::bind(&server::handle_accept, this, | |
71 | boost::asio::placeholders::error)); | |
72 | } | |
73 | ||
74 | void server::handle_accept(const boost::system::error_code& e) | |
75 | { | |
76 | if (!e) | |
77 | { | |
78 | new_connection_->start(); | |
79 | } | |
80 | ||
81 | start_accept(); | |
82 | } | |
83 | ||
84 | void server::handle_stop() | |
85 | { | |
b32b8144 | 86 | io_context_.stop(); |
7c673cae FG |
87 | } |
88 | ||
89 | } // namespace server3 | |
90 | } // namespace http |