DefaultBtInteractive.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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_DEFAULT_BT_INTERACTIVE_H
  36. #define D_DEFAULT_BT_INTERACTIVE_H
  37. #include "BtInteractive.h"
  38. #include <limits.h>
  39. #include <vector>
  40. #include "TimerA2.h"
  41. #include "Command.h"
  42. namespace aria2 {
  43. class DownloadContext;
  44. class BtRuntime;
  45. class PieceStorage;
  46. class PeerStorage;
  47. class Peer;
  48. class BtMessage;
  49. class BtMessageReceiver;
  50. class BtMessageDispatcher;
  51. class BtMessageFactory;
  52. class BtRequestFactory;
  53. class PeerConnection;
  54. class ExtensionMessageFactory;
  55. class ExtensionMessageRegistry;
  56. class DHTNode;
  57. class RequestGroupMan;
  58. class UTMetadataRequestFactory;
  59. class UTMetadataRequestTracker;
  60. class FloodingStat {
  61. private:
  62. int chokeUnchokeCount;
  63. int keepAliveCount;
  64. public:
  65. FloodingStat() : chokeUnchokeCount(0), keepAliveCount(0) {}
  66. void incChokeUnchokeCount()
  67. {
  68. if (chokeUnchokeCount < INT_MAX) {
  69. ++chokeUnchokeCount;
  70. }
  71. }
  72. void incKeepAliveCount()
  73. {
  74. if (keepAliveCount < INT_MAX) {
  75. ++keepAliveCount;
  76. }
  77. }
  78. int getChokeUnchokeCount() const { return chokeUnchokeCount; }
  79. int getKeepAliveCount() const { return keepAliveCount; }
  80. void reset()
  81. {
  82. chokeUnchokeCount = 0;
  83. keepAliveCount = 0;
  84. }
  85. };
  86. class DefaultBtInteractive : public BtInteractive {
  87. private:
  88. cuid_t cuid_;
  89. std::shared_ptr<DownloadContext> downloadContext_;
  90. std::shared_ptr<BtRuntime> btRuntime_;
  91. std::shared_ptr<PieceStorage> pieceStorage_;
  92. std::shared_ptr<PeerStorage> peerStorage_;
  93. std::shared_ptr<Peer> peer_;
  94. std::unique_ptr<BtMessageReceiver> btMessageReceiver_;
  95. std::unique_ptr<BtMessageDispatcher> dispatcher_;
  96. std::unique_ptr<BtRequestFactory> btRequestFactory_;
  97. std::unique_ptr<PeerConnection> peerConnection_;
  98. std::unique_ptr<BtMessageFactory> messageFactory_;
  99. std::unique_ptr<ExtensionMessageFactory> extensionMessageFactory_;
  100. std::unique_ptr<ExtensionMessageRegistry> extensionMessageRegistry_;
  101. std::unique_ptr<UTMetadataRequestFactory> utMetadataRequestFactory_;
  102. std::unique_ptr<UTMetadataRequestTracker> utMetadataRequestTracker_;
  103. bool metadataGetMode_;
  104. DHTNode* localNode_;
  105. // The last haveIndex we have advertised to the peer.
  106. uint64_t lastHaveIndex_;
  107. size_t allowedFastSetSize_;
  108. Timer keepAliveTimer_;
  109. Timer floodingTimer_;
  110. FloodingStat floodingStat_;
  111. Timer inactiveTimer_;
  112. Timer pexTimer_;
  113. Timer perSecTimer_;
  114. std::chrono::seconds keepAliveInterval_;
  115. bool utPexEnabled_;
  116. bool dhtEnabled_;
  117. size_t numReceivedMessage_;
  118. size_t maxOutstandingRequest_;
  119. RequestGroupMan* requestGroupMan_;
  120. uint16_t tcpPort_;
  121. void addBitfieldMessageToQueue();
  122. void addAllowedFastMessageToQueue();
  123. void addHandshakeExtendedMessageToQueue();
  124. void decideChoking();
  125. void checkHave();
  126. void sendKeepAlive();
  127. void decideInterest();
  128. void fillPiece(size_t maxMissingBlock);
  129. void addRequests();
  130. void detectMessageFlooding();
  131. void checkActiveInteraction();
  132. void addPeerExchangeMessage();
  133. void addPortMessageToQueue();
  134. public:
  135. DefaultBtInteractive(const std::shared_ptr<DownloadContext>& downloadContext,
  136. const std::shared_ptr<Peer>& peer);
  137. virtual ~DefaultBtInteractive();
  138. virtual void initiateHandshake() CXX11_OVERRIDE;
  139. virtual std::unique_ptr<BtHandshakeMessage>
  140. receiveHandshake(bool quickReply = false) CXX11_OVERRIDE;
  141. virtual std::unique_ptr<BtHandshakeMessage>
  142. receiveAndSendHandshake() CXX11_OVERRIDE;
  143. virtual void doPostHandshakeProcessing() CXX11_OVERRIDE;
  144. virtual void doInteractionProcessing() CXX11_OVERRIDE;
  145. virtual void cancelAllPiece() CXX11_OVERRIDE;
  146. virtual void sendPendingMessage() CXX11_OVERRIDE;
  147. size_t receiveMessages();
  148. virtual size_t countPendingMessage() CXX11_OVERRIDE;
  149. virtual bool isSendingMessageInProgress() CXX11_OVERRIDE;
  150. virtual size_t countReceivedMessageInIteration() const CXX11_OVERRIDE;
  151. virtual size_t countOutstandingRequest() CXX11_OVERRIDE;
  152. void setCuid(cuid_t cuid) { cuid_ = cuid; }
  153. void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
  154. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  155. void setPeerStorage(const std::shared_ptr<PeerStorage>& peerStorage);
  156. void setPeer(const std::shared_ptr<Peer>& peer);
  157. void setBtMessageReceiver(std::unique_ptr<BtMessageReceiver> receiver);
  158. void setDispatcher(std::unique_ptr<BtMessageDispatcher> dispatcher);
  159. void setBtRequestFactory(std::unique_ptr<BtRequestFactory> factory);
  160. void setPeerConnection(std::unique_ptr<PeerConnection> peerConnection);
  161. void setBtMessageFactory(std::unique_ptr<BtMessageFactory> factory);
  162. void
  163. setExtensionMessageFactory(std::unique_ptr<ExtensionMessageFactory> factory);
  164. void setExtensionMessageRegistry(
  165. std::unique_ptr<ExtensionMessageRegistry> registry);
  166. void setKeepAliveInterval(std::chrono::seconds keepAliveInterval)
  167. {
  168. keepAliveInterval_ = std::move(keepAliveInterval);
  169. }
  170. void setUTPexEnabled(bool f) { utPexEnabled_ = f; }
  171. void setLocalNode(DHTNode* node);
  172. void setDHTEnabled(bool f) { dhtEnabled_ = f; }
  173. void setRequestGroupMan(RequestGroupMan* rgman);
  174. void setUTMetadataRequestTracker(
  175. std::unique_ptr<UTMetadataRequestTracker> tracker);
  176. void setUTMetadataRequestFactory(
  177. std::unique_ptr<UTMetadataRequestFactory> factory);
  178. void enableMetadataGetMode() { metadataGetMode_ = true; }
  179. void setTcpPort(uint16_t port) { tcpPort_ = port; }
  180. };
  181. } // namespace aria2
  182. #endif // D_DEFAULT_BT_INTERACTIVE_H