-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasio_server_base_handler.h
120 lines (82 loc) · 3.04 KB
/
asio_server_base_handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef ASIO_SERVER_BASE_HANDLER_H
#define ASIO_SERVER_BASE_HANDLER_H
#include "nghttp2_config.h"
#include <map>
#include <functional>
#include <string>
#include <mutex>
#include <boost/array.hpp>
#include <nghttp2/asio_httpx_server.h>
namespace nghttp2
{
namespace asio_http2
{
namespace server
{
class base_handler;
class asio_server_stream;
class serve_mux;
class asio_server_response;
using connection_write = std::function<void(void)>;
struct callback_guard
{
callback_guard(base_handler& h);
~callback_guard();
base_handler& handler;
};
class base_handler
{
public:
base_handler(boost::asio::io_service& io_service,
boost::asio::ip::tcp::endpoint ep, connection_write writefun,
serve_mux& mux,
const H2Server_Config_Schema& conf);
virtual ~base_handler();
virtual int start() = 0;;
virtual bool should_stop() const = 0;
virtual int start_response(asio_server_stream& s) = 0;
virtual int submit_trailer(asio_server_stream& s, header_map h) = 0;
virtual void stream_error(int32_t stream_id, uint32_t error_code) = 0;
virtual void resume(asio_server_stream& s) = 0;
virtual asio_server_response* push_promise(boost::system::error_code& ec, asio_server_stream& s,
std::string method, std::string raw_path_query,
header_map h) = 0;
virtual int on_read(const std::vector<uint8_t>& buffer, std::size_t len) = 0;
virtual int on_write(std::vector<uint8_t>& buffer, std::size_t& len) = 0;
virtual void initiate_write() = 0;
virtual void signal_write() = 0;
asio_server_stream* create_stream(int32_t stream_id);
void close_stream(int32_t stream_id);
asio_server_stream* find_stream(int32_t stream_id);
void enter_callback();
void leave_callback();
boost::asio::io_service& io_service();
const boost::asio::ip::tcp::endpoint& remote_endpoint();
const std::string& http_date();
static base_handler* find_handler(uint64_t handler_id);
static boost::asio::io_service* find_io_service(uint64_t handler_id);
uint64_t get_handler_id();
void reset_writefun();
const H2Server_Config_Schema& get_config();
protected:
std::map<int32_t, std::shared_ptr<asio_server_stream>> streams_;
connection_write writefun_;
serve_mux& mux_;
boost::asio::io_service& io_service_;
boost::asio::ip::tcp::endpoint remote_ep_;
bool inside_callback_;
// true if we have pending on_write call. This avoids repeated call
// of io_service::post.
bool write_signaled_;
time_t tstamp_cached_;
std::string formatted_date_;
thread_local static std::atomic<uint64_t> handler_unique_id;
thread_local static std::map<uint64_t, base_handler*> alive_handlers;
thread_local static std::map<uint64_t, boost::asio::io_service*> handler_io_service;
uint64_t this_handler_id;
const H2Server_Config_Schema& config;
};
} // namespace server
} // namespace asio_http2
} // namespace nghttp2
#endif // ASIO_SERVER_HTTP2_HANDLER_H