-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactor.cc
More file actions
288 lines (239 loc) · 7.37 KB
/
Copy pathreactor.cc
File metadata and controls
288 lines (239 loc) · 7.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "reactor.h"
namespace reactor {
// Reactor
void Reactor::register_handler(EventHandler *eh, int events) {
Handle handle = eh->get_handle();
eh->events |= events;
handlers[handle] = eh;
demultiplexer->add(handle, events);
}
void Reactor::modify_handler(EventHandler *eh, int events) {
Handle handle = eh->get_handle();
eh->events = events;
handlers[handle] = eh;
demultiplexer->set(handle, events);
}
void Reactor::remove_handler(EventHandler *eh) {
Handle handle = eh->get_handle();
handlers.erase(handle);
demultiplexer->del(handle);
}
void Reactor::handle_events(int timeout) { demultiplexer->wait(timeout); }
// SelectDemultiplexer
void SelectDemultiplexer::add(Handle handle, int events) {
if (handle > FD_SETSIZE) {
std::cout << "SelectDemultiplexer cannot add anymore" << std::endl;
return;
}
if (handle > maxfd_) {
maxfd_ = handle;
}
}
void SelectDemultiplexer::set(Handle handle, int events) {
std::cout << "SelectDemultiplexer set nothing" << std::endl;
}
void SelectDemultiplexer::del(Handle handle) {
FD_CLR(handle, &rfds_);
FD_CLR(handle, &wfds_);
FD_CLR(handle, &efds_);
}
void SelectDemultiplexer::wait(int timeout) {
int ret;
for (;;) {
FD_ZERO(&rfds_);
FD_ZERO(&wfds_);
FD_ZERO(&efds_);
for (auto it : Reactor::get_instance().handlers) {
int fd = it.first;
int events = it.second->events;
if (events & READ_EVENT) {
FD_SET(fd, &rfds_);
}
if (events & WRITE_EVENT) {
FD_SET(fd, &wfds_);
}
if (events & ERROR_EVENT) {
FD_SET(fd, &efds_);
}
}
struct timeval _timeout;
_timeout.tv_sec = timeout / 1000;
_timeout.tv_usec = (timeout % 1000) * 1000;
ret = select(maxfd_ + 1, &rfds_, &wfds_, &efds_, &_timeout);
if (ret < 0) {
if (errno != EINTR) {
std::cout << "SelectDemultiplexer wait error:" << errno
<< std::endl;
break;
} else {
continue;
}
} else if (ret == 0) {
continue;
} else {
for (int i = 0; i <= maxfd_; i++) {
if (Reactor::get_instance().handlers.count(i)) {
EventHandler *handler = Reactor::get_instance().handlers[i];
if (FD_ISSET(i, &rfds_)) {
handler->handle_read();
}
if (FD_ISSET(i, &wfds_)) {
handler->handle_write();
}
if (FD_ISSET(i, &efds_)) {
handler->handle_error();
}
}
}
}
}
}
// PollDemultiplexer
void PollDemultiplexer::add(Handle handle, int events) {
if (nfds_ > size_) {
std::cout << "PollDemultiplexer cannot add anymore" << std::endl;
return;
}
fds_[nfds_].fd = handle;
if (events & READ_EVENT) {
fds_[nfds_].events |= POLLIN;
}
if (events & WRITE_EVENT) {
fds_[nfds_].events |= POLLOUT;
}
if (events & ERROR_EVENT) {
fds_[nfds_].events |= (POLLERR | POLLHUP);
}
nfds_++;
}
void PollDemultiplexer::set(Handle handle, int events) {
std::cout << "PollDemultiplexer set nothing" << std::endl;
}
void PollDemultiplexer::del(Handle handle) {
for (int i = 0; i < nfds_; i++) {
if (fds_[i].fd == handle) {
fds_[i].fd = 0;
fds_[i].events = 0;
break;
}
}
}
void PollDemultiplexer::wait(int timeout) {
for (;;) {
int ret = poll(fds_, nfds_, timeout);
if (ret < 0) {
if (errno != EINTR) {
std::cout << "PollDemultiplexer wait error:" << errno
<< std::endl;
break;
} else {
continue;
}
} else if (ret == 0) {
continue;
} else {
for (int i = 0; i < nfds_; i++) {
int fd = fds_[i].fd;
if (fd == 0) {
continue;
}
if (Reactor::get_instance().handlers.count(fd)) {
EventHandler *handler =
Reactor::get_instance().handlers[fd];
if (fds_[i].revents & POLLIN) {
handler->handle_read();
}
if (fds_[i].revents & POLLOUT) {
handler->handle_write();
}
if (fds_[i].revents & (POLLERR | POLLHUP)) {
handler->handle_error();
}
}
}
}
}
}
// EpollDemultiplexer
void EpollDemultiplexer::add(Handle handle, int events) {
if (nfds_ > size_) {
std::cout << "EpollDemultiplexer cannot add anymore" << std::endl;
return;
}
struct epoll_event ev;
ev.data.fd = handle;
ev.events = 0;
if (events & READ_EVENT) {
ev.events |= EPOLLIN;
}
if (events & WRITE_EVENT) {
ev.events |= EPOLLOUT;
}
if (events & ERROR_EVENT) {
ev.events |= (EPOLLRDHUP | EPOLLHUP | EPOLLERR);
}
if (epoll_ctl(epfd_, EPOLL_CTL_ADD, handle, &ev) < 0) {
std::cout << "EpollDemultiplexer add error:" << errno << std::endl;
} else {
nfds_++;
}
}
void EpollDemultiplexer::set(Handle handle, int events) {
struct epoll_event ev;
ev.data.fd = handle;
ev.events = 0;
if (events & READ_EVENT) {
ev.events |= EPOLLIN;
}
if (events & WRITE_EVENT) {
ev.events |= EPOLLOUT;
}
if (events & ERROR_EVENT) {
ev.events |= (EPOLLRDHUP | EPOLLHUP | EPOLLERR);
}
if (epoll_ctl(epfd_, EPOLL_CTL_MOD, handle, &ev) < 0) {
std::cout << "EpollDemultiplexer set error: " << errno << std::endl;
}
}
void EpollDemultiplexer::del(Handle handle) {
if (epoll_ctl(epfd_, EPOLL_CTL_DEL, handle, nullptr) < 0) {
std::cout << "EpollDemultiplexer del error: " << errno << std::endl;
} else {
nfds_--;
}
}
void EpollDemultiplexer::wait(int timeout) {
for (;;) {
int ret = epoll_wait(epfd_, events_, nfds_, timeout);
if (ret < 0) {
if (errno != EINTR) {
std::cout << "EpollDemultiplexer wait error:" << errno
<< std::endl;
break;
} else {
continue;
}
} else if (ret == 0) {
continue;
} else {
for (int i = 0; i < ret; i++) {
int fd = events_[i].data.fd;
if (Reactor::get_instance().handlers.count(fd)) {
EventHandler *handler =
Reactor::get_instance().handlers[fd];
if (events_[i].events & EPOLLIN) {
handler->handle_read();
}
if (events_[i].events & EPOLLOUT) {
handler->handle_write();
}
if (events_[i].events &
(EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
handler->handle_error();
}
}
}
}
}
}
} // namespace reactor