DefaultBtInteractive.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "BtContextAwareCommand.h"
  39. #include "TimeA2.h"
  40. namespace aria2 {
  41. class Peer;
  42. class BtMessage;
  43. class BtMessageReceiver;
  44. class BtMessageDispatcher;
  45. class BtMessageFactory;
  46. class BtRequestFactory;
  47. class PeerConnection;
  48. class DHTNode;
  49. class Logger;
  50. class FloodingStat {
  51. private:
  52. int32_t chokeUnchokeCount;
  53. int32_t keepAliveCount;
  54. public:
  55. FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
  56. void incChokeUnchokeCount() {
  57. if(chokeUnchokeCount < INT32_MAX) {
  58. chokeUnchokeCount++;
  59. }
  60. }
  61. void incKeepAliveCount() {
  62. if(keepAliveCount < INT32_MAX) {
  63. keepAliveCount++;
  64. }
  65. }
  66. int32_t getChokeUnchokeCount() const {
  67. return chokeUnchokeCount;
  68. }
  69. int32_t getKeepAliveCount() const {
  70. return keepAliveCount;
  71. }
  72. void reset() {
  73. chokeUnchokeCount = 0;
  74. keepAliveCount = 0;
  75. }
  76. };
  77. class DefaultBtInteractive : public BtInteractive, public BtContextAwareCommand {
  78. private:
  79. int32_t cuid;
  80. SharedHandle<Peer> peer;
  81. WeakHandle<BtMessageReceiver> btMessageReceiver;
  82. WeakHandle<BtMessageDispatcher> dispatcher;
  83. WeakHandle<BtRequestFactory> btRequestFactory;
  84. WeakHandle<PeerConnection> peerConnection;
  85. WeakHandle<BtMessageFactory> messageFactory;
  86. WeakHandle<DHTNode> _localNode;
  87. const Logger* logger;
  88. int32_t allowedFastSetSize;
  89. Time haveCheckPoint;
  90. Time keepAliveCheckPoint;
  91. Time floodingCheckPoint;
  92. FloodingStat floodingStat;
  93. Time inactiveCheckPoint;
  94. Time _pexCheckPoint;
  95. int32_t keepAliveInterval;
  96. int32_t maxDownloadSpeedLimit;
  97. bool _utPexEnabled;
  98. bool _dhtEnabled;
  99. size_t _numReceivedMessage;
  100. static const int32_t FLOODING_CHECK_INTERVAL = 5;
  101. void addBitfieldMessageToQueue();
  102. void addAllowedFastMessageToQueue();
  103. void addHandshakeExtendedMessageToQueue();
  104. void decideChoking();
  105. void checkHave();
  106. void sendKeepAlive();
  107. void decideInterest();
  108. void fillPiece(int maxPieceNum);
  109. void addRequests();
  110. void detectMessageFlooding();
  111. void checkActiveInteraction();
  112. void addPeerExchangeMessage();
  113. void addPortMessageToQueue();
  114. public:
  115. DefaultBtInteractive(const SharedHandle<BtContext>& btContext,
  116. const SharedHandle<Peer>& peer);
  117. virtual ~DefaultBtInteractive();
  118. virtual void initiateHandshake();
  119. virtual SharedHandle<BtMessage> receiveHandshake(bool quickReply = false);
  120. virtual SharedHandle<BtMessage> receiveAndSendHandshake();
  121. virtual void doPostHandshakeProcessing();
  122. virtual void doInteractionProcessing();
  123. virtual void cancelAllPiece();
  124. virtual void sendPendingMessage();
  125. size_t receiveMessages();
  126. virtual int32_t countPendingMessage();
  127. virtual bool isSendingMessageInProgress();
  128. virtual size_t countReceivedMessageInIteration() const;
  129. void setCuid(int32_t cuid) {
  130. this->cuid = cuid;
  131. }
  132. void setPeer(const SharedHandle<Peer>& peer);
  133. void setBtMessageReceiver(const WeakHandle<BtMessageReceiver>& receiver);
  134. void setDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher);
  135. void setBtRequestFactory(const WeakHandle<BtRequestFactory>& factory);
  136. void setPeerConnection(const WeakHandle<PeerConnection>& peerConnection);
  137. void setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory);
  138. void setKeepAliveInterval(int32_t keepAliveInterval) {
  139. this->keepAliveInterval = keepAliveInterval;
  140. }
  141. void setMaxDownloadSpeedLimit(int32_t maxDownloadSpeedLimit) {
  142. this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
  143. }
  144. void setUTPexEnabled(bool f)
  145. {
  146. _utPexEnabled = f;
  147. }
  148. void setLocalNode(const WeakHandle<DHTNode>& node);
  149. void setDHTEnabled(bool f)
  150. {
  151. _dhtEnabled = f;
  152. }
  153. };
  154. typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
  155. } // namespace aria2
  156. #endif // _D_DEFAULT_BT_INTERACTIVE_H_