DefaultBtInteractive.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "TimerA2.h"
  40. #include "Command.h"
  41. namespace aria2 {
  42. class DownloadContext;
  43. class BtRuntime;
  44. class PieceStorage;
  45. class PeerStorage;
  46. class Peer;
  47. class BtMessage;
  48. class BtMessageReceiver;
  49. class BtMessageDispatcher;
  50. class BtMessageFactory;
  51. class BtRequestFactory;
  52. class PeerConnection;
  53. class ExtensionMessageFactory;
  54. class ExtensionMessageRegistry;
  55. class DHTNode;
  56. class Logger;
  57. class RequestGroupMan;
  58. class UTMetadataRequestFactory;
  59. class UTMetadataRequestTracker;
  60. class FloodingStat {
  61. private:
  62. unsigned int chokeUnchokeCount;
  63. unsigned int keepAliveCount;
  64. public:
  65. FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
  66. void incChokeUnchokeCount() {
  67. if(chokeUnchokeCount < UINT_MAX) {
  68. chokeUnchokeCount++;
  69. }
  70. }
  71. void incKeepAliveCount() {
  72. if(keepAliveCount < UINT_MAX) {
  73. keepAliveCount++;
  74. }
  75. }
  76. unsigned int getChokeUnchokeCount() const {
  77. return chokeUnchokeCount;
  78. }
  79. unsigned 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. SharedHandle<DownloadContext> _downloadContext;
  91. SharedHandle<BtRuntime> _btRuntime;
  92. SharedHandle<PieceStorage> _pieceStorage;
  93. SharedHandle<PeerStorage> _peerStorage;
  94. SharedHandle<Peer> peer;
  95. SharedHandle<BtMessageReceiver> btMessageReceiver;
  96. SharedHandle<BtMessageDispatcher> dispatcher;
  97. SharedHandle<BtRequestFactory> btRequestFactory;
  98. SharedHandle<PeerConnection> peerConnection;
  99. SharedHandle<BtMessageFactory> messageFactory;
  100. SharedHandle<ExtensionMessageFactory> _extensionMessageFactory;
  101. SharedHandle<ExtensionMessageRegistry> _extensionMessageRegistry;
  102. SharedHandle<UTMetadataRequestFactory> _utMetadataRequestFactory;
  103. SharedHandle<UTMetadataRequestTracker> _utMetadataRequestTracker;
  104. bool _metadataGetMode;
  105. WeakHandle<DHTNode> _localNode;
  106. Logger* logger;
  107. size_t allowedFastSetSize;
  108. Timer haveTimer;
  109. Timer keepAliveTimer;
  110. Timer floodingTimer;
  111. FloodingStat floodingStat;
  112. Timer inactiveTimer;
  113. Timer _pexTimer;
  114. Timer _perSecTimer;
  115. time_t keepAliveInterval;
  116. bool _utPexEnabled;
  117. bool _dhtEnabled;
  118. size_t _numReceivedMessage;
  119. size_t _maxOutstandingRequest;
  120. WeakHandle<RequestGroupMan> _requestGroupMan;
  121. static const time_t FLOODING_CHECK_INTERVAL = 5;
  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 SharedHandle<DownloadContext>& downloadContext,
  137. const SharedHandle<Peer>& peer);
  138. virtual ~DefaultBtInteractive();
  139. virtual void initiateHandshake();
  140. virtual SharedHandle<BtMessage> receiveHandshake(bool quickReply = false);
  141. virtual SharedHandle<BtMessage> receiveAndSendHandshake();
  142. virtual void doPostHandshakeProcessing();
  143. virtual void doInteractionProcessing();
  144. virtual void cancelAllPiece();
  145. virtual void sendPendingMessage();
  146. size_t receiveMessages();
  147. virtual size_t countPendingMessage();
  148. virtual bool isSendingMessageInProgress();
  149. virtual size_t countReceivedMessageInIteration() const;
  150. virtual size_t countOutstandingRequest();
  151. void setCuid(cuid_t cuid)
  152. {
  153. this->cuid = cuid;
  154. }
  155. void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
  156. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  157. void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
  158. void setPeer(const SharedHandle<Peer>& peer);
  159. void setBtMessageReceiver(const SharedHandle<BtMessageReceiver>& receiver);
  160. void setDispatcher(const SharedHandle<BtMessageDispatcher>& dispatcher);
  161. void setBtRequestFactory(const SharedHandle<BtRequestFactory>& factory);
  162. void setPeerConnection(const SharedHandle<PeerConnection>& peerConnection);
  163. void setBtMessageFactory(const SharedHandle<BtMessageFactory>& factory);
  164. void setExtensionMessageFactory
  165. (const SharedHandle<ExtensionMessageFactory>& factory);
  166. void setExtensionMessageRegistry
  167. (const SharedHandle<ExtensionMessageRegistry>& registry)
  168. {
  169. _extensionMessageRegistry = registry;
  170. }
  171. void setKeepAliveInterval(time_t keepAliveInterval) {
  172. this->keepAliveInterval = keepAliveInterval;
  173. }
  174. void setUTPexEnabled(bool f)
  175. {
  176. _utPexEnabled = f;
  177. }
  178. void setLocalNode(const WeakHandle<DHTNode>& node);
  179. void setDHTEnabled(bool f)
  180. {
  181. _dhtEnabled = f;
  182. }
  183. void setRequestGroupMan(const WeakHandle<RequestGroupMan>& rgman);
  184. void setUTMetadataRequestTracker
  185. (const SharedHandle<UTMetadataRequestTracker>& tracker)
  186. {
  187. _utMetadataRequestTracker = tracker;
  188. }
  189. void setUTMetadataRequestFactory
  190. (const SharedHandle<UTMetadataRequestFactory>& factory)
  191. {
  192. _utMetadataRequestFactory = factory;
  193. }
  194. void enableMetadataGetMode()
  195. {
  196. _metadataGetMode = true;
  197. }
  198. };
  199. typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
  200. } // namespace aria2
  201. #endif // _D_DEFAULT_BT_INTERACTIVE_H_