-
Notifications
You must be signed in to change notification settings - Fork 1
/
pd_event.c
executable file
·197 lines (181 loc) · 4.57 KB
/
pd_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdlib.h>
#include <sys/epoll.h>
#include <errno.h>
#include <string.h>
#include "pd_event.h"
#include "pd_log.h"
#include "pd_ioc.h"
#include "pd_socket.h"
struct PdSocketEvent
{
int epoll_fd;
};
static struct PdSocketEvent *alloc_();
static void free_(struct PdSocketEvent *ev);
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PdSocketEvent *alloc_()
{
struct PdSocketEvent *ret = (struct PdSocketEvent*)malloc(sizeof(struct PdSocketEvent));
if (NULL != ret)
{
ret->epoll_fd = -1;
}
return ret;
}
void free_(struct PdSocketEvent *ev)
{
if (NULL != ev)
{
free(ev);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PdSocketEvent *pd_event_init()
{
struct PdSocketEvent *ret = NULL;
if (NULL == (ret = alloc_()))
{
PD_LOG(WARN, "alloc PdSocketEvent fail");
}
else if (-1 == (ret->epoll_fd = epoll_create(PD_MAX_SOCKET_EVENTS)))
{
PD_LOG(WARN, "epoll_create fail, errno=%u", errno);
free(ret);
ret = NULL;
}
else
{
PD_LOG(INFO, "epoll_create succ, epoll_fd=%d", ret->epoll_fd);
}
return ret;
}
void pd_event_destroy(struct PdSocketEvent *ev)
{
if (NULL == ev)
{
PD_LOG(WARN, "ev null pointer");
}
else
{
if (-1 != ev->epoll_fd)
{
close(ev->epoll_fd);
ev->epoll_fd = -1;
}
free_(ev);
}
}
int pd_event_set_event(struct PdSocketEvent *ev, struct PdIOComponent *ioc, int enable_read, int enable_write)
{
int ret = 0;
if (NULL == ev || NULL == ioc || NULL == ioc->sock)
{
PD_LOG(WARN, "invalid param, ev=%p ioc=%p sock=%p", ev, ioc, ioc->sock);
ret = -1;
}
else if ((ioc->in_epoll_read == enable_read)
&& (ioc->in_epoll_write == enable_write))
{
// need not
}
else
{
struct epoll_event epoll_ev;
memset(&epoll_ev, 0, sizeof(epoll_ev));
epoll_ev.data.ptr = ioc;
epoll_ev.events = 0;
if (enable_write)
{
epoll_ev.events |= EPOLLOUT;
}
if (enable_read)
{
epoll_ev.events |= EPOLLIN;
}
int op = (ioc->in_epoll_read || ioc->in_epoll_write) ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
if (0 != epoll_ctl(ev->epoll_fd, op, pd_socket_get_fd(ioc->sock), &epoll_ev))
{
PD_LOG(WARN, "epoll_ctl fail, op=%d errno=%u epoll_fd=%d sock_fd=%d ioc=%p",
errno, op, ev->epoll_fd, pd_socket_get_fd(ioc->sock), ioc);
ret = -1;
}
else
{
ioc->in_epoll_read = enable_read;
ioc->in_epoll_write = enable_write;
PD_LOG(DEBUG, "epoll_ctl mod succ, epoll_fd=%d sock_fd=%d ioc=%p",
ev->epoll_fd, pd_socket_get_fd(ioc->sock), ioc);
}
}
return ret;
}
int pd_event_remove_event(struct PdSocketEvent *ev, struct PdIOComponent *ioc)
{
int ret = 0;
if (NULL == ev || NULL == ioc || NULL == ioc->sock)
{
PD_LOG(WARN, "invalid param, ev=%p ioc=%p sock=%p", ev, ioc, ioc->sock);
ret = -1;
}
else
{
struct epoll_event epoll_ev;
memset(&epoll_ev, 0, sizeof(epoll_ev));
epoll_ev.data.ptr = ioc;
epoll_ev.events = 0;
if (0 != epoll_ctl(ev->epoll_fd, EPOLL_CTL_DEL, pd_socket_get_fd(ioc->sock), &epoll_ev))
{
PD_LOG(WARN, "epoll_ctl del fail, errno=%u epoll_fd=%d sock_fd=%d ioc=%p",
errno, ev->epoll_fd, pd_socket_get_fd(ioc->sock), ioc);
ret = -1;
}
else
{
ioc->in_epoll_read = 0;
ioc->in_epoll_write = 0;
PD_LOG(DEBUG, "epoll_ctl del succ, epoll_fd=%d sock_fd=%d ioc=%p",
ev->epoll_fd, pd_socket_get_fd(ioc->sock), ioc);
}
}
return ret;
}
int pd_event_get_events(struct PdSocketEvent *ev, int timeout_us, struct PdIOEvent *events, int events_cnt)
{
int ret = 0;
if (NULL == ev || NULL == events || 0 >= events_cnt)
{
PD_LOG(WARN, "invalid param, ev=%p events=%p events_cnt=%d", ev, events, events_cnt);
ret = -1;
}
else
{
struct epoll_event epoll_events[PD_MAX_SOCKET_EVENTS];
if (events_cnt > PD_MAX_SOCKET_EVENTS)
{
events_cnt = PD_MAX_SOCKET_EVENTS;
}
ret = epoll_wait(ev->epoll_fd, epoll_events, events_cnt, timeout_us / 1000);
if (0 < ret)
{
memset(events, 0, sizeof(struct PdIOEvent) * (uint32_t)ret);
}
int i = 0;
for (; i < ret; i++)
{
events[i].ioc = epoll_events[i].data.ptr;
if (epoll_events[i].events & (EPOLLERR | EPOLLHUP))
{
events[i].error = 1;
}
if ((epoll_events[i].events & EPOLLIN) != 0)
{
events[i].readable = 1;
}
if ((epoll_events[i].events & EPOLLOUT) != 0)
{
events[i].writeable = 1;
}
}
}
return ret;
}