server.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // server.hpp
  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. #ifndef HTTP_SERVER_HPP
  11. #define HTTP_SERVER_HPP
  12. #include <boost/asio.hpp>
  13. #include <string>
  14. #include "connection.hpp"
  15. #include "connection_manager.hpp"
  16. #include "request_handler.hpp"
  17. namespace http {
  18. namespace server {
  19. /// The top-level class of the HTTP server.
  20. class server
  21. {
  22. public:
  23. server(const server&) = delete;
  24. server& operator=(const server&) = delete;
  25. /// Construct the server to listen on the specified TCP address and port, and
  26. /// serve up files from the given directory.
  27. explicit server(const std::string& address, const std::string& port,
  28. const std::string& doc_root);
  29. /// Run the server's io_context loop.
  30. void run();
  31. private:
  32. /// Perform an asynchronous accept operation.
  33. void do_accept();
  34. /// Wait for a request to stop the server.
  35. void do_await_stop();
  36. /// The io_context used to perform asynchronous operations.
  37. boost::asio::io_context io_context_;
  38. /// The signal_set is used to register for process termination notifications.
  39. boost::asio::signal_set signals_;
  40. /// Acceptor used to listen for incoming connections.
  41. boost::asio::ip::tcp::acceptor acceptor_;
  42. /// The connection manager which owns all live connections.
  43. connection_manager connection_manager_;
  44. /// The handler for all incoming requests.
  45. request_handler request_handler_;
  46. };
  47. } // namespace server
  48. } // namespace http
  49. #endif // HTTP_SERVER_HPP