server.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // server.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #include "server.hpp"
  11. #include <signal.h>
  12. #include <utility>
  13. namespace http {
  14. namespace server {
  15. server::server(const std::string& address, const std::string& port,
  16. const std::string& doc_root)
  17. : io_context_(1),
  18. signals_(io_context_),
  19. acceptor_(io_context_),
  20. connection_manager_(),
  21. request_handler_(doc_root)
  22. {
  23. // Register to handle the signals that indicate when the server should exit.
  24. // It is safe to register for the same signal multiple times in a program,
  25. // provided all registration for the specified signal is made through Asio.
  26. signals_.add(SIGINT);
  27. signals_.add(SIGTERM);
  28. #if defined(SIGQUIT)
  29. signals_.add(SIGQUIT);
  30. #endif // defined(SIGQUIT)
  31. do_await_stop();
  32. // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
  33. boost::asio::ip::tcp::resolver resolver(io_context_);
  34. boost::asio::ip::tcp::endpoint endpoint =
  35. *resolver.resolve(address, port).begin();
  36. acceptor_.open(endpoint.protocol());
  37. acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  38. acceptor_.bind(endpoint);
  39. acceptor_.listen();
  40. do_accept();
  41. }
  42. void server::run()
  43. {
  44. // The io_context::run() call will block until all asynchronous operations
  45. // have finished. While the server is running, there is always at least one
  46. // asynchronous operation outstanding: the asynchronous accept call waiting
  47. // for new incoming connections.
  48. io_context_.run();
  49. }
  50. void server::do_accept()
  51. {
  52. acceptor_.async_accept(
  53. [this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket)
  54. {
  55. // Check whether the server was stopped by a signal before this
  56. // completion handler had a chance to run.
  57. if (!acceptor_.is_open())
  58. {
  59. return;
  60. }
  61. if (!ec)
  62. {
  63. connection_manager_.start(std::make_shared<connection>(
  64. std::move(socket), connection_manager_, request_handler_));
  65. }
  66. do_accept();
  67. });
  68. }
  69. void server::do_await_stop()
  70. {
  71. signals_.async_wait(
  72. [this](boost::system::error_code /*ec*/, int /*signo*/)
  73. {
  74. // The server is stopped by cancelling all outstanding asynchronous
  75. // operations. Once all operations have finished the io_context::run()
  76. // call will exit.
  77. acceptor_.close();
  78. connection_manager_.stop_all();
  79. });
  80. }
  81. } // namespace server
  82. } // namespace http