Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 1 | // |
| 2 | // server.cpp |
| 3 | // ~~~~~~~~~~ |
| 4 | // |
| 5 | // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 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 <boost/bind.hpp> |
| 13 | #include <signal.h> |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 14 | #include "logging.h" |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 15 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 16 | INIT_LOGGER ("HttpServer"); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 17 | namespace http { |
| 18 | namespace server { |
| 19 | |
| 20 | server::server(const std::string& address, const std::string& port, |
| 21 | const std::string& doc_root) |
| 22 | : io_service_(), |
| 23 | signals_(io_service_), |
| 24 | acceptor_(io_service_), |
| 25 | connection_manager_(), |
| 26 | new_connection_(), |
| 27 | request_handler_(doc_root) |
| 28 | { |
| 29 | // Register to handle the signals that indicate when the server should exit. |
| 30 | // It is safe to register for the same signal multiple times in a program, |
| 31 | // provided all registration for the specified signal is made through Asio. |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame^] | 32 | /* |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 33 | signals_.add(SIGINT); |
| 34 | signals_.add(SIGTERM); |
| 35 | #if defined(SIGQUIT) |
| 36 | signals_.add(SIGQUIT); |
| 37 | #endif // defined(SIGQUIT) |
| 38 | signals_.async_wait(boost::bind(&server::handle_stop, this)); |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame^] | 39 | */ |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 40 | |
| 41 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). |
| 42 | boost::asio::ip::tcp::resolver resolver(io_service_); |
| 43 | boost::asio::ip::tcp::resolver::query query(address, port); |
| 44 | boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); |
| 45 | acceptor_.open(endpoint.protocol()); |
| 46 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); |
| 47 | acceptor_.bind(endpoint); |
| 48 | acceptor_.listen(); |
| 49 | |
| 50 | start_accept(); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 51 | |
| 52 | _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame^] | 55 | server::~server() |
| 56 | { |
| 57 | handle_stop(); |
| 58 | } |
| 59 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 60 | void server::run() |
| 61 | { |
| 62 | // The io_service::run() call will block until all asynchronous operations |
| 63 | // have finished. While the server is running, there is always at least one |
| 64 | // asynchronous operation outstanding: the asynchronous accept call waiting |
| 65 | // for new incoming connections. |
| 66 | io_service_.run(); |
| 67 | } |
| 68 | |
| 69 | void server::start_accept() |
| 70 | { |
| 71 | new_connection_.reset(new connection(io_service_, |
| 72 | connection_manager_, request_handler_)); |
| 73 | acceptor_.async_accept(new_connection_->socket(), |
| 74 | boost::bind(&server::handle_accept, this, |
| 75 | boost::asio::placeholders::error)); |
| 76 | } |
| 77 | |
| 78 | void server::handle_accept(const boost::system::error_code& e) |
| 79 | { |
| 80 | // Check whether the server was stopped by a signal before this completion |
| 81 | // handler had a chance to run. |
| 82 | if (!acceptor_.is_open()) |
| 83 | { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (!e) |
| 88 | { |
| 89 | connection_manager_.start(new_connection_); |
| 90 | } |
| 91 | |
| 92 | start_accept(); |
| 93 | } |
| 94 | |
| 95 | void server::handle_stop() |
| 96 | { |
| 97 | // The server is stopped by cancelling all outstanding asynchronous |
| 98 | // operations. Once all operations have finished the io_service::run() call |
| 99 | // will exit. |
| 100 | acceptor_.close(); |
| 101 | connection_manager_.stop_all(); |
| 102 | } |
| 103 | |
| 104 | } // namespace server |
| 105 | } // namespace http |