-
Notifications
You must be signed in to change notification settings - Fork 1
/
WsClient.cpp
263 lines (224 loc) · 6.24 KB
/
WsClient.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
#include "pch.h"
#include "WsClient.h"
#include <utility>
#include <iostream>
#include <assert.h>
#define WS_MESSAGE_MAX_SIZE 32768
#define WS_KEEPALIVE_INTERVAL 10
WsClient::WsClient(std::string address, int port, std::string url, std::string ca_path, WsCallback* callback) {
this->address_ = address;
this->port_ = port;
this->path_ = url;
this->ca_path_ = ca_path;
TRACE("[ws Reconnection] Connecting to ==> %s:%d%s\n", this->address_.c_str(), this->port_, this->path_.c_str());
this->lws_thread_ = nullptr;
this->stop_ = false;
this->no_msg_ = true;
this->callback_ = callback;
this->last_ping_tick_ = 0;
this->message_ = "";
this->lws_client_ = nullptr;
// please check https://libwebsockets.org/lws-api-doc-master/html/structlws__protocols.html
// adjust to suit your needs
protocols_[0] = {
"protoo",
lwsCallback,
0,
1024,
0,
this,
10
};
protocols_[1] = {
nullptr,
nullptr,
0,
0
};
buffer_ = new unsigned char[WS_MESSAGE_MAX_SIZE];
}
//Join operation to ensure that the thread exits
WsClient::~WsClient() {
// Try to stop
stop();
if (buffer_) {
delete[] buffer_;
buffer_ = nullptr;
}
}
int WsClient::lwsCallback(struct lws* wsi, enum lws_callback_reasons reason, void* user_data, void* in, size_t len) {
WsClient* client = (WsClient*)user_data;
switch (reason) {
case LWS_CALLBACK_CLIENT_ESTABLISHED: {
TRACE("[ws connection succeeded]\n");
if (client->callback_) {
client->callback_->onConnected();
}
break;
}
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: {
TRACE("[ws error connecting]\n");
if (client->callback_) {
client->callback_->onConnectionError();
}
break;
}
case LWS_CALLBACK_CLOSED: {
TRACE("[ws Connection closed]");
client->lws_client_ = nullptr;
if (client->callback_) {
client->callback_->onDisconnected();
}
break;
}
case LWS_CALLBACK_CLIENT_RECEIVE: {
if (len > 0) {
client->message_.append((const char *)in, len);
if (lws_is_final_fragment(wsi)) {
if (client->callback_) {
client->callback_->onReceiveMessage(client->message_);
}
client->message_.clear();
}
}
break;
}
case LWS_CALLBACK_CLIENT_WRITEABLE: {
client->drainMessageQueue(wsi);
break;
}
case LWS_CALLBACK_WSI_DESTROY: {
TRACE("[ws Connection destruction]\n");
client->lws_client_ = nullptr;
break;
}
case LWS_SERVER_OPTION_FALLBACK_TO_RAW: {
TRACE("[ws Connection destruction]\n");
client->lws_client_ = nullptr;
break;
}
case LWS_CALLBACK_CLIENT_RECEIVE_PONG: {
TRACE("[ws Connection PONG]\n");
break;
}
default: {
TRACE("on_websocket_callback:%d\n", reason);
break;
}
}
return 0;
}
void WsClient::drainMessageQueue(struct lws* wsi) {
std::lock_guard<std::recursive_mutex> lock_guard(lock_);
if (!message_queue_.empty()) {
std::shared_ptr<std::string> message = message_queue_.front();
unsigned char* p = buffer_ + LWS_PRE;
size_t len = static_cast<size_t>(snprintf((char *)p, WS_MESSAGE_MAX_SIZE, "%s", message->c_str()));
int offset = 0;
while (offset < len) {
int ret = lws_write(wsi, p + offset, len - offset, LWS_WRITE_TEXT);
if (ret <= 0) {
break;
}
offset += ret;
}
message_queue_.pop();
no_msg_ = message_queue_.empty();
} else if (GetTickCount64() - last_ping_tick_ >= WS_KEEPALIVE_INTERVAL*1000) {
uint8_t ping[LWS_PRE + 125];
lws_write(wsi, ping + LWS_PRE, 0, LWS_WRITE_PING);
TRACE("[ws Connection PING]\n");
last_ping_tick_ = GetTickCount64();
}
}
void WsClient::sendMessage(std::shared_ptr<std::string> msg) {
std::lock_guard<std::recursive_mutex> lock_guard(lock_);
message_queue_.push(msg);
no_msg_ = false;
if (lws_client_) {
lws_callback_on_writable(lws_client_);
}
}
void WsClient::lwsThread(WsClient* pClient) {
pClient->lwsTask();
}
void lws_log_func(int level, const char* line) {
TRACE(line);
}
void WsClient::lwsTask() {
TRACE("[ws Thread start]\n");
struct lws_context_creation_info ctxInfo {};
memset(&ctxInfo, 0, sizeof(ctxInfo));
ctxInfo.port = CONTEXT_PORT_NO_LISTEN;
ctxInfo.iface = NULL;
ctxInfo.protocols = protocols_;
ctxInfo.ssl_cert_filepath = NULL;
ctxInfo.ssl_private_key_filepath = NULL;
ctxInfo.ssl_ca_filepath = ca_path_.c_str();
ctxInfo.extensions = NULL;
ctxInfo.gid = -1;
ctxInfo.uid = -1;
ctxInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
ctxInfo.fd_limit_per_thread = 1024;
ctxInfo.max_http_header_pool = 1024;
ctxInfo.ws_ping_pong_interval = WS_KEEPALIVE_INTERVAL;
ctxInfo.ka_time = WS_KEEPALIVE_INTERVAL;
ctxInfo.ka_probes = WS_KEEPALIVE_INTERVAL;
ctxInfo.ka_interval = WS_KEEPALIVE_INTERVAL;
struct lws_context* wsContext = lws_create_context(&ctxInfo);
if (wsContext == nullptr) {
TRACE("[ERROR] lws_create_context() failure\n");
TRACE("[ws Thread exit]\n");
return;
}
//lws_set_log_level(LLL_DEBUG, lws_log_func);
struct lws_client_connect_info clientInfo;
memset(&clientInfo, 0, sizeof(clientInfo));
clientInfo.context = wsContext;
clientInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK/* | LCCSCF_ALLOW_INSECURE*/;
clientInfo.host = this->address_.c_str();
clientInfo.address = this->address_.c_str();
clientInfo.port = this->port_;
clientInfo.path = this->path_.c_str();
clientInfo.origin = "origin";
clientInfo.ietf_version_or_minus_one = -1;
clientInfo.protocol = protocols_[0].name;
clientInfo.userdata = this;
lws_client_ = lws_client_connect_via_info(&clientInfo);
int countdown = 5;
while (!stop_ || !no_msg_) {
if (!lws_client_) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
--countdown;
if (stop_) break;
if (countdown == 0) {
TRACE("[ws Reconnection] Reconnection to ==> %s:%d%s\n", this->address_.c_str(), this->port_, this->path_.c_str());
lws_client_ = lws_client_connect_via_info(&clientInfo);
countdown = 5;
} else {
continue;
}
}
lws_service(wsContext, 50);
}
lws_context_destroy(wsContext);
lws_client_ = nullptr;
TRACE("[ws Thread exit]\n");
}
void WsClient::start() {
if (lws_thread_ != nullptr) {
TRACE("[ERROR] ws Thread is already running\n");
} else {
lws_thread_ = new std::thread(lwsThread, this);
}
}
void WsClient::stop() {
stop_ = true;
if (lws_thread_ && lws_thread_->joinable()) {
lws_thread_->join();
}
if (lws_thread_) {
delete lws_thread_;
lws_thread_ = nullptr;
}
}