• 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 
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 
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 SetData(void *data);
69 
70     [[nodiscard]] void *GetData();
71 
72     void EmitByUvWithoutCheckShared(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
73 
74     bool HasEventListener(const std::string &type);
75 
76     void DeleteListener(const std::string &type);
77 
78     void SetQueueData(void *data);
79 
80     void *GetQueueData();
81 
82     void CreateEventReference(napi_env env, napi_value value);
83 
84     void DeleteEventReference(napi_env env);
85 
86     void SetEventDestroy(bool flag);
87 
88     bool IsEventDestroy();
89 
90     const std::string &GetWebSocketTextData();
91 
92     void AppendWebSocketTextData(void *data, size_t length);
93 
94     const std::string &GetWebSocketBinaryData();
95 
96     std::shared_mutex &GetDataMutex();
97 
98     void AppendWebSocketBinaryData(void *data, size_t length);
99 
100     void ClearWebSocketTextData();
101 
102     void ClearWebSocketBinaryData();
103 
104     void NotifyRcvThdExit();
105 
106     void WaitForRcvThdExit();
107 
108     void SetReuseAddr(bool reuse);
109 
110     void SetWebSocketUserData(const std::shared_ptr<Websocket::UserData> &userData);
111 
112     std::shared_ptr<Websocket::UserData> GetWebSocketUserData();
113 
114     bool GetReuseAddr();
115 
116     std::shared_ptr<Socks5::Socks5Instance> GetProxyData();
117 
118     void SetProxyData(std::shared_ptr<Socks5::Socks5Instance> data);
119 
120 private:
121     std::shared_mutex mutexForListenersAndEmitByUv_;
122     std::mutex mutexForEmitAndEmitByUv_;
123     std::shared_mutex dataMutex_;
124     std::mutex dataQueueMutex_;
125     std::list<EventListener> listeners_;
126     void *data_;
127     std::queue<void *> dataQueue_;
128     static EventManagerMagic magic_;
129     static std::mutex mutexForManager_;
130     napi_ref eventRef_;
131     std::atomic_bool isDestroy_;
132     std::string webSocketTextData_;
133     std::string webSocketBinaryData_;
134     std::mutex sockRcvThdMtx_;
135     std::condition_variable sockRcvThdCon_;
136     bool sockRcvExit_ = false;
137     std::atomic_bool isReuseAddr_ = false;
138     std::shared_ptr<Websocket::UserData> webSocketUserData_;
139     std::shared_ptr<Socks5::Socks5Instance> proxyData_;
140 
141 public:
142     struct {
143         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
144     } innerMagic_;
145 };
146 
147 class EventManagerForHttp {
148 private:
149     [[maybe_unused]] std::mutex mutexForListenersAndEmitByUv_;
150     [[maybe_unused]] std::mutex mutexForEmitAndEmitByUv_;
151     [[maybe_unused]] std::mutex dataMutex_;
152     [[maybe_unused]] std::mutex dataQueueMutex_;
153     [[maybe_unused]] std::list<EventListener> listeners_;
154     [[maybe_unused]] void *data_ = nullptr;
155     [[maybe_unused]] std::queue<void *> dataQueue_;
156     [[maybe_unused]] static EventManagerMagic magic_;
157     [[maybe_unused]] static std::mutex mutexForManager_;
158     [[maybe_unused]] static std::unordered_set<EventManager *> validManager_;
159     [[maybe_unused]] napi_ref eventRef_ = nullptr;
160     [[maybe_unused]] std::atomic_bool isDestroy_;
161     [[maybe_unused]] std::string webSocketTextData_;
162     [[maybe_unused]] std::string webSocketBinaryData_;
163     [[maybe_unused]] std::mutex sockRcvThdMtx_;
164     [[maybe_unused]] std::condition_variable sockRcvThdCon_;
165     [[maybe_unused]] bool sockRcvExit_ = false;
166     [[maybe_unused]] std::atomic_bool isReuseAddr_ = false;
167     [[maybe_unused]] std::shared_ptr<Websocket::UserData> webSocketUserData_;
168 
169 public:
170     [[maybe_unused]] struct {
171         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
172     } innerMagic_;
173 };
174 
175 struct EventManagerWrapper {
176     EventManagerForHttp eventManager;
177     std::shared_ptr<EventManager> sharedManager;
178 };
179 
180 struct UvWorkWrapperShared {
181     UvWorkWrapperShared() = delete;
182 
183     UvWorkWrapperShared(void *theData, napi_env theEnv, std::string eventType,
184                         const std::shared_ptr<EventManager> &eventManager);
185 
186     void *data = nullptr;
187     napi_env env = nullptr;
188     std::string type;
189     std::shared_ptr<EventManager> manager;
190 };
191 } // namespace OHOS::NetStack
192 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */
193