LibuvEventPoll.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. * Copyright (C) 2013 Tatsuhiro Tsujikawa, Nils Maier
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * In addition, as a special exception, the copyright holders give
  23. * permission to link the code of portions of this program with the
  24. * OpenSSL library under certain conditions as described in each
  25. * individual source file, and distribute linked combinations
  26. * including the two.
  27. * You must obey the GNU General Public License in all respects
  28. * for all of the code used other than OpenSSL. If you modify
  29. * file(s) with this exception, you may extend this exception to your
  30. * version of the file(s), but you are not obligated to do so. If you
  31. * do not wish to do so, delete this exception statement from your
  32. * version. If you delete this exception statement from all source
  33. * files in the program, then also delete it here.
  34. */
  35. /* copyright --> */
  36. #ifdef __MINGW32__
  37. #ifdef _WIN32_WINNT
  38. #undef _WIN32_WINNT
  39. #endif // _WIN32_WINNT
  40. #define _WIN32_WINNT 0x0600
  41. #endif // __MINGW32__
  42. #include "LibuvEventPoll.h"
  43. #include <algorithm>
  44. #include <cstring>
  45. #include <numeric>
  46. #include <stdexcept>
  47. #include <uv.h>
  48. #include "Command.h"
  49. #include "LogFactory.h"
  50. #include "Logger.h"
  51. #include "a2functional.h"
  52. #include "fmt.h"
  53. #include "util.h"
  54. namespace {
  55. using namespace aria2;
  56. template<typename T>
  57. static void close_callback(uv_handle_t* handle)
  58. {
  59. delete reinterpret_cast<T*>(handle);
  60. }
  61. static void timer_callback(uv_timer_t* handle, int status)
  62. {
  63. uv_stop(handle->loop);
  64. }
  65. }
  66. namespace aria2 {
  67. LibuvEventPoll::KSocketEntry::KSocketEntry(sock_t s)
  68. : SocketEntry<KCommandEvent, KADNSEvent>(s)
  69. {}
  70. inline int accumulateEvent(int events, const LibuvEventPoll::KEvent& event)
  71. {
  72. return events | event.getEvents();
  73. }
  74. int LibuvEventPoll::KSocketEntry::getEvents() const
  75. {
  76. int events = 0;
  77. #ifdef ENABLE_ASYNC_DNS
  78. events = std::accumulate(adnsEvents_.begin(), adnsEvents_.end(),
  79. std::accumulate(commandEvents_.begin(),
  80. commandEvents_.end(), 0,
  81. accumulateEvent),
  82. accumulateEvent);
  83. #else // !ENABLE_ASYNC_DNS
  84. events = std::accumulate(commandEvents_.begin(), commandEvents_.end(), 0,
  85. accumulateEvent);
  86. #endif // !ENABLE_ASYNC_DNS
  87. return events;
  88. }
  89. LibuvEventPoll::LibuvEventPoll()
  90. {
  91. loop_ = uv_loop_new();
  92. }
  93. LibuvEventPoll::~LibuvEventPoll()
  94. {
  95. for (KPolls::iterator i = polls_.begin(), e = polls_.end(); i != e; ++i) {
  96. i->second->close();
  97. }
  98. // Actually kill the polls, and timers, if any.
  99. uv_run(loop_, (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
  100. if (loop_) {
  101. uv_loop_delete(loop_);
  102. loop_ = 0;
  103. }
  104. // Need this to free only after the loop is gone.
  105. polls_.clear();
  106. }
  107. void LibuvEventPoll::poll(const struct timeval& tv)
  108. {
  109. const int timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  110. // timeout == 0 will tick once
  111. if (timeout >= 0) {
  112. uv_timer_t* timer = new uv_timer_t;
  113. uv_timer_init(loop_, timer);
  114. uv_timer_start(timer, timer_callback, timeout, timeout);
  115. uv_run(loop_, UV_RUN_DEFAULT);
  116. // Remove timer again.
  117. uv_timer_stop(timer);
  118. uv_close((uv_handle_t*)timer, close_callback<uv_timer_t>);
  119. }
  120. else {
  121. while (uv_run(loop_, (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT)) > 0) {}
  122. }
  123. #ifdef ENABLE_ASYNC_DNS
  124. // It turns out that we have to call ares_process_fd before ares's
  125. // own timeout and ares may create new sockets or closes socket in
  126. // their API. So we call ares_process_fd for all ares_channel and
  127. // re-register their sockets.
  128. for(KAsyncNameResolverEntrySet::iterator i = nameResolverEntries_.begin(),
  129. eoi = nameResolverEntries_.end(); i != eoi; ++i) {
  130. (*i)->processTimeout();
  131. (*i)->removeSocketEvents(this);
  132. (*i)->addSocketEvents(this);
  133. }
  134. #endif // ENABLE_ASYNC_DNS
  135. // TODO timeout of name resolver is determined in Command(AbstractCommand,
  136. // DHTEntryPoint...Command)
  137. }
  138. int LibuvEventPoll::translateEvents(EventPoll::EventType events)
  139. {
  140. int newEvents = 0;
  141. if (EventPoll::EVENT_READ & events) {
  142. newEvents |= IEV_READ;
  143. }
  144. if (EventPoll::EVENT_WRITE & events) {
  145. newEvents |= IEV_WRITE;
  146. }
  147. if (EventPoll::EVENT_ERROR & events) {
  148. newEvents |= IEV_ERROR;
  149. }
  150. if (EventPoll::EVENT_HUP & events) {
  151. newEvents |= IEV_HUP;
  152. }
  153. return newEvents;
  154. }
  155. void LibuvEventPoll::pollCallback(KPoll* poll, int status, int events)
  156. {
  157. if (status == -1) {
  158. uv_err_t err = uv_last_error(loop_);
  159. switch (err.code) {
  160. case UV_EAGAIN:
  161. case UV_EINTR:
  162. return;
  163. case UV_EOF:
  164. case UV_ECONNABORTED:
  165. case UV_ECONNREFUSED:
  166. case UV_ECONNRESET:
  167. case UV_ENOTCONN:
  168. case UV_EPIPE:
  169. case UV_ESHUTDOWN:
  170. events = IEV_HUP;
  171. poll->processEvents(events);
  172. poll->stop();
  173. uv_stop(loop_);
  174. return;
  175. default:
  176. events = IEV_ERROR;
  177. poll->processEvents(events);
  178. poll->stop();
  179. uv_stop(loop_);
  180. return;
  181. }
  182. }
  183. // Got something
  184. poll->processEvents(events);
  185. uv_stop(loop_);
  186. }
  187. bool LibuvEventPoll::addEvents(sock_t socket,
  188. const LibuvEventPoll::KEvent& event)
  189. {
  190. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  191. KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
  192. if (i != socketEntries_.end() && **i == *socketEntry) {
  193. event.addSelf(*i);
  194. KPolls::iterator poll = polls_.find(socket);
  195. if (poll == polls_.end()) {
  196. throw std::logic_error("Invalid socket");
  197. }
  198. poll->second->start();
  199. return true;
  200. }
  201. socketEntries_.insert(i, socketEntry);
  202. event.addSelf(socketEntry);
  203. KPoll* poll = new KPoll(this, socketEntry.get(), socket);
  204. polls_[socket] = poll;
  205. poll->start();
  206. return true;
  207. }
  208. bool LibuvEventPoll::addEvents(sock_t socket, Command* command, EventPoll::EventType events)
  209. {
  210. int pollEvents = translateEvents(events);
  211. return addEvents(socket, KCommandEvent(command, pollEvents));
  212. }
  213. #ifdef ENABLE_ASYNC_DNS
  214. bool LibuvEventPoll::addEvents(sock_t socket, Command* command, int events,
  215. const SharedHandle<AsyncNameResolver>& rs)
  216. {
  217. return addEvents(socket, KADNSEvent(rs, command, socket, events));
  218. }
  219. #endif // ENABLE_ASYNC_DNS
  220. bool LibuvEventPoll::deleteEvents(sock_t socket,
  221. const LibuvEventPoll::KEvent& event)
  222. {
  223. SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
  224. KSocketEntrySet::iterator i = socketEntries_.find(socketEntry);
  225. if (i == socketEntries_.end()) {
  226. A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
  227. return false;
  228. }
  229. event.removeSelf(*i);
  230. KPolls::iterator poll = polls_.find(socket);
  231. if (poll == polls_.end()) {
  232. return false;
  233. }
  234. if ((*i)->eventEmpty()) {
  235. poll->second->close();
  236. polls_.erase(poll);
  237. socketEntries_.erase(i);
  238. return true;
  239. }
  240. poll->second->start();
  241. return true;
  242. }
  243. #ifdef ENABLE_ASYNC_DNS
  244. bool LibuvEventPoll::deleteEvents(sock_t socket, Command* command,
  245. const SharedHandle<AsyncNameResolver>& rs)
  246. {
  247. return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
  248. }
  249. #endif // ENABLE_ASYNC_DNS
  250. bool LibuvEventPoll::deleteEvents(sock_t socket, Command* command,
  251. EventPoll::EventType events)
  252. {
  253. int pollEvents = translateEvents(events);
  254. return deleteEvents(socket, KCommandEvent(command, pollEvents));
  255. }
  256. #ifdef ENABLE_ASYNC_DNS
  257. bool LibuvEventPoll::addNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
  258. Command* command)
  259. {
  260. SharedHandle<KAsyncNameResolverEntry> entry(
  261. new KAsyncNameResolverEntry(resolver, command));
  262. KAsyncNameResolverEntrySet::iterator itr =
  263. nameResolverEntries_.lower_bound(entry);
  264. if (itr != nameResolverEntries_.end() && *(*itr) == *entry) {
  265. return false;
  266. }
  267. nameResolverEntries_.insert(itr, entry);
  268. entry->addSocketEvents(this);
  269. return true;
  270. }
  271. bool LibuvEventPoll::deleteNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
  272. Command* command)
  273. {
  274. SharedHandle<KAsyncNameResolverEntry> entry(
  275. new KAsyncNameResolverEntry(resolver, command));
  276. KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
  277. if (itr == nameResolverEntries_.end()) {
  278. return false;
  279. }
  280. (*itr)->removeSocketEvents(this);
  281. nameResolverEntries_.erase(itr);
  282. return true;
  283. }
  284. #endif // ENABLE_ASYNC_DNS
  285. } // namespace aria2