server.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // server.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 EXPERIMENTAL_HTTP_SERVER_HTTP_SERVER_H
  11. #define EXPERIMENTAL_HTTP_SERVER_HTTP_SERVER_H
  12. #include <boost/asio.hpp>
  13. #include <string>
  14. #include <boost/noncopyable.hpp>
  15. #include "Experimental/http_server/connection.h"
  16. #include "Experimental/http_server/connection_manager.h"
  17. #include "Experimental/http_server/request_handler.h"
  18. namespace http_server {
  19. /// The top-level class of the HTTP server.
  20. class server: private boost::noncopyable
  21. {
  22. public:
  23. /// Construct the server to listen on the specified TCP address and port, and
  24. /// serve up files from the given directory.
  25. explicit server(const std::string& address, const std::string& port,
  26. const std::string& doc_root);
  27. /// Run the server's io_service loop.
  28. void run();
  29. /// Stop the server.
  30. void stop();
  31. private:
  32. /// Handle completion of an asynchronous accept operation.
  33. void handle_accept(const boost::system::error_code& e);
  34. /// Handle a request to stop the server.
  35. void handle_stop();
  36. /// The io_service used to perform asynchronous operations.
  37. boost::asio::io_service io_service_;
  38. /// Acceptor used to listen for incoming connections.
  39. boost::asio::ip::tcp::acceptor acceptor_;
  40. /// The connection manager which owns all live connections.
  41. connection_manager connection_manager_;
  42. /// The next connection to be accepted.
  43. connection_ptr new_connection_;
  44. /// The handler for all incoming requests.
  45. request_handler request_handler_;
  46. };
  47. } // namespace http_server
  48. #endif // EXPERIMENTAL_HTTP_SERVER_HTTP_SERVER_H