1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 // We wrote this code based on the original code which has the
26 // following license:
27 //
28 // server.hpp
29 // ~~~~~~~~~~
30 //
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
32 //
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
35 //
36 
37 #ifndef ASIO_SERVER_H
38 #define ASIO_SERVER_H
39 
40 #include "nghttp2_config.h"
41 
42 #include <string>
43 #include <vector>
44 #include <memory>
45 
46 #include <boost/noncopyable.hpp>
47 
48 #include <nghttp2/asio_http2_server.h>
49 
50 #include "asio_io_service_pool.h"
51 
52 namespace nghttp2 {
53 
54 namespace asio_http2 {
55 
56 namespace server {
57 
58 class serve_mux;
59 
60 using boost::asio::ip::tcp;
61 
62 using ssl_socket = boost::asio::ssl::stream<tcp::socket>;
63 
64 class server : private boost::noncopyable {
65 public:
66   explicit server(std::size_t io_service_pool_size,
67                   const boost::posix_time::time_duration &tls_handshake_timeout,
68                   const boost::posix_time::time_duration &read_timeout);
69 
70   boost::system::error_code
71   listen_and_serve(boost::system::error_code &ec,
72                    boost::asio::ssl::context *tls_context,
73                    const std::string &address, const std::string &port,
74                    int backlog, serve_mux &mux, bool asynchronous = false);
75   void join();
76   void stop();
77 
78   /// Get access to all io_service objects.
79   const std::vector<std::shared_ptr<boost::asio::io_service>> &
80   io_services() const;
81 
82   /// Returns a vector with all the acceptors ports in use.
83   const std::vector<int> ports() const;
84 
85 private:
86   /// Initiate an asynchronous accept operation.
87   void start_accept(tcp::acceptor &acceptor, serve_mux &mux);
88   /// Same as above but with tls_context
89   void start_accept(boost::asio::ssl::context &tls_context,
90                     tcp::acceptor &acceptor, serve_mux &mux);
91 
92   /// Resolves address and bind socket to the resolved addresses.
93   boost::system::error_code bind_and_listen(boost::system::error_code &ec,
94                                             const std::string &address,
95                                             const std::string &port,
96                                             int backlog);
97 
98   /// The pool of io_service objects used to perform asynchronous
99   /// operations.
100   io_service_pool io_service_pool_;
101 
102   /// Acceptor used to listen for incoming connections.
103   std::vector<tcp::acceptor> acceptors_;
104 
105   std::unique_ptr<boost::asio::ssl::context> ssl_ctx_;
106 
107   boost::posix_time::time_duration tls_handshake_timeout_;
108   boost::posix_time::time_duration read_timeout_;
109 };
110 
111 } // namespace server
112 
113 } // namespace asio_http2
114 
115 } // namespace nghttp2
116 
117 #endif // ASIO_SERVER_H
118