1 // 2 // server.hpp 3 // ~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2020 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 11 #ifndef HTTP_SERVER3_SERVER_HPP 12 #define HTTP_SERVER3_SERVER_HPP 13 14 #include <boost/asio.hpp> 15 #include <string> 16 #include <vector> 17 #include <boost/noncopyable.hpp> 18 #include <boost/shared_ptr.hpp> 19 #include "connection.hpp" 20 #include "request_handler.hpp" 21 22 namespace http { 23 namespace server3 { 24 25 /// The top-level class of the HTTP server. 26 class server 27 : private boost::noncopyable 28 { 29 public: 30 /// Construct the server to listen on the specified TCP address and port, and 31 /// serve up files from the given directory. 32 explicit server(const std::string& address, const std::string& port, 33 const std::string& doc_root, std::size_t thread_pool_size); 34 35 /// Run the server's io_context loop. 36 void run(); 37 38 private: 39 /// Initiate an asynchronous accept operation. 40 void start_accept(); 41 42 /// Handle completion of an asynchronous accept operation. 43 void handle_accept(const boost::system::error_code& e); 44 45 /// Handle a request to stop the server. 46 void handle_stop(); 47 48 /// The number of threads that will call io_context::run(). 49 std::size_t thread_pool_size_; 50 51 /// The io_context used to perform asynchronous operations. 52 boost::asio::io_context io_context_; 53 54 /// The signal_set is used to register for process termination notifications. 55 boost::asio::signal_set signals_; 56 57 /// Acceptor used to listen for incoming connections. 58 boost::asio::ip::tcp::acceptor acceptor_; 59 60 /// The next connection to be accepted. 61 connection_ptr new_connection_; 62 63 /// The handler for all incoming requests. 64 request_handler request_handler_; 65 }; 66 67 } // namespace server3 68 } // namespace http 69 70 #endif // HTTP_SERVER3_SERVER_HPP 71