forked from cloudwu/socket-server
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_kqueue.h
More file actions
Latest commit
93 lines (80 loc) · 1.85 KB
/
Copy pathsocket_kqueue.h
File metadata and controls
93 lines (80 loc) · 1.85 KB
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
#ifndefpoll_socket_kqueue_h
#definepoll_socket_kqueue_h
#include<netdb.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/event.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
staticbool
sp_invalid(intkfd) {
returnkfd==-1;
}
staticint
sp_create() {
returnkqueue();
}
staticvoid
sp_release(intkfd) {
close(kfd);
}
staticvoid
sp_del(intkfd, intsock) {
structkeventke;
EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL);
kevent(kfd, &ke, 1, NULL, 0, NULL);
EV_SET(&ke, sock, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
kevent(kfd, &ke, 1, NULL, 0, NULL);
}
staticint
sp_add(intkfd, intsock, void*ud) {
structkeventke;
EV_SET(&ke, sock, EVFILT_READ, EV_ADD, 0, 0, ud);
if (kevent(kfd, &ke, 1, NULL, 0, NULL) ==-1) {
return1;
}
EV_SET(&ke, sock, EVFILT_WRITE, EV_ADD, 0, 0, ud);
if (kevent(kfd, &ke, 1, NULL, 0, NULL) ==-1) {
EV_SET(&ke, sock, EVFILT_READ, EV_DELETE, 0, 0, NULL);
kevent(kfd, &ke, 1, NULL, 0, NULL);
return1;
}
EV_SET(&ke, sock, EVFILT_WRITE, EV_DISABLE, 0, 0, ud);
if (kevent(kfd, &ke, 1, NULL, 0, NULL) ==-1) {
sp_del(kfd, sock);
return1;
}
return0;
}
staticvoid
sp_write(intkfd, intsock, void*ud, boolenable) {
structkeventke;
EV_SET(&ke, sock, EVFILT_WRITE, enable ? EV_ENABLE : EV_DISABLE, 0, 0, ud);
if (kevent(kfd, &ke, 1, NULL, 0, NULL) ==-1) {
// todo: check error
}
}
staticint
sp_wait(intkfd, structevent*e, intmax) {
structkeventev[max];
intn=kevent(kfd, NULL, 0, ev, max, NULL);
inti;
for (i=0;i<n;i++) {
e[i].s=ev[i].udata;
unsignedfilter=ev[i].filter;
e[i].write= (filter==EVFILT_WRITE);
e[i].read= (filter==EVFILT_READ);
}
returnn;
}
staticvoid
sp_nonblocking(intfd) {
intflag=fcntl(fd, F_GETFL, 0);
if ( -1==flag ) {
return;
}
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}
#endif