tizen 2.4 release
[external/nghttp2.git] / src / asio_server.h
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 HTTP_SERVER2_SERVER_HPP
38 #define HTTP_SERVER2_SERVER_HPP
39
40 #include "nghttp2_config.h"
41
42 #include <string>
43 #include <vector>
44 #include <memory>
45 #include <boost/noncopyable.hpp>
46 #include <boost/asio.hpp>
47 #include <boost/asio/ssl.hpp>
48
49 #include <nghttp2/asio_http2.h>
50
51 #include "asio_connection.h"
52 #include "asio_io_service_pool.h"
53
54 namespace nghttp2 {
55
56 namespace asio_http2 {
57
58 namespace server {
59
60 /// The top-level class of the HTTP server.
61 class server : private boost::noncopyable {
62 public:
63   /// Construct the server to listen on the specified TCP address and port, and
64   /// serve up files from the given directory.
65   explicit server(const std::string &address, uint16_t port,
66                   std::size_t io_service_pool_size,
67                   std::size_t thread_pool_size, request_cb cb,
68                   std::unique_ptr<boost::asio::ssl::context> ssl_ctx,
69                   int backlog = -1);
70
71   /// Run the server's io_service loop.
72   void run();
73
74 private:
75   /// Initiate an asynchronous accept operation.
76   void start_accept();
77
78   void start_timer();
79
80   /// The pool of io_service objects used to perform asynchronous operations.
81   io_service_pool io_service_pool_;
82
83   /// The signal_set is used to register for process termination notifications.
84   boost::asio::signal_set signals_;
85
86   boost::asio::deadline_timer tick_timer_;
87
88   /// Acceptor used to listen for incoming connections.
89   std::vector<boost::asio::ip::tcp::acceptor> acceptors_;
90
91   std::unique_ptr<boost::asio::ssl::context> ssl_ctx_;
92
93   request_cb request_cb_;
94 };
95
96 } // namespace server
97
98 } // namespace asio_http2
99
100 } // namespace nghttp2
101
102 #endif // HTTP_SERVER2_SERVER_HPP