|
| 1 | +#ifndef BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_IMPL_IPP_20120318 |
| 2 | +#defineBOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_IMPL_IPP_20120318 |
| 3 | + |
| 4 | +// Copyright 2012 Dean Michael Berris <dberris@google.com>. |
| 5 | +// Copyright 2012 Google, Inc. |
| 6 | +// Distributed under the Boost Software License, Version 1.0. |
| 7 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | + |
| 10 | +#include<boost/network/protocol/http/server/async_impl.hpp> |
| 11 | +#include<boost/asio/io_service.hpp> |
| 12 | +#include<boost/asio/ip/tcp.hpp> |
| 13 | +#include<boost/asio/placeholders.hpp> |
| 14 | +#include<boost/bind.hpp> |
| 15 | +#include<boost/network/detail/debug.hpp> |
| 16 | + |
| 17 | +namespaceboost {namespacenetwork {namespacehttp { |
| 18 | + |
| 19 | +async_server_impl::async_server_impl(server_optionsconst &options, |
| 20 | + function<void(requestconst &, connection_ptr)> handler, |
| 21 | + utils::thread_pool &thread_pool) |
| 22 | +: options_(options) |
| 23 | +, address_(options.address()) |
| 24 | +, port_(options.port()) |
| 25 | +, service_(options.io_service()) |
| 26 | +, acceptor_(0) |
| 27 | +, new_connection_() |
| 28 | +, listening_mutex_() |
| 29 | +, stopping_mutex_() |
| 30 | +, handler_(handler) |
| 31 | +, pool_(thread_pool) |
| 32 | +, listening_(false) |
| 33 | +, owned_service_(false) |
| 34 | +, stopping_(false) { |
| 35 | +if (service_ ==0) { |
| 36 | + service_ =new (std::nothrow) asio::io_service; |
| 37 | + owned_service_ =true; |
| 38 | + } |
| 39 | +BOOST_ASSERT(service_ !=0); |
| 40 | + acceptor_ =new (std::nothrow)asio::ip::tcp::acceptor(*service_); |
| 41 | +BOOST_ASSERT(acceptor_ !=0); |
| 42 | +} |
| 43 | + |
| 44 | +async_server_impl::~async_server_impl() { |
| 45 | +if (owned_service_)delete service_; |
| 46 | +delete acceptor_; |
| 47 | +} |
| 48 | + |
| 49 | +voidasync_server_impl::run() { |
| 50 | +listen(); |
| 51 | + service_->run(); |
| 52 | +} |
| 53 | + |
| 54 | +voidasync_server_impl::stop() { |
| 55 | + lock_guard<mutex>listening_lock(listening_mutex_); |
| 56 | +if (listening_) { |
| 57 | + lock_guard<mutex>stopping_lock(stopping_mutex_); |
| 58 | + stopping_ =true; |
| 59 | + system::error_code ignored; |
| 60 | + acceptor_->close(ignored); |
| 61 | + listening_ =false; |
| 62 | + service_->post( |
| 63 | +boost::bind(&async_server_impl::handle_stop,this)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +voidasync_server_impl::listen() { |
| 68 | + lock_guard<mutex>listening_lock(listening_mutex_); |
| 69 | +BOOST_NETWORK_MESSAGE("listening on" << address_ <<':' << port_); |
| 70 | +if (!listening_)start_listening(); |
| 71 | +if (!listening_) { |
| 72 | +BOOST_NETWORK_MESSAGE("error listening on" << address_ <<':' << port_); |
| 73 | +BOOST_THROW_EXCEPTION(std::runtime_error("Error listening on provided address:port.")); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +voidasync_server_impl::handle_stop() { |
| 78 | + lock_guard<mutex>stopping_lock(stopping_mutex_); |
| 79 | +// A user may have stopped listening again before the stop command is |
| 80 | +// reached. |
| 81 | +if (stopping_) service_->stop(); |
| 82 | +} |
| 83 | + |
| 84 | +voidasync_server_impl::handle_accept(boost::system::error_codeconst & ec) { |
| 85 | + { |
| 86 | + lock_guard<mutex>stopping_lock(stopping_mutex_); |
| 87 | +// We dont want to add another handler instance, and we dont want to know |
| 88 | +// about errors for a socket we dont need anymore. |
| 89 | +if (stopping_)return; |
| 90 | + } |
| 91 | +if (!ec) { |
| 92 | +set_socket_options(new_connection_->socket()); |
| 93 | + new_connection_->start(); |
| 94 | + new_connection_.reset( |
| 95 | +newasync_server_connection(*service_, handler_, pool_)); |
| 96 | + acceptor_->async_accept( |
| 97 | + new_connection_->socket(), |
| 98 | +boost::bind( |
| 99 | + &async_server_impl::handle_accept, |
| 100 | +this, |
| 101 | + asio::placeholders::error)); |
| 102 | + }else { |
| 103 | +BOOST_NETWORK_MESSAGE("Error accepting connection, reason:" << ec); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +voidasync_server_impl::start_listening() { |
| 108 | +using asio::ip::tcp; |
| 109 | + system::error_code error; |
| 110 | + service_->reset();// allows repeated cycles of run->stop->run |
| 111 | + tcp::resolverresolver(*service_); |
| 112 | + tcp::resolver::queryquery(address_, port_); |
| 113 | + tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error); |
| 114 | +if (error) { |
| 115 | +BOOST_NETWORK_MESSAGE("error resolving '" << address_ <<':' << port_); |
| 116 | +BOOST_THROW_EXCEPTION(std::runtime_error("Error resolving address:port combination.")); |
| 117 | + } |
| 118 | + tcp::endpoint endpoint = *endpoint_iterator; |
| 119 | + acceptor_->open(endpoint.protocol(), error); |
| 120 | +if (error) { |
| 121 | +BOOST_NETWORK_MESSAGE("error opening socket:" << address_ <<":" << port_); |
| 122 | +BOOST_THROW_EXCEPTION(std::runtime_error("Error opening socket.")); |
| 123 | + } |
| 124 | +set_acceptor_options(*acceptor_); |
| 125 | + acceptor_->bind(endpoint, error); |
| 126 | +if (error) { |
| 127 | +BOOST_NETWORK_MESSAGE("error binding socket:" << address_ <<":" << port_); |
| 128 | +BOOST_THROW_EXCEPTION(std::runtime_error("Error binding socket.")); |
| 129 | + } |
| 130 | + acceptor_->listen(asio::socket_base::max_connections, error); |
| 131 | +if (error) { |
| 132 | +BOOST_NETWORK_MESSAGE("error listening on socket: '" << error <<"' on" << address_ <<":" << port_); |
| 133 | +BOOST_THROW_EXCEPTION(std::runtime_error("Error listening on socket.")); |
| 134 | + } |
| 135 | + new_connection_.reset(newasync_server_connection(*service_, handler_, pool_)); |
| 136 | + acceptor_->async_accept( |
| 137 | + new_connection_->socket(), |
| 138 | +boost::bind( |
| 139 | + &async_server_impl::handle_accept, |
| 140 | +this, |
| 141 | + asio::placeholders::error)); |
| 142 | + listening_ =true; |
| 143 | + lock_guard<mutex>stopping_lock(stopping_mutex_); |
| 144 | + stopping_ =false;// if we were in the process of stopping, we revoke that command and continue listening |
| 145 | +BOOST_NETWORK_MESSAGE("now listening on '" << address_ <<":" << port_ <<"'"); |
| 146 | +} |
| 147 | + |
| 148 | +voidasync_server_impl::set_socket_options(asio::ip::tcp::socket &socket) { |
| 149 | + system::error_code ignored; |
| 150 | + socket.non_blocking(options_.non_blocking_io(), ignored); |
| 151 | +if (options_.linger()) { |
| 152 | + asio::ip::tcp::socket::lingerlinger(true, options_.linger_timeout()); |
| 153 | + socket.set_option(linger, ignored); |
| 154 | + } |
| 155 | +if (int buf_size = options_.receive_buffer_size() >=0) { |
| 156 | + asio::ip::tcp::socket::receive_buffer_sizereceive_buffer_size(buf_size); |
| 157 | + socket.set_option(receive_buffer_size, ignored); |
| 158 | + } |
| 159 | +if (int buf_size = options_.send_buffer_size() >=0) { |
| 160 | + asio::ip::tcp::socket::send_buffer_sizesend_buffer_size(buf_size); |
| 161 | + socket.set_option(send_buffer_size, ignored); |
| 162 | + } |
| 163 | +if (int buf_size = options_.receive_low_watermark() >=0) { |
| 164 | + asio::ip::tcp::socket::receive_low_watermarkreceive_low_watermark(buf_size); |
| 165 | + socket.set_option(receive_low_watermark, ignored); |
| 166 | + } |
| 167 | +if (int buf_size = options_.send_low_watermark() >=0) { |
| 168 | + asio::ip::tcp::socket::send_low_watermarksend_low_watermark(buf_size); |
| 169 | + socket.set_option(send_low_watermark, ignored); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +voidasync_server_impl::set_acceptor_options(asio::ip::tcp::acceptor &acceptor) { |
| 174 | + system::error_code ignored; |
| 175 | + acceptor.set_option( |
| 176 | +asio::ip::tcp::acceptor::reuse_address(options_.reuse_address()), |
| 177 | + ignored); |
| 178 | + acceptor.set_option( |
| 179 | +asio::ip::tcp::acceptor::enable_connection_aborted(options_.report_aborted()), |
| 180 | + ignored); |
| 181 | +} |
| 182 | + |
| 183 | +}// namespace http |
| 184 | + |
| 185 | +}// namespace network |
| 186 | + |
| 187 | +}// namespace boost |
| 188 | + |
| 189 | +#endif// BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_IMPL_IPP_20120318 |