blob: fa20be710e7ef5b902b4e41498c79be40fdecb76 [file] [log] [blame]
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -08001//
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 Zhud9429222013-02-25 22:34:09 -080014#include "logging.h"
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080015
Zhenkai Zhud9429222013-02-25 22:34:09 -080016INIT_LOGGER ("HttpServer");
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080017namespace http {
18namespace server {
19
20server::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.
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));
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 Zhud9429222013-02-25 22:34:09 -080049
50 _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080051}
52
53void server::run()
54{
55 // The io_service::run() call will block until all asynchronous operations
56 // have finished. While the server is running, there is always at least one
57 // asynchronous operation outstanding: the asynchronous accept call waiting
58 // for new incoming connections.
59 io_service_.run();
60}
61
62void server::start_accept()
63{
64 new_connection_.reset(new connection(io_service_,
65 connection_manager_, request_handler_));
66 acceptor_.async_accept(new_connection_->socket(),
67 boost::bind(&server::handle_accept, this,
68 boost::asio::placeholders::error));
69}
70
71void server::handle_accept(const boost::system::error_code& e)
72{
73 // Check whether the server was stopped by a signal before this completion
74 // handler had a chance to run.
75 if (!acceptor_.is_open())
76 {
77 return;
78 }
79
80 if (!e)
81 {
82 connection_manager_.start(new_connection_);
83 }
84
85 start_accept();
86}
87
88void server::handle_stop()
89{
90 // The server is stopped by cancelling all outstanding asynchronous
91 // operations. Once all operations have finished the io_service::run() call
92 // will exit.
93 acceptor_.close();
94 connection_manager_.stop_all();
95}
96
97} // namespace server
98} // namespace http