Event.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #ifndef D_EVENT_H
  36. #define D_EVENT_H
  37. #include "common.h"
  38. #include <deque>
  39. #include <algorithm>
  40. #include <functional>
  41. #include <memory>
  42. #include "a2netcompat.h"
  43. #include "Command.h"
  44. #ifdef ENABLE_ASYNC_DNS
  45. # include "AsyncNameResolver.h"
  46. #endif // ENABLE_ASYNC_DNS
  47. namespace aria2 {
  48. template <typename SocketEntry> class Event {
  49. public:
  50. virtual ~Event() = default;
  51. virtual void processEvents(int events) = 0;
  52. virtual int getEvents() const = 0;
  53. virtual void addSelf(SocketEntry* socketEntry) const = 0;
  54. virtual void removeSelf(SocketEntry* socketEntry) const = 0;
  55. };
  56. template <typename SocketEntry, typename EventPoll>
  57. class CommandEvent : public Event<SocketEntry> {
  58. private:
  59. Command* command_;
  60. int events_;
  61. public:
  62. CommandEvent(Command* command, int events)
  63. : command_(command), events_(events)
  64. {
  65. }
  66. Command* getCommand() const { return command_; }
  67. void addEvents(int events) { events_ |= events; }
  68. void removeEvents(int events) { events_ &= (~events); }
  69. bool eventsEmpty() const { return events_ == 0; }
  70. bool operator==(const CommandEvent& commandEvent) const
  71. {
  72. return command_ == commandEvent.command_;
  73. }
  74. virtual int getEvents() const { return events_; }
  75. virtual void processEvents(int events)
  76. {
  77. if ((events_ & events) ||
  78. ((EventPoll::IEV_ERROR | EventPoll::IEV_HUP) & events)) {
  79. command_->setStatusActive();
  80. }
  81. if (EventPoll::IEV_READ & events) {
  82. command_->readEventReceived();
  83. }
  84. if (EventPoll::IEV_WRITE & events) {
  85. command_->writeEventReceived();
  86. }
  87. if (EventPoll::IEV_ERROR & events) {
  88. command_->errorEventReceived();
  89. }
  90. if (EventPoll::IEV_HUP & events) {
  91. command_->hupEventReceived();
  92. }
  93. }
  94. virtual void addSelf(SocketEntry* socketEntry) const
  95. {
  96. socketEntry->addCommandEvent(*this);
  97. }
  98. virtual void removeSelf(SocketEntry* socketEntry) const
  99. {
  100. socketEntry->removeCommandEvent(*this);
  101. }
  102. };
  103. #ifdef ENABLE_ASYNC_DNS
  104. template <typename SocketEntry, typename EventPoll>
  105. class ADNSEvent : public Event<SocketEntry> {
  106. private:
  107. std::shared_ptr<AsyncNameResolver> resolver_;
  108. Command* command_;
  109. sock_t socket_;
  110. int events_;
  111. public:
  112. ADNSEvent(const std::shared_ptr<AsyncNameResolver>& resolver,
  113. Command* command, sock_t socket, int events)
  114. : resolver_(resolver), command_(command), socket_(socket), events_(events)
  115. {
  116. }
  117. bool operator==(const ADNSEvent& event) const
  118. {
  119. return *resolver_ == *event.resolver_;
  120. }
  121. virtual int getEvents() const { return events_; }
  122. virtual void processEvents(int events)
  123. {
  124. ares_socket_t readfd;
  125. ares_socket_t writefd;
  126. if (events &
  127. (EventPoll::IEV_READ | EventPoll::IEV_ERROR | EventPoll::IEV_HUP)) {
  128. readfd = socket_;
  129. }
  130. else {
  131. readfd = ARES_SOCKET_BAD;
  132. }
  133. if (events &
  134. (EventPoll::IEV_WRITE | EventPoll::IEV_ERROR | EventPoll::IEV_HUP)) {
  135. writefd = socket_;
  136. }
  137. else {
  138. writefd = ARES_SOCKET_BAD;
  139. }
  140. resolver_->process(readfd, writefd);
  141. command_->setStatusActive();
  142. }
  143. virtual void addSelf(SocketEntry* socketEntry) const
  144. {
  145. socketEntry->addADNSEvent(*this);
  146. }
  147. virtual void removeSelf(SocketEntry* socketEntry) const
  148. {
  149. socketEntry->removeADNSEvent(*this);
  150. }
  151. };
  152. #else // !ENABLE_ASYNC_DNS
  153. template <typename SocketEntry, typename EventPoll>
  154. class ADNSEvent : public Event<SocketEntry> {
  155. };
  156. #endif // !ENABLE_ASYNC_DNS
  157. template <typename CommandEvent, typename ADNSEvent> class SocketEntry {
  158. protected:
  159. sock_t socket_;
  160. std::deque<CommandEvent> commandEvents_;
  161. #ifdef ENABLE_ASYNC_DNS
  162. std::deque<ADNSEvent> adnsEvents_;
  163. #endif // ENABLE_ASYNC_DNS
  164. public:
  165. SocketEntry(sock_t socket) : socket_(socket) {}
  166. SocketEntry(const SocketEntry&) = delete;
  167. SocketEntry(SocketEntry&&) = default;
  168. bool operator==(const SocketEntry& entry) const
  169. {
  170. return socket_ == entry.socket_;
  171. }
  172. bool operator<(const SocketEntry& entry) const
  173. {
  174. return socket_ < entry.socket_;
  175. }
  176. void addCommandEvent(const CommandEvent& cev)
  177. {
  178. typename std::deque<CommandEvent>::iterator i =
  179. std::find(commandEvents_.begin(), commandEvents_.end(), cev);
  180. if (i == commandEvents_.end()) {
  181. commandEvents_.push_back(cev);
  182. }
  183. else {
  184. (*i).addEvents(cev.getEvents());
  185. }
  186. }
  187. void removeCommandEvent(const CommandEvent& cev)
  188. {
  189. typename std::deque<CommandEvent>::iterator i =
  190. std::find(commandEvents_.begin(), commandEvents_.end(), cev);
  191. if (i == commandEvents_.end()) {
  192. // not found
  193. }
  194. else {
  195. (*i).removeEvents(cev.getEvents());
  196. if ((*i).eventsEmpty()) {
  197. commandEvents_.erase(i);
  198. }
  199. }
  200. }
  201. #ifdef ENABLE_ASYNC_DNS
  202. void addADNSEvent(const ADNSEvent& aev)
  203. {
  204. typename std::deque<ADNSEvent>::iterator i =
  205. std::find(adnsEvents_.begin(), adnsEvents_.end(), aev);
  206. if (i == adnsEvents_.end()) {
  207. adnsEvents_.push_back(aev);
  208. }
  209. }
  210. void removeADNSEvent(const ADNSEvent& aev)
  211. {
  212. typename std::deque<ADNSEvent>::iterator i =
  213. std::find(adnsEvents_.begin(), adnsEvents_.end(), aev);
  214. if (i == adnsEvents_.end()) {
  215. // not found
  216. }
  217. else {
  218. adnsEvents_.erase(i);
  219. }
  220. }
  221. #endif // ENABLE_ASYNC_DNS
  222. sock_t getSocket() const { return socket_; }
  223. void setSocket(sock_t socket) { socket_ = socket; }
  224. bool eventEmpty() const
  225. {
  226. #ifdef ENABLE_ASYNC_DNS
  227. return commandEvents_.empty() && adnsEvents_.empty();
  228. #else // !ENABLE_ASYNC_DNS
  229. return commandEvents_.empty();
  230. #endif // !ENABLE_ASYNC_DNS)
  231. }
  232. void processEvents(int events)
  233. {
  234. using namespace std::placeholders;
  235. std::for_each(commandEvents_.begin(), commandEvents_.end(),
  236. std::bind(&CommandEvent::processEvents, _1, events));
  237. #ifdef ENABLE_ASYNC_DNS
  238. std::for_each(adnsEvents_.begin(), adnsEvents_.end(),
  239. std::bind(&ADNSEvent::processEvents, _1, events));
  240. #endif // ENABLE_ASYNC_DNS
  241. }
  242. };
  243. #ifdef ENABLE_ASYNC_DNS
  244. template <typename EventPoll> class AsyncNameResolverEntry {
  245. private:
  246. std::shared_ptr<AsyncNameResolver> nameResolver_;
  247. Command* command_;
  248. size_t socketsSize_;
  249. sock_t sockets_[ARES_GETSOCK_MAXNUM];
  250. public:
  251. AsyncNameResolverEntry(std::shared_ptr<AsyncNameResolver> nameResolver,
  252. Command* command)
  253. : nameResolver_(std::move(nameResolver)),
  254. command_(command),
  255. socketsSize_(0)
  256. {
  257. }
  258. AsyncNameResolverEntry(const AsyncNameResolverEntry&) = delete;
  259. AsyncNameResolverEntry(AsyncNameResolverEntry&&) = default;
  260. bool operator==(const AsyncNameResolverEntry& entry)
  261. {
  262. return *nameResolver_ == *entry.nameResolver_ && command_ == entry.command_;
  263. }
  264. bool operator<(const AsyncNameResolverEntry& entry)
  265. {
  266. return nameResolver_.get() < entry.nameResolver_.get() ||
  267. (nameResolver_.get() == entry.nameResolver_.get() &&
  268. command_ < entry.command_);
  269. }
  270. void addSocketEvents(EventPoll* e)
  271. {
  272. socketsSize_ = 0;
  273. int mask = nameResolver_->getsock(sockets_);
  274. if (mask == 0) {
  275. return;
  276. }
  277. size_t i;
  278. for (i = 0; i < ARES_GETSOCK_MAXNUM; ++i) {
  279. int events = 0;
  280. if (ARES_GETSOCK_READABLE(mask, i)) {
  281. events |= EventPoll::IEV_READ;
  282. }
  283. if (ARES_GETSOCK_WRITABLE(mask, i)) {
  284. events |= EventPoll::IEV_WRITE;
  285. }
  286. if (events == 0) {
  287. // assume no further sockets are returned.
  288. break;
  289. }
  290. e->addEvents(sockets_[i], command_, events, nameResolver_);
  291. }
  292. socketsSize_ = i;
  293. }
  294. void removeSocketEvents(EventPoll* e)
  295. {
  296. for (size_t i = 0; i < socketsSize_; ++i) {
  297. e->deleteEvents(sockets_[i], command_, nameResolver_);
  298. }
  299. }
  300. // Calls AsyncNameResolver::process(ARES_SOCKET_BAD,
  301. // ARES_SOCKET_BAD).
  302. void processTimeout()
  303. {
  304. nameResolver_->process(ARES_SOCKET_BAD, ARES_SOCKET_BAD);
  305. }
  306. };
  307. #else // !ENABLE_ASYNC_DNS
  308. template <typename EventPoll> class AsyncNameResolverEntry {
  309. };
  310. #endif // !ENABLE_ASYNC_DNS
  311. } // namespace aria2
  312. #endif // D_EVENT_H