DefaultBtInteractive.h 6.9 KB

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