SelectEventPoll.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_SELECT_EVENT_POLL_H
  36. #define D_SELECT_EVENT_POLL_H
  37. #include "EventPoll.h"
  38. #include <deque>
  39. #ifdef ENABLE_ASYNC_DNS
  40. # include "AsyncNameResolver.h"
  41. #endif // ENABLE_ASYNC_DNS
  42. namespace aria2 {
  43. class SelectEventPoll : public EventPoll {
  44. private:
  45. class CommandEvent {
  46. private:
  47. Command* command_;
  48. int events_;
  49. public:
  50. CommandEvent(Command* command, int events);
  51. Command* getCommand() const
  52. {
  53. return command_;
  54. }
  55. void addEvents(int events)
  56. {
  57. events_ |= events;
  58. }
  59. void removeEvents(int events)
  60. {
  61. events_ &= (~events);
  62. }
  63. bool eventsEmpty() const
  64. {
  65. return events_ == 0;
  66. }
  67. bool operator==(const CommandEvent& commandEvent) const
  68. {
  69. return command_ == commandEvent.command_;
  70. }
  71. int getEvents() const
  72. {
  73. return events_;
  74. }
  75. void processEvents(int events);
  76. };
  77. friend int accumulateEvent
  78. (int events, const SelectEventPoll::CommandEvent& event);
  79. class SocketEntry {
  80. private:
  81. sock_t socket_;
  82. std::deque<CommandEvent> commandEvents_;
  83. public:
  84. SocketEntry(sock_t socket);
  85. bool operator==(const SocketEntry& entry) const
  86. {
  87. return socket_ == entry.socket_;
  88. }
  89. bool operator<(const SocketEntry& entry) const
  90. {
  91. return socket_ < entry.socket_;
  92. }
  93. void addCommandEvent(Command* command, int events);
  94. void removeCommandEvent(Command* command, int events);
  95. int getEvents();
  96. sock_t getSocket() const
  97. {
  98. return socket_;
  99. }
  100. bool eventEmpty() const
  101. {
  102. return commandEvents_.empty();
  103. }
  104. void processEvents(int events);
  105. };
  106. #ifdef ENABLE_ASYNC_DNS
  107. class AsyncNameResolverEntry {
  108. private:
  109. SharedHandle<AsyncNameResolver> nameResolver_;
  110. Command* command_;
  111. public:
  112. AsyncNameResolverEntry(const SharedHandle<AsyncNameResolver>& nameResolver,
  113. Command* command);
  114. bool operator==(const AsyncNameResolverEntry& entry)
  115. {
  116. return *nameResolver_ == *entry.nameResolver_ &&
  117. command_ == entry.command_;
  118. }
  119. int getFds(fd_set* rfdsPtr, fd_set* wfdsPtr);
  120. void process(fd_set* rfdsPtr, fd_set* wfdsPtr);
  121. };
  122. #endif // ENABLE_ASYNC_DNS
  123. fd_set rfdset_;
  124. fd_set wfdset_;
  125. sock_t fdmax_;
  126. std::deque<SharedHandle<SocketEntry> > socketEntries_;
  127. #ifdef ENABLE_ASYNC_DNS
  128. std::deque<SharedHandle<AsyncNameResolverEntry> > nameResolverEntries_;
  129. #endif // ENABLE_ASYNC_DNS
  130. #ifdef __MINGW32__
  131. // Winsock select() doesn't work if no socket is in FD_SET. We add
  132. // this dummy socket to work around this problem
  133. sock_t dummySocket_;
  134. #endif // __MINGW32__
  135. void updateFdSet();
  136. public:
  137. SelectEventPoll();
  138. virtual ~SelectEventPoll();
  139. virtual void poll(const struct timeval& tv);
  140. virtual bool addEvents(sock_t socket,
  141. Command* command, EventPoll::EventType events);
  142. virtual bool deleteEvents(sock_t socket,
  143. Command* command, EventPoll::EventType events);
  144. #ifdef ENABLE_ASYNC_DNS
  145. virtual bool addNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
  146. Command* command);
  147. virtual bool deleteNameResolver
  148. (const SharedHandle<AsyncNameResolver>& resolver, Command* command);
  149. #endif // ENABLE_ASYNC_DNS
  150. };
  151. } // namespace aria2
  152. #endif // D_SELECT_EVENT_POLL_H