forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsock-bsd.c
118 lines (100 loc) · 2.13 KB
/
sock-bsd.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
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include "dat.h"
enum
{
Infinity = 1 << 30
};
static void handle(Socket*, int, int);
static Handle tick;
static void *tickval;
static int kq;
static int64 ival;
static struct timespec ivalts;
void
sockinit(Handle f, void *x, int64 ns)
{
tick = f;
tickval = x;
ival = ns;
ivalts.tv_sec = ns / 1000000000;
ivalts.tv_nsec = ns % 1000000000;
kq = kqueue();
if (kq == -1) {
twarn("kqueue");
exit(1);
}
}
int
sockwant(Socket *s, int rw)
{
int n = 0;
struct kevent evs[2] = {}, *ev = evs;
struct timespec ts = {};
if (s->added) {
ev->ident = s->fd;
ev->filter = s->added;
ev->flags = EV_DELETE;
ev++;
n++;
}
if (rw) {
ev->ident = s->fd;
switch (rw) {
case 'r':
ev->filter = EVFILT_READ;
break;
case 'w':
ev->filter = EVFILT_WRITE;
break;
default:
// check only for hangup
ev->filter = EVFILT_READ;
ev->fflags = NOTE_LOWAT;
ev->data = Infinity;
}
ev->flags = EV_ADD;
ev->udata = s;
s->added = ev->filter;
ev++;
n++;
}
return kevent(kq, evs, n, NULL, 0, &ts);
}
void
sockmain()
{
int i, r, n = 1;
int64 e, t = nanoseconds();
struct kevent evs[n];
for (;;) {
r = kevent(kq, NULL, 0, evs, n, &ivalts);
if (r == -1 && errno != EINTR) {
twarn("kevent");
exit(1);
}
// should tick?
e = nanoseconds();
if (e-t > ival) {
tick(tickval, 0);
t = e;
}
for (i=0; i<r; i++) {
handle(evs[i].udata, evs[i].filter, evs[i].flags);
}
}
}
static void
handle(Socket *s, int filt, int flags)
{
if (flags & EV_EOF) {
s->f(s->x, 'h');
} else if (filt == EVFILT_READ) {
s->f(s->x, 'r');
} else if (filt == EVFILT_WRITE) {
s->f(s->x, 'w');
}
}