DefaultBtInteractive.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if(chokeUnchokeCount < INT_MAX) {
  68. ++chokeUnchokeCount;
  69. }
  70. }
  71. void incKeepAliveCount() {
  72. if(keepAliveCount < INT_MAX) {
  73. ++keepAliveCount;
  74. }
  75. }
  76. int getChokeUnchokeCount() const {
  77. return chokeUnchokeCount;
  78. }
  79. int getKeepAliveCount() const {
  80. return keepAliveCount;
  81. }
  82. void reset() {
  83. chokeUnchokeCount = 0;
  84. keepAliveCount = 0;
  85. }
  86. };
  87. class DefaultBtInteractive : public BtInteractive {
  88. private:
  89. cuid_t cuid_;
  90. std::shared_ptr<DownloadContext> downloadContext_;
  91. std::shared_ptr<BtRuntime> btRuntime_;
  92. std::shared_ptr<PieceStorage> pieceStorage_;
  93. std::shared_ptr<PeerStorage> peerStorage_;
  94. std::shared_ptr<Peer> peer_;
  95. std::shared_ptr<BtMessageReceiver> btMessageReceiver_;
  96. std::shared_ptr<BtMessageDispatcher> dispatcher_;
  97. std::shared_ptr<BtRequestFactory> btRequestFactory_;
  98. // Although peerStorage_ is not used in this class, this object
  99. // holds the reference so that peerConnection_ is not deleted.
  100. std::shared_ptr<PeerConnection> peerConnection_;
  101. std::shared_ptr<BtMessageFactory> messageFactory_;
  102. std::unique_ptr<ExtensionMessageFactory> extensionMessageFactory_;
  103. std::shared_ptr<ExtensionMessageRegistry> extensionMessageRegistry_;
  104. std::shared_ptr<UTMetadataRequestFactory> utMetadataRequestFactory_;
  105. std::shared_ptr<UTMetadataRequestTracker> utMetadataRequestTracker_;
  106. bool metadataGetMode_;
  107. DHTNode* localNode_;
  108. size_t allowedFastSetSize_;
  109. Timer haveTimer_;
  110. Timer keepAliveTimer_;
  111. Timer floodingTimer_;
  112. FloodingStat floodingStat_;
  113. Timer inactiveTimer_;
  114. Timer pexTimer_;
  115. Timer perSecTimer_;
  116. time_t keepAliveInterval_;
  117. bool utPexEnabled_;
  118. bool dhtEnabled_;
  119. size_t numReceivedMessage_;
  120. size_t maxOutstandingRequest_;
  121. RequestGroupMan* requestGroupMan_;
  122. uint16_t tcpPort_;
  123. std::vector<size_t> haveIndexes_;
  124. Timer haveLastSent_;
  125. static const time_t FLOODING_CHECK_INTERVAL = 5;
  126. void addBitfieldMessageToQueue();
  127. void addAllowedFastMessageToQueue();
  128. void addHandshakeExtendedMessageToQueue();
  129. void decideChoking();
  130. void checkHave();
  131. void sendKeepAlive();
  132. void decideInterest();
  133. void fillPiece(size_t maxMissingBlock);
  134. void addRequests();
  135. void detectMessageFlooding();
  136. void checkActiveInteraction();
  137. void addPeerExchangeMessage();
  138. void addPortMessageToQueue();
  139. public:
  140. DefaultBtInteractive(const std::shared_ptr<DownloadContext>& downloadContext,
  141. const std::shared_ptr<Peer>& peer);
  142. virtual ~DefaultBtInteractive();
  143. virtual void initiateHandshake();
  144. virtual std::unique_ptr<BtHandshakeMessage> receiveHandshake
  145. (bool quickReply = false);
  146. virtual std::unique_ptr<BtHandshakeMessage> receiveAndSendHandshake();
  147. virtual void doPostHandshakeProcessing();
  148. virtual void doInteractionProcessing();
  149. virtual void cancelAllPiece();
  150. virtual void sendPendingMessage();
  151. size_t receiveMessages();
  152. virtual size_t countPendingMessage();
  153. virtual bool isSendingMessageInProgress();
  154. virtual size_t countReceivedMessageInIteration() const;
  155. virtual size_t countOutstandingRequest();
  156. void setCuid(cuid_t cuid)
  157. {
  158. cuid_ = cuid;
  159. }
  160. void setBtRuntime(const std::shared_ptr<BtRuntime>& btRuntime);
  161. void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
  162. void setPeerStorage(const std::shared_ptr<PeerStorage>& peerStorage);
  163. void setPeer(const std::shared_ptr<Peer>& peer);
  164. void setBtMessageReceiver(const std::shared_ptr<BtMessageReceiver>& receiver);
  165. void setDispatcher(const std::shared_ptr<BtMessageDispatcher>& dispatcher);
  166. void setBtRequestFactory(const std::shared_ptr<BtRequestFactory>& factory);
  167. void setPeerConnection(const std::shared_ptr<PeerConnection>& peerConnection);
  168. void setBtMessageFactory(const std::shared_ptr<BtMessageFactory>& factory);
  169. void setExtensionMessageFactory
  170. (std::unique_ptr<ExtensionMessageFactory> factory);
  171. void setExtensionMessageRegistry
  172. (const std::shared_ptr<ExtensionMessageRegistry>& registry)
  173. {
  174. extensionMessageRegistry_ = registry;
  175. }
  176. void setKeepAliveInterval(time_t keepAliveInterval) {
  177. keepAliveInterval_ = keepAliveInterval;
  178. }
  179. void setUTPexEnabled(bool f)
  180. {
  181. utPexEnabled_ = f;
  182. }
  183. void setLocalNode(DHTNode* node);
  184. void setDHTEnabled(bool f)
  185. {
  186. dhtEnabled_ = f;
  187. }
  188. void setRequestGroupMan(RequestGroupMan* rgman);
  189. void setUTMetadataRequestTracker
  190. (const std::shared_ptr<UTMetadataRequestTracker>& tracker)
  191. {
  192. utMetadataRequestTracker_ = tracker;
  193. }
  194. void setUTMetadataRequestFactory
  195. (const std::shared_ptr<UTMetadataRequestFactory>& factory)
  196. {
  197. utMetadataRequestFactory_ = factory;
  198. }
  199. void enableMetadataGetMode()
  200. {
  201. metadataGetMode_ = true;
  202. }
  203. void setTcpPort(uint16_t port)
  204. {
  205. tcpPort_ = port;
  206. }
  207. };
  208. } // namespace aria2
  209. #endif // D_DEFAULT_BT_INTERACTIVE_H