-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdwrapper.cpp
53 lines (45 loc) · 1.07 KB
/
fdwrapper.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
51
52
53
#ifndef FDWRAPPER_H
#define FDWRAPPER_H
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
int setnonblocking( int fd )
{
int old_option = fcntl( fd, F_GETFL );
int new_option = old_option | O_NONBLOCK;
fcntl( fd, F_SETFL, new_option );
return old_option;
}
void add_read_fd( int epollfd, int fd )
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event );
setnonblocking( fd );
}
void add_write_fd( int epollfd, int fd )
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLOUT | EPOLLET;
epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event );
setnonblocking( fd );
}
void closefd( int epollfd, int fd )
{
epoll_ctl( epollfd, EPOLL_CTL_DEL, fd, 0 );
close( fd );
}
void removefd( int epollfd, int fd )
{
epoll_ctl( epollfd, EPOLL_CTL_DEL, fd, 0 );
}
void modfd( int epollfd, int fd, int ev )
{
epoll_event event;
event.data.fd = fd;
event.events = ev | EPOLLET;
epoll_ctl( epollfd, EPOLL_CTL_MOD, fd, &event );
}
#endif