-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patheventloop.h
197 lines (145 loc) · 4.03 KB
/
eventloop.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef EVENT_LOOP_H_
#define EVENT_LOOP_H_
#include <stdint.h>
#include <time.h>
#include <sys/epoll.h>
namespace eventloop {
class EventLoop;
class SignalManager;
class BaseEvent {
friend class EventLoop;
friend class SignalManager;
public:
static const uint32_t NONE = 0;
static const uint32_t ONESHOT = 1 << 30;
static const uint32_t TIMEOUT = 1 << 31;
public:
BaseEvent(uint32_t events = 0) { events_ = events; }
virtual ~BaseEvent() {};
virtual void OnEvents(uint32_t events) = 0;
public:
virtual void SetEvents(uint32_t events) { events_ = events; }
virtual uint32_t Events() const { return events_; }
protected:
uint32_t events_;
};
class BaseFileEvent : public BaseEvent {
friend class EventLoop;
public:
static const uint32_t READ = 1 << 0;
static const uint32_t WRITE = 1 << 1;
static const uint32_t ERROR = 1 << 2;
public:
explicit BaseFileEvent(uint32_t events = BaseEvent::NONE) : BaseEvent(events) {}
virtual ~BaseFileEvent() {};
public:
void SetFile(int fd) { file = fd; }
int File() const { return file; }
public:
virtual void OnEvents(uint32_t events) = 0;
protected:
int file;
};
class BufferFileEvent : public BaseFileEvent {
friend class EventLoop;
public:
explicit BufferFileEvent()
:BaseFileEvent(BaseFileEvent::READ | BaseFileEvent::ERROR) {
}
virtual ~BufferFileEvent() {};
public:
void Recive(char *buffer, uint32_t len);
void Send(char *buffer, uint32_t len);
virtual void OnRecived(char *buffer, uint32_t len) {};
virtual void OnSent(char *buffer, uint32_t len) {};
virtual void OnError() {};
private:
void OnEvents(uint32_t events);
private:
char *recvbuf_;
uint32_t torecv_;
uint32_t recvd_;
char *sendbuf_;
uint32_t tosend_;
uint32_t sent_;
EventLoop *el_;
};
class BaseSignalEvent : public BaseEvent {
friend class EventLoop;
public:
static const uint32_t INT = 1 << 0;
static const uint32_t PIPE = 1 << 1;
static const uint32_t TERM = 1 << 2;
public:
explicit BaseSignalEvent(uint32_t events = BaseEvent::NONE) : BaseEvent(events) {}
virtual ~BaseSignalEvent() {};
};
class BaseTimerEvent : public BaseEvent {
friend class EventLoop;
public:
static const uint32_t TIMER = 1 << 0;
public:
explicit BaseTimerEvent(uint32_t events = BaseEvent::NONE) : BaseEvent(events) {}
virtual ~BaseTimerEvent() {};
public:
void SetTime(timeval tv) { time_ = tv; }
timeval Time() const { return time_; }
protected:
timeval time_;
};
class PeriodicTimerEvent : public BaseTimerEvent {
friend class EventLoop;
public:
explicit PeriodicTimerEvent() :BaseTimerEvent(BaseEvent::NONE), el_(NULL) {};
explicit PeriodicTimerEvent(timeval inter) :BaseTimerEvent(BaseEvent::NONE), interval_(inter), el_(NULL) {};
virtual ~PeriodicTimerEvent() {};
void SetInterval(timeval inter) { interval_ = inter; }
void Start();
void Stop();
bool IsRunning() { return running_; }
virtual void OnTimer() = 0;
private:
void OnEvents(uint32_t events);
private:
timeval interval_;
bool running_;
EventLoop *el_;
};
class EventLoop {
public:
EventLoop();
~EventLoop();
public:
// add delete & update event objects
int AddEvent(BaseFileEvent *e);
int DeleteEvent(BaseFileEvent *e);
int UpdateEvent(BaseFileEvent *e);
int AddEvent(BaseTimerEvent *e);
int DeleteEvent(BaseTimerEvent *e);
int UpdateEvent(BaseTimerEvent *e);
int AddEvent(BaseSignalEvent *e);
int DeleteEvent(BaseSignalEvent *e);
int UpdateEvent(BaseSignalEvent *e);
int AddEvent(BufferFileEvent *e);
int AddEvent(PeriodicTimerEvent *e);
// do epoll_waite and collect events
int ProcessEvents(int timeout);
// event loop control
void StartLoop();
void StopLoop();
timeval Now() const { return now_; }
private:
int CollectFileEvents(int timeout);
int DoTimeout();
private:
int epfd_;
epoll_event evs_[256];
timeval now_;
bool stop_;
void *timermanager_;
};
int SetNonblocking(int fd);
int BindTo(const char *host, short port);
int ConnectTo(const char *host, short port, bool async);
}
#endif // EVENT_LOOP_H_