2014-09-21 11:28:06 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
// We wrote this code based on the original code which has the
|
|
|
|
// following license:
|
|
|
|
//
|
|
|
|
// server.cpp
|
|
|
|
// ~~~~~~~~~~
|
|
|
|
//
|
|
|
|
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "asio_server.h"
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
#include "asio_server_connection.h"
|
2015-11-07 02:32:08 +01:00
|
|
|
#include "asio_common.h"
|
2015-03-05 13:42:48 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
namespace nghttp2 {
|
|
|
|
namespace asio_http2 {
|
|
|
|
namespace server {
|
|
|
|
|
2015-12-21 16:33:12 +01:00
|
|
|
server::server(std::size_t io_service_pool_size,
|
|
|
|
const boost::posix_time::time_duration &tls_handshake_timeout,
|
|
|
|
const boost::posix_time::time_duration &read_timeout)
|
|
|
|
: io_service_pool_(io_service_pool_size),
|
|
|
|
tls_handshake_timeout_(tls_handshake_timeout),
|
|
|
|
read_timeout_(read_timeout) {}
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
boost::system::error_code
|
|
|
|
server::listen_and_serve(boost::system::error_code &ec,
|
|
|
|
boost::asio::ssl::context *tls_context,
|
|
|
|
const std::string &address, const std::string &port,
|
2015-04-22 11:51:28 +02:00
|
|
|
int backlog, serve_mux &mux, bool asynchronous) {
|
2015-03-06 15:20:08 +01:00
|
|
|
ec.clear();
|
|
|
|
|
|
|
|
if (bind_and_listen(ec, address, port, backlog)) {
|
|
|
|
return ec;
|
2014-11-10 15:26:01 +01:00
|
|
|
}
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-03 18:27:33 +01:00
|
|
|
for (auto &acceptor : acceptors_) {
|
2015-03-06 15:20:08 +01:00
|
|
|
if (tls_context) {
|
|
|
|
start_accept(*tls_context, acceptor, mux);
|
|
|
|
} else {
|
|
|
|
start_accept(acceptor, mux);
|
|
|
|
}
|
2015-03-03 18:27:33 +01:00
|
|
|
}
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-04-22 11:51:28 +02:00
|
|
|
io_service_pool_.run(asynchronous);
|
2015-03-06 15:20:08 +01:00
|
|
|
|
|
|
|
return ec;
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
boost::system::error_code server::bind_and_listen(boost::system::error_code &ec,
|
|
|
|
const std::string &address,
|
|
|
|
const std::string &port,
|
|
|
|
int backlog) {
|
|
|
|
// Open the acceptor with the option to reuse the address (i.e.
|
|
|
|
// SO_REUSEADDR).
|
|
|
|
tcp::resolver resolver(io_service_pool_.get_io_service());
|
|
|
|
tcp::resolver::query query(address, port);
|
|
|
|
auto it = resolver.resolve(query, ec);
|
|
|
|
if (ec) {
|
|
|
|
return ec;
|
|
|
|
}
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
for (; it != tcp::resolver::iterator(); ++it) {
|
|
|
|
tcp::endpoint endpoint = *it;
|
|
|
|
auto acceptor = tcp::acceptor(io_service_pool_.get_io_service());
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
if (acceptor.open(endpoint.protocol(), ec)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptor.set_option(tcp::acceptor::reuse_address(true));
|
|
|
|
|
|
|
|
if (acceptor.bind(endpoint, ec)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptor.listen(
|
|
|
|
backlog == -1 ? boost::asio::socket_base::max_connections : backlog,
|
|
|
|
ec)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptors_.push_back(std::move(acceptor));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptors_.empty()) {
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ec could have some errors since we may have failed to bind some
|
|
|
|
// interfaces.
|
|
|
|
ec.clear();
|
|
|
|
|
|
|
|
return ec;
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
2015-03-06 15:20:08 +01:00
|
|
|
|
|
|
|
void server::start_accept(boost::asio::ssl::context &tls_context,
|
|
|
|
tcp::acceptor &acceptor, serve_mux &mux) {
|
2017-02-10 05:34:19 +01:00
|
|
|
|
|
|
|
if (!acceptor.is_open()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
auto new_connection = std::make_shared<connection<ssl_socket>>(
|
2015-12-21 16:33:12 +01:00
|
|
|
mux, tls_handshake_timeout_, read_timeout_,
|
|
|
|
io_service_pool_.get_io_service(), tls_context);
|
2015-03-06 15:20:08 +01:00
|
|
|
|
2015-11-12 16:53:29 +01:00
|
|
|
acceptor.async_accept(
|
|
|
|
new_connection->socket().lowest_layer(),
|
2016-10-15 11:36:04 +02:00
|
|
|
[this, &tls_context, &acceptor, &mux,
|
|
|
|
new_connection](const boost::system::error_code &e) {
|
2015-11-12 16:53:29 +01:00
|
|
|
if (!e) {
|
|
|
|
new_connection->socket().lowest_layer().set_option(
|
|
|
|
tcp::no_delay(true));
|
2015-12-21 16:33:12 +01:00
|
|
|
new_connection->start_tls_handshake_deadline();
|
2015-11-12 16:53:29 +01:00
|
|
|
new_connection->socket().async_handshake(
|
|
|
|
boost::asio::ssl::stream_base::server,
|
|
|
|
[new_connection](const boost::system::error_code &e) {
|
|
|
|
if (e) {
|
2015-12-21 16:33:12 +01:00
|
|
|
new_connection->stop();
|
2015-11-12 16:53:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tls_h2_negotiated(new_connection->socket())) {
|
2015-12-21 16:33:12 +01:00
|
|
|
new_connection->stop();
|
2015-11-12 16:53:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_connection->start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
start_accept(tls_context, acceptor, mux);
|
|
|
|
});
|
2015-03-06 15:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void server::start_accept(tcp::acceptor &acceptor, serve_mux &mux) {
|
2017-02-10 05:34:19 +01:00
|
|
|
|
2017-02-11 05:08:08 +01:00
|
|
|
if (!acceptor.is_open()) {
|
2017-02-10 05:34:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-06 15:20:08 +01:00
|
|
|
auto new_connection = std::make_shared<connection<tcp::socket>>(
|
2015-12-21 16:33:12 +01:00
|
|
|
mux, tls_handshake_timeout_, read_timeout_,
|
|
|
|
io_service_pool_.get_io_service());
|
2015-03-06 15:20:08 +01:00
|
|
|
|
2015-11-12 16:53:29 +01:00
|
|
|
acceptor.async_accept(
|
|
|
|
new_connection->socket(), [this, &acceptor, &mux, new_connection](
|
|
|
|
const boost::system::error_code &e) {
|
|
|
|
if (!e) {
|
|
|
|
new_connection->socket().set_option(tcp::no_delay(true));
|
2015-12-21 16:33:12 +01:00
|
|
|
new_connection->start_read_deadline();
|
2015-11-12 16:53:29 +01:00
|
|
|
new_connection->start();
|
|
|
|
}
|
2017-02-04 03:33:21 +01:00
|
|
|
if (acceptor.is_open()) {
|
2017-01-31 18:40:43 +01:00
|
|
|
start_accept(acceptor, mux);
|
2017-02-04 03:33:21 +01:00
|
|
|
}
|
2015-11-12 16:53:29 +01:00
|
|
|
});
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2016-01-21 11:15:32 +01:00
|
|
|
void server::stop() {
|
|
|
|
for (auto &acceptor : acceptors_) {
|
|
|
|
acceptor.close();
|
|
|
|
}
|
2017-02-10 05:34:19 +01:00
|
|
|
io_service_pool_.stop();
|
2016-01-21 11:15:32 +01:00
|
|
|
}
|
2015-04-22 11:51:28 +02:00
|
|
|
|
2015-04-23 17:16:14 +02:00
|
|
|
void server::join() { io_service_pool_.join(); }
|
2015-04-22 11:51:28 +02:00
|
|
|
|
2015-12-20 04:48:39 +01:00
|
|
|
const std::vector<std::shared_ptr<boost::asio::io_service>> &
|
2015-12-25 13:06:25 +01:00
|
|
|
server::io_services() const {
|
|
|
|
return io_service_pool_.io_services();
|
2015-12-08 15:47:04 +01:00
|
|
|
}
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
} // namespace server
|
|
|
|
} // namespace asio_http2
|
|
|
|
} // namespace nghttp2
|