-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.h
502 lines (474 loc) · 15.6 KB
/
server.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#pragma once
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <drogon/HttpAppFramework.h>
#include <exception>
#include <functional>
#include <json/value.h>
#include <memory>
#include <mutex>
#include <optional>
#include <pstl/glue_algorithm_defs.h>
#include <thread>
#include <vector>
#ifdef _WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0A00
#endif
#endif
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define ASIO_STANDALONE
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <drogon/HttpController.h>
#include <drogon/HttpTypes.h>
#include <drogon/WebSocketConnection.h>
#include <drogon/WebSocketController.h>
#include <drogon/utils/FunctionTraits.h>
#include <filesystem>
#include <glog/logging.h>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
using namespace drogon;
namespace Silenced {
static void PostToMainThread(std::function<void()> f_);
class Server;
class Connection;
static Server *server{nullptr};
static void ClearThisConnection(std::shared_ptr<Connection> connection_);
static void NotifyMainThread();
struct MessageHeader {
uint32_t _id;
uint32_t _size{0};
};
struct Message {
MessageHeader _header{};
std::vector<char> _body{};
template <typename DataType>
friend auto &operator<<(Message &msg_, const DataType &data_) {
static_assert(std::is_standard_layout<DataType>::value,
"Data is too complex to be pushed into vector");
size_t i = msg_._body.size();
msg_._body.resize(i + sizeof(DataType));
std::memcpy(msg_._body.data() + i, &data_, sizeof(DataType));
msg_._header._size = msg_._body.size();
return msg_;
}
template <typename DataType>
friend auto &operator>>(Message &msg, const DataType &data) {
static_assert(std::is_standard_layout<DataType>::value,
"Data is too complex to be pushed into vector");
size_t i = msg._body.size() - sizeof(DataType);
std::memcpy(&data, msg._body.size() + i, sizeof(DataType));
msg._body.resize(i);
msg._header._size = msg._body.size();
return msg;
}
};
class Connection;
struct OwnedMessage {
std::shared_ptr<Connection> _remote{nullptr};
Message _msg;
};
template <typename T, bool IsCanWait> class Queue {
protected:
std::mutex _mux_queue;
std::deque<T> _deq_queue;
std::condition_variable _cv_blocking;
std::mutex _mux_blocking;
public:
Queue() = default;
Queue(const Queue<T, IsCanWait> &) = delete;
virtual ~Queue() {}
public:
const T &front() {
std::scoped_lock lock(_mux_queue);
return _deq_queue.front();
}
T pop_front() {
std::scoped_lock lock(_mux_queue);
auto t = std::move(_deq_queue.front());
_deq_queue.pop_front();
return t;
}
bool empty() {
std::scoped_lock lock(_mux_queue);
return _deq_queue.empty();
}
size_t size() {
std::scoped_lock loc(_mux_queue);
return _deq_queue.size();
}
size_t count() { return size(); }
void clear() {
std::scoped_lock lock(_mux_queue);
_deq_queue.clear();
}
void wait() {
if constexpr (IsCanWait) {
while (empty()) {
std::unique_lock<std::mutex> ul(_mux_blocking);
_cv_blocking.wait(ul);
}
}
}
void push_back(const T &it_) {
std::scoped_lock lock(_mux_queue);
_deq_queue.emplace_back(std::move(it_));
if constexpr (IsCanWait) {
_cv_blocking.notify_one();
}
}
};
class Connection : public std::enable_shared_from_this<Connection> {
public:
asio::ip::tcp::socket _socket;
asio::io_context &_asio_context;
Queue<Message *, false> _message_out;
std::unique_ptr<Message> _msg_temporary_out{nullptr};
Queue<OwnedMessage, false> &_message_in;
Message _msg_temporary_in;
char _pwd[6];
uint32_t _id{0};
std::function<void()> _clear_this_connection;
public:
Connection(asio::io_context &asioContext, asio::ip::tcp::socket socket,
Queue<OwnedMessage, false> &in)
: _socket(std::move(socket)), _asio_context(asioContext),
_message_in(in) {}
virtual ~Connection() {}
uint32_t getId() { return _id; }
public:
void startClient(uint32_t uid = 0) {
if (_socket.is_open()) {
_id = uid;
readValidation();
}
}
bool isConnect() const { return _socket.is_open(); }
void disconnect() {
if (isConnect()) {
asio::post(_asio_context, [this]() { _socket.close(); });
}
}
private:
void readValidation() {
asio::async_read(_socket, asio::buffer(_pwd, sizeof(_pwd)),
[this](std::error_code ec, std::size_t length) {
if (!ec) {
std::string_view pwd(_pwd, sizeof(_pwd));
if (pwd == "zyy123") {
readMessage();
} else {
LOG(WARNING) << "pwd err";
_socket.close();
PostToMainThread([this]() {
ClearThisConnection(this->shared_from_this());
});
}
} else {
LOG(WARNING) << ec.message();
_socket.close();
PostToMainThread([this]() {
ClearThisConnection(this->shared_from_this());
});
}
});
}
void readMessage() {
asio::async_read(
_socket, asio::buffer(&_msg_temporary_in, sizeof(MessageHeader)),
[this](std::error_code ec, std::size_t length) {
if (!ec) {
if (_msg_temporary_in._header._size > 0) {
_msg_temporary_in._body.resize(_msg_temporary_in._header._size);
asio::async_read(_socket,
asio::buffer(_msg_temporary_in._body.data(),
_msg_temporary_in._header._size),
[this](std::error_code ec, std::size_t length) {
if (!ec) {
OwnedMessage msg;
msg._msg = _msg_temporary_in;
msg._remote = this->shared_from_this();
_message_in.push_back(msg);
NotifyMainThread();
readMessage();
} else {
LOG(WARNING) << ec.message();
_socket.close();
PostToMainThread([this]() {
ClearThisConnection(
this->shared_from_this());
});
}
});
} else {
readMessage();
}
} else {
LOG(WARNING) << ec.message();
_socket.close();
PostToMainThread(
[this]() { ClearThisConnection(this->shared_from_this()); });
}
});
}
public:
// Thread Safe
void writeMessage() {
if (_msg_temporary_out.get() != nullptr || _message_out.empty()) {
return;
}
_msg_temporary_out.reset(_message_out.pop_front());
asio::async_write(
_socket,
asio::buffer(&_msg_temporary_out->_header, sizeof(MessageHeader)),
[this](std::error_code ec, std::size_t lenght) {
if (!ec) {
if (_msg_temporary_out->_body.size() > 0) {
asio::async_write(_socket,
asio::buffer(_msg_temporary_out->_body.data(),
_msg_temporary_out->_body.size()),
[this](std::error_code ec, std::size_t length) {
if (!ec) {
_msg_temporary_out.reset(nullptr);
writeMessage();
} else {
LOG(WARNING) << ec.message();
}
});
} else {
_msg_temporary_out.reset(nullptr);
writeMessage();
}
} else {
// write Err
LOG(WARNING) << ec.message();
_socket.close();
}
});
}
};
class WebSock : public WebSocketController<WebSock, false> {
public:
WebSock(){};
public:
virtual void handleNewMessage(const WebSocketConnectionPtr &web_ptr_,
std::string &&str_,
const WebSocketMessageType &) override {
web_ptr_->send(str_);
// DO NOTHING
};
virtual void
handleNewConnection(const HttpRequestPtr &request_,
const WebSocketConnectionPtr &websoc_str_) override {
auto id = request_->parameters().find("id");
if (id != request_->parameters().end()) {
std::scoped_lock lock(_m);
_connection[id->second].push_back(websoc_str_);
}
};
virtual void
handleConnectionClosed(const WebSocketConnectionPtr &websoc_ptr) override {
std::scoped_lock lock(_m);
for (auto &it : _connection) {
auto e = std::remove_if(it.second.begin(), it.second.end(),
[&websoc_ptr](const WebSocketConnectionPtr &it) {
if (it == websoc_ptr) {
return true;
}
return false;
});
it.second.erase(e, it.second.end());
if (it.second.empty()) {
_connection.erase(it.first);
break;
}
}
};
void sendMessageToWebSocket(const OwnedMessage &msg_) {
try {
std::scoped_lock lock(_m);
std::string info(msg_._msg._body.data(), msg_._msg._header._size);
std::regex regex_(R"(\$.*?\$)");
std::smatch match_;
std::string::const_iterator iter_begin = info.cbegin();
std::string::const_iterator iter_end = info.cend();
if (regex_search(iter_begin, iter_end, match_, regex_)) {
std::string aim(match_[0]);
info.replace(0, aim.length(), "");
aim = aim.substr(1, aim.length() - 2);
auto backchar = aim.back();
auto frontchar = aim.front();
std::regex reg(",");
std::sregex_token_iterator pos(aim.begin(), aim.end(), reg, -1);
decltype(pos) end;
for (; pos != end; ++pos) {
std::vector<WebSocketConnectionPtr> &_c = _connection[pos->str()];
for (auto &v : _c) {
if (v) {
v->send(info);
// v->send(const std::string &msg)
}
}
}
} else {
throw std::invalid_argument("Can't find aim from body!");
}
auto returnMsg = new Message();
returnMsg->_header._id = msg_._msg._header._id;
*returnMsg << "1";
msg_._remote->_message_out.push_back(returnMsg);
} catch (std::exception e) {
auto returnMsg = new Message();
returnMsg->_header._id = msg_._msg._header._id;
*returnMsg << "0"
<< "Err";
msg_._remote->_message_out.push_back(returnMsg);
}
msg_._remote->writeMessage();
}
const Json::Value getConnectedInfo() {
Json::Value connected_info;
for (auto &v : _connection) {
Json::Value s;
s["Id"] = v.first;
for (auto &c : v.second) {
Json::Value Ip;
Ip["Ip"] = c->peerAddr().toIp().c_str();
s["Client"].append(Ip);
}
connected_info.append(s);
}
return connected_info;
}
WS_PATH_LIST_BEGIN
WS_PATH_ADD("/");
WS_PATH_LIST_END
private:
std::unordered_map<std::string, std::vector<WebSocketConnectionPtr>>
_connection;
std::mutex _m;
};
class Server {
protected:
Queue<OwnedMessage, false> _message_in;
std::vector<std::shared_ptr<Connection>> _s_connection;
asio::io_context _asio_context;
std::thread _thread_context;
std::thread _thread_websocket;
asio::ip::tcp::acceptor _asio_acceptor;
std::shared_ptr<WebSock> _websock{std::make_shared<WebSock>()};
uint32_t _id_count{10000};
public:
std::deque<std::function<void()>> _task;
std::condition_variable _cv_blocking;
std::mutex _mux_blocking;
std::mutex _mux_queue;
public:
void postTaskToMainThread(std::function<void()> f_) {
std::scoped_lock lock(_mux_queue);
_task.emplace_back(f_);
_cv_blocking.notify_one();
}
void notifyMainThread() { _cv_blocking.notify_one(); }
public:
Server(uint16_t port)
: _asio_acceptor(_asio_context,
asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)) {
assert(server == nullptr);
server = this;
}
virtual ~Server() {}
int start() {
try {
waitForClientConnection();
_thread_context = std::thread([this]() { _asio_context.run(); });
_thread_websocket = std::thread([this]() {
drogon::app()
.addListener("0.0.0.0", 6868)
.setThreadNum(4)
.registerController(_websock)
.registerHandler(
"/GetConnectedInfo",
[this](
const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpJsonResponse(
_websock->getConnectedInfo());
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->addHeader("Access-Control-Allow-Methods", "GET, POST");
resp->addHeader("Access-Control-Allow-Private-Network",
"true");
resp->addHeader("Access-Control-Allow-Credentials", "true");
callback(resp);
},
{Get})
.run();
});
while (true) {
while (!_message_in.empty()) {
_websock->sendMessageToWebSocket(_message_in.pop_front());
}
if (!_task.empty()) {
std::scoped_lock lock(_mux_queue);
do {
_task.front()();
_task.pop_front();
} while (!_task.empty());
}
std::unique_lock<std::mutex> ul(_mux_blocking);
_cv_blocking.wait(ul);
}
return -1;
} catch (std::exception &e) {
LOG(WARNING) << e.what();
return -1;
}
}
void waitForClientConnection() {
_asio_acceptor.async_accept(
[this](std::error_code ec, asio::ip::tcp::socket socket) {
if (!ec) {
_s_connection.push_back(std::make_shared<Connection>(
_asio_context, std::move(socket), _message_in));
_s_connection.back()->startClient(_id_count++);
} else {
LOG(WARNING) << ec.message();
}
waitForClientConnection();
});
}
void clearConnection(std::shared_ptr<Connection> connection_) {
auto isIt = [&connection_](std::shared_ptr<Connection> &i) {
if (i.get() == connection_.get()) {
return true;
}
return false;
};
auto result = std::find_if(begin(_s_connection), end(_s_connection), isIt);
// at runtime C++ don't do any decide!
if (result != _s_connection.end()) {
_s_connection.erase(result);
}
LOG(INFO) << "An Connection DisConnect,Now Connection Number:"
<< std::to_string(_s_connection.size());
}
};
static void PostToMainThread(std::function<void()> f_) {
server->postTaskToMainThread(f_);
}
static void ClearThisConnection(std::shared_ptr<Connection> connection_) {
server->clearConnection(connection_);
}
static void NotifyMainThread() { server->notifyMainThread(); }
} // namespace Silenced