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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2009 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "EpollEventPoll.h"
#include <cstring>
#include <algorithm>
#include <numeric>
#include "Command.h"
#include "LogFactory.h"
#include "Logger.h"
namespace aria2 {
EpollEventPoll::CommandEvent::CommandEvent(Command* command, int events):
_command(command), _events(events) {}
int EpollEventPoll::CommandEvent::getEvents() const
{
return _events;
}
void EpollEventPoll::CommandEvent::processEvents(int events)
{
if((_events&events) ||
((EpollEventPoll::EVENT_ERROR|EpollEventPoll::EVENT_HUP)&events)) {
_command->setStatusActive();
}
if(EpollEventPoll::EVENT_READ&events) {
_command->readEventReceived();
}
if(EpollEventPoll::EVENT_WRITE&events) {
_command->writeEventReceived();
}
if(EpollEventPoll::EVENT_ERROR&events) {
_command->errorEventReceived();
}
if(EpollEventPoll::EVENT_HUP&events) {
_command->hupEventReceived();
}
}
void EpollEventPoll::CommandEvent::addSelf
(const SharedHandle<SocketEntry>& socketEntry) const
{
socketEntry->addCommandEvent(*this);
}
void EpollEventPoll::CommandEvent::removeSelf
(const SharedHandle<SocketEntry>& socketEntry) const
{
socketEntry->removeCommandEvent(*this);
}
#ifdef ENABLE_ASYNC_DNS
EpollEventPoll::ADNSEvent::ADNSEvent
(const SharedHandle<AsyncNameResolver>& resolver,
Command* command,
sock_t socket, int events):
_resolver(resolver), _command(command), _socket(socket), _events(events) {}
int EpollEventPoll::ADNSEvent::getEvents() const
{
return _events;
}
void EpollEventPoll::ADNSEvent::processEvents(int events)
{
ares_socket_t readfd;
ares_socket_t writefd;
if(events&(EpollEventPoll::EVENT_READ|EpollEventPoll::EVENT_ERROR|
EpollEventPoll::EVENT_HUP)) {
readfd = _socket;
} else {
readfd = ARES_SOCKET_BAD;
}
if(events&(EpollEventPoll::EVENT_WRITE|EpollEventPoll::EVENT_ERROR|
EpollEventPoll::EVENT_HUP)) {
writefd = _socket;
} else {
writefd = ARES_SOCKET_BAD;
}
_resolver->process(readfd, writefd);
_command->setStatusActive();
}
void EpollEventPoll::ADNSEvent::addSelf
(const SharedHandle<SocketEntry>& socketEntry) const
{
socketEntry->addADNSEvent(*this);
}
void EpollEventPoll::ADNSEvent::removeSelf
(const SharedHandle<SocketEntry>& socketEntry) const
{
socketEntry->removeADNSEvent(*this);
}
#endif // ENABLE_ASYNC_DNS
EpollEventPoll::SocketEntry::SocketEntry(sock_t socket):_socket(socket)
{
memset(&_epEvent, 0, sizeof(struct epoll_event));
}
void EpollEventPoll::SocketEntry::addCommandEvent(const CommandEvent& cev)
{
std::deque<CommandEvent>::iterator i = std::find(_commandEvents.begin(),
_commandEvents.end(),
cev);
if(i == _commandEvents.end()) {
_commandEvents.push_back(cev);
} else {
(*i).addEvents(cev.getEvents());
}
}
void EpollEventPoll::SocketEntry::removeCommandEvent(const CommandEvent& cev)
{
std::deque<CommandEvent>::iterator i = std::find(_commandEvents.begin(),
_commandEvents.end(),
cev);
if(i == _commandEvents.end()) {
// not found
} else {
(*i).removeEvents(cev.getEvents());
if((*i).eventsEmpty()) {
_commandEvents.erase(i);
}
}
}
#ifdef ENABLE_ASYNC_DNS
void EpollEventPoll::SocketEntry::addADNSEvent(const ADNSEvent& aev)
{
std::deque<ADNSEvent>::iterator i = std::find(_adnsEvents.begin(),
_adnsEvents.end(),
aev);
if(i == _adnsEvents.end()) {
_adnsEvents.push_back(aev);
}
}
void EpollEventPoll::SocketEntry::removeADNSEvent(const ADNSEvent& aev)
{
std::deque<ADNSEvent>::iterator i = std::find(_adnsEvents.begin(),
_adnsEvents.end(),
aev);
if(i == _adnsEvents.end()) {
// not found
} else {
_adnsEvents.erase(i);
}
}
#endif // ENABLE_ASYNC_DNS
void EpollEventPoll::SocketEntry::processEvents(int events)
{
std::for_each(_commandEvents.begin(), _commandEvents.end(),
std::bind2nd(std::mem_fun_ref
(&EpollEventPoll::CommandEvent::processEvents),
events));
#ifdef ENABLE_ASYNC_DNS
std::for_each(_adnsEvents.begin(), _adnsEvents.end(),
std::bind2nd(std::mem_fun_ref
(&EpollEventPoll::ADNSEvent::processEvents),
events));
#endif // ENABLE_ASYNC_DNS
}
bool EpollEventPoll::SocketEntry::eventEmpty() const
{
#ifdef ENABLE_ASYNC_DNS
return _commandEvents.empty() && _adnsEvents.empty();
#else // !ENABLE_ASYNC_DNS
return _commandEvents.empty();
#endif // !ENABLE_ASYNC_DNS)
}
int accumulateEvent(int events, const EpollEventPoll::Event& event)
{
return events|event.getEvents();
}
struct epoll_event& EpollEventPoll::SocketEntry::getEpEvent()
{
_epEvent.data.ptr = this;
#ifdef ENABLE_ASYNC_DNS
_epEvent.events =
std::accumulate(_adnsEvents.begin(),
_adnsEvents.end(),
std::accumulate(_commandEvents.begin(),
_commandEvents.end(), 0, accumulateEvent),
accumulateEvent);
#else // !ENABLE_ASYNC_DNS
_epEvent.events =
std::accumulate(_commandEvents.begin(), _commandEvents.end(), 0,
accumulateEvent);
#endif // !ENABLE_ASYNC_DNS
return _epEvent;
}
#ifdef ENABLE_ASYNC_DNS
EpollEventPoll::AsyncNameResolverEntry::AsyncNameResolverEntry
(const SharedHandle<AsyncNameResolver>& nameResolver, Command* command):
_nameResolver(nameResolver), _command(command), _socketsSize(0)
{}
void EpollEventPoll::AsyncNameResolverEntry::addSocketEvents
(EpollEventPoll* e)
{
_socketsSize = 0;
int mask = _nameResolver->getsock(_sockets);
if(mask == 0) {
return;
}
size_t i;
for(i = 0; i < ARES_GETSOCK_MAXNUM; ++i) {
//epoll_event_t* epEventPtr = &_epEvents[_socketsSize];
int events = 0;
if(ARES_GETSOCK_READABLE(mask, i)) {
events |= EPOLLIN;
}
if(ARES_GETSOCK_WRITABLE(mask, i)) {
events |= EPOLLOUT;
}
if(events == 0) {
// assume no further sockets are returned.
break;
}
e->addEvents(_sockets[i], _command, events, _nameResolver);
}
_socketsSize = i;
}
void EpollEventPoll::AsyncNameResolverEntry::removeSocketEvents
(EpollEventPoll* e)
{
for(size_t i = 0; i < _socketsSize; ++i) {
e->deleteEvents(_sockets[i], _command, _nameResolver);
}
}
#endif // ENABLE_ASYNC_DNS
EpollEventPoll::EpollEventPoll():_logger(LogFactory::getInstance())
{
_epfd = epoll_create(EPOLL_EVENTS_MAX);
_epEvents = new struct epoll_event[EPOLL_EVENTS_MAX];
}
EpollEventPoll::~EpollEventPoll()
{
if(_epfd != -1) {
int r;
while((r = close(_epfd)) == -1 && errno == EINTR);
if(r == -1) {
_logger->error("Error occurred while closing epoll file descriptor"
" %d: %s",
_epfd, strerror(errno));
}
}
delete [] _epEvents;
}
bool EpollEventPoll::good() const
{
return _epfd != -1;
}
void EpollEventPoll::poll(const struct timeval& tv)
{
// timeout is millisec
int timeout = tv.tv_sec*1000+tv.tv_usec/1000;
int res;
while((res = epoll_wait(_epfd, _epEvents, EPOLL_EVENTS_MAX, timeout)) == -1 &&
errno == EINTR);
if(res > 0) {
for(int i = 0; i < res; ++i) {
SocketEntry* p = (SocketEntry*)_epEvents[i].data.ptr;
p->processEvents(_epEvents[i].events);
}
}
// TODO timeout of name resolver is determined in Command(AbstractCommand,
// DHTEntryPoint...Command)
}
static int translateEvents(EventPoll::EventType events)
{
int newEvents = 0;
if(EventPoll::EVENT_READ&events) {
newEvents |= EPOLLIN;
}
if(EventPoll::EVENT_WRITE&events) {
newEvents |= EPOLLOUT;
}
if(EventPoll::EVENT_ERROR&events) {
newEvents |= EPOLLERR;
}
if(EventPoll::EVENT_HUP&events) {
newEvents |= EPOLLHUP;
}
return newEvents;
}
bool EpollEventPoll::addEvents(sock_t socket,
const EpollEventPoll::Event& event)
{
SharedHandle<SocketEntry> socketEntry(new SocketEntry(socket));
std::deque<SharedHandle<SocketEntry> >::iterator i =
std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
int r = 0;
if(i != _socketEntries.end() && (*i) == socketEntry) {
event.addSelf(*i);
r = epoll_ctl(_epfd, EPOLL_CTL_MOD, (*i)->getSocket(), &(*i)->getEpEvent());
if(r == -1) {
// try EPOLL_CTL_ADD: There is a chance that previously socket X is
// added to epoll, but it is closed and is not yet removed from
// SocketEntries. In this case, EPOLL_CTL_MOD is failed with ENOENT.
r = epoll_ctl(_epfd, EPOLL_CTL_ADD, (*i)->getSocket(),
&(*i)->getEpEvent());
}
} else {
_socketEntries.insert(i, socketEntry);
event.addSelf(socketEntry);
r = epoll_ctl(_epfd, EPOLL_CTL_ADD, socketEntry->getSocket(),
&socketEntry->getEpEvent());
}
if(r == -1) {
_logger->debug("Failed to add socket event %d:%s", socket, strerror(errno));
return false;
} else {
return true;
}
}
bool EpollEventPoll::addEvents(sock_t socket, Command* command,
EventPoll::EventType events)
{
int epEvents = translateEvents(events);
return addEvents(socket, CommandEvent(command, epEvents));
}
#ifdef ENABLE_ASYNC_DNS
bool EpollEventPoll::addEvents(sock_t socket, Command* command, int events,
const SharedHandle<AsyncNameResolver>& rs)
{
return addEvents(socket, ADNSEvent(rs, command, socket, events));
}
#endif // ENABLE_ASYNC_DNS
bool EpollEventPoll::deleteEvents(sock_t socket,
const EpollEventPoll::Event& event)
{
SharedHandle<SocketEntry> socketEntry(new SocketEntry(socket));
std::deque<SharedHandle<SocketEntry> >::iterator i =
std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
if(i != _socketEntries.end() && (*i) == socketEntry) {
event.removeSelf(*i);
int r = 0;
if((*i)->eventEmpty()) {
// In kernel before 2.6.9, epoll_ctl with EPOLL_CTL_DEL requires non-null
// pointer of epoll_event.
struct epoll_event ev = {0,{0}};
r = epoll_ctl(_epfd, EPOLL_CTL_DEL, (*i)->getSocket(), &ev);
_socketEntries.erase(i);
} else {
// If socket is closed, then it seems it is automatically removed from
// epoll, so following EPOLL_CTL_MOD may fail.
r = epoll_ctl(_epfd, EPOLL_CTL_MOD, (*i)->getSocket(),
&(*i)->getEpEvent());
if(r == -1) {
_logger->debug("Failed to delete socket event, but may be ignored:%s",
strerror(errno));
}
}
if(r == -1) {
_logger->debug("Failed to delete socket event:%s", strerror(errno));
return false;
} else {
return true;
}
} else {
_logger->debug("Socket %d is not found in SocketEntries.", socket);
return false;
}
}
#ifdef ENABLE_ASYNC_DNS
bool EpollEventPoll::deleteEvents(sock_t socket, Command* command,
const SharedHandle<AsyncNameResolver>& rs)
{
return deleteEvents(socket, ADNSEvent(rs, command, socket, 0));
}
#endif // ENABLE_ASYNC_DNS
bool EpollEventPoll::deleteEvents(sock_t socket, Command* command,
EventPoll::EventType events)
{
int epEvents = translateEvents(events);
return deleteEvents(socket, CommandEvent(command, epEvents));
}
#ifdef ENABLE_ASYNC_DNS
bool EpollEventPoll::addNameResolver
(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
{
SharedHandle<AsyncNameResolverEntry> entry
(new AsyncNameResolverEntry(resolver, command));
std::deque<SharedHandle<AsyncNameResolverEntry> >::iterator itr =
std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
if(itr == _nameResolverEntries.end()) {
_nameResolverEntries.push_back(entry);
entry->addSocketEvents(this);
return true;
} else {
return false;
}
}
bool EpollEventPoll::deleteNameResolver
(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
{
SharedHandle<AsyncNameResolverEntry> entry
(new AsyncNameResolverEntry(resolver, command));
std::deque<SharedHandle<AsyncNameResolverEntry> >::iterator itr =
std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
if(itr == _nameResolverEntries.end()) {
return false;
} else {
(*itr)->removeSocketEvents(this);
_nameResolverEntries.erase(itr);
return true;
}
}
#endif // ENABLE_ASYNC_DNS
} // namespace aria2
|