1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "monitor_server.h"
17
18 #include <cstddef>
19 #include <utility>
20
21 #include <napi/native_api.h>
22 #include <napi/native_common.h>
23 #include <securec.h>
24 #include <uv.h>
25
26 #include "module_template.h"
27 #include "napi_utils.h"
28 #include "netstack_log.h"
29
30 #include "tls_socket_server.h"
31 #include "tlssocketserver_module.h"
32
33 namespace OHOS {
34 namespace NetStack {
35 namespace TlsSocketServer {
36 namespace {
37 constexpr int PARAM_OPTION = 1;
38 constexpr int PARAM_OPTION_CALLBACK = 2;
39 constexpr std::string_view EVENT_MESSAGE = "message";
40 constexpr std::string_view EVENT_CONNECT = "connect";
41 constexpr std::string_view EVENT_CLOSE = "close";
42 constexpr std::string_view EVENT_ERROR = "error";
43
44 constexpr const char *PROPERTY_ADDRESS = "address";
45 constexpr const char *PROPERTY_FAMILY = "family";
46 constexpr const char *PROPERTY_PORT = "port";
47 constexpr const char *PROPERTY_SIZE = "size";
48 constexpr const char *ON_MESSAGE = "message";
49 constexpr const char *ON_REMOTE_INFO = "remoteInfo";
50
NewInstanceWithConstructor(napi_env env,napi_callback_info info,napi_value jsConstructor,int32_t counter,std::shared_ptr<EventManager> eventManager)51 napi_value NewInstanceWithConstructor(napi_env env, napi_callback_info info, napi_value jsConstructor, int32_t counter,
52 std::shared_ptr<EventManager> eventManager)
53 {
54 napi_value result = nullptr;
55 NAPI_CALL(env, napi_new_instance(env, jsConstructor, 0, nullptr, &result));
56
57 std::shared_ptr<EventManager> manager = eventManager;
58
59 napi_wrap(
60 env, result, reinterpret_cast<void *>(manager.get()),
61 [](napi_env, void *data, void *) {
62 NETSTACK_LOGI("socket handle is finalized");
63 auto manager = static_cast<EventManager *>(data);
64 if (manager != nullptr) {
65 auto tlsServer = static_cast<TLSSocketServer *>(manager->GetData());
66 if (tlsServer != nullptr) {
67 tlsServer->CloseConnectionByEventManager(manager);
68 tlsServer->DeleteConnectionByEventManager(manager);
69 }
70 EventManager::SetInvalid(manager);
71 }
72 },
73 nullptr, nullptr);
74
75 return result;
76 }
77
ConstructTLSSocketConnection(napi_env env,napi_callback_info info,int32_t counter,std::shared_ptr<EventManager> eventManager)78 napi_value ConstructTLSSocketConnection(napi_env env, napi_callback_info info, int32_t counter,
79 std::shared_ptr<EventManager> eventManager)
80 {
81 napi_value jsConstructor = nullptr;
82 std::initializer_list<napi_property_descriptor> properties = {
83 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_GET_CERTIFICATE,
84 TLSSocketServerModuleExports::TLSSocketConnection::GetCertificate),
85 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_GET_REMOTE_CERTIFICATE,
86 TlsSocketServer::TLSSocketServerModuleExports::TLSSocketConnection::GetRemoteCertificate),
87 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_GET_SIGNATURE_ALGORITHMS,
88 TLSSocketServerModuleExports::TLSSocketConnection::GetSignatureAlgorithms),
89 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_GET_CIPHER_SUITE,
90 TLSSocketServerModuleExports::TLSSocketConnection::GetCipherSuites),
91 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_SEND,
92 TLSSocketServerModuleExports::TLSSocketConnection::Send),
93 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_CLOSE,
94 TLSSocketServerModuleExports::TLSSocketConnection::Close),
95 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_GET_REMOTE_ADDRESS,
96 TLSSocketServerModuleExports::TLSSocketConnection::GetRemoteAddress),
97 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_ON,
98 TLSSocketServerModuleExports::TLSSocketConnection::On),
99 DECLARE_NAPI_FUNCTION(TLSSocketServerModuleExports::TLSSocketConnection::FUNCTION_OFF,
100 TLSSocketServerModuleExports::TLSSocketConnection::Off),
101 };
102
103 auto constructor = [](napi_env env, napi_callback_info info) -> napi_value {
104 napi_value thisVal = nullptr;
105 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVal, nullptr));
106
107 return thisVal;
108 };
109
110 napi_property_descriptor descriptors[properties.size()];
111 std::copy(properties.begin(), properties.end(), descriptors);
112
113 NAPI_CALL_BASE(env,
114 napi_define_class(env, TLSSocketServerModuleExports::INTERFACE_TLS_SOCKET_SERVER_CONNECTION,
115 NAPI_AUTO_LENGTH, constructor, nullptr, properties.size(), descriptors,
116 &jsConstructor),
117 NapiUtils::GetUndefined(env));
118
119 if (jsConstructor != nullptr) {
120 napi_value result = NewInstanceWithConstructor(env, info, jsConstructor, counter, eventManager);
121 NapiUtils::SetInt32Property(env, result, TLSSocketServerModuleExports::TLSSocketConnection::PROPERTY_CLIENT_ID,
122 counter);
123 return result;
124 }
125 return NapiUtils::GetUndefined(env);
126 }
127
MakeMessageObj(napi_env env,std::shared_ptr<MonitorServer::MessageRecvParma> MessagePara)128 napi_value MakeMessageObj(napi_env env, std::shared_ptr<MonitorServer::MessageRecvParma> MessagePara)
129 {
130 void *data = nullptr;
131 napi_value arrayBuffer = NapiUtils::CreateArrayBuffer(env, MessagePara->data.size(), &data);
132 if (data != nullptr && arrayBuffer != nullptr) {
133 if (memcpy_s(data, MessagePara->data.size(), MessagePara->data.c_str(), MessagePara->data.size()) != EOK) {
134 NETSTACK_LOGE("memcpy_s failed!");
135 return nullptr;
136 }
137 } else {
138 return nullptr;
139 }
140
141 napi_value obj = NapiUtils::CreateObject(env);
142 napi_value remoteInfo = NapiUtils::CreateObject(env);
143
144 napi_value message = nullptr;
145 napi_create_typedarray(env, napi_uint8_array, MessagePara->data.size(), arrayBuffer, 0, &message);
146 napi_value address = NapiUtils::CreateStringUtf8(env, MessagePara->remoteInfo_.GetAddress());
147 napi_value family = NapiUtils::CreateStringUtf8(env, MessagePara->remoteInfo_.GetFamily());
148 napi_value port = NapiUtils::CreateInt32(env, MessagePara->remoteInfo_.GetPort());
149 napi_value size = NapiUtils::CreateInt32(env, MessagePara->remoteInfo_.GetSize());
150 NapiUtils::SetNamedProperty(env, remoteInfo, PROPERTY_ADDRESS, address);
151 NapiUtils::SetNamedProperty(env, remoteInfo, PROPERTY_FAMILY, family);
152 NapiUtils::SetNamedProperty(env, remoteInfo, PROPERTY_PORT, port);
153 NapiUtils::SetNamedProperty(env, remoteInfo, PROPERTY_SIZE, size);
154 NapiUtils::SetNamedProperty(env, obj, ON_MESSAGE, message);
155 NapiUtils::SetNamedProperty(env, obj, ON_REMOTE_INFO, remoteInfo);
156
157 return obj;
158 }
159
EventMessageCallback(uv_work_t * work,int status)160 void EventMessageCallback(uv_work_t *work, int status)
161 {
162 (void)status;
163 if (work == nullptr) {
164 NETSTACK_LOGE("work is nullptr");
165 return;
166 }
167 auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
168 if (workWrapper == nullptr) {
169 NETSTACK_LOGE("workWrapper is nullptr");
170 delete work;
171 return;
172 }
173 std::shared_ptr<MonitorServer::MessageRecvParma> ptrMessageRecvParma(
174 static_cast<MonitorServer::MessageRecvParma *>(workWrapper->data));
175 if (ptrMessageRecvParma == nullptr) {
176 NETSTACK_LOGE("messageRecvParma is nullptr");
177 delete workWrapper;
178 delete work;
179 return;
180 }
181 if (workWrapper->manager == nullptr) {
182 NETSTACK_LOGE("manager is nullptr");
183 delete workWrapper;
184 delete work;
185 return;
186 }
187 napi_handle_scope scope = NapiUtils::OpenScope(workWrapper->env);
188 auto obj = MakeMessageObj(workWrapper->env, ptrMessageRecvParma);
189 if (obj == nullptr) {
190 NapiUtils::CloseScope(workWrapper->env, scope);
191 delete workWrapper;
192 delete work;
193 return;
194 }
195
196 workWrapper->manager->Emit(workWrapper->type, std::make_pair(NapiUtils::GetUndefined(workWrapper->env), obj));
197 NapiUtils::CloseScope(workWrapper->env, scope);
198
199 delete workWrapper;
200 delete work;
201 }
202
EventConnectCallback(uv_work_t * work,int status)203 void EventConnectCallback(uv_work_t *work, int status)
204 {
205 (void)status;
206 if (work == nullptr) {
207 NETSTACK_LOGE("work is nullptr");
208 return;
209 }
210 auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
211 if (workWrapper == nullptr) {
212 NETSTACK_LOGE("workWrapper is nullptr");
213 delete work;
214 return;
215 }
216 std::shared_ptr<MonitorServer::MessageParma> messageParma(
217 static_cast<MonitorServer::MessageParma *>(workWrapper->data));
218 if (messageParma == nullptr) {
219 NETSTACK_LOGE("messageParma is nullptr");
220 delete workWrapper;
221 delete work;
222 return;
223 }
224 auto clientid = messageParma->clientID;
225 auto eventManager = messageParma->eventManager;
226 napi_handle_scope scope = NapiUtils::OpenScope(workWrapper->env);
227 napi_callback_info info = nullptr;
228 napi_value obj = ConstructTLSSocketConnection(workWrapper->env, info, clientid, eventManager);
229 workWrapper->manager->Emit(workWrapper->type, std::make_pair(NapiUtils::GetUndefined(workWrapper->env), obj));
230 NapiUtils::CloseScope(workWrapper->env, scope);
231 delete workWrapper;
232 delete work;
233 }
234
EventCloseCallback(uv_work_t * work,int status)235 void EventCloseCallback(uv_work_t *work, int status)
236 {
237 (void)status;
238 if (work == nullptr) {
239 NETSTACK_LOGE("work is nullptr");
240 return;
241 }
242 auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
243 if (workWrapper == nullptr) {
244 NETSTACK_LOGE("workWrapper is nullptr");
245 delete work;
246 return;
247 }
248 std::shared_ptr<int> ptrClientID(static_cast<int *>(workWrapper->data));
249 if (ptrClientID == nullptr) {
250 NETSTACK_LOGE("ptrClientID == nullptr");
251 delete workWrapper;
252 delete work;
253 return;
254 }
255 if (workWrapper->manager == nullptr) {
256 NETSTACK_LOGE("manager is nullptr");
257 delete workWrapper;
258 delete work;
259 return;
260 }
261 napi_handle_scope scope = NapiUtils::OpenScope(workWrapper->env);
262 napi_value obj = nullptr;
263 workWrapper->manager->Emit(workWrapper->type, std::make_pair(NapiUtils::GetUndefined(workWrapper->env), obj));
264 NapiUtils::CloseScope(workWrapper->env, scope);
265 delete workWrapper;
266 delete work;
267 }
268
EventErrorCallback(uv_work_t * work,int status)269 void EventErrorCallback(uv_work_t *work, int status)
270 {
271 (void)status;
272 if (work == nullptr) {
273 NETSTACK_LOGE("work is nullptr");
274 return;
275 }
276 auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
277 if (workWrapper == nullptr) {
278 NETSTACK_LOGE("workWrapper is nullptr");
279 delete work;
280 return;
281 }
282 auto monitor = static_cast<MonitorServer *>(workWrapper->data);
283 if (monitor == nullptr) {
284 NETSTACK_LOGE("monitor is nullptr");
285 delete workWrapper;
286 delete work;
287 return;
288 }
289 if (workWrapper->manager == nullptr) {
290 NETSTACK_LOGE("manager is nullptr");
291 delete workWrapper;
292 delete work;
293 return;
294 }
295 napi_handle_scope scope = NapiUtils::OpenScope(workWrapper->env);
296 napi_value obj = NapiUtils::CreateObject(workWrapper->env);
297 napi_value errorNumber = NapiUtils::CreateInt32(workWrapper->env, monitor->errorNumber_);
298 napi_value errorString = NapiUtils::CreateStringUtf8(workWrapper->env, monitor->errorString_);
299 NapiUtils::SetNamedProperty(workWrapper->env, obj, "errorNumber", errorNumber);
300 NapiUtils::SetNamedProperty(workWrapper->env, obj, "errorString", errorString);
301 std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
302 workWrapper->manager->Emit(workWrapper->type, arg);
303 NapiUtils::CloseScope(workWrapper->env, scope);
304 delete workWrapper;
305 delete work;
306 }
307 } // namespace
308
MonitorServer()309 MonitorServer::MonitorServer() {}
310
~MonitorServer()311 MonitorServer::~MonitorServer() {}
312
On(napi_env env,napi_callback_info info)313 napi_value MonitorServer::On(napi_env env, napi_callback_info info)
314 {
315 napi_value thisVal = nullptr;
316 size_t paramsCount = MAX_PARAM_NUM;
317 napi_value params[MAX_PARAM_NUM] = {nullptr};
318 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
319
320 if (paramsCount != PARAM_OPTION_CALLBACK || NapiUtils::GetValueType(env, params[0]) != napi_string ||
321 NapiUtils::GetValueType(env, params[1]) != napi_function) {
322 NETSTACK_LOGE("on off once interface para: [string, function]");
323 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
324 return NapiUtils::GetUndefined(env);
325 }
326 EventManager *manager_ = nullptr;
327 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager_));
328 if (manager_ == nullptr) {
329 NETSTACK_LOGE("manager is nullptr");
330 return NapiUtils::GetUndefined(env);
331 }
332 auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager_->GetData());
333 if (tlsSocketServer == nullptr) {
334 NETSTACK_LOGE("tlsSocketServer is null");
335 return NapiUtils::GetUndefined(env);
336 }
337
338 const std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
339 auto itor = monitors_.find(event);
340 if (itor != monitors_.end()) {
341 NETSTACK_LOGE("monitor is exits %{public}s", event.c_str());
342 return NapiUtils::GetUndefined(env);
343 }
344 manager_->AddListener(env, event, params[1], false, false);
345 TLSServerRegEvent(event, tlsSocketServer, manager_);
346 return NapiUtils::GetUndefined(env);
347 }
348
ConnectionOn(napi_env env,napi_callback_info info)349 napi_value MonitorServer::ConnectionOn(napi_env env, napi_callback_info info)
350 {
351 napi_value thisVal = nullptr;
352 size_t paramsCount = MAX_PARAM_NUM;
353 napi_value params[MAX_PARAM_NUM] = {nullptr};
354 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
355
356 if (false == NapiUtils::HasNamedProperty(env, thisVal,
357 TLSSocketServerModuleExports::TLSSocketConnection::PROPERTY_CLIENT_ID)) {
358 NETSTACK_LOGI("not found Property clientId");
359 return NapiUtils::GetUndefined(env);
360 }
361
362 int clientid = NapiUtils::GetInt32Property(env, thisVal,
363 TLSSocketServerModuleExports::TLSSocketConnection::PROPERTY_CLIENT_ID);
364
365 if (paramsCount != PARAM_OPTION_CALLBACK || NapiUtils::GetValueType(env, params[0]) != napi_string ||
366 NapiUtils::GetValueType(env, params[1]) != napi_function) {
367 NETSTACK_LOGE("on off once interface para: [string, function]");
368 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
369 return NapiUtils::GetUndefined(env);
370 }
371 EventManager *manager_ = nullptr;
372 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager_));
373 if (manager_ == nullptr) {
374 NETSTACK_LOGE("manager is nullptr");
375 return NapiUtils::GetUndefined(env);
376 }
377 auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager_->GetData());
378 if (tlsSocketServer == nullptr) {
379 NETSTACK_LOGE("tlsSocketServer is null");
380 return NapiUtils::GetUndefined(env);
381 }
382
383 const std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
384
385 manager_->AddListener(env, event, params[1], false, false);
386 TLSConnectionRegEvent(event, tlsSocketServer, clientid, manager_);
387 return NapiUtils::GetUndefined(env);
388 }
389
Off(napi_env env,napi_callback_info info)390 napi_value MonitorServer::Off(napi_env env, napi_callback_info info)
391 {
392 napi_value thisVal = nullptr;
393 size_t paramsCount = MAX_PARAM_NUM;
394 napi_value params[MAX_PARAM_NUM] = {nullptr};
395 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
396
397 if ((paramsCount != PARAM_OPTION && paramsCount != PARAM_OPTION_CALLBACK) ||
398 NapiUtils::GetValueType(env, params[0]) != napi_string) {
399 NETSTACK_LOGE("on off once interface para: [string, function?]");
400 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
401 return NapiUtils::GetUndefined(env);
402 }
403
404 if (paramsCount == PARAM_OPTION_CALLBACK && NapiUtils::GetValueType(env, params[1]) != napi_function) {
405 NETSTACK_LOGE("on off once interface para: [string, function]");
406 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
407 return NapiUtils::GetUndefined(env);
408 }
409 EventManager *manager_ = nullptr;
410 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager_));
411 if (manager_ == nullptr) {
412 NETSTACK_LOGE("manager is nullptr");
413 return NapiUtils::GetUndefined(env);
414 }
415 auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager_->GetData());
416 if (tlsSocketServer == nullptr) {
417 NETSTACK_LOGE("tlsSocketServer is null");
418 return NapiUtils::GetUndefined(env);
419 }
420
421 const std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
422 auto itor = monitors_.find(event);
423 if (itor == monitors_.end()) {
424 NETSTACK_LOGE("monitor is off %{public}s", event.c_str());
425 return NapiUtils::GetUndefined(env);
426 }
427 if (manager_ != nullptr) {
428 if (paramsCount == PARAM_OPTION_CALLBACK) {
429 manager_->DeleteListener(event, params[1]);
430 } else {
431 manager_->DeleteListener(event);
432 }
433 }
434
435 if (event == EVENT_CONNECT) {
436 monitors_.erase(EVENT_CONNECT);
437 tlsSocketServer->OffConnect();
438 }
439 if (event == EVENT_ERROR) {
440 monitors_.erase(EVENT_ERROR);
441 tlsSocketServer->OffError();
442 }
443 return NapiUtils::GetUndefined(env);
444 }
445
ConnectionOff(napi_env env,napi_callback_info info)446 napi_value MonitorServer::ConnectionOff(napi_env env, napi_callback_info info)
447 {
448 napi_value thisVal = nullptr;
449 size_t paramsCount = MAX_PARAM_NUM;
450 napi_value params[MAX_PARAM_NUM] = {nullptr};
451 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
452
453 int clientid = NapiUtils::GetInt32Property(env, thisVal,
454 TLSSocketServerModuleExports::TLSSocketConnection::PROPERTY_CLIENT_ID);
455
456 if ((paramsCount != PARAM_OPTION && paramsCount != PARAM_OPTION_CALLBACK) ||
457 NapiUtils::GetValueType(env, params[0]) != napi_string) {
458 NETSTACK_LOGE("on off once interface para: [string, function?]");
459 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
460 return NapiUtils::GetUndefined(env);
461 }
462
463 if (paramsCount == PARAM_OPTION_CALLBACK && NapiUtils::GetValueType(env, params[1]) != napi_function) {
464 NETSTACK_LOGE("on off once interface para: [string, function]");
465 napi_throw_error(env, std::to_string(PARSE_ERROR_CODE).c_str(), PARSE_ERROR_MSG);
466 return NapiUtils::GetUndefined(env);
467 }
468 EventManager *manager_ = nullptr;
469 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager_));
470 if (manager_ == nullptr) {
471 NETSTACK_LOGE("manager is nullptr");
472 return NapiUtils::GetUndefined(env);
473 }
474 auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager_->GetData());
475 if (tlsSocketServer == nullptr) {
476 NETSTACK_LOGE("tlsSocketServer is null");
477 return NapiUtils::GetUndefined(env);
478 }
479
480 const std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
481 auto itor = monitors_.find(event);
482 if (itor == monitors_.end()) {
483 NETSTACK_LOGE("monitor is off %{public}s", event.c_str());
484 return NapiUtils::GetUndefined(env);
485 }
486 if (manager_ != nullptr) {
487 if (paramsCount == PARAM_OPTION_CALLBACK) {
488 manager_->DeleteListener(event, params[1]);
489 } else {
490 manager_->DeleteListener(event);
491 }
492 }
493 TLSConnectionUnRegEvent(event, tlsSocketServer, clientid);
494 return NapiUtils::GetUndefined(env);
495 }
496
TLSServerRegEvent(std::string event,TLSSocketServer * tlsSocketServer,EventManager * ServerEventManager)497 void MonitorServer::TLSServerRegEvent(std::string event, TLSSocketServer *tlsSocketServer,
498 EventManager *ServerEventManager)
499 {
500 if (event == EVENT_CONNECT) {
501 monitors_.insert(EVENT_CONNECT);
502 tlsSocketServer->OnConnect(
503 [this, ServerEventManager](auto clientFd, std::shared_ptr<EventManager> eventManager) {
504 auto messageParma = new MonitorServer::MessageParma();
505 messageParma->clientID = clientFd;
506 messageParma->eventManager = eventManager;
507 ServerEventManager->EmitByUv(std::string(EVENT_CONNECT), messageParma, EventConnectCallback);
508 });
509 }
510 if (event == EVENT_ERROR) {
511 monitors_.insert(EVENT_ERROR);
512 tlsSocketServer->OnError([this, ServerEventManager](auto errorNumber, auto errorString) {
513 errorNumber_ = errorNumber;
514 errorString_ = errorString;
515 ServerEventManager->EmitByUv(std::string(EVENT_ERROR), static_cast<void *>(this), EventErrorCallback);
516 });
517 }
518 }
519
TLSConnectionRegEvent(std::string event,TLSSocketServer * tlsSocketServer,int clientId,EventManager * eventManager)520 void MonitorServer::TLSConnectionRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId,
521 EventManager *eventManager)
522 {
523 if (event == EVENT_MESSAGE) {
524 monitors_.insert(EVENT_MESSAGE);
525 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
526 if (ptrConnection != nullptr) {
527 ptrConnection->OnMessage([this, eventManager](auto clientFd, auto data, auto remoteInfo) {
528 auto messageRecvParma = new MessageRecvParma();
529 messageRecvParma->clientID = clientFd;
530 messageRecvParma->data = data;
531 messageRecvParma->remoteInfo_.SetAddress(remoteInfo.GetAddress());
532 messageRecvParma->remoteInfo_.SetFamilyByStr(remoteInfo.GetFamily());
533 messageRecvParma->remoteInfo_.SetPort(remoteInfo.GetPort());
534 messageRecvParma->remoteInfo_.SetSize(remoteInfo.GetSize());
535 eventManager->EmitByUv(std::string(EVENT_MESSAGE), messageRecvParma, EventMessageCallback);
536 });
537 }
538 }
539 if (event == EVENT_CLOSE) {
540 monitors_.insert(EVENT_CLOSE);
541 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
542 if (ptrConnection != nullptr) {
543 ptrConnection->OnClose([this, eventManager](auto clientFd) {
544 eventManager->EmitByUv(std::string(EVENT_CLOSE), static_cast<void *>(this), EventCloseCallback);
545 });
546 }
547 }
548 if (event == EVENT_ERROR) {
549 monitors_.insert(EVENT_ERROR);
550 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
551 if (ptrConnection != nullptr) {
552 ptrConnection->OnError([this, eventManager](auto errorNumber, auto errorString) {
553 eventManager->EmitByUv(std::string(EVENT_ERROR), static_cast<void *>(this), EventErrorCallback);
554 });
555 }
556 }
557 }
558
TLSConnectionUnRegEvent(std::string event,TLSSocketServer * tlsSocketServer,int clientId)559 void MonitorServer::TLSConnectionUnRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId)
560 {
561 if (event == EVENT_MESSAGE) {
562 monitors_.erase(EVENT_MESSAGE);
563 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
564 if (ptrConnection != nullptr) {
565 ptrConnection->OffMessage();
566 }
567 }
568 if (event == EVENT_CLOSE) {
569 monitors_.erase(EVENT_CLOSE);
570 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
571 if (ptrConnection != nullptr) {
572 ptrConnection->OffClose();
573 }
574 }
575 if (event == EVENT_ERROR) {
576 monitors_.erase(EVENT_ERROR);
577 auto ptrConnection = tlsSocketServer->GetConnectionByClientID(clientId);
578 if (ptrConnection != nullptr) {
579 ptrConnection->OffError();
580 }
581 }
582 }
583 } // namespace TlsSocketServer
584 } // namespace NetStack
585 } // namespace OHOS
586