-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_client.cpp
307 lines (229 loc) · 6.5 KB
/
user_client.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <queue>
#include "user_client.hpp"
void user_client::run()
{
// Accept the web socket handshake
ws_.async_accept(
strand_.wrap(
std::bind(
&user_client::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void user_client::on_accept(boost::system::error_code ec)
{
if (ec)
{
return logger::fail(ec, "accept");
}
do_read();
}
void user_client::do_read()
{
ws_.async_read(
buffer_,
strand_.wrap(
std::bind(
&user_client::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void user_client::on_read(boost::system::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// This indicates that the session was closed
if (ec == websocket::error::closed)
{
check_requests_timer_.cancel();
return;
}
if (ec)
{
logger::fail(ec, "read");
disconnect();
return;
}
try
{
if (is_first_read_ == true)
{
is_first_read_ = false;
run_check_requests_timer();
}
auto received_buffer = boost::beast::buffers(buffer_.data());
std::stringstream sreceived;
sreceived << received_buffer;
// Clear the buffer
buffer_.consume(buffer_.size());
handle_request(sreceived.str());
}
catch (std::exception& ex)
{
std::ostringstream swhat;
swhat << boost::format("Error request: %s") % ex.what();
logger::fail(swhat.str().c_str());
}
}
void user_client::handle_request(std::string request)
{
std::string response;
rapidjson::Document request_doc;
bool is_valid_request = false;
logger::debug(request);
request_doc.Parse(request.c_str());
if (request_doc.HasMember("type") == true && request_doc["type"].IsString() == true
&& request_doc.HasMember("id") == true && request_doc["id"].IsInt64() == true)
{
auto request_type = request_doc["type"].GetString();
auto response_id = request_doc["id"].GetInt64();
if (boost::iequals(request_type, "send") == true && validate_request_parameters(request_doc, send_params_) == true)
{
bool is_valid = true;
for (int i = 0; send_params_[i] != 0x00; i++)
{
if (request_doc.HasMember(send_params_[i]) == false || request_doc[send_params_[i]].IsString() == false)
{
is_valid = false;
break;
}
}
if (is_valid == true)
{
is_valid_request = true;
add_response(response_id, "");
std::make_shared<http_requester>(ios_, ctx_, request_doc["method"].GetString(), request_doc["contentType"].GetString(), request_doc["host"].GetString(),
request_doc["port"].GetString(), request_doc["uri"].GetString(), request_doc["data"].GetString(), request_doc["headers"].GetString(),
response_id, static_cast<http_response_receiver*>(this))->run();
std::ostringstream sresponse;
sresponse << boost::format("{ \"status\": \"Sent\", \"id\": %d }") % response_id;
response = sresponse.str();
}
}
else if (boost::iequals(request_type, "ping") == true)
{
std::ostringstream sresponse;
sresponse << boost::format("{ \"status\": \"pong\", \"id\": %d }") % response_id;
response = sresponse.str();
responses_.push_back(std::pair<int64_t, std::string>(response_id, response));
}
}
if (is_valid_request == false || response.empty())
{
response = "{ \"status\": \"Invalid\" }";
}
do_read();
}
void user_client::run_check_requests_timer()
{
check_requests_timer_.expires_at(check_requests_timer_.expires_at() + boost::posix_time::milliseconds(20));
check_requests_timer_.async_wait(
strand_.wrap(
std::bind(&user_client::check_requests,
shared_from_this())));
}
void user_client::check_requests()
{
auto timestamp = get_milliseconds_unix_timestamp();
std::string response;
std::lock_guard<std::mutex> lock(access_responses_);
for (int i = 0; i < responses_.size(); i++)
{
if (timestamp - responses_[i].first > REQUEST_TIMEOUT * 1000)
{
std::ostringstream sresponse;
sresponse << boost::format("{ \"status\": \"Timedout\", \"id\": %d }") % responses_[i].first;
response = sresponse.str();
responses_.erase(responses_.begin() + i);
break;
}
if (is_writing_ == true)
{
break;
}
response = get_response_ex(responses_[i].first);
if (response.empty() == false)
{
responses_.erase(responses_.begin() + i);
break;
}
}
if (response.empty() == false)
{
is_writing_ = true;
logger::debug(response);
send_data_ = response;
ws_.async_write(
boost::asio::buffer(send_data_),
strand_.wrap(
std::bind(
&user_client::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
run_check_requests_timer();
}
bool user_client::validate_request_parameters(rapidjson::Document &request_doc, const char* params_to_check[])
{
bool is_valid = true;
for (int i = 0; params_to_check[i] != 0x00; i++)
{
if (request_doc.HasMember(params_to_check[i]) == false || request_doc[params_to_check[i]].IsString() == false)
{
is_valid = false;
break;
}
}
return is_valid;
}
void user_client::on_write(boost::system::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if (ec)
{
ws_.async_close(websocket::close_code::normal,
strand_.wrap(
std::bind(
&user_client::on_close,
shared_from_this(),
std::placeholders::_1)));
logger::fail(ec, "write");
is_writing_ = false;
return;
}
is_writing_ = false;
}
void user_client::disconnect()
{
ws_.async_close(websocket::close_code::normal,
strand_.wrap(
std::bind(
&user_client::on_close,
shared_from_this(),
std::placeholders::_1)));
}
void user_client::on_close(boost::system::error_code ec)
{
check_requests_timer_.cancel();
// WebSocket says that to close a connection you have
// to keep reading messages until you receive a close frame.
// Beast delivers the close frame as an error from read.
for(;;)
{
// Keep reading messages...
ws_.read(buffer_, ec);
// Clear the buffer
buffer_.consume(buffer_.size());
// ...until we get the special error code
if (ec == boost::beast::websocket::error::closed)
{
break;
}
// Some other error occurred, report it and exit.
if (ec)
{
return logger::fail(ec, "close");
}
}
}