1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2015 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 #ifndef ASIO_CLIENT_SESSION_IMPL_H 26 #define ASIO_CLIENT_SESSION_IMPL_H 27 28 #include "nghttp2_config.h" 29 30 #include <boost/array.hpp> 31 32 #include <nghttp2/asio_http2_client.h> 33 34 #include "template.h" 35 36 namespace nghttp2 { 37 namespace asio_http2 { 38 namespace client { 39 40 class stream; 41 42 using boost::asio::ip::tcp; 43 44 class session_impl : public std::enable_shared_from_this<session_impl> { 45 public: 46 session_impl(boost::asio::io_service &io_service, 47 const boost::posix_time::time_duration &connect_timeout); 48 virtual ~session_impl(); 49 50 void start_resolve(const std::string &host, const std::string &service); 51 52 void connected(tcp::resolver::iterator endpoint_it); 53 void not_connected(const boost::system::error_code &ec); 54 55 void on_connect(connect_cb cb); 56 void on_error(error_cb cb); 57 58 const connect_cb &on_connect() const; 59 const error_cb &on_error() const; 60 61 int write_trailer(stream &strm, header_map h); 62 63 void cancel(stream &strm, uint32_t error_code); 64 void resume(stream &strm); 65 66 std::unique_ptr<stream> create_stream(); 67 std::unique_ptr<stream> pop_stream(int32_t stream_id); 68 stream *create_push_stream(int32_t stream_id); 69 stream *find_stream(int32_t stream_id); 70 71 const request *submit(boost::system::error_code &ec, 72 const std::string &method, const std::string &uri, 73 generator_cb cb, header_map h, priority_spec spec); 74 75 virtual void start_connect(tcp::resolver::iterator endpoint_it) = 0; 76 virtual tcp::socket &socket() = 0; 77 virtual void read_socket( 78 std::function<void(const boost::system::error_code &ec, std::size_t n)> 79 h) = 0; 80 virtual void write_socket( 81 std::function<void(const boost::system::error_code &ec, std::size_t n)> 82 h) = 0; 83 virtual void shutdown_socket() = 0; 84 85 void shutdown(); 86 87 boost::asio::io_service &io_service(); 88 89 void signal_write(); 90 91 void enter_callback(); 92 void leave_callback(); 93 94 void do_read(); 95 void do_write(); 96 97 void read_timeout(const boost::posix_time::time_duration &t); 98 99 void stop(); 100 bool stopped() const; 101 102 protected: 103 boost::array<uint8_t, 8_k> rb_; 104 boost::array<uint8_t, 64_k> wb_; 105 std::size_t wblen_; 106 107 private: 108 bool should_stop() const; 109 bool setup_session(); 110 void call_error_cb(const boost::system::error_code &ec); 111 void handle_deadline(); 112 void start_ping(); 113 void handle_ping(const boost::system::error_code &ec); 114 115 boost::asio::io_service &io_service_; 116 tcp::resolver resolver_; 117 118 std::map<int32_t, std::unique_ptr<stream>> streams_; 119 120 connect_cb connect_cb_; 121 error_cb error_cb_; 122 123 boost::asio::deadline_timer deadline_; 124 boost::posix_time::time_duration connect_timeout_; 125 boost::posix_time::time_duration read_timeout_; 126 127 boost::asio::deadline_timer ping_; 128 129 nghttp2_session *session_; 130 131 const uint8_t *data_pending_; 132 std::size_t data_pendinglen_; 133 134 bool writing_; 135 bool inside_callback_; 136 bool stopped_; 137 }; 138 139 } // namespace client 140 } // namespace asio_http2 141 } // namespace nghttp2 142 143 #endif // ASIO_CLIENT_SESSION_IMPL_H 144