DefaultBtInteractive.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. size_t allowedFastSetSize_;
  106. Timer haveTimer_;
  107. Timer keepAliveTimer_;
  108. Timer floodingTimer_;
  109. FloodingStat floodingStat_;
  110. Timer inactiveTimer_;
  111. Timer pexTimer_;
  112. Timer perSecTimer_;
  113. std::chrono::seconds keepAliveInterval_;
  114. bool utPexEnabled_;
  115. bool dhtEnabled_;
  116. size_t numReceivedMessage_;
  117. size_t maxOutstandingRequest_;
  118. RequestGroupMan* requestGroupMan_;
  119. uint16_t tcpPort_;
  120. std::vector<size_t> haveIndexes_;
  121. Timer haveLastSent_;
  122. void addBitfieldMessageToQueue();
  123. void addAllowedFastMessageToQueue();
  124. void addHandshakeExtendedMessageToQueue();
  125. void decideChoking();
  126. void checkHave();
  127. void sendKeepAlive();
  128. void decideInterest();
  129. void fillPiece(size_t maxMissingBlock);
  130. void addRequests();
  131. void detectMessageFlooding();
  132. void checkActiveInteraction();
  133. void addPeerExchangeMessage();
  134. void addPortMessageToQueue();
  135. public:
  136. DefaultBtInteractive(const std::shared_ptr<DownloadContext>& downloadContext,
  137. const std::shared_ptr<Peer>& peer);
  138. virtual ~DefaultBtInteractive();
  139. virtual void initiateHandshake() CXX11_OVERRIDE;
  140. virtual std::unique_ptr<BtHandshakeMessage>
  141. receiveHandshake(bool quickReply = false) CXX11_OVERRIDE;
  142. virtual std::unique_ptr<BtHandshakeMessage>
  143. receiveAndSendHandshake() CXX11_OVERRIDE;
  144. virtual void doPostHandshakeProcessing() CXX11_OVERRIDE;
  145. virtual void doInteractionProcessing() CXX11_OVERRIDE;
  146. virtual void cancelAllPiece() CXX11_OVERRIDE;
  147. virtual void sendPendingMessage() CXX11_OVERRIDE;
  148. size_t receiveMessages();
  149. virtual size_t countPendingMessage() CXX11_OVERRIDE;
  150. virtual bool isSendingMessageInProgress() CXX11_OVERRIDE;
  151. virtual size_t countReceivedMessageInIteration() const CXX11_OVERRIDE;
  152. virtual size_t countOutstandingRequest() CXX11_OVERRIDE;
  153. void setCuid(cuid_t cuid) { cuid_ = cuid; }
  154. void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
  155. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  156. void setPeerStorage(const std::shared_ptr<PeerStorage>& peerStorage);
  157. void setPeer(const std::shared_ptr<Peer>& peer);
  158. void setBtMessageReceiver(std::unique_ptr<BtMessageReceiver> receiver);
  159. void setDispatcher(std::unique_ptr<BtMessageDispatcher> dispatcher);
  160. void setBtRequestFactory(std::unique_ptr<BtRequestFactory> factory);
  161. void setPeerConnection(std::unique_ptr<PeerConnection> peerConnection);
  162. void setBtMessageFactory(std::unique_ptr<BtMessageFactory> factory);
  163. void
  164. setExtensionMessageFactory(std::unique_ptr<ExtensionMessageFactory> factory);
  165. void setExtensionMessageRegistry(
  166. std::unique_ptr<ExtensionMessageRegistry> registry);
  167. void setKeepAliveInterval(std::chrono::seconds keepAliveInterval)
  168. {
  169. keepAliveInterval_ = std::move(keepAliveInterval);
  170. }
  171. void setUTPexEnabled(bool f) { utPexEnabled_ = f; }
  172. void setLocalNode(DHTNode* node);
  173. void setDHTEnabled(bool f) { dhtEnabled_ = f; }
  174. void setRequestGroupMan(RequestGroupMan* rgman);
  175. void setUTMetadataRequestTracker(
  176. std::unique_ptr<UTMetadataRequestTracker> tracker);
  177. void setUTMetadataRequestFactory(
  178. std::unique_ptr<UTMetadataRequestFactory> factory);
  179. void enableMetadataGetMode() { metadataGetMode_ = true; }
  180. void setTcpPort(uint16_t port) { tcpPort_ = port; }
  181. };
  182. } // namespace aria2
  183. #endif // D_DEFAULT_BT_INTERACTIVE_H