forked from oza/zkproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.c
149 lines (126 loc) · 2.93 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2011 MORITA Kazutaka <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include "list.h"
#include "util.h"
#include "event.h"
#include "coroutine.h"
static int efd;
static LIST_HEAD(events_list);
struct event_info {
event_handler_t handler;
int fd;
void *data;
struct list_head ei_list;
};
int init_event(int nr)
{
efd = epoll_create(nr);
if (efd < 0) {
fprintf(stderr, "can't create epoll fd\n");
return -1;
}
return 0;
}
static struct event_info *lookup_event(int fd)
{
struct event_info *ei;
list_for_each_entry(ei, &events_list, ei_list) {
if (ei->fd == fd)
return ei;
}
return NULL;
}
int register_event(int fd, event_handler_t h, void *data)
{
int ret;
struct epoll_event ev;
struct event_info *ei;
ei = zalloc(sizeof(*ei));
if (!ei)
return -ENOMEM;
ei->fd = fd;
ei->handler = h;
ei->data = data;
ev.events = EPOLLIN;
ev.data.ptr = ei;
ret = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
if (ret) {
fprintf(stderr, "can't add epoll event, %m\n");
free(ei);
} else
list_add(&ei->ei_list, &events_list);
return ret;
}
void unregister_event(int fd)
{
int ret;
struct event_info *ei;
ei = lookup_event(fd);
if (!ei) {
fprintf(stderr, "can't find a event\n");
return;
}
ret = epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL);
if (ret)
fprintf(stderr, "can't del epoll event, %m\n");
list_del(&ei->ei_list);
free(ei);
}
int modify_event(int fd, unsigned int events)
{
int ret;
struct epoll_event ev;
struct event_info *ei;
ei = lookup_event(fd);
if (!ei) {
fprintf(stderr, "can't find event info %d\n", fd);
return 1;
}
memset(&ev, 0, sizeof(ev));
ev.events = events;
ev.data.ptr = ei;
ret = epoll_ctl(efd, EPOLL_CTL_MOD, fd, &ev);
if (ret) {
fprintf(stderr, "can't del epoll event, %m\n");
return 1;
}
return 0;
}
void event_loop(int timeout)
{
int i, nr;
struct epoll_event events[128];
retry:
nr = epoll_wait(efd, events, ARRAY_SIZE(events), timeout);
if (nr < 0) {
if (errno == EINTR)
goto retry;
fprintf(stderr, "epoll_wait failed, %m\n");
exit(1);
} else if (nr) {
for (i = 0; i < nr; i++) {
struct event_info *ei;
ei = (struct event_info *)events[i].data.ptr;
ei->handler(ei->fd, events[i].events, ei->data);
}
}
goto retry;
}