EpollEventPoll.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_EPLLL_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. void addEvents(int events);
  66. void removeEvents(int events);
  67. bool eventsEmpty() const;
  68. bool operator==(const CommandEvent& event) const;
  69. virtual int getEvents() const;
  70. virtual void processEvents(int events);
  71. virtual void addSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  72. virtual void removeSelf(const SharedHandle<SocketEntry>& socketEntry) const;
  73. };
  74. class ADNSEvent : public Event {
  75. private:
  76. SharedHandle<AsyncNameResolver> _resolver;
  77. Command* _command;
  78. sock_t _socket;
  79. int _events;
  80. public:
  81. ADNSEvent(const SharedHandle<AsyncNameResolver>& resolver, Command* command,
  82. sock_t socket, int events);
  83. bool operator==(const ADNSEvent& event) const;
  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. class SocketEntry {
  90. private:
  91. sock_t _socket;
  92. std::deque<CommandEvent> _commandEvents;
  93. #ifdef ENABLE_ASYNC_DNS
  94. std::deque<ADNSEvent> _adnsEvents;
  95. #endif // ENABLE_ASYNC_DNS
  96. struct epoll_event _epEvent;
  97. public:
  98. SocketEntry(sock_t socket);
  99. bool operator==(const SocketEntry& entry) const;
  100. bool operator<(const SocketEntry& entry) const;
  101. void addCommandEvent(const CommandEvent& cev);
  102. void removeCommandEvent(const CommandEvent& cev);
  103. #ifdef ENABLE_ASYNC_DNS
  104. void addADNSEvent(const ADNSEvent& aev);
  105. void removeADNSEvent(const ADNSEvent& aev);
  106. #endif // ENABLE_ASYNC_DNS
  107. struct epoll_event& getEpEvent();
  108. sock_t getSocket() const;
  109. bool eventEmpty() const;
  110. void processEvents(int events);
  111. };
  112. #ifdef ENABLE_ASYNC_DNS
  113. class AsyncNameResolverEntry {
  114. private:
  115. SharedHandle<AsyncNameResolver> _nameResolver;
  116. Command* _command;
  117. size_t _socketsSize;
  118. sock_t _sockets[ARES_GETSOCK_MAXNUM];
  119. public:
  120. AsyncNameResolverEntry(const SharedHandle<AsyncNameResolver>& nameResolver,
  121. Command* command);
  122. bool operator==(const AsyncNameResolverEntry& entry);
  123. void addSocketEvents(EpollEventPoll* socketPoll);
  124. void removeSocketEvents(EpollEventPoll* socketPoll);
  125. };
  126. #endif // ENABLE_ASYNC_DNS
  127. private:
  128. enum EventType {
  129. EVENT_READ = EPOLLIN,
  130. EVENT_WRITE = EPOLLOUT,
  131. EVENT_ERROR = EPOLLERR,
  132. EVENT_HUP = EPOLLHUP,
  133. };
  134. std::deque<SharedHandle<SocketEntry> > _socketEntries;
  135. #ifdef ENABLE_ASYNC_DNS
  136. std::deque<SharedHandle<AsyncNameResolverEntry> > _nameResolverEntries;
  137. #endif // ENABLE_ASYNC_DNS
  138. int _epfd;
  139. struct epoll_event* _epEvents;
  140. static const size_t EPOLL_EVENTS_MAX = 1024;
  141. Logger* _logger;
  142. bool addEvents(sock_t socket, const Event& event);
  143. bool deleteEvents(sock_t socket, const Event& event);
  144. bool addEvents(sock_t socket, Command* command, int events,
  145. const SharedHandle<AsyncNameResolver>& rs);
  146. bool deleteEvents(sock_t socket, Command* command,
  147. const SharedHandle<AsyncNameResolver>& rs);
  148. public:
  149. EpollEventPoll();
  150. bool good() const;
  151. virtual ~EpollEventPoll();
  152. virtual void poll(const struct timeval& tv);
  153. virtual bool addEvents(sock_t socket,
  154. Command* command, EventPoll::EventType events);
  155. virtual bool deleteEvents(sock_t socket,
  156. Command* command, EventPoll::EventType events);
  157. #ifdef ENABLE_ASYNC_DNS
  158. virtual bool addNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
  159. Command* command);
  160. virtual bool deleteNameResolver
  161. (const SharedHandle<AsyncNameResolver>& resolver, Command* command);
  162. #endif // ENABLE_ASYNC_DNS
  163. };
  164. } // namespace aria2
  165. #endif // _D_EVENT_POLL_H_