• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TLS_MONITOR_SERVER_H
17 #define TLS_MONITOR_SERVER_H
18 
19 #include <cstdint>
20 #include <napi/native_api.h>
21 #include <queue>
22 #include <set>
23 #include <string>
24 #include <string_view>
25 
26 #include "event_manager.h"
27 #include "singleton.h"
28 #include "socket_remote_info.h"
29 #include "tls.h"
30 #include "tls_socket_server.h"
31 namespace OHOS {
32 namespace NetStack {
33 namespace TlsSocketServer {
34 struct MessagerServerQueue {
35     std::queue<int> clientFdQueue;
36     std::queue<std::string> dataQueue;
37     std::queue<Socket::SocketRemoteInfo> remoteInfoQueue;
38 };
39 
40 class MonitorServer final {
41     DECLARE_DELAYED_SINGLETON(MonitorServer);
42 
43 public:
44     napi_value On(napi_env env, napi_callback_info info);
45     napi_value Off(napi_env env, napi_callback_info info);
46 
47     napi_value ConnectionOn(napi_env env, napi_callback_info info);
48     napi_value ConnectionOff(napi_env env, napi_callback_info info);
49     void TLSServerRegEvent(std::string event, TLSSocketServer *tlsSocketServer,
50         const std::shared_ptr<EventManager> &eventManager);
51     void TLSConnectionRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId,
52                                const std::shared_ptr<EventManager> &eventManager);
53     void TLSConnectionUnRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId);
54     class MessageParma {
55     public:
56         int clientID;
57         std::shared_ptr<EventManager> eventManager;
58     };
59 
60     class MessageRecvParma {
61     public:
62         int clientID;
63         std::string data;
64         Socket::SocketRemoteInfo remoteInfo_;
65     };
66 
67 public:
68     int clientFd_ = -1;
69     std::string data_;
70     Socket::SocketRemoteInfo remoteInfo_;
71     MessagerServerQueue messagerServerQueue_;
72     int32_t errorNumber_ = 0;
73     std::string errorString_;
74 
75 private:
76     std::set<std::string_view> monitors_;
77 
78     void InsertEventMessage(TLSSocketServer *tlsSocketServer, int clientId,
79         const std::shared_ptr<EventManager> &eventManager);
80 };
81 } // namespace TlsSocketServer
82 } // namespace NetStack
83 } // namespace OHOS
84 #endif // TLS_MONITOR_SERVER_H
85