blob: 5d73549b8428e593ed239b14a937bd22a4cd360b [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.
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080032 /*
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080033 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 Zhu8e3e9072013-02-28 15:40:51 -080039 */
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080040
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 Zhud9429222013-02-25 22:34:09 -080051
52 _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080053}
54
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080055server::~server()
56{
57 handle_stop();
58}
59
Zhenkai Zhua9a7e1d2013-02-25 18:29:07 -080060void 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
69void 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
78void 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
95void 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