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