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
|
#include "epoll.h"
#include <errno.h>
#include <string.h>
static int efd = -1;
#define MAXEVENTS 10
static struct epoll_event events[MAXEVENTS];
int ePoll_init()
{
if (efd != -1) return 0;
efd = epoll_create1(0);
if (efd == -1) return -1;
return 0;
}
int ePoll_wait(const int timeoutMs)
{
const int num = epoll_wait(efd, events, MAXEVENTS, timeoutMs);
if (num < 0 && (errno == EINTR || errno == EAGAIN)) return 0;
if (num < 0) return -1;
for (int i = 0; i < num; ++i) {
epoll_item_t *data = (epoll_item_t *)events[i].data.ptr;
(*data->callback)(data, events[i].events & EPOLLIN, events[i].events & EPOLLOUT, events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP));
}
return 0;
}
/**
* Returns -1 on error, just like epoll_ctl
*/
int ePoll_add(uint32_t flags, epoll_item_t* callback)
{
struct epoll_event event;
memset(&event, 0, sizeof event);
event.data.ptr = (void*)callback;
event.events = flags;
return epoll_ctl(efd, EPOLL_CTL_ADD, callback->fd, &event);
}
int ePoll_remove(int fd)
{
return epoll_ctl(efd, EPOLL_CTL_DEL, fd, (struct epoll_event*)67775);
}
|