5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
12 #include <boost/thread/thread.hpp>
13 #include <boost/bind.hpp>
14 #include <boost/shared_ptr.hpp>
20 server::server(const std::string& address, const std::string& port,
21 const std::string& doc_root, std::size_t thread_pool_size)
22 : thread_pool_size_(thread_pool_size),
23 signals_(io_service_),
24 acceptor_(io_service_),
26 request_handler_(doc_root)
28 // Register to handle the signals that indicate when the server should exit.
29 // It is safe to register for the same signal multiple times in a program,
30 // provided all registration for the specified signal is made through Asio.
32 signals_.add(SIGTERM);
34 signals_.add(SIGQUIT);
35 #endif // defined(SIGQUIT)
36 signals_.async_wait(boost::bind(&server::handle_stop, this));
38 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
39 boost::asio::ip::tcp::resolver resolver(io_service_);
40 boost::asio::ip::tcp::resolver::query query(address, port);
41 boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
42 acceptor_.open(endpoint.protocol());
43 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
44 acceptor_.bind(endpoint);
52 // Create a pool of threads to run all of the io_services.
53 std::vector<boost::shared_ptr<boost::thread> > threads;
54 for (std::size_t i = 0; i < thread_pool_size_; ++i)
56 boost::shared_ptr<boost::thread> thread(new boost::thread(
57 boost::bind(&boost::asio::io_service::run, &io_service_)));
58 threads.push_back(thread);
61 // Wait for all threads in the pool to exit.
62 for (std::size_t i = 0; i < threads.size(); ++i)
66 void server::start_accept()
68 new_connection_.reset(new connection(io_service_, request_handler_));
69 acceptor_.async_accept(new_connection_->socket(),
70 boost::bind(&server::handle_accept, this,
71 boost::asio::placeholders::error));
74 void server::handle_accept(const boost::system::error_code& e)
78 new_connection_->start();
84 void server::handle_stop()
89 } // namespace server3