-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocket_control.cpp
42 lines (36 loc) · 1.07 KB
/
socket_control.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
#include "socket_control.h"
// 设置文件描述符非阻塞
void setnonblocking(int fd)
{
int old_flag = fcntl(fd, F_GETFL);
int new_flag = old_flag | O_NONBLOCK;
fcntl(fd, F_SETFL, new_flag);
}
// 添加需要监听的文件描述符到epoll
void addfd(int epollfd, int fd, bool one_shot)
{
epoll_event event;
event.data.fd = fd;
// EPOLLRDHUP判断是否挂起
// event.events=EPOLLIN | EPOLLRDHUP;
event.events = EPOLLIN | EPOLLRDHUP;
if (one_shot)
event.events |= EPOLLONESHOT;
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
// 设置文件描述符非阻塞
setnonblocking(fd);
}
// 从epoll中删除文件描述符,并close文件描述符
void removefd(int epollfd, int fd)
{
epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
}
// 修改文件描述符,重置socket上EPOLLONESHOT事件,确保下次可读时被触发
void modfd(int epollfd, int fd, int ev)
{
epoll_event event;
event.data.fd = fd;
event.events = ev | EPOLLONESHOT | EPOLLRDHUP;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
}