1 /*
2 * Copyright (c) 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 SOFTBUS_WATCH_EVENT_INTERFACE_H
17 #define SOFTBUS_WATCH_EVENT_INTERFACE_H
18
19 #include "softbus_adapter_mem.h"
20 #include "softbus_socket.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 struct FdNode {
27 ListNode node;
28 int32_t fd;
29 uint32_t triggerSet;
30 };
31
32 typedef int32_t (*GetAllFdEventCallback)(ListNode *list);
33
34 typedef struct {
35 GetAllFdEventCallback callback;
36 int32_t watcherId;
37 } EventWatcher;
38
39 EventWatcher* RegisterEventWatcher(const GetAllFdEventCallback callback);
40 int32_t AddEvent(EventWatcher *watcher, int32_t fd, uint32_t event);
41 int32_t ModifyEvent(EventWatcher *watcher, int32_t fd, uint32_t event);
42 int32_t RemoveEvent(EventWatcher *watcher, int32_t fd);
43 int32_t WatchEvent(EventWatcher *watcher, int32_t timeoutMS, ListNode *out);
44 void CloseEventWatcher(EventWatcher *watcher);
45
46 int32_t WaitEvent(int32_t fd, enum SocketEvent events, int32_t timeout);
47
ReleaseFdNode(ListNode * fdNode)48 inline static void ReleaseFdNode(ListNode *fdNode)
49 {
50 struct FdNode *it = NULL;
51 struct FdNode *next = NULL;
52 LIST_FOR_EACH_ENTRY_SAFE(it, next, fdNode, struct FdNode, node) {
53 ListDelete(&it->node);
54 SoftBusFree(it);
55 }
56 }
57
58 #ifdef __cplusplus
59 }
60 #endif /* __cplusplus */
61 #endif /* SOFTBUS_WATCH_EVENT_INTERFACE_H */