EpollEventPoll.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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_EPOLL_EVENT_POLL_H_
  36. #define _D_EPOLL_EVENT_POLL_H_
  37. #include "EventPoll.h"
  38. # include <sys/epoll.h>
  39. #include <deque>
  40. #ifdef ENABLE_ASYNC_DNS
  41. # include "AsyncNameResolver.h"
  42. #endif // ENABLE_ASYNC_DNS
  43. namespace aria2 {
  44. class Logger;
  45. class EpollEventPoll : public EventPoll {
  46. private:
  47. class SocketEntry;
  48. class Event {
  49. public:
  50. virtual ~Event() {}
  51. virtual void processEvents(int events) = 0;
  52. virtual int getEvents() const = 0;
  53. virtual void addSelf(const SharedHandle<SocketEntry>& socketEntry) const =0;
  54. virtual void removeSelf
  55. (const SharedHandle<SocketEntry>& socketEntry) const =0;
  56. };
  57. friend int accumulateEvent(int events, const Event& event);
  58. class CommandEvent : public Event {
  59. private:
  60. Command* _command;
  61. int _events;
  62. public:
  63. CommandEvent(Command* command, int events);
  64. Command* getCommand() const
  65. {
  66. return _command;
  67. }
  68. void addEvents(int events)
  69. {
  70. _events |= events;
  71. }
  72. void removeEvents(int events)
  73. {
  74. _events &= (~events);
  75. }
  76. bool eventsEmpty() const
  77. {
  78. return _events == 0;
  79. }
  80. bool operator==(const CommandEvent& commandEvent) const
  81. {
  82. return _command == commandEvent._command;
  83. }
  84. virtual int getEvents() const;
  85. virtual void processEvents(int events);
  86. virtual void addSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  87. virtual void removeSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  88. };
  89. #ifdef ENABLE_ASYNC_DNS
  90. class ADNSEvent : public Event {
  91. private:
  92. SharedHandle<AsyncNameResolver> _resolver;
  93. Command* _command;
  94. sock_t _socket;
  95. int _events;
  96. public:
  97. ADNSEvent(const SharedHandle<AsyncNameResolver>& resolver, Command* command,
  98. sock_t socket, int events);
  99. bool operator==(const ADNSEvent& event) const
  100. {
  101. return _resolver == event._resolver;
  102. }
  103. virtual int getEvents() const;
  104. virtual void processEvents(int events);
  105. virtual void addSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  106. virtual void removeSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  107. };
  108. #endif // ENABLE_ASYNC_DNS
  109. class SocketEntry {
  110. private:
  111. sock_t _socket;
  112. std::deque<CommandEvent> _commandEvents;
  113. #ifdef ENABLE_ASYNC_DNS
  114. std::deque<ADNSEvent> _adnsEvents;
  115. #endif // ENABLE_ASYNC_DNS
  116. struct epoll_event _epEvent;
  117. public:
  118. SocketEntry(sock_t socket);
  119. bool operator==(const SocketEntry& entry) const
  120. {
  121. return _socket == entry._socket;
  122. }
  123. bool operator<(const SocketEntry& entry) const
  124. {
  125. return _socket < entry._socket;
  126. }
  127. void addCommandEvent(const CommandEvent& cev);
  128. void removeCommandEvent(const CommandEvent& cev);
  129. #ifdef ENABLE_ASYNC_DNS
  130. void addADNSEvent(const ADNSEvent& aev);
  131. void removeADNSEvent(const ADNSEvent& aev);
  132. #endif // ENABLE_ASYNC_DNS
  133. struct epoll_event& getEpEvent();
  134. sock_t getSocket() const
  135. {
  136. return _socket;
  137. }
  138. bool eventEmpty() const;
  139. void processEvents(int events);
  140. };
  141. #ifdef ENABLE_ASYNC_DNS
  142. class AsyncNameResolverEntry {
  143. private:
  144. SharedHandle<AsyncNameResolver> _nameResolver;
  145. Command* _command;
  146. size_t _socketsSize;
  147. sock_t _sockets[ARES_GETSOCK_MAXNUM];
  148. public:
  149. AsyncNameResolverEntry(const SharedHandle<AsyncNameResolver>& nameResolver,
  150. Command* command);
  151. bool operator==(const AsyncNameResolverEntry& entry)
  152. {
  153. return _nameResolver == entry._nameResolver &&
  154. _command == entry._command;
  155. }
  156. void addSocketEvents(EpollEventPoll* socketPoll);
  157. void removeSocketEvents(EpollEventPoll* socketPoll);
  158. };
  159. #endif // ENABLE_ASYNC_DNS
  160. private:
  161. enum EventType {
  162. EVENT_READ = EPOLLIN,
  163. EVENT_WRITE = EPOLLOUT,
  164. EVENT_ERROR = EPOLLERR,
  165. EVENT_HUP = EPOLLHUP,
  166. };
  167. std::deque<SharedHandle<SocketEntry> > _socketEntries;
  168. #ifdef ENABLE_ASYNC_DNS
  169. std::deque<SharedHandle<AsyncNameResolverEntry> > _nameResolverEntries;
  170. #endif // ENABLE_ASYNC_DNS
  171. int _epfd;
  172. struct epoll_event* _epEvents;
  173. static const size_t EPOLL_EVENTS_MAX = 1024;
  174. Logger* _logger;
  175. bool addEvents(sock_t socket, const Event& event);
  176. bool deleteEvents(sock_t socket, const Event& event);
  177. bool addEvents(sock_t socket, Command* command, int events,
  178. const SharedHandle<AsyncNameResolver>& rs);
  179. bool deleteEvents(sock_t socket, Command* command,
  180. const SharedHandle<AsyncNameResolver>& rs);
  181. public:
  182. EpollEventPoll();
  183. bool good() const;
  184. virtual ~EpollEventPoll();
  185. virtual void poll(const struct timeval& tv);
  186. virtual bool addEvents(sock_t socket,
  187. Command* command, EventPoll::EventType events);
  188. virtual bool deleteEvents(sock_t socket,
  189. Command* command, EventPoll::EventType events);
  190. #ifdef ENABLE_ASYNC_DNS
  191. virtual bool addNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
  192. Command* command);
  193. virtual bool deleteNameResolver
  194. (const SharedHandle<AsyncNameResolver>& resolver, Command* command);
  195. #endif // ENABLE_ASYNC_DNS
  196. };
  197. } // namespace aria2
  198. #endif // _D_EPOLL_EVENT_POLL_H_