PollEventPoll.cc 8.8 KB

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