-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxevent_kqueue.h
189 lines (185 loc) · 5.94 KB
/
xevent_kqueue.h
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
/*
* xevent backend
*
* Copyright (c) 2020 Rain Liang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#ifndef _X_EVENT_KQUE_
#define _X_EVENT_KQUE_
#if defined(__APPLE__)
#include <sys/event.h>
int xfilter2filter(int xfilter) {
switch (xfilter) {
case xfilter_read:
return EVFILT_READ;
case xfilter_write:
return EVFILT_WRITE;
case xfilter_error:
return EVFILT_EXCEPT;
default:;
}
return EVFILT_EXCEPT;
}
xevent_filter filter2xfilter(int kfilter) {
switch (kfilter) {
case EVFILT_READ:
return xfilter_read;
case EVFILT_WRITE:
return xfilter_write;
case EVFILT_EXCEPT:
return xfilter_error;
default:;
}
return xfilter_error;
}
// build kevent from info
struct kevent buildkevent(int fd, int xfilter, xevent_action act) {
int flag = EV_ADD | EV_ENABLE;
if (act == xaction_del)
flag = EV_DELETE;
struct kevent kevt;
EV_SET(&kevt, fd, xfilter2filter(xfilter), flag, 0, 0, NULL);
return kevt;
}
// get proc func from fd and filter
xevent_callback geteventcallback(int fd, int kfilter) {
xevent &evt = xeventpool()[fd];
if (evt.fd != fd)
return NULL;
xevent_filter xflt = filter2xfilter(kfilter);
if (evt.funcs[xflt].filter == -1)
return NULL;
return evt.funcs[xflt].func;
}
xevent_callback geteventcallback(struct kevent &kevt) {
return geteventcallback(kevt.ident, kevt.filter);
}
int initxevent() {
_epfd = kqueue();
if (_epfd == -1) {
LOG_E("init xevent with kqueue error");
return -1;
}
return 0;
}
const char *xfilterdesc(int xfilter);
// 0-Succ, -1-failed
int regxevent(int fd, xevent_filter filter, xevent_callback func) {
if (fd >= MAX_EVENT_POOL) {
LOG_E("fd exceed max event pool size(%d >= %d)", fd, MAX_EVENT_POOL);
return -1;
}
xevent &evt = xeventpool()[fd];
// only update func
if (evt.funcs[filter].filter != -1) {
evt.funcs[filter].func = func;
return 0;
}
// modify data
evt.fd = fd;
evt.funcs[filter].filter = filter;
evt.funcs[filter].func = func;
// add event
struct kevent kevt = buildkevent(fd, filter, xaction_add);
kevent(_epfd, &kevt, 1, NULL, 0, NULL);
LOG_D("regevent: fd-%d, filter-%s", fd, xfilterdesc(filter));
_fdnums++;
return 0;
};
int unregxevent(int fd, xevent_filter filter) {
xevent &evt = xeventpool()[fd];
if (evt.fd == -1 || evt.funcs[filter].filter == -1) {
LOG_D("remove fd-filter(%d-%d) event not exist", fd, filter);
return 0;
}
// remove data
evt.funcs[filter].reset();
// remove event
struct kevent kevt = buildkevent(fd, filter, xaction_del);
kevent(_epfd, &kevt, 1, NULL, 0, NULL);
_fdnums--;
LOG_D("unregevent: fd-%d, filter-%s, left-%d", fd, xfilterdesc(filter), _fdnums);
return 0;
};
int unregxevent(int fd) {
xevent &evt = xeventpool()[fd];
if (evt.fd == -1) {
LOG_D("remove fd(%d) event not exist", fd);
return 0;
}
struct kevent kevts[xfilter_count];
int nums = 0;
// modify data
for (int i = 0; i < xfilter_count; i++) {
if (evt.funcs[i].filter != -1) {
kevts[nums] = buildkevent(fd, evt.funcs[i].filter, xaction_del);
evt.funcs[i].reset();
nums++;
}
}
evt.fd = -1;
// remove event
kevent(_epfd, kevts, nums, NULL, 0, NULL);
_fdnums -= nums;
LOG_R("unregevent-fd: fd-%d, nums-%d, left-%d", fd, nums, _fdnums);
return 0;
}
int call_event_func(struct kevent &kevt) {
xevent_callback func = geteventcallback(kevt);
if (func == NULL) {
LOG_D("call_event_func return NULL, fd=%d", int(kevt.ident));
return -1;
}
return func(kevt.ident, filter2xfilter(kevt.filter));
}
struct kevent _events[MAX_EVENT_RECV];
struct timespec _tvs;
// 0-succ, -1-failed
int dispatchxevent(int timeoutsecond) {
_tvs.tv_sec = timeoutsecond;
_tvs.tv_nsec = 0;
int nfds = kevent(_epfd, NULL, 0, _events, MAX_EVENT_RECV, &_tvs);
if (nfds < 0) {
LOG_E("epoll wait error[%d], ignored!", errno);
return -1;
}
for (int i = 0; i < nfds; i++) {
call_event_func(_events[i]);
}
return 0;
};
const char *evfiltdesc(int flag) { return xfilterdesc(filter2xfilter(flag)); }
#endif // end #define __APPLE__
#endif // end #define _X_EVENT_KQUE_