DefaultBtInteractive.h 5.4 KB

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