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 CRPC_EVENT_LOOP_H
17 #define CRPC_EVENT_LOOP_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 typedef struct FdMask FdMask;
24 struct FdMask {
25     int fd;
26     unsigned int mask;
27 };
28 
29 typedef struct EventLoop EventLoop;
30 struct EventLoop {
31     int maxFd;
32     int setSize;
33     FdMask *fdMasks;
34     int stop;
35     int epfd;
36     struct epoll_event *epEvents;
37 };
38 
39 /**
40  * @Description Create an Event Loop object
41  *
42  * @param size - Number of event type
43  * @return EventLoop* - pointer to the event loop object or NULL if failed
44  */
45 EventLoop *CreateEventLoop(int size);
46 
47 /**
48  * @Description Destroy Events loop
49  *
50  * @param loop - EventLoop object's pointer
51  */
52 void DestroyEventLoop(EventLoop *loop);
53 
54 /**
55  * @Description Stop Events Loop
56  *
57  * @param loop - EventLoop object's pointer
58  */
59 void StopEventLoop(EventLoop *loop);
60 
61 /**
62  * @Description Add an event on socket
63  *
64  * @param loop - EventLoop object's pointer
65  * @param fd - Socket fd
66  * @param addMask - Event mask
67  * @return int - 0 success; -1 add failed
68  */
69 int AddFdEvent(EventLoop *loop, int fd, unsigned int addMask);
70 
71 /**
72  * @Description Remove an event from socket fd
73  *
74  * @param loop - EventLoop object's pointer
75  * @param fd - Socket fd
76  * @param delMask - Event mask
77  * @return int - 0 success; -1 remove failed
78  */
79 int DelFdEvent(EventLoop *loop, int fd, unsigned int delMask);
80 
81 #ifdef __cplusplus
82 }
83 #endif
84 #endif
85