2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 // We wrote this code based on the original code which has the
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
37 #include "asio_server.h"
39 #include <boost/date_time/posix_time/posix_time.hpp>
42 namespace asio_http2 {
45 server::server(const std::string &address, uint16_t port,
46 std::size_t io_service_pool_size, std::size_t thread_pool_size,
48 std::unique_ptr<boost::asio::ssl::context> ssl_ctx, int backlog)
49 : io_service_pool_(io_service_pool_size, thread_pool_size),
50 signals_(io_service_pool_.get_io_service()),
51 tick_timer_(io_service_pool_.get_io_service(),
52 boost::posix_time::seconds(1)),
53 ssl_ctx_(std::move(ssl_ctx)), request_cb_(std::move(cb)) {
54 // Register to handle the signals that indicate when the server should exit.
55 // It is safe to register for the same signal multiple times in a program,
56 // provided all registration for the specified signal is made through Asio.
58 signals_.add(SIGTERM);
60 signals_.add(SIGQUIT);
61 #endif // defined(SIGQUIT)
62 signals_.async_wait([this](const boost::system::error_code &error,
63 int signal_number) { io_service_pool_.stop(); });
65 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
66 boost::asio::ip::tcp::resolver resolver(io_service_pool_.get_io_service());
67 boost::asio::ip::tcp::resolver::query query(address, std::to_string(port));
69 for (auto itr = resolver.resolve(query);
70 itr != boost::asio::ip::tcp::resolver::iterator(); ++itr) {
71 boost::asio::ip::tcp::endpoint endpoint = *itr;
73 boost::asio::ip::tcp::acceptor(io_service_pool_.get_io_service());
75 acceptor.open(endpoint.protocol());
76 acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
77 acceptor.bind(endpoint);
81 acceptor.listen(backlog);
83 acceptors_.push_back(std::move(acceptor));
91 void server::run() { io_service_pool_.run(); }
93 std::shared_ptr<std::string> cached_date;
97 cached_date = std::make_shared<std::string>(util::http_date(time(nullptr)));
101 void server::start_timer() {
104 tick_timer_.async_wait([this](const boost::system::error_code &e) {
105 tick_timer_.expires_at(tick_timer_.expires_at() +
106 boost::posix_time::seconds(1));
111 typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
113 void server::start_accept() {
115 auto new_connection = std::make_shared<connection<ssl_socket>>(
116 request_cb_, io_service_pool_.get_task_io_service(),
117 io_service_pool_.get_io_service(), *ssl_ctx_);
119 for (auto &acceptor : acceptors_) {
120 acceptor.async_accept(
121 new_connection->socket().lowest_layer(),
122 [this, new_connection](const boost::system::error_code &e) {
124 new_connection->socket().lowest_layer().set_option(
125 boost::asio::ip::tcp::no_delay(true));
126 new_connection->socket().async_handshake(
127 boost::asio::ssl::stream_base::server,
128 [new_connection](const boost::system::error_code &e) {
130 new_connection->start();
139 auto new_connection =
140 std::make_shared<connection<boost::asio::ip::tcp::socket>>(
141 request_cb_, io_service_pool_.get_task_io_service(),
142 io_service_pool_.get_io_service());
144 for (auto &acceptor : acceptors_) {
145 acceptor.async_accept(
146 new_connection->socket(),
147 [this, new_connection](const boost::system::error_code &e) {
149 new_connection->socket().set_option(
150 boost::asio::ip::tcp::no_delay(true));
151 new_connection->start();
160 } // namespace server
161 } // namespace asio_http2
162 } // namespace nghttp2