• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 COMMUNICATIONNETSTACK_EVENT_MANAGER_H
17 #define COMMUNICATIONNETSTACK_EVENT_MANAGER_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <iosfwd>
22 #include <list>
23 #include <memory>
24 #include <mutex>
25 #include <shared_mutex>
26 #include <queue>
27 #include <string>
28 #include <unordered_set>
29 #include <utility>
30 #include <unordered_map>
31 #include "event_listener.h"
32 #include "napi/native_api.h"
33 #include "uv.h"
34 
35 namespace OHOS::NetStack {
36 static constexpr const uint32_t EVENT_MANAGER_MAGIC_NUMBER = 0x86161616;
37 struct EventManagerMagic {
38     uint32_t magicNumber_ = EVENT_MANAGER_MAGIC_NUMBER;
~EventManagerMagicEventManagerMagic39     ~EventManagerMagic()
40     {
41         magicNumber_ = ~magicNumber_;
42     }
43 };
44 
45 namespace Websocket {
46 class UserData;
47 }
48 
49 namespace Socks5 {
50     class Socks5Instance;
51 }
52 using Finalizer = void (*)(napi_env, void *data, void *);
53 class EventManager : public std::enable_shared_from_this<EventManager> {
54 public:
55     EventManager();
56 
57     ~EventManager();
58 
59     EventManager(const EventManager &) = delete;
60     EventManager &operator=(const EventManager &manager) = delete;
61 
62     void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback);
63 
64     void DeleteListener(const std::string &type, napi_value callback);
65 
66     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
67 
68     void EmitWithTwoPara(const std::string &type, const std::tuple<napi_value, napi_value, napi_value> &argv);
69 
70     void SetData(void *data);
71 
72     [[nodiscard]] void *GetData();
73 
74     void EmitByUvWithoutCheckShared(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
75 
76     bool HasEventListener(const std::string &type);
77 
78     void DeleteListener(const std::string &type);
79 
80     void SetQueueData(void *data);
81 
82     void *GetQueueData();
83 
84     void SetServerQueueData(void *wsi, void *data);
85 
86     void *GetServerQueueData(void *wsi);
87 
88     void CreateEventReference(napi_env env, napi_value value);
89 
90     void DeleteEventReference(napi_env env);
91 
92     void SetEventDestroy(bool flag);
93 
94     bool IsEventDestroy();
95 
96     const std::string &GetWebSocketTextData();
97 
98     void AppendWebSocketTextData(void *data, size_t length);
99 
100     const std::string &GetWebSocketBinaryData();
101 
102     std::shared_mutex &GetDataMutex();
103 
104     void AppendWebSocketBinaryData(void *data, size_t length);
105 
106     void ClearWebSocketTextData();
107 
108     void ClearWebSocketBinaryData();
109 
110     void NotifyRcvThdExit();
111 
112     void WaitForRcvThdExit();
113 
114     void SetReuseAddr(bool reuse);
115 
116     void SetContextState(bool enable);
117 
118     bool GetContextState();
119 
120     void SetWebSocketUserData(const std::shared_ptr<Websocket::UserData> &userData);
121 
122     std::shared_ptr<Websocket::UserData> GetWebSocketUserData();
123 
124     bool GetReuseAddr();
125 
126     std::shared_ptr<Socks5::Socks5Instance> GetProxyData();
127 
128     void SetProxyData(std::shared_ptr<Socks5::Socks5Instance> data);
129 
130     const std::string &GetWsServerBinaryData(void *wsi);
131 
132     const std::string &GetWsServerTextData(void *wsi);
133 
134     void AppendWsServerBinaryData(void *wsi, void *data, size_t length);
135 
136     void AppendWsServerTextData(void *wsi, void *data, size_t length);
137 
138     void ClearWsServerBinaryData(void *wsi);
139 
140     void ClearWsServerTextData(void *wsi);
141 
142     void SetMaxConnClientCnt(const uint32_t &cnt);
143 
144     void SetMaxConnForOneClient(const uint32_t &cnt);
145 
146     void AddClientUserData(void *wsi, std::shared_ptr<Websocket::UserData> &data);
147 
148     void RemoveClientUserData(void *wsi);
149 
150     [[nodiscard]] uint32_t GetMaxConcurrentClientCnt()const;
151 
152     [[nodiscard]] uint32_t GetMaxConnForOneClient() const;
153 private:
154     std::shared_mutex mutexForListenersAndEmitByUv_;
155     std::shared_mutex dataMutex_;
156     std::mutex dataQueueMutex_;
157     std::list<std::shared_ptr<EventListener>> listeners_;
158     void *data_;
159     std::queue<void *> dataQueue_;
160     static EventManagerMagic magic_;
161     static std::mutex mutexForManager_;
162     napi_ref eventRef_;
163     std::atomic_bool isDestroy_;
164     std::string webSocketTextData_;
165     std::string webSocketBinaryData_;
166     std::mutex sockRcvThdMtx_;
167     std::condition_variable sockRcvThdCon_;
168     bool sockRcvExit_ = false;
169     std::atomic_bool isReuseAddr_ = false;
170     std::shared_ptr<Websocket::UserData> webSocketUserData_;
171     std::shared_ptr<Socks5::Socks5Instance> proxyData_;
172     std::shared_mutex dataServerQueueMutex_;
173     std::mutex mapMutex_;
174     std::unordered_map<void *, std::queue<void *>> serverDataQueue_;
175     std::unordered_map<void *, std::string> wsServerBinaryData_;
176     std::unordered_map<void *, std::string> wsServerTextData_;
177     std::unordered_map<void *, std::shared_ptr<Websocket::UserData>> userDataMap_;
178     uint32_t maxConnClientCnt_ = 0;
179     uint32_t maxConnForOneClient_ = 0;
180     bool isOpened_ = true;
181 
182 public:
183     struct {
184         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
185     } innerMagic_;
186     napi_env env_ = nullptr;
187     std::string className_;
188     Finalizer finalizer_ = nullptr;
189 };
190 
191 class EventManagerForHttp {
192 private:
193     [[maybe_unused]] std::mutex mutexForListenersAndEmitByUv_;
194     [[maybe_unused]] std::mutex mutexForEmitAndEmitByUv_;
195     [[maybe_unused]] std::mutex dataMutex_;
196     [[maybe_unused]] std::mutex dataQueueMutex_;
197     [[maybe_unused]] std::shared_mutex dataServerQueueMutex_;
198     [[maybe_unused]] std::list<EventListener> listeners_;
199     [[maybe_unused]] void *data_ = nullptr;
200     [[maybe_unused]] std::queue<void *> dataQueue_;
201     [[maybe_unused]] static EventManagerMagic magic_;
202     [[maybe_unused]] static std::mutex mutexForManager_;
203     [[maybe_unused]] static std::unordered_set<EventManager *> validManager_;
204     [[maybe_unused]] napi_ref eventRef_ = nullptr;
205     [[maybe_unused]] std::atomic_bool isDestroy_;
206     [[maybe_unused]] std::string webSocketTextData_;
207     [[maybe_unused]] std::string webSocketBinaryData_;
208     [[maybe_unused]] std::mutex sockRcvThdMtx_;
209     [[maybe_unused]] std::condition_variable sockRcvThdCon_;
210     [[maybe_unused]] bool sockRcvExit_ = false;
211     [[maybe_unused]] std::atomic_bool isReuseAddr_ = false;
212     [[maybe_unused]] std::shared_ptr<Websocket::UserData> webSocketUserData_;
213 
214 public:
215     [[maybe_unused]] struct {
216         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
217     } innerMagic_;
218 };
219 
220 struct EventManagerWrapper {
221     EventManagerForHttp eventManager;
222     std::shared_ptr<EventManager> sharedManager;
223 };
224 
225 struct UvWorkWrapperShared {
226     UvWorkWrapperShared() = delete;
227 
228     UvWorkWrapperShared(void *theData, napi_env theEnv, std::string eventType,
229                         const std::shared_ptr<EventManager> &eventManager);
230 
231     void *data = nullptr;
232     napi_env env = nullptr;
233     std::string type;
234     std::shared_ptr<EventManager> manager;
235 };
236 } // namespace OHOS::NetStack
237 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */
238