DefaultBtInteractive.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "TimeA2.h"
  40. namespace aria2 {
  41. class DownloadContext;
  42. class BtRuntime;
  43. class PieceStorage;
  44. class PeerStorage;
  45. class Peer;
  46. class BtMessage;
  47. class BtMessageReceiver;
  48. class BtMessageDispatcher;
  49. class BtMessageFactory;
  50. class BtRequestFactory;
  51. class PeerConnection;
  52. class ExtensionMessageFactory;
  53. class ExtensionMessageRegistry;
  54. class DHTNode;
  55. class Logger;
  56. class RequestGroupMan;
  57. class FloodingStat {
  58. private:
  59. unsigned int chokeUnchokeCount;
  60. unsigned int keepAliveCount;
  61. public:
  62. FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
  63. void incChokeUnchokeCount() {
  64. if(chokeUnchokeCount < UINT_MAX) {
  65. chokeUnchokeCount++;
  66. }
  67. }
  68. void incKeepAliveCount() {
  69. if(keepAliveCount < UINT_MAX) {
  70. keepAliveCount++;
  71. }
  72. }
  73. unsigned int getChokeUnchokeCount() const {
  74. return chokeUnchokeCount;
  75. }
  76. unsigned int getKeepAliveCount() const {
  77. return keepAliveCount;
  78. }
  79. void reset() {
  80. chokeUnchokeCount = 0;
  81. keepAliveCount = 0;
  82. }
  83. };
  84. class DefaultBtInteractive : public BtInteractive {
  85. private:
  86. int32_t cuid;
  87. SharedHandle<DownloadContext> _downloadContext;
  88. SharedHandle<BtRuntime> _btRuntime;
  89. SharedHandle<PieceStorage> _pieceStorage;
  90. SharedHandle<PeerStorage> _peerStorage;
  91. SharedHandle<Peer> peer;
  92. SharedHandle<BtMessageReceiver> btMessageReceiver;
  93. SharedHandle<BtMessageDispatcher> dispatcher;
  94. SharedHandle<BtRequestFactory> btRequestFactory;
  95. SharedHandle<PeerConnection> peerConnection;
  96. SharedHandle<BtMessageFactory> messageFactory;
  97. SharedHandle<ExtensionMessageFactory> _extensionMessageFactory;
  98. SharedHandle<ExtensionMessageRegistry> _extensionMessageRegistry;
  99. WeakHandle<DHTNode> _localNode;
  100. Logger* logger;
  101. size_t allowedFastSetSize;
  102. Time haveCheckPoint;
  103. Time keepAliveCheckPoint;
  104. Time floodingCheckPoint;
  105. FloodingStat floodingStat;
  106. Time inactiveCheckPoint;
  107. Time _pexCheckPoint;
  108. Time _perSecCheckPoint;
  109. time_t keepAliveInterval;
  110. bool _utPexEnabled;
  111. bool _dhtEnabled;
  112. size_t _numReceivedMessage;
  113. WeakHandle<RequestGroupMan> _requestGroupMan;
  114. static const time_t FLOODING_CHECK_INTERVAL = 5;
  115. void addBitfieldMessageToQueue();
  116. void addAllowedFastMessageToQueue();
  117. void addHandshakeExtendedMessageToQueue();
  118. void decideChoking();
  119. void checkHave();
  120. void sendKeepAlive();
  121. void decideInterest();
  122. void fillPiece(size_t maxMissingBlock);
  123. void addRequests();
  124. void detectMessageFlooding();
  125. void checkActiveInteraction();
  126. void addPeerExchangeMessage();
  127. void addPortMessageToQueue();
  128. public:
  129. DefaultBtInteractive(const SharedHandle<DownloadContext>& downloadContext,
  130. const SharedHandle<Peer>& peer);
  131. virtual ~DefaultBtInteractive();
  132. virtual void initiateHandshake();
  133. virtual SharedHandle<BtMessage> receiveHandshake(bool quickReply = false);
  134. virtual SharedHandle<BtMessage> receiveAndSendHandshake();
  135. virtual void doPostHandshakeProcessing();
  136. virtual void doInteractionProcessing();
  137. virtual void cancelAllPiece();
  138. virtual void sendPendingMessage();
  139. size_t receiveMessages();
  140. virtual size_t countPendingMessage();
  141. virtual bool isSendingMessageInProgress();
  142. virtual size_t countReceivedMessageInIteration() const;
  143. virtual size_t countOutstandingRequest();
  144. void setCuid(int32_t cuid) {
  145. this->cuid = cuid;
  146. }
  147. void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
  148. void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
  149. void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
  150. void setPeer(const SharedHandle<Peer>& peer);
  151. void setBtMessageReceiver(const SharedHandle<BtMessageReceiver>& receiver);
  152. void setDispatcher(const SharedHandle<BtMessageDispatcher>& dispatcher);
  153. void setBtRequestFactory(const SharedHandle<BtRequestFactory>& factory);
  154. void setPeerConnection(const SharedHandle<PeerConnection>& peerConnection);
  155. void setBtMessageFactory(const SharedHandle<BtMessageFactory>& factory);
  156. void setExtensionMessageFactory
  157. (const SharedHandle<ExtensionMessageFactory>& factory);
  158. void setExtensionMessageRegistry
  159. (const SharedHandle<ExtensionMessageRegistry>& registry)
  160. {
  161. _extensionMessageRegistry = registry;
  162. }
  163. void setKeepAliveInterval(time_t keepAliveInterval) {
  164. this->keepAliveInterval = keepAliveInterval;
  165. }
  166. void setUTPexEnabled(bool f)
  167. {
  168. _utPexEnabled = f;
  169. }
  170. void setLocalNode(const WeakHandle<DHTNode>& node);
  171. void setDHTEnabled(bool f)
  172. {
  173. _dhtEnabled = f;
  174. }
  175. void setRequestGroupMan(const WeakHandle<RequestGroupMan>& rgman);
  176. };
  177. typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
  178. } // namespace aria2
  179. #endif // _D_DEFAULT_BT_INTERACTIVE_H_