-
Notifications
You must be signed in to change notification settings - Fork 2
/
nio4c_monitor.c
104 lines (82 loc) · 2.64 KB
/
nio4c_monitor.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
/*
* nio4c_monitor.c
*
* copyright (c) 2019, 2020 Xiongfei Shi
*
* author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
* license: Apache-2.0
*
* https://github.com/shixiongfei/nio4c
*/
#include "nio4c_internal.h"
niomonitor_t *monitor_new(nioselector_t *selector, niosocket_t *io,
int interest, void *ud) {
niomonitor_t *monitor = (niomonitor_t *)nio_malloc(sizeof(niomonitor_t));
if (!monitor)
return NULL;
monitor->selector = selector;
monitor->io = io;
monitor->ud = ud;
monitor->interests = interest;
monitor->readiness = 0;
monitor->closed = 0;
return monitor;
}
void monitor_destroy(niomonitor_t *monitor) {
if (!monitor_closed(monitor))
monitor_close(monitor, 1);
nio_free(monitor);
}
void *monitor_userdata(niomonitor_t *monitor) { return monitor->ud; }
niosocket_t *monitor_io(niomonitor_t *monitor) { return monitor->io; }
int monitor_close(niomonitor_t *monitor, int deregister) {
if (monitor_closed(monitor))
return -1;
if (deregister)
selector_deregister(monitor->selector, monitor->io);
monitor->closed = 1;
return 0;
}
int monitor_getinterests(niomonitor_t *monitor) { return monitor->interests; }
int monitor_resetinterests(niomonitor_t *monitor) {
return niopoll_ioevent(monitor->selector->selector, nio_sockfd(monitor->io),
NIO_READ == (monitor->interests & NIO_READ),
NIO_WRITE == (monitor->interests & NIO_WRITE),
monitor);
}
int monitor_setinterests(niomonitor_t *monitor, int interests) {
if (monitor_closed(monitor))
return -1;
if (interests == monitor->interests)
return 0;
monitor->interests = interests;
return monitor_resetinterests(monitor);
}
int monitor_addinterest(niomonitor_t *monitor, int interest) {
if (monitor_closed(monitor))
return -1;
if (interest == (monitor->interests & interest))
return 0;
monitor->interests |= interest;
return monitor_resetinterests(monitor);
}
int monitor_removeinterest(niomonitor_t *monitor, int interest) {
if (monitor_closed(monitor))
return -1;
if (NIO_NIL == interest)
return 0;
if (interest != (monitor->interests & interest))
return 0;
monitor->interests &= ~interest;
return monitor_resetinterests(monitor);
}
int monitor_readable(niomonitor_t *monitor) {
return NIO_READ == (monitor->readiness & NIO_READ);
}
int monitor_writable(niomonitor_t *monitor) {
return NIO_WRITE == (monitor->readiness & NIO_WRITE);
}
int monitor_exception(niomonitor_t *monitor) {
return NIO_IOERROR == (monitor->readiness & NIO_IOERROR);
}
int monitor_closed(niomonitor_t *monitor) { return monitor->closed; }