SelectEventPoll.h 5.5 KB

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