-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepoll.cpp
50 lines (40 loc) · 1.2 KB
/
epoll.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
42
43
44
45
46
47
48
49
50
// encode UTF-8
// @Author : Aged_cat
// @Date : 2021-05-20
#include "epoller.h"
Epoller::Epoller(int maxEvent):epollerFd_(epoll_create(512)), events_(maxEvent){
assert(epollerFd_ >= 0 && events_.size() > 0);
}
Epoller::~Epoller() {
close(epollerFd_);
}
bool Epoller::addFd(int fd, uint32_t events) {
if(fd < 0) return false;
epoll_event ev = {0};
ev.data.fd = fd;
ev.events = events;
return 0 == epoll_ctl(epollerFd_, EPOLL_CTL_ADD, fd, &ev);
}
bool Epoller::modFd(int fd, uint32_t events) {
if(fd < 0) return false;
epoll_event ev = {0};
ev.data.fd = fd;
ev.events = events;
return 0 == epoll_ctl(epollerFd_, EPOLL_CTL_MOD, fd, &ev);
}
bool Epoller::delFd(int fd) {
if(fd < 0) return false;
epoll_event ev = {0};
return 0 == epoll_ctl(epollerFd_, EPOLL_CTL_DEL, fd, &ev);
}
int Epoller::wait(int timeoutMs) {
return epoll_wait(epollerFd_, &events_[0], static_cast<int>(events_.size()), timeoutMs);
}
int Epoller::getEventFd(size_t i) const {
assert(i < events_.size() && i >= 0);
return events_[i].data.fd;
}
uint32_t Epoller::getEvents(size_t i) const {
assert(i < events_.size() && i >= 0);
return events_[i].events;
}