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