-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepoll_reactor.h
98 lines (78 loc) · 1.97 KB
/
epoll_reactor.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* epoll_reactor.h
*
* Created on: 2011-10-14
* Author: gxl2007@hotmail.com
*/
#ifndef EPOLL_REACTOR_H_
#define EPOLL_REACTOR_H_
#include <sys/epoll.h>
namespace xlnet
{
class io_handler ;
class epoll_reactor
{
public:
enum
{
EVENT_NONE = 0x0 ,
EVENT_READ = EPOLLIN ,
EVENT_WRITE = EPOLLOUT ,
EVENT_ERROR = EPOLLERR ,
EVENT_ALL = EVENT_READ | EVENT_WRITE ,
EVENT_ONESHOT = EPOLLONESHOT ,
} ;
enum
{
MIN_FD_SIZE = 8 ,
MAX_EVENTS_ONCE = 256 ,
} ;
public:
epoll_reactor();
~epoll_reactor();
public:
/*
* @brief initialize , create epoll fd and alloc memory
* @param [in] max fd slot
* @return 0 on success , nagtive on failed
*/
int init(int maxfd) ;
void fini() ;
/*
* @brief register fd handler to epoll engine
* @param [in] fd to be watched
* @param [in] handler to callback
* @param [in] event type
* @return 0 on success , nagtive on failed
*/
int add_handler(int fd , io_handler* handler,int event_type) ;
/*
* @brief modify fd watched
* @param [in] fd have been watched
* @param [in] handler to callback
* @param [in] event type
* @return 0 on success
*/
int mod_handler(int fd , io_handler* handler ,int event_type ) ;
/*
* @brief remove fd watched
*/
void del_handler(int fd ) ;
io_handler* get_handler(int fd) ;
/*
* @brief watch events one time , will block msec milliseconds at most
* @param [in] max milliseconds to wait
* @return events count
*/
int run_once(int msec) ;
private:
epoll_reactor(const epoll_reactor& ) ;
epoll_reactor& operator=(const epoll_reactor&) ;
private:
int m_maxfd ;
int m_epfd ;
struct epoll_event* m_events ;
io_handler** m_handlers ;
};
}
#endif /* EPOLL_REACTOR_H_ */