PollEventPoll.cc 8.9 KB

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