]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/http/server2/server.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / http / server2 / server.cpp
CommitLineData
7c673cae
FG
1//
2// server.cpp
3// ~~~~~~~~~~
4//
92f5a8d4 5// Copyright (c) 2003-2019 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/bind.hpp>
13
14namespace http {
15namespace server2 {
16
17server::server(const std::string& address, const std::string& port,
b32b8144
FG
18 const std::string& doc_root, std::size_t io_context_pool_size)
19 : io_context_pool_(io_context_pool_size),
20 signals_(io_context_pool_.get_io_context()),
21 acceptor_(io_context_pool_.get_io_context()),
7c673cae
FG
22 new_connection_(),
23 request_handler_(doc_root)
24{
25 // Register to handle the signals that indicate when the server should exit.
26 // It is safe to register for the same signal multiple times in a program,
27 // provided all registration for the specified signal is made through Asio.
28 signals_.add(SIGINT);
29 signals_.add(SIGTERM);
30#if defined(SIGQUIT)
31 signals_.add(SIGQUIT);
32#endif // defined(SIGQUIT)
33 signals_.async_wait(boost::bind(&server::handle_stop, this));
34
35 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
92f5a8d4 36 boost::asio::ip::tcp::resolver resolver(acceptor_.get_executor());
b32b8144
FG
37 boost::asio::ip::tcp::endpoint endpoint =
38 *resolver.resolve(address, port).begin();
7c673cae
FG
39 acceptor_.open(endpoint.protocol());
40 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
41 acceptor_.bind(endpoint);
42 acceptor_.listen();
43
44 start_accept();
45}
46
47void server::run()
48{
b32b8144 49 io_context_pool_.run();
7c673cae
FG
50}
51
52void server::start_accept()
53{
54 new_connection_.reset(new connection(
b32b8144 55 io_context_pool_.get_io_context(), request_handler_));
7c673cae
FG
56 acceptor_.async_accept(new_connection_->socket(),
57 boost::bind(&server::handle_accept, this,
58 boost::asio::placeholders::error));
59}
60
61void server::handle_accept(const boost::system::error_code& e)
62{
63 if (!e)
64 {
65 new_connection_->start();
66 }
67
68 start_accept();
69}
70
71void server::handle_stop()
72{
b32b8144 73 io_context_pool_.stop();
7c673cae
FG
74}
75
76} // namespace server2
77} // namespace http