-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.c
94 lines (70 loc) · 1.23 KB
/
event.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
/*
* event.c
*
* Holger Eitzenberger, 2007.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <assert.h>
#include <sys/queue.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include "event.h"
struct ev_lh ev_list;
int epoll_fd;
struct epoll_event *events;
int
ev_init(void)
{
epoll_fd = epoll_create(10);
if ((events = calloc(10, sizeof(struct epoll_event))) == NULL)
return -1;
return epoll_fd;
}
struct ev *
ev_new(void)
{
struct ev *ev;
if ((ev = calloc(1, sizeof(struct ev))) == NULL)
return NULL;
return ev;
}
int
ev_add(struct ev *ev)
{
struct epoll_event eev = {
.data = { .ptr = ev, },
.events = EPOLLIN,
};
if (!ev || ev->fd <= 0 || !ev->cb) {
errno = EINVAL;
return -1;
}
if ((epoll_ctl(epoll_fd, EPOLL_CTL_ADD, ev->fd, &eev)) < 0) {
perror("epoll_ctl");
return -1;
}
LIST_INSERT_HEAD(&ev_list, ev, link);
return 0;
}
int
ev_dispatch(void)
{
for (;;) {
int i, nfds;
nfds = epoll_wait(epoll_fd, events, 1, -1);
for (i = 0; i < nfds; i++) {
struct ev *ev = events[i].data.ptr;
assert(ev->cb != NULL);
ev->cb(ev, NULL);
}
}
return 0;
}