-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
43 lines (38 loc) · 1.15 KB
/
Copy pathutil.cpp
File metadata and controls
43 lines (38 loc) · 1.15 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
//
// Created by qwskyz on 2022/3/19.
//
#include "util.h"
#include <sys/epoll.h>
#include <fcntl.h>
#include <unistd.h>
// add the file description to epoll
void addFd(int epoll_fd, int fd, bool one_shot) {
epoll_event even{};
even.data.fd = fd;
even.events = EPOLLIN | EPOLLRDHUP;
// 只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把
// 这个socket加入到EPOLL队列里
if (one_shot) {
even.events |= EPOLLONESHOT;
}
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &even);
// core epoll ET mode, set fd nonblocking
setSockNonBlocking(fd);
}
// remove fd from epoll
void removeFd(int epoll_fd, int fd) {
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
// core remember to close fd
close(fd);
}
void modifyFd(int epoll_fd, int fd, int ev) {
epoll_event event;
event.data.fd = fd;
event.events = ev | EPOLLONESHOT | EPOLLRDHUP;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event);
}
void setSockNonBlocking(int fd) {
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
}