-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpollOps.cpp
More file actions
64 lines (51 loc) · 1.55 KB
/
EpollOps.cpp
File metadata and controls
64 lines (51 loc) · 1.55 KB
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
#include "EpollOps.h"
#include "Logger.h"
#include <iostream>
#include <string.h>
using namespace std;
int createEpollOrDie() {
int epollfd = epoll_create(5);
if (epollfd == -1)
LOG_ERROR("%s : %s", "epoll_create error", strerror(errno));
return epollfd;
}
/*
* struct epoll_event {
* __uint32_t events; // epoll事件
* epoll_data_t data; // 用户数据
* }
*
* typedef union epoll_data {
* void* ptr;
* int fd; // 事件所属对应的fd
* uint32_t u32;
* uint64_t u64;
* } epoll_data_t;
*
*/
void addFd(int epollfd, int sockFd, bool oneShot, bool enableET) {
struct epoll_event event;
event.data.fd = sockFd;
event.events = EPOLLIN | EPOLLRDHUP;
if (enableET)
event.events |= EPOLLET;
if (oneShot)
event.events |= EPOLLONESHOT;
int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, sockFd, &event);
if (ret < 0)
LOG_ERROR("%s : %s", "epoll_ctl EPOLL_CTL_ADD error", strerror(errno));
}
void delFd(int epollFd, int sockFd) {
int ret = epoll_ctl(epollFd, EPOLL_CTL_DEL, sockFd, NULL);
LOG_INFO("fd = %d delFd done!", sockFd);
if (ret < 0)
LOG_ERROR("fd = %d EPOLL_CTL_DEL ERROR : %s", sockFd, strerror(errno));
}
void modFd(int epollFd, int sockFd, uint32_t events) {
struct epoll_event event;
event.data.fd = sockFd;
event.events = events | EPOLLRDHUP | EPOLLONESHOT;
int ret = epoll_ctl(epollFd, EPOLL_CTL_MOD, sockFd, &event);
if (ret < 0)
LOG_ERROR("fd = %d EPOLL_CTL_MOD ERROR : %s", sockFd, strerror(errno));
}