forked from InfpRC/INFPRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKqueue.cpp
41 lines (33 loc) · 893 Bytes
/
Kqueue.cpp
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
#include "Kqueue.hpp"
Kqueue::Kqueue() {
kq = kqueue();
if (kq == -1) {
throw std::runtime_error("kqueue() error");
}
timeout.tv_sec = TIME;
timeout.tv_nsec = 0;
}
Kqueue::~Kqueue() {}
void Kqueue::addEvent(int fd, int filter) {
EV_SET(&change, fd, filter, EV_ADD|EV_ENABLE, 0, 0, NULL);
change_list.push_back(change);
}
void Kqueue::delEvent(int fd, int filter) {
EV_SET(&change, fd, filter, EV_DELETE, 0, 0, NULL);
change_list.push_back(change);
}
void Kqueue::setTimer(int fd) {
EV_SET(&change, fd, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 600000, NULL);
change_list.push_back(change);
}
int Kqueue::updateEvent() {
int size = kevent(kq, &change_list[0], change_list.size(), events, MAX_CLIENTS, &timeout);
if (size == -1) {
throw std::runtime_error("kevent() error");
}
change_list.clear();
return size;
}
struct kevent Kqueue::getEvent(int i) {
return events[i];
}