PollEventPoll.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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::KSocketEntry::KSocketEntry(sock_t s):
  44. SocketEntry<KCommandEvent, KADNSEvent>(s) {}
  45. int accumulateEvent(int events, const PollEventPoll::KEvent& event)
  46. {
  47. return events|event.getEvents();
  48. }
  49. struct pollfd PollEventPoll::KSocketEntry::getEvents()
  50. {
  51. struct pollfd pollEvent;
  52. pollEvent.fd = _socket;
  53. #ifdef ENABLE_ASYNC_DNS
  54. pollEvent.events =
  55. std::accumulate(_adnsEvents.begin(),
  56. _adnsEvents.end(),
  57. std::accumulate(_commandEvents.begin(),
  58. _commandEvents.end(), 0, accumulateEvent),
  59. accumulateEvent);
  60. #else // !ENABLE_ASYNC_DNS
  61. pollEvent.events =
  62. std::accumulate(_commandEvents.begin(), _commandEvents.end(), 0,
  63. accumulateEvent);
  64. #endif // !ENABLE_ASYNC_DNS
  65. pollEvent.revents = 0;
  66. return pollEvent;
  67. }
  68. PollEventPoll::PollEventPoll():
  69. _pollfdCapacity(1024), _pollfdNum(0), _logger(LogFactory::getInstance())
  70. {
  71. _pollfds = new struct pollfd[_pollfdCapacity];
  72. }
  73. PollEventPoll::~PollEventPoll()
  74. {
  75. delete [] _pollfds;
  76. }
  77. void PollEventPoll::poll(const struct timeval& tv)
  78. {
  79. // timeout is millisec
  80. int timeout = tv.tv_sec*1000+tv.tv_usec/1000;
  81. int res;
  82. while((res = ::poll(_pollfds, _pollfdNum, timeout)) == -1 &&
  83. errno == EINTR);
  84. if(res > 0) {
  85. SharedHandle<KSocketEntry> se(new KSocketEntry(0));
  86. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  87. first != last; ++first) {
  88. if(first->revents) {
  89. se->setSocket(first->fd);
  90. std::deque<SharedHandle<KSocketEntry> >::iterator itr =
  91. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), se);
  92. if(itr != _socketEntries.end() && (*itr) == se) {
  93. (*itr)->processEvents(first->revents);
  94. } else {
  95. if(_logger->debug()) {
  96. _logger->debug("Socket %d is not found in SocketEntries.",
  97. first->fd);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. #ifdef ENABLE_ASYNC_DNS
  104. // It turns out that we have to call ares_process_fd before ares's
  105. // own timeout and ares may create new sockets or closes socket in
  106. // their API. So we call ares_process_fd for all ares_channel and
  107. // re-register their sockets.
  108. for(std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator i =
  109. _nameResolverEntries.begin(), eoi = _nameResolverEntries.end();
  110. i != eoi; ++i) {
  111. (*i)->processTimeout();
  112. (*i)->removeSocketEvents(this);
  113. (*i)->addSocketEvents(this);
  114. }
  115. #endif // ENABLE_ASYNC_DNS
  116. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  117. // DHTEntryPoint...Command)
  118. }
  119. int PollEventPoll::translateEvents(EventPoll::EventType events)
  120. {
  121. int newEvents = 0;
  122. if(EventPoll::EVENT_READ&events) {
  123. newEvents |= IEV_READ;
  124. }
  125. if(EventPoll::EVENT_WRITE&events) {
  126. newEvents |= IEV_WRITE;
  127. }
  128. if(EventPoll::EVENT_ERROR&events) {
  129. newEvents |= IEV_ERROR;
  130. }
  131. if(EventPoll::EVENT_HUP&events) {
  132. newEvents |= IEV_HUP;
  133. }
  134. return newEvents;
  135. }
  136. bool PollEventPoll::addEvents
  137. (sock_t socket, const PollEventPoll::KEvent& event)
  138. {
  139. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  140. std::deque<SharedHandle<KSocketEntry> >::iterator i =
  141. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  142. if(i != _socketEntries.end() && (*i) == socketEntry) {
  143. event.addSelf(*i);
  144. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  145. first != last; ++first) {
  146. if(first->fd == socket) {
  147. *first = (*i)->getEvents();
  148. break;
  149. }
  150. }
  151. } else {
  152. _socketEntries.insert(i, socketEntry);
  153. event.addSelf(socketEntry);
  154. if(_pollfdCapacity == _pollfdNum) {
  155. _pollfdCapacity *= 2;
  156. struct pollfd* newPollfds = new struct pollfd[_pollfdCapacity];
  157. memcpy(newPollfds, _pollfds, _pollfdNum*sizeof(struct pollfd));
  158. delete [] _pollfds;
  159. _pollfds = newPollfds;
  160. }
  161. _pollfds[_pollfdNum] = socketEntry->getEvents();
  162. ++_pollfdNum;
  163. }
  164. return true;
  165. }
  166. bool PollEventPoll::addEvents
  167. (sock_t socket, Command* command, EventPoll::EventType events)
  168. {
  169. int pollEvents = translateEvents(events);
  170. return addEvents(socket, KCommandEvent(command, pollEvents));
  171. }
  172. #ifdef ENABLE_ASYNC_DNS
  173. bool PollEventPoll::addEvents
  174. (sock_t socket, Command* command, int events,
  175. const SharedHandle<AsyncNameResolver>& rs)
  176. {
  177. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  178. }
  179. #endif // ENABLE_ASYNC_DNS
  180. bool PollEventPoll::deleteEvents
  181. (sock_t socket, const PollEventPoll::KEvent& event)
  182. {
  183. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  184. std::deque<SharedHandle<KSocketEntry> >::iterator i =
  185. std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
  186. if(i != _socketEntries.end() && (*i) == socketEntry) {
  187. event.removeSelf(*i);
  188. for(struct pollfd* first = _pollfds, *last = _pollfds+_pollfdNum;
  189. first != last; ++first) {
  190. if(first->fd == socket) {
  191. if((*i)->eventEmpty()) {
  192. if(_pollfdNum >= 2) {
  193. *first = *(last-1);
  194. }
  195. --_pollfdNum;
  196. _socketEntries.erase(i);
  197. } else {
  198. *first = (*i)->getEvents();
  199. }
  200. break;
  201. }
  202. }
  203. return true;
  204. } else {
  205. if(_logger->debug()) {
  206. _logger->debug("Socket %d is not found in SocketEntries.", socket);
  207. }
  208. return false;
  209. }
  210. }
  211. #ifdef ENABLE_ASYNC_DNS
  212. bool PollEventPoll::deleteEvents
  213. (sock_t socket, Command* command, const SharedHandle<AsyncNameResolver>& rs)
  214. {
  215. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  216. }
  217. #endif // ENABLE_ASYNC_DNS
  218. bool PollEventPoll::deleteEvents
  219. (sock_t socket, Command* command, EventPoll::EventType events)
  220. {
  221. int pollEvents = translateEvents(events);
  222. return deleteEvents(socket, KCommandEvent(command, pollEvents));
  223. }
  224. #ifdef ENABLE_ASYNC_DNS
  225. bool PollEventPoll::addNameResolver
  226. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  227. {
  228. SharedHandle<KAsyncNameResolverEntry> entry
  229. (new KAsyncNameResolverEntry(resolver, command));
  230. std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
  231. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  232. if(itr == _nameResolverEntries.end()) {
  233. _nameResolverEntries.push_back(entry);
  234. entry->addSocketEvents(this);
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. }
  240. bool PollEventPoll::deleteNameResolver
  241. (const SharedHandle<AsyncNameResolver>& resolver, Command* command)
  242. {
  243. SharedHandle<KAsyncNameResolverEntry> entry
  244. (new KAsyncNameResolverEntry(resolver, command));
  245. std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
  246. std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
  247. if(itr == _nameResolverEntries.end()) {
  248. return false;
  249. } else {
  250. (*itr)->removeSocketEvents(this);
  251. _nameResolverEntries.erase(itr);
  252. return true;
  253. }
  254. }
  255. #endif // ENABLE_ASYNC_DNS
  256. } // namespace aria2