]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp11/http/server/server.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / http / server / 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 <signal.h>
13#include <utility>
14
15namespace http {
16namespace server {
17
18server::server(const std::string& address, const std::string& port,
19 const std::string& doc_root)
b32b8144
FG
20 : io_context_(1),
21 signals_(io_context_),
22 acceptor_(io_context_),
7c673cae 23 connection_manager_(),
7c673cae
FG
24 request_handler_(doc_root)
25{
26 // Register to handle the signals that indicate when the server should exit.
27 // It is safe to register for the same signal multiple times in a program,
28 // provided all registration for the specified signal is made through Asio.
29 signals_.add(SIGINT);
30 signals_.add(SIGTERM);
31#if defined(SIGQUIT)
32 signals_.add(SIGQUIT);
33#endif // defined(SIGQUIT)
34
35 do_await_stop();
36
37 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
b32b8144
FG
38 boost::asio::ip::tcp::resolver resolver(io_context_);
39 boost::asio::ip::tcp::endpoint endpoint =
40 *resolver.resolve(address, port).begin();
7c673cae
FG
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
49void server::run()
50{
b32b8144 51 // The io_context::run() call will block until all asynchronous operations
7c673cae
FG
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.
b32b8144 55 io_context_.run();
7c673cae
FG
56}
57
58void server::do_accept()
59{
b32b8144
FG
60 acceptor_.async_accept(
61 [this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket)
7c673cae
FG
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>(
b32b8144 73 std::move(socket), connection_manager_, request_handler_));
7c673cae
FG
74 }
75
76 do_accept();
77 });
78}
79
80void 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
b32b8144 86 // operations. Once all operations have finished the io_context::run()
7c673cae
FG
87 // call will exit.
88 acceptor_.close();
89 connection_manager_.stop_all();
90 });
91}
92
93} // namespace server
94} // namespace http