-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll.hpp
More file actions
59 lines (51 loc) · 1.37 KB
/
Copy pathpoll.hpp
File metadata and controls
59 lines (51 loc) · 1.37 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
/**
* @file poll.hpp
* @brief Thin epoll wrapper. Adds and removes file descriptors in EPOLLET mode.
*
* CSCI 599: Network Systems for Cloud Computing
* University of Southern California
*/
#ifndef __POLL_HPP__
#define __POLL_HPP__
#include <iostream>
#include <sys/epoll.h>
class __epoll {
private:
int __epoll_fd;
int __timeout;
public:
const static int gnum = 128;
public:
__epoll(int timeout)
: __timeout(timeout) { }
~__epoll() { }
public:
void create_poll() {
__epoll_fd = epoll_create(gnum);
if (__epoll_fd < 0)
exit(5);
}
bool add_sock_to_poll(int sock, uint32_t events) {
struct epoll_event ev;
ev.events = events;
ev.data.fd = sock;
int n = epoll_ctl(__epoll_fd, EPOLL_CTL_ADD, sock, &ev);
return n == 0;
}
bool delete_from_epoll(int sock) {
return epoll_ctl(__epoll_fd, EPOLL_CTL_DEL, sock, nullptr) == 0;
}
int wait_poll(struct epoll_event revs[], int num) {
return epoll_wait(__epoll_fd, revs, num, __timeout);
}
bool control_poll(int sock, uint32_t events) {
events |= EPOLLET; // 无论是谁,一律设置为ET模式
struct epoll_event ev;
ev.events = events;
ev.data.fd = sock;
int n = epoll_ctl(__epoll_fd, EPOLL_CTL_MOD, sock, &ev);
return n == 0;
}
public:
};
#endif