DefaultBtInteractive.h 6.0 KB

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