Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 1 | // |
| 2 | // server.hpp |
| 3 | // ~~~~~~~~~~ |
| 4 | // |
| 5 | // Copyright (c) 2003-2012 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_SERVER_HPP |
| 12 | #define HTTP_SERVER_HPP |
| 13 | |
| 14 | #include <boost/asio.hpp> |
| 15 | #include <string> |
| 16 | #include <boost/noncopyable.hpp> |
| 17 | #include "connection.hpp" |
| 18 | #include "connection_manager.hpp" |
| 19 | #include "request_handler.hpp" |
| 20 | |
| 21 | namespace http { |
| 22 | namespace server { |
| 23 | |
| 24 | /// The top-level class of the HTTP server. |
| 25 | class server |
| 26 | : private boost::noncopyable |
| 27 | { |
| 28 | public: |
| 29 | /// Construct the server to listen on the specified TCP address and port, and |
| 30 | /// serve up files from the given directory. |
| 31 | explicit server(const std::string& address, const std::string& port, |
| 32 | const std::string& doc_root); |
| 33 | |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 34 | ~server(); |
| 35 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 36 | /// Run the server's io_service loop. |
| 37 | void run(); |
| 38 | |
Zhenkai Zhu | 907cbaf | 2013-02-27 22:29:23 -0800 | [diff] [blame] | 39 | /// Handle a request to stop the server. |
| 40 | void handle_stop(); |
| 41 | |
Zhenkai Zhu | 377cbeb | 2013-02-28 16:36:26 -0800 | [diff] [blame] | 42 | private: |
| 43 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 44 | /// Initiate an asynchronous accept operation. |
| 45 | void start_accept(); |
| 46 | |
| 47 | /// Handle completion of an asynchronous accept operation. |
| 48 | void handle_accept(const boost::system::error_code& e); |
| 49 | |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 50 | /// The io_service used to perform asynchronous operations. |
| 51 | boost::asio::io_service io_service_; |
| 52 | |
| 53 | /// The signal_set is used to register for process termination notifications. |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 54 | // boost::asio::signal_set signals_; |
Zhenkai Zhu | a9a7e1d | 2013-02-25 18:29:07 -0800 | [diff] [blame] | 55 | |
| 56 | /// Acceptor used to listen for incoming connections. |
| 57 | boost::asio::ip::tcp::acceptor acceptor_; |
| 58 | |
| 59 | /// The connection manager which owns all live connections. |
| 60 | connection_manager connection_manager_; |
| 61 | |
| 62 | /// The next connection to be accepted. |
| 63 | connection_ptr new_connection_; |
| 64 | |
| 65 | /// The handler for all incoming requests. |
| 66 | request_handler request_handler_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace server |
| 70 | } // namespace http |
| 71 | |
| 72 | #endif // HTTP_SERVER_HPP |