blob: 9f03568e361807368a97adf09020730f58b6c085
1 | #include "zepoll.h" |
2 | |
3 | |
4 | Epoll::Epoll(int _max, int maxevents): max(_max), |
5 | epoll_fd(-1), |
6 | epoll_timeout(0), |
7 | epoll_maxevents(maxevents), |
8 | backEvents(0) |
9 | { |
10 | |
11 | } |
12 | |
13 | Epoll::~Epoll() |
14 | { |
15 | if (isValid()) { |
16 | close(epoll_fd); |
17 | } |
18 | delete[] backEvents; |
19 | } |
20 | |
21 | |
22 | inline |
23 | bool Epoll::isValid() const |
24 | { |
25 | return epoll_fd > 0; |
26 | } |
27 | |
28 | void Epoll::setTimeout(int timeout) |
29 | { |
30 | epoll_timeout = timeout; |
31 | } |
32 | |
33 | inline |
34 | void Epoll::setMaxEvents(int maxevents) |
35 | { |
36 | epoll_maxevents = maxevents; |
37 | } |
38 | |
39 | inline |
40 | const epoll_event *Epoll::events() const |
41 | { |
42 | return backEvents; |
43 | } |
44 | |
45 | |
46 | |
47 | int Epoll::create() |
48 | { |
49 | epoll_fd = ::epoll_create(max); |
50 | if (isValid()) { |
51 | backEvents = new epoll_event[epoll_maxevents]; |
52 | } |
53 | return epoll_fd; |
54 | } |
55 | |
56 | int Epoll::add(int fd, epoll_event *event) |
57 | { |
58 | if (isValid()) { |
59 | return ::epoll_ctl(epoll_fd, ADD, fd, event); |
60 | } |
61 | return -1; |
62 | |
63 | } |
64 | |
65 | int Epoll::mod(int fd, epoll_event *event) |
66 | { |
67 | if (isValid()) { |
68 | return ::epoll_ctl(epoll_fd, MOD, fd, event); |
69 | } |
70 | return -1; |
71 | |
72 | } |
73 | |
74 | int Epoll::del(int fd, epoll_event *event) |
75 | { |
76 | if (isValid()) { |
77 | return ::epoll_ctl(epoll_fd, DEL, fd, event); |
78 | } |
79 | return -1; |
80 | } |
81 | |
82 | int Epoll::wait() |
83 | { |
84 | if (isValid()) { |
85 | return ::epoll_wait(epoll_fd, backEvents, epoll_maxevents, epoll_timeout); |
86 | } |
87 | return -1; |
88 | } |
89 |