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_(), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame^] | 23 | // signals_(io_service_), |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame^] | 32 | // signals_.add(SIGINT); |
| 33 | // signals_.add(SIGTERM); |
| 34 | //#if defined(SIGQUIT) |
| 35 | // signals_.add(SIGQUIT); |
| 36 | //#endif // defined(SIGQUIT) |
| 37 | // signals_.async_wait(boost::bind(&server::handle_stop, this)); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 38 | |
| 39 | // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). |
| 40 | boost::asio::ip::tcp::resolver resolver(io_service_); |
| 41 | boost::asio::ip::tcp::resolver::query query(address, port); |
| 42 | boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); |
| 43 | acceptor_.open(endpoint.protocol()); |
| 44 | acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); |
| 45 | acceptor_.bind(endpoint); |
| 46 | acceptor_.listen(); |
| 47 | |
| 48 | start_accept(); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 49 | |
| 50 | _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 53 | server::~server() |
| 54 | { |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 55 | // handle_stop(); |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 58 | void server::run() |
| 59 | { |
| 60 | // The io_service::run() call will block until all asynchronous operations |
| 61 | // have finished. While the server is running, there is always at least one |
| 62 | // asynchronous operation outstanding: the asynchronous accept call waiting |
| 63 | // for new incoming connections. |
| 64 | io_service_.run(); |
| 65 | } |
| 66 | |
| 67 | void server::start_accept() |
| 68 | { |
| 69 | new_connection_.reset(new connection(io_service_, |
| 70 | connection_manager_, request_handler_)); |
| 71 | acceptor_.async_accept(new_connection_->socket(), |
| 72 | boost::bind(&server::handle_accept, this, |
| 73 | boost::asio::placeholders::error)); |
| 74 | } |
| 75 | |
| 76 | void server::handle_accept(const boost::system::error_code& e) |
| 77 | { |
| 78 | // Check whether the server was stopped by a signal before this completion |
| 79 | // handler had a chance to run. |
| 80 | if (!acceptor_.is_open()) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (!e) |
| 86 | { |
| 87 | connection_manager_.start(new_connection_); |
| 88 | } |
| 89 | |
| 90 | start_accept(); |
| 91 | } |
| 92 | |
| 93 | void server::handle_stop() |
| 94 | { |
| 95 | // The server is stopped by cancelling all outstanding asynchronous |
| 96 | // operations. Once all operations have finished the io_service::run() call |
| 97 | // will exit. |
| 98 | acceptor_.close(); |
| 99 | connection_manager_.stop_all(); |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 100 | // although they say io_service::run() would stop, but it didn't happen.. |
| 101 | // the thread join was blocking, waiting for io_service::run() to finish |
| 102 | // even after handle_stop() call.. |
| 103 | // so just force it quit, no harm here. |
| 104 | io_service_.stop(); |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace server |
| 108 | } // namespace http |