PollEventPoll.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. std::shared_ptr<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. KSocketEntrySet::iterator itr = socketEntries_.find(se);
  96. if(itr == socketEntries_.end()) {
  97. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.",
  98. first->fd));
  99. } else {
  100. (*itr)->processEvents(first->revents);
  101. }
  102. }
  103. }
  104. } else if(res == -1) {
  105. int errNum = errno;
  106. A2_LOG_INFO(fmt("poll error: %s", util::safeStrerror(errNum).c_str()));
  107. }
  108. #ifdef ENABLE_ASYNC_DNS
  109. // It turns out that we have to call ares_process_fd before ares's
  110. // own timeout and ares may create new sockets or closes socket in
  111. // their API. So we call ares_process_fd for all ares_channel and
  112. // re-register their sockets.
  113. for(KAsyncNameResolverEntrySet::iterator i = nameResolverEntries_.begin(),
  114. eoi = nameResolverEntries_.end(); i != eoi; ++i) {
  115. (*i)->processTimeout();
  116. (*i)->removeSocketEvents(this);
  117. (*i)->addSocketEvents(this);
  118. }
  119. #endif // ENABLE_ASYNC_DNS
  120. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  121. // DHTEntryPoint...Command)
  122. }
  123. int PollEventPoll::translateEvents(EventPoll::EventType events)
  124. {
  125. int newEvents = 0;
  126. if(EventPoll::EVENT_READ&events) {
  127. newEvents |= IEV_READ;
  128. }
  129. if(EventPoll::EVENT_WRITE&events) {
  130. newEvents |= IEV_WRITE;
  131. }
  132. if(EventPoll::EVENT_ERROR&events) {
  133. newEvents |= IEV_ERROR;
  134. }
  135. if(EventPoll::EVENT_HUP&events) {
  136. newEvents |= IEV_HUP;
  137. }
  138. return newEvents;
  139. }
  140. bool PollEventPoll::addEvents
  141. (sock_t socket, const PollEventPoll::KEvent& event)
  142. {
  143. std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
  144. KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
  145. if(i != socketEntries_.end() && *(*i) == *socketEntry) {
  146. event.addSelf(*i);
  147. for(struct pollfd* first = pollfds_, *last = pollfds_+pollfdNum_;
  148. first != last; ++first) {
  149. if(first->fd == socket) {
  150. *first = (*i)->getEvents();
  151. break;
  152. }
  153. }
  154. } else {
  155. socketEntries_.insert(i, socketEntry);
  156. event.addSelf(socketEntry);
  157. if(pollfdCapacity_ == pollfdNum_) {
  158. pollfdCapacity_ *= 2;
  159. auto newPollfds = new struct pollfd[pollfdCapacity_];
  160. memcpy(newPollfds, pollfds_, pollfdNum_*sizeof(struct pollfd));
  161. delete [] pollfds_;
  162. pollfds_ = newPollfds;
  163. }
  164. pollfds_[pollfdNum_] = socketEntry->getEvents();
  165. ++pollfdNum_;
  166. }
  167. return true;
  168. }
  169. bool PollEventPoll::addEvents
  170. (sock_t socket, Command* command, EventPoll::EventType events)
  171. {
  172. int pollEvents = translateEvents(events);
  173. return addEvents(socket, KCommandEvent(command, pollEvents));
  174. }
  175. #ifdef ENABLE_ASYNC_DNS
  176. bool PollEventPoll::addEvents
  177. (sock_t socket, Command* command, int events,
  178. const std::shared_ptr<AsyncNameResolver>& rs)
  179. {
  180. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  181. }
  182. #endif // ENABLE_ASYNC_DNS
  183. bool PollEventPoll::deleteEvents
  184. (sock_t socket, const PollEventPoll::KEvent& event)
  185. {
  186. std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
  187. KSocketEntrySet::iterator i = socketEntries_.find(socketEntry);
  188. if(i == socketEntries_.end()) {
  189. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  190. return false;
  191. } else {
  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. }
  210. }
  211. #ifdef ENABLE_ASYNC_DNS
  212. bool PollEventPoll::deleteEvents
  213. (sock_t socket, Command* command, const std::shared_ptr<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 std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  227. {
  228. std::shared_ptr<KAsyncNameResolverEntry> entry
  229. (new KAsyncNameResolverEntry(resolver, command));
  230. KAsyncNameResolverEntrySet::iterator itr =
  231. nameResolverEntries_.lower_bound(entry);
  232. if(itr != nameResolverEntries_.end() && *(*itr) == *entry) {
  233. return false;
  234. } else {
  235. nameResolverEntries_.insert(itr, entry);
  236. entry->addSocketEvents(this);
  237. return true;
  238. }
  239. }
  240. bool PollEventPoll::deleteNameResolver
  241. (const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  242. {
  243. std::shared_ptr<KAsyncNameResolverEntry> entry
  244. (new KAsyncNameResolverEntry(resolver, command));
  245. KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
  246. if(itr == nameResolverEntries_.end()) {
  247. return false;
  248. } else {
  249. (*itr)->removeSocketEvents(this);
  250. nameResolverEntries_.erase(itr);
  251. return true;
  252. }
  253. }
  254. #endif // ENABLE_ASYNC_DNS
  255. } // namespace aria2