-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkqueue_handler.c
98 lines (79 loc) · 2.52 KB
/
kqueue_handler.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
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/event.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "io_handler.h"
#include "server.h"
#include "client.h"
struct io_handler_t {
int kq;
struct kevent events[MAX_EVENTS];
};
io_handler_t *create_io_handler(void) {
io_handler_t *handler = malloc(sizeof(io_handler_t));
if (!handler) return NULL;
handler->kq = kqueue();
if (handler->kq == -1) {
free(handler);
return NULL;
}
return handler;
}
void destroy_io_handler(io_handler_t *handler) {
if (handler) {
close(handler->kq);
free(handler);
}
}
int add_to_io_handler(io_handler_t *handler, int fd, int events) {
struct kevent ev[2];
int n = 0;
if (events & EVENT_READ) {
EV_SET(&ev[n++], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);
}
if (events & EVENT_WRITE) {
EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, NULL);
}
return kevent(handler->kq, ev, n, NULL, 0, NULL) != -1;
}
int remove_from_io_handler(io_handler_t *handler, int fd) {
struct kevent ev[2];
EV_SET(&ev[0], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
EV_SET(&ev[1], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
return kevent(handler->kq, ev, 2, NULL, 0, NULL) != -1;
}
int handle_events(io_handler_t *handler) {
struct timespec timeout = {1, 0}; // 1 second timeout
int nev = kevent(handler->kq, NULL, 0, handler->events, MAX_EVENTS, &timeout);
if (nev == -1) {
if (errno == EINTR) {
// Interrupted system call, not an error
return 0;
}
fprintf(stderr, "kevent error: %s\n", strerror(errno));
return -1;
}
if (nev == 0) {
// Timeout, no events
return 0;
}
for (int i = 0; i < nev; i++) {
int fd = (int)handler->events[i].ident;
int events = 0;
if (handler->events[i].filter == EVFILT_READ) events |= EVENT_READ;
if (handler->events[i].filter == EVFILT_WRITE) events |= EVENT_WRITE;
if (fd == get_server_socket()) {
if (handle_new_connection(handler, fd) < 0) {
fprintf(stderr, "Error handling new connection\n");
}
} else {
handle_client_event(handler, fd, events);
}
}
return nev;
}
#endif // defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)