]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/http/server2/server.cpp
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
12 #include <boost/bind.hpp>
17 server::server(const std::string
& address
, const std::string
& port
,
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()),
23 request_handler_(doc_root
)
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.
29 signals_
.add(SIGTERM
);
31 signals_
.add(SIGQUIT
);
32 #endif // defined(SIGQUIT)
33 signals_
.async_wait(boost::bind(&server::handle_stop
, this));
35 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
36 boost::asio::ip::tcp::resolver
resolver(acceptor_
.get_executor().context());
37 boost::asio::ip::tcp::endpoint endpoint
=
38 *resolver
.resolve(address
, port
).begin();
39 acceptor_
.open(endpoint
.protocol());
40 acceptor_
.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
41 acceptor_
.bind(endpoint
);
49 io_context_pool_
.run();
52 void server::start_accept()
54 new_connection_
.reset(new connection(
55 io_context_pool_
.get_io_context(), request_handler_
));
56 acceptor_
.async_accept(new_connection_
->socket(),
57 boost::bind(&server::handle_accept
, this,
58 boost::asio::placeholders::error
));
61 void server::handle_accept(const boost::system::error_code
& e
)
65 new_connection_
->start();
71 void server::handle_stop()
73 io_context_pool_
.stop();
76 } // namespace server2