1 /*
2  * Copyright (c) 2021-2024 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 <queue>
26 #include <string>
27 #include <unordered_set>
28 #include <utility>
29 
30 #include "event_listener.h"
31 #include "napi/native_api.h"
32 #include "uv.h"
33 
34 namespace OHOS::NetStack {
35 static constexpr const uint32_t EVENT_MANAGER_MAGIC_NUMBER = 0x86161616;
36 
37 namespace Websocket {
38 class UserData;
39 }
40 
41 class EventManager : public std::enable_shared_from_this<EventManager> {
42 public:
43     EventManager();
44 
45     ~EventManager();
46 
47     EventManager(const EventManager &) = delete;
48     EventManager &operator=(const EventManager &manager) = delete;
49 
50     void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback);
51 
52     void DeleteListener(const std::string &type, napi_value callback);
53 
54     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
55 
56     void SetData(void *data);
57 
58     [[nodiscard]] void *GetData();
59 
60     void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
61 
62     void EmitByUvWithoutCheck(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
63 
64     void EmitByUvWithoutCheckShared(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
65 
66     bool HasEventListener(const std::string &type);
67 
68     void DeleteListener(const std::string &type);
69 
70     static void SetInvalid(EventManager *manager);
71 
72     static bool IsManagerValid(EventManager *manager);
73 
74     static void SetValid(EventManager *manager);
75 
76     void SetQueueData(void *data);
77 
78     void *GetQueueData();
79 
80     void CreateEventReference(napi_env env, napi_value value);
81 
82     void DeleteEventReference(napi_env env);
83 
84     void SetEventDestroy(bool flag);
85 
86     bool IsEventDestroy();
87 
88     const std::string &GetWebSocketTextData();
89 
90     void AppendWebSocketTextData(void *data, size_t length);
91 
92     const std::string &GetWebSocketBinaryData();
93 
94     void AppendWebSocketBinaryData(void *data, size_t length);
95 
96     void ClearWebSocketTextData();
97 
98     void ClearWebSocketBinaryData();
99 
100     void NotifyRcvThdExit();
101 
102     void WaitForRcvThdExit();
103 
104     void SetReuseAddr(bool reuse);
105 
106     void SetWebSocketUserData(const std::shared_ptr<Websocket::UserData> &userData);
107 
108     std::shared_ptr<Websocket::UserData> GetWebSocketUserData();
109 
110     bool GetReuseAddr();
111 
112 private:
113     std::mutex mutexForListenersAndEmitByUv_;
114     std::mutex mutexForEmitAndEmitByUv_;
115     std::mutex dataMutex_;
116     std::mutex dataQueueMutex_;
117     std::list<EventListener> listeners_;
118     void *data_;
119     std::queue<void *> dataQueue_;
120     static std::mutex mutexForManager_;
121     static std::unordered_set<EventManager *> validManager_;
122     napi_ref eventRef_;
123     std::atomic_bool isDestroy_;
124     std::string webSocketTextData_;
125     std::string webSocketBinaryData_;
126     std::mutex sockRcvThdMtx_;
127     std::condition_variable sockRcvThdCon_;
128     bool sockRcvExit_ = false;
129     std::atomic_bool isReuseAddr_ = false;
130     std::shared_ptr<Websocket::UserData> webSocketUserData_;
131 
132 public:
133     struct {
134         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
135     } innerMagic_;
136 };
137 
138 struct UvWorkWrapper {
139     UvWorkWrapper() = delete;
140 
141     UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager);
142 
143     void *data = nullptr;
144     napi_env env = nullptr;
145     std::string type;
146     EventManager *manager = nullptr;
147 };
148 
149 class EventManagerForHttp {
150 private:
151     [[maybe_unused]] std::mutex mutexForListenersAndEmitByUv_;
152     [[maybe_unused]] std::mutex mutexForEmitAndEmitByUv_;
153     [[maybe_unused]] std::mutex dataMutex_;
154     [[maybe_unused]] std::mutex dataQueueMutex_;
155     [[maybe_unused]] std::list<EventListener> listeners_;
156     [[maybe_unused]] void *data_ = nullptr;
157     [[maybe_unused]] std::queue<void *> dataQueue_;
158     [[maybe_unused]] static std::mutex mutexForManager_;
159     [[maybe_unused]] static std::unordered_set<EventManager *> validManager_;
160     [[maybe_unused]] napi_ref eventRef_ = nullptr;
161     [[maybe_unused]] std::atomic_bool isDestroy_;
162     [[maybe_unused]] std::string webSocketTextData_;
163     [[maybe_unused]] std::string webSocketBinaryData_;
164     [[maybe_unused]] std::mutex sockRcvThdMtx_;
165     [[maybe_unused]] std::condition_variable sockRcvThdCon_;
166     [[maybe_unused]] bool sockRcvExit_ = false;
167     [[maybe_unused]] std::atomic_bool isReuseAddr_ = false;
168     [[maybe_unused]] std::shared_ptr<Websocket::UserData> webSocketUserData_;
169 
170 public:
171     [[maybe_unused]] struct {
172         uint32_t magicNumber = EVENT_MANAGER_MAGIC_NUMBER;
173     } innerMagic_;
174 };
175 
176 struct EventManagerWrapper {
177     EventManagerForHttp eventManager;
178     std::shared_ptr<EventManager> sharedManager;
179 };
180 
181 struct UvWorkWrapperShared {
182     UvWorkWrapperShared() = delete;
183 
184     UvWorkWrapperShared(void *theData, napi_env theEnv, std::string eventType,
185                         const std::shared_ptr<EventManager> &eventManager);
186 
187     void *data = nullptr;
188     napi_env env = nullptr;
189     std::string type;
190     std::shared_ptr<EventManager> manager;
191 };
192 } // namespace OHOS::NetStack
193 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */
194