http_server.cpp
Go to the documentation of this file.
2 
4 
5 namespace async_web_server_cpp
6 {
7 
8 HttpServer::HttpServer(const std::string& address, const std::string& port,
9  HttpServerRequestHandler request_handler,
10  std::size_t thread_pool_size)
11  : acceptor_(io_service_), thread_pool_size_(thread_pool_size),
12  request_handler_(request_handler)
13 {
14 
15  boost::asio::ip::tcp::resolver resolver(io_service_);
16  boost::asio::ip::tcp::resolver::query query(
17  address, port, boost::asio::ip::resolver_query_base::flags());
18  boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
19  acceptor_.open(endpoint.protocol());
20  acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
21  acceptor_.bind(endpoint);
22  acceptor_.listen();
23 }
24 
26 {
27  stop();
28 }
29 
31 {
32  start_accept();
33  for (std::size_t i = 0; i < thread_pool_size_; ++i)
34  {
35  boost::shared_ptr<boost::thread> thread(new boost::thread(
36  boost::bind(&boost::asio::io_service::run, &io_service_)));
37  threads_.push_back(thread);
38  }
39 }
40 
42 {
44  acceptor_.async_accept(new_connection_->socket(),
45  boost::bind(&HttpServer::handle_accept, this,
46  boost::asio::placeholders::error));
47 }
48 
49 void HttpServer::handle_accept(const boost::system::error_code& e)
50 {
51  if (!e)
52  {
53  new_connection_->start();
54  }
55  start_accept();
56 }
57 
59 {
60  if (acceptor_.is_open())
61  {
62  acceptor_.cancel();
63  acceptor_.close();
64  }
65  io_service_.stop();
66  // Wait for all threads in the pool to exit.
67  for (std::size_t i = 0; i < threads_.size(); ++i)
68  threads_[i]->join();
69  threads_.clear();
70 }
71 
72 } // namespace async_web_server_cpp
async_web_server_cpp::HttpServer::~HttpServer
~HttpServer()
Definition: http_server.cpp:25
async_web_server_cpp::HttpServer::start_accept
void start_accept()
Definition: http_server.cpp:41
async_web_server_cpp::HttpServer::HttpServer
HttpServer(const std::string &address, const std::string &port, HttpServerRequestHandler request_handler, std::size_t thread_pool_size)
Definition: http_server.cpp:8
async_web_server_cpp::HttpServer::request_handler_
HttpServerRequestHandler request_handler_
Definition: http_server.hpp:48
async_web_server_cpp::HttpServerRequestHandler
boost::function< bool(const HttpRequest &, boost::shared_ptr< HttpConnection >, const char *begin, const char *end)> HttpServerRequestHandler
Definition: http_request_handler.hpp:12
async_web_server_cpp::HttpConnection
Definition: http_connection.hpp:34
async_web_server_cpp::HttpServer::thread_pool_size_
std::size_t thread_pool_size_
Definition: http_server.hpp:45
async_web_server_cpp::HttpServer::acceptor_
boost::asio::ip::tcp::acceptor acceptor_
Definition: http_server.hpp:44
async_web_server_cpp
Definition: http_connection.hpp:14
http_server.hpp
async_web_server_cpp::HttpServer::threads_
std::vector< boost::shared_ptr< boost::thread > > threads_
Definition: http_server.hpp:46
async_web_server_cpp::HttpServer::handle_accept
void handle_accept(const boost::system::error_code &e)
Definition: http_server.cpp:49
async_web_server_cpp::HttpServer::new_connection_
boost::shared_ptr< HttpConnection > new_connection_
Definition: http_server.hpp:47
async_web_server_cpp::HttpServer::io_service_
boost::asio::io_service io_service_
Definition: http_server.hpp:43
http_reply.hpp
async_web_server_cpp::HttpServer::run
void run()
Definition: http_server.cpp:30
async_web_server_cpp::HttpServer::stop
void stop()
Definition: http_server.cpp:58


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06