PollEventPoll.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "PollEventPoll.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include <numeric>
  39. #include "Command.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. namespace aria2 {
  43. PollEventPoll::CommandEvent::CommandEvent(Command* command, int events):
  44. _command(command), _events(events) {}
  45. int PollEventPoll::CommandEvent::getEvents() const
  46. {
  47. return _events;
  48. }
  49. void PollEventPoll::CommandEvent::processEvents(int events)
  50. {
  51. if((_events&events) ||
  52. ((PollEventPoll::EVENT_ERROR|PollEventPoll::EVENT_HUP)&events)) {
  53. _command->setStatusActive();
  54. }
  55. if(PollEventPoll::EVENT_READ&events) {
  56. _command->readEventReceived();
  57. }
  58. if(PollEventPoll::EVENT_WRITE&events) {
  59. _command->writeEventReceived();
  60. }
  61. if(PollEventPoll::EVENT_ERROR&events) {
  62. _command->errorEventReceived();
  63. }
  64. if(PollEventPoll::EVENT_HUP&events) {
  65. _command->hupEventReceived();
  66. }
  67. }
  68. void PollEventPoll::CommandEvent::addSelf
  69. (const SharedHandle<SocketEntry>& socketEntry) const
  70. {
  71. socketEntry->addCommandEvent(*this);
  72. }
  73. void PollEventPoll::CommandEvent::removeSelf
  74. (const SharedHandle<SocketEntry>& socketEntry) const
  75. {
  76. socketEntry->removeCommandEvent(*this);
  77. }
  78. #ifdef ENABLE_ASYNC_DNS
  79. PollEventPoll::ADNSEvent::ADNSEvent
  80. (const SharedHandle<AsyncNameResolver>& resolver,
  81. Command* command,
  82. sock_t socket, int events):
  83. _resolver(resolver), _command(command), _socket(socket), _events(events) {}
  84. int PollEventPoll::ADNSEvent::getEvents() const
  85. {
  86. return _events;
  87. }
  88. void PollEventPoll::ADNSEvent::processEvents(int events)
  89. {
  90. ares_socket_t readfd;
  91. ares_socket_t writefd;
  92. if(events&(PollEventPoll::EVENT_READ|PollEventPoll::EVENT_ERROR|
  93. PollEventPoll::EVENT_HUP)) {
  94. readfd = _socket;
  95. } else {
  96. readfd = ARES_SOCKET_BAD;
  97. }
  98. if(events&(PollEventPoll::EVENT_WRITE|PollEventPoll::EVENT_ERROR|
  99. PollEventPoll::EVENT_HUP)) {
  100. writefd = _socket;
  101. } else {
  102. writefd = ARES_SOCKET_BAD;
  103. }
  104. _resolver->process(readfd, writefd);
  105. _command->setStatusActive();
  106. }
  107. void PollEventPoll::ADNSEvent::addSelf
  108. (const SharedHandle<SocketEntry>& socketEntry) const
  109. {
  110. socketEntry->addADNSEvent(*this);
  111. }
  112. void PollEventPoll::ADNSEvent::removeSelf
  113. (const SharedHandle<SocketEntry>& socketEntry) const
  114. {
  115. socketEntry->removeADNSEvent(*this);
  116. }
  117. #endif // ENABLE_ASYNC_DNS
  118. PollEventPoll::SocketEntry::SocketEntry(sock_t socket):_socket(socket)
  119. {
  120. memset(&_pollEvent, 0, sizeof(struct pollfd));
  121. }
  122. void PollEventPoll::SocketEntry::addCommandEvent(const CommandEvent& cev)
  123. {
  124. std::deque<CommandEvent>::iterator i = std::find(_commandEvents.begin(),
  125. _commandEvents.end(),
  126. cev);
  127. if(i == _commandEvents.end()) {
  128. _commandEvents.push_back(cev);
  129. } else {
  130. (*i).addEvents(cev.getEvents());
  131. }
  132. }
  133. void PollEventPoll::SocketEntry::removeCommandEvent(const CommandEvent& cev)
  134. {
  135. std::deque<CommandEvent>::iterator i = std::find(_commandEvents.begin(),
  136. _commandEvents.end(),
  137. cev);
  138. if(i == _commandEvents.end()) {
  139. // not found
  140. } else {
  141. (*i).removeEvents(cev.getEvents());
  142. if((*i).eventsEmpty()) {
  143. _commandEvents.erase(i);
  144. }
  145. }
  146. }
  147. #ifdef ENABLE_ASYNC_DNS
  148. void PollEventPoll::SocketEntry::addADNSEvent(const ADNSEvent& aev)
  149. {
  150. std::deque<ADNSEvent>::iterator i = std::find(_adnsEvents.begin(),
  151. _adnsEvents.end(),
  152. aev);
  153. if(i == _adnsEvents.end()) {
  154. _adnsEvents.push_back(aev);
  155. }
  156. }
  157. void PollEventPoll::SocketEntry::removeADNSEvent(const ADNSEvent& aev)
  158. {
  159. std::deque<ADNSEvent>::iterator i = std::find(_adnsEvents.begin(),
  160. _adnsEvents.end(),
  161. aev);
  162. if(i == _adnsEvents.end()) {
  163. // not found
  164. } else {
  165. _adnsEvents.erase(i);
  166. }
  167. }
  168. #endif // ENABLE_ASYNC_DNS
  169. void PollEventPoll::SocketEntry::processEvents(int events)
  170. {
  171. std::for_each(_commandEvents.begin(), _commandEvents.end(),
  172. std::bind2nd(std::mem_fun_ref
  173. (&PollEventPoll::CommandEvent::processEvents),
  174. events));
  175. #ifdef ENABLE_ASYNC_DNS
  176. std::for_each(_adnsEvents.begin(), _adnsEvents.end(),
  177. std::bind2nd(std::mem_fun_ref
  178. (&PollEventPoll::ADNSEvent::processEvents),
  179. events));
  180. #endif // ENABLE_ASYNC_DNS
  181. }
  182. bool PollEventPoll::SocketEntry::eventEmpty() const
  183. {
  184. #ifdef ENABLE_ASYNC_DNS
  185. return _commandEvents.empty() && _adnsEvents.empty();
  186. #else // !ENABLE_ASYNC_DNS
  187. return _commandEvents.empty();
  188. #endif // !ENABLE_ASYNC_DNS)
  189. }
  190. int accumulateEvent(int events, const PollEventPoll::Event& event)
  191. {
  192. return events|event.getEvents();
  193. }
  194. struct pollfd& PollEventPoll::SocketEntry::getPollEvent()
  195. {
  196. _pollEvent.fd = _socket;
  197. #ifdef ENABLE_ASYNC_DNS
  198. _pollEvent.events =
  199. std::accumulate(_adnsEvents.begin(),
  200. _adnsEvents.end(),
  201. std::accumulate(_commandEvents.begin(),
  202. _commandEvents.end(), 0, accumulateEvent),
  203. accumulateEvent);
  204. #else // !ENABLE_ASYNC_DNS
  205. _pollEvent.events =
  206. std::accumulate(_commandEvents.begin(), _commandEvents.end(), 0,
  207. accumulateEvent);
  208. #endif // !ENABLE_ASYNC_DNS
  209. return _pollEvent;
  210. }
  211. #ifdef ENABLE_ASYNC_DNS
  212. PollEventPoll::AsyncNameResolverEntry::AsyncNameResolverEntry
  213. (const SharedHandle<AsyncNameResolver>& nameResolver, Command* command):
  214. _nameResolver(nameResolver), _command(command), _socketsSize(0)
  215. {}
  216. void PollEventPoll::AsyncNameResolverEntry::addSocketEvents(PollEventPoll* e)
  217. {
  218. _socketsSize = 0;
  219. int mask = _nameResolver->getsock(_sockets);
  220. if(mask == 0) {
  221. return;
  222. }
  223. size_t i;
  224. for(i = 0; i < ARES_GETSOCK_MAXNUM; ++i) {
  225. int events = 0;
  226. if(ARES_GETSOCK_READABLE(mask, i)) {
  227. events |= PollEventPoll::EVENT_READ;
  228. }
  229. if(ARES_GETSOCK_WRITABLE(mask, i)) {
  230. events |= PollEventPoll::EVENT_WRITE;
  231. }
  232. if(events == 0) {
  233. // assume no further sockets are returned.
  234. break;
  235. }
  236. LogFactory::getInstance()->debug("addSocketEvents, %d", _sockets[i]);
  237. e->addEvents(_sockets[i], _command, events, _nameResolver);
  238. }
  239. _socketsSize = i;
  240. }
  241. void PollEventPoll::AsyncNameResolverEntry::removeSocketEvents(PollEventPoll* e)
  242. {
  243. for(size_t i = 0; i < _socketsSize; ++i) {
  244. e->deleteEvents(_sockets[i], _command, _nameResolver);
  245. }
  246. }
  247. void PollEventPoll::AsyncNameResolverEntry::processTimeout()
  248. {
  249. _nameResolver->process(ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  250. }
  251. #endif // ENABLE_ASYNC_DNS
  252. PollEventPoll::PollEventPoll():
  253. _pollfdCapacity(1024), _pollfdNum(0), _logger(LogFactory::getInstance())
  254. {
  255. _pollfds = new struct pollfd[_pollfdCapacity];
  256. }
  257. PollEventPoll::~PollEventPoll()
  258. {
  259. delete [] _pollfds;
  260. }
  261. void PollEventPoll::poll(const struct timeval& tv)
  262. {
  263. // timeout is millisec
  264. int timeout = tv.tv_sec*1000+tv.tv_usec/1000;
  265. int res;
  266. while((res = ::poll(_pollfds, _pollfdNum, timeout)) == -1 &&
  267. errno == EINTR);
  268. if(res > 0) {
  269. SharedHandle<SocketEntry> se(new SocketEntry(0));
  270. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  271. first != last; ++first) {
  272. if(first->revents) {
  273. se->setSocket(first->fd);
  274. std::deque<SharedHandle<SocketEntry> >::iterator itr =
  275. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), se);
  276. if(itr != _socketEntries.end() && (*itr) == se) {
  277. (*itr)->processEvents(first->revents);
  278. } else {
  279. if(_logger->debug()) {
  280. _logger->debug("Socket %d is not found in SocketEntries.",
  281. first->fd);
  282. }
  283. }
  284. }
  285. }
  286. }
  287. #ifdef ENABLE_ASYNC_DNS
  288. // It turns out that we have to call ares_process_fd before ares's
  289. // own timeout and ares may create new sockets or closes socket in
  290. // their API. So we call ares_process_fd for all ares_channel and
  291. // re-register their sockets.
  292. for(std::deque<SharedHandle<AsyncNameResolverEntry> >::iterator i =
  293. _nameResolverEntries.begin(), eoi = _nameResolverEntries.end();
  294. i != eoi; ++i) {
  295. (*i)->processTimeout();
  296. (*i)->removeSocketEvents(this);
  297. (*i)->addSocketEvents(this);
  298. }
  299. #endif // ENABLE_ASYNC_DNS
  300. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  301. // DHTEntryPoint...Command)
  302. }
  303. int PollEventPoll::translateEvents(EventPoll::EventType events)
  304. {
  305. int newEvents = 0;
  306. if(EventPoll::EVENT_READ&events) {
  307. newEvents |= PollEventPoll::EVENT_READ;
  308. }
  309. if(EventPoll::EVENT_WRITE&events) {
  310. newEvents |= PollEventPoll::EVENT_WRITE;
  311. }
  312. if(EventPoll::EVENT_ERROR&events) {
  313. newEvents |= PollEventPoll::EVENT_ERROR;
  314. }
  315. if(EventPoll::EVENT_HUP&events) {
  316. newEvents |= PollEventPoll::EVENT_HUP;
  317. }
  318. return newEvents;
  319. }
  320. bool PollEventPoll::addEvents
  321. (sock_t socket, const PollEventPoll::Event& event)
  322. {
  323. SharedHandle<SocketEntry> socketEntry(new SocketEntry(socket));
  324. std::deque<SharedHandle<SocketEntry> >::iterator i =
  325. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  326. if(i != _socketEntries.end() && (*i) == socketEntry) {
  327. event.addSelf(*i);
  328. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  329. first != last; ++first) {
  330. if(first->fd == socket) {
  331. *first = (*i)->getPollEvent();
  332. break;
  333. }
  334. }
  335. } else {
  336. _socketEntries.insert(i, socketEntry);
  337. event.addSelf(socketEntry);
  338. if(_pollfdCapacity == _pollfdNum) {
  339. _pollfdCapacity *= 2;
  340. struct pollfd* newPollfds = new struct pollfd[_pollfdCapacity];
  341. memcpy(newPollfds, _pollfds, _pollfdNum*sizeof(struct pollfd));
  342. delete [] _pollfds;
  343. _pollfds = newPollfds;
  344. }
  345. _pollfds[_pollfdNum] = socketEntry->getPollEvent();
  346. ++_pollfdNum;
  347. }
  348. return true;
  349. }
  350. bool PollEventPoll::addEvents
  351. (sock_t socket, Command* command, EventPoll::EventType events)
  352. {
  353. int pollEvents = translateEvents(events);
  354. return addEvents(socket, CommandEvent(command, pollEvents));
  355. }
  356. #ifdef ENABLE_ASYNC_DNS
  357. bool PollEventPoll::addEvents
  358. (sock_t socket, Command* command, int events,
  359. const SharedHandle<AsyncNameResolver>& rs)
  360. {
  361. return addEvents(socket, ADNSEvent(rs, command, socket, events));
  362. }
  363. #endif // ENABLE_ASYNC_DNS
  364. bool PollEventPoll::deleteEvents
  365. (sock_t socket, const PollEventPoll::Event& event)
  366. {
  367. SharedHandle<SocketEntry> socketEntry(new SocketEntry(socket));
  368. std::deque<SharedHandle<SocketEntry> >::iterator i =
  369. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  370. if(i != _socketEntries.end() && (*i) == socketEntry) {
  371. event.removeSelf(*i);
  372. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  373. first != last; ++first) {
  374. if(first->fd == socket) {
  375. if((*i)->eventEmpty()) {
  376. if(_pollfdNum >= 2) {
  377. *first = *(last-1);
  378. }
  379. --_pollfdNum;
  380. _socketEntries.erase(i);
  381. } else {
  382. *first = (*i)->getPollEvent();
  383. }
  384. break;
  385. }
  386. }
  387. return true;
  388. } else {
  389. if(_logger->debug()) {
  390. _logger->debug("Socket %d is not found in SocketEntries.", socket);
  391. }
  392. return false;
  393. }
  394. }
  395. #ifdef ENABLE_ASYNC_DNS
  396. bool PollEventPoll::deleteEvents
  397. (sock_t socket, Command* command, const SharedHandle<AsyncNameResolver>& rs)
  398. {
  399. return deleteEvents(socket, ADNSEvent(rs, command, socket, 0));
  400. }
  401. #endif // ENABLE_ASYNC_DNS
  402. bool PollEventPoll::deleteEvents
  403. (sock_t socket, Command* command, EventPoll::EventType events)
  404. {
  405. int pollEvents = translateEvents(events);
  406. return deleteEvents(socket, CommandEvent(command, pollEvents));
  407. }
  408. #ifdef ENABLE_ASYNC_DNS
  409. bool PollEventPoll::addNameResolver
  410. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  411. {
  412. SharedHandle<AsyncNameResolverEntry> entry
  413. (new AsyncNameResolverEntry(resolver, command));
  414. std::deque<SharedHandle<AsyncNameResolverEntry> >::iterator itr =
  415. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  416. if(itr == _nameResolverEntries.end()) {
  417. _nameResolverEntries.push_back(entry);
  418. entry->addSocketEvents(this);
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. bool PollEventPoll::deleteNameResolver
  425. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  426. {
  427. SharedHandle<AsyncNameResolverEntry> entry
  428. (new AsyncNameResolverEntry(resolver, command));
  429. std::deque<SharedHandle<AsyncNameResolverEntry> >::iterator itr =
  430. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  431. if(itr == _nameResolverEntries.end()) {
  432. return false;
  433. } else {
  434. (*itr)->removeSocketEvents(this);
  435. _nameResolverEntries.erase(itr);
  436. return true;
  437. }
  438. }
  439. #endif // ENABLE_ASYNC_DNS
  440. } // namespace aria2