-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasio_server_base_handler.cc
161 lines (132 loc) · 3.39 KB
/
asio_server_base_handler.cc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "asio_server_base_handler.h"
#include <iostream>
#include "asio_common.h"
#include "asio_server_serve_mux.h"
#include "asio_server_stream.h"
#include "asio_server_request.h"
#include "asio_server_response.h"
#include "http2.h"
#include "util.h"
#include "template.h"
#include "H2Server_Config_Schema.h"
namespace nghttp2
{
namespace asio_http2
{
namespace server
{
thread_local std::atomic<uint64_t> base_handler::handler_unique_id(0);
thread_local std::map<uint64_t, base_handler*> base_handler::alive_handlers;
thread_local std::map<uint64_t, boost::asio::io_service*> base_handler::handler_io_service;
base_handler::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)
: writefun_(writefun),
mux_(mux),
io_service_(io_service),
remote_ep_(ep),
inside_callback_(false),
write_signaled_(false),
tstamp_cached_(time(nullptr)),
formatted_date_(util::http_date(tstamp_cached_)),
this_handler_id(handler_unique_id++),
config(conf)
{
alive_handlers[this_handler_id] = this;
handler_io_service[this_handler_id] = &io_service_;
}
base_handler::~base_handler()
{
alive_handlers.erase(this_handler_id);
handler_io_service.erase(this_handler_id);
}
void base_handler::reset_writefun()
{
auto tmp = std::move(writefun_);
}
base_handler* base_handler::find_handler(uint64_t handler_id)
{
auto it = alive_handlers.find(handler_id);
if (it != alive_handlers.end())
{
return it->second;
}
return nullptr;
}
boost::asio::io_service* base_handler::find_io_service(uint64_t handler_id)
{
auto it = handler_io_service.find(handler_id);
if (it != handler_io_service.end())
{
return it->second;
}
return nullptr;
}
uint64_t base_handler::get_handler_id()
{
return this_handler_id;
}
const std::string& base_handler::http_date()
{
auto t = time(nullptr);
if (t != tstamp_cached_)
{
tstamp_cached_ = t;
formatted_date_ = util::http_date(t);
}
return formatted_date_;
}
asio_server_stream* base_handler::create_stream(int32_t stream_id)
{
auto p =
streams_.emplace(stream_id, std::make_unique<asio_server_stream>(this, stream_id));
assert(p.second);
return (*p.first).second.get();
}
void base_handler::close_stream(int32_t stream_id)
{
streams_.erase(stream_id);
}
asio_server_stream* base_handler::find_stream(int32_t stream_id)
{
auto i = streams_.find(stream_id);
if (i == std::end(streams_))
{
return nullptr;
}
return (*i).second.get();
}
void base_handler::enter_callback()
{
assert(!inside_callback_);
inside_callback_ = true;
}
void base_handler::leave_callback()
{
assert(inside_callback_);
inside_callback_ = false;
}
boost::asio::io_service& base_handler::io_service()
{
return io_service_;
}
const boost::asio::ip::tcp::endpoint& base_handler::remote_endpoint()
{
return remote_ep_;
}
const H2Server_Config_Schema& base_handler::get_config()
{
return config;
}
callback_guard::callback_guard(base_handler& h) : handler(h)
{
handler.enter_callback();
}
callback_guard::~callback_guard()
{
handler.leave_callback();
}
} // namespace server
} // namespace asio_http2
} // namespace nghttp2