• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <iosfwd>
21 #include <list>
22 #include <mutex>
23 #include <unordered_set>
24 #include <string>
25 #include <utility>
26 #include <queue>
27 
28 #include "event_listener.h"
29 #include "napi/native_api.h"
30 #include "uv.h"
31 
32 namespace OHOS::NetStack {
33 class EventManager {
34 public:
35     EventManager();
36 
37     ~EventManager();
38 
39     EventManager(const EventManager &) = delete;
40     EventManager &operator=(const EventManager &manager) = delete;
41 
42     void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback);
43 
44     void DeleteListener(const std::string &type, napi_value callback);
45 
46     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
47 
48     void SetData(void *data);
49 
50     [[nodiscard]] void *GetData();
51 
52     void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
53 
54     bool HasEventListener(const std::string &type);
55 
56     void DeleteListener(const std::string &type);
57 
58     static void SetInvalid(EventManager *manager);
59 
60     static bool IsManagerValid(EventManager *manager);
61 
62     static void SetValid(EventManager *manager);
63 
64     void SetQueueData(void *data);
65 
66     void *GetQueueData();
67 
68     void PopQueueData();
69 
70     void CreateEventReference(napi_env env, napi_value value);
71 
72     void DeleteEventReference(napi_env env);
73 
74     void SetEventDestroy(bool flag);
75 
76     bool IsEventDestroy();
77 
78     const std::string &GetWebSocketTextData();
79 
80     void AppendWebSocketTextData(void *data, size_t length);
81 
82     const std::string &GetWebSocketBinaryData();
83 
84     void AppendWebSocketBinaryData(void *data, size_t length);
85 
86     void ClearWebSocketTextData();
87 
88     void ClearWebSocketBinaryData();
89 
90 private:
91     std::mutex mutexForListenersAndEmitByUv_;
92     std::mutex mutexForEmitAndEmitByUv_;
93     std::mutex dataMutex_;
94     std::mutex dataQueueMutex_;
95     std::list<EventListener> listeners_;
96     void *data_;
97     std::queue<void *> dataQueue_;
98     static std::mutex mutexForManager_;
99     static std::unordered_set<EventManager *> validManager_;
100     napi_ref eventRef_;
101     std::atomic_bool isDestroy_;
102     std::string webSocketTextData_;
103     std::string webSocketBinaryData_;
104 };
105 
106 struct UvWorkWrapper {
107     UvWorkWrapper() = delete;
108 
109     explicit UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager);
110 
111     void *data;
112     napi_env env;
113     std::string type;
114     EventManager *manager;
115 };
116 } // namespace OHOS::NetStack
117 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */
118