2015-03-01 02:02:49 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef ASIO_CLIENT_SESSION_IMPL_H
|
|
|
|
#define ASIO_CLIENT_SESSION_IMPL_H
|
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
2015-03-05 18:05:01 +01:00
|
|
|
#include <boost/array.hpp>
|
|
|
|
|
2015-03-03 17:34:00 +01:00
|
|
|
#include <nghttp2/asio_http2_client.h>
|
2015-03-01 02:02:49 +01:00
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
#include "template.h"
|
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
namespace nghttp2 {
|
|
|
|
namespace asio_http2 {
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
class stream;
|
|
|
|
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
2015-12-21 15:38:32 +01:00
|
|
|
class session_impl : public std::enable_shared_from_this<session_impl> {
|
2015-03-01 02:02:49 +01:00
|
|
|
public:
|
2016-02-06 03:05:13 +01:00
|
|
|
session_impl(boost::asio::io_service &io_service,
|
|
|
|
const boost::posix_time::time_duration &connect_timeout);
|
2015-03-01 02:02:49 +01:00
|
|
|
virtual ~session_impl();
|
|
|
|
|
2015-03-03 16:08:47 +01:00
|
|
|
void start_resolve(const std::string &host, const std::string &service);
|
|
|
|
|
2015-03-03 17:23:35 +01:00
|
|
|
void connected(tcp::resolver::iterator endpoint_it);
|
2015-03-01 02:02:49 +01:00
|
|
|
void not_connected(const boost::system::error_code &ec);
|
|
|
|
|
2015-03-03 17:23:35 +01:00
|
|
|
void on_connect(connect_cb cb);
|
2015-03-01 02:02:49 +01:00
|
|
|
void on_error(error_cb cb);
|
|
|
|
|
2015-03-03 17:23:35 +01:00
|
|
|
const connect_cb &on_connect() const;
|
2015-03-01 02:02:49 +01:00
|
|
|
const error_cb &on_error() const;
|
|
|
|
|
2015-03-07 15:04:31 +01:00
|
|
|
int write_trailer(stream &strm, header_map h);
|
|
|
|
|
2015-03-04 15:53:54 +01:00
|
|
|
void cancel(stream &strm, uint32_t error_code);
|
2015-03-04 16:09:26 +01:00
|
|
|
void resume(stream &strm);
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
std::unique_ptr<stream> create_stream();
|
|
|
|
std::unique_ptr<stream> pop_stream(int32_t stream_id);
|
|
|
|
stream *create_push_stream(int32_t stream_id);
|
|
|
|
stream *find_stream(int32_t stream_id);
|
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
const request *submit(boost::system::error_code &ec,
|
|
|
|
const std::string &method, const std::string &uri,
|
2015-03-05 17:47:55 +01:00
|
|
|
generator_cb cb, header_map h);
|
2015-03-01 02:02:49 +01:00
|
|
|
|
2015-03-03 16:08:47 +01:00
|
|
|
virtual void start_connect(tcp::resolver::iterator endpoint_it) = 0;
|
2015-03-01 02:02:49 +01:00
|
|
|
virtual tcp::socket &socket() = 0;
|
|
|
|
virtual void read_socket(std::function<
|
|
|
|
void(const boost::system::error_code &ec, std::size_t n)> h) = 0;
|
|
|
|
virtual void write_socket(std::function<
|
|
|
|
void(const boost::system::error_code &ec, std::size_t n)> h) = 0;
|
|
|
|
virtual void shutdown_socket() = 0;
|
|
|
|
|
|
|
|
void shutdown();
|
|
|
|
|
2015-03-03 16:08:47 +01:00
|
|
|
boost::asio::io_service &io_service();
|
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
void signal_write();
|
|
|
|
|
|
|
|
void enter_callback();
|
|
|
|
void leave_callback();
|
|
|
|
|
|
|
|
void do_read();
|
|
|
|
void do_write();
|
|
|
|
|
2015-12-21 15:38:32 +01:00
|
|
|
void read_timeout(const boost::posix_time::time_duration &t);
|
|
|
|
|
|
|
|
void stop();
|
2016-02-06 03:05:13 +01:00
|
|
|
bool stopped() const;
|
2015-12-21 15:38:32 +01:00
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
protected:
|
2015-06-21 07:32:47 +02:00
|
|
|
boost::array<uint8_t, 8_k> rb_;
|
|
|
|
boost::array<uint8_t, 64_k> wb_;
|
2015-03-01 02:02:49 +01:00
|
|
|
std::size_t wblen_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool should_stop() const;
|
|
|
|
bool setup_session();
|
2015-04-13 14:33:43 +02:00
|
|
|
void call_error_cb(const boost::system::error_code &ec);
|
2015-12-21 15:38:32 +01:00
|
|
|
void handle_deadline();
|
2015-03-01 02:02:49 +01:00
|
|
|
|
2015-03-03 16:08:47 +01:00
|
|
|
boost::asio::io_service &io_service_;
|
|
|
|
tcp::resolver resolver_;
|
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
std::map<int32_t, std::unique_ptr<stream>> streams_;
|
|
|
|
|
2015-03-03 17:23:35 +01:00
|
|
|
connect_cb connect_cb_;
|
2015-03-01 02:02:49 +01:00
|
|
|
error_cb error_cb_;
|
|
|
|
|
2015-12-21 15:38:32 +01:00
|
|
|
boost::asio::deadline_timer deadline_;
|
|
|
|
boost::posix_time::time_duration connect_timeout_;
|
|
|
|
boost::posix_time::time_duration read_timeout_;
|
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
nghttp2_session *session_;
|
|
|
|
|
|
|
|
const uint8_t *data_pending_;
|
|
|
|
std::size_t data_pendinglen_;
|
|
|
|
|
|
|
|
bool writing_;
|
|
|
|
bool inside_callback_;
|
2015-12-21 15:38:32 +01:00
|
|
|
bool stopped_;
|
2015-03-01 02:02:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace client
|
|
|
|
} // namespace asio_http2
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // ASIO_CLIENT_SESSION_IMPL_H
|