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