DefaultBtInteractive.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "Peer.h"
  39. #include "BtContext.h"
  40. #include "PieceStorage.h"
  41. #include "PeerStorage.h"
  42. #include "BtMessageReceiver.h"
  43. #include "BtMessageDispatcher.h"
  44. #include "BtRequestFactory.h"
  45. #include "PeerConnection.h"
  46. #include "BtRegistry.h"
  47. #include "Logger.h"
  48. #include "LogFactory.h"
  49. #include "TimeA2.h"
  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 {
  78. private:
  79. int32_t cuid;
  80. PeerHandle peer;
  81. BtContextHandle btContext;
  82. PeerStorageHandle peerStorage;
  83. PieceStorageHandle pieceStorage;
  84. BtRuntimeHandle btRuntime;
  85. BtMessageReceiverWeakHandle btMessageReceiver;
  86. BtMessageDispatcherWeakHandle dispatcher;
  87. BtRequestFactoryWeakHandle btRequestFactory;
  88. PeerConnectionWeakHandle peerConnection;
  89. BtMessageFactoryWeakHandle messageFactory;
  90. const Logger* logger;
  91. int32_t allowedFastSetSize;
  92. Time haveCheckPoint;
  93. Time keepAliveCheckPoint;
  94. Time floodingCheckPoint;
  95. FloodingStat floodingStat;
  96. Time inactiveCheckPoint;
  97. int32_t keepAliveInterval;
  98. int32_t maxDownloadSpeedLimit;
  99. static const int32_t FLOODING_CHECK_INTERVAL = 5;
  100. void addBitfieldMessageToQueue();
  101. void addAllowedFastMessageToQueue();
  102. void decideChoking();
  103. void checkHave();
  104. void sendKeepAlive();
  105. void decideInterest();
  106. void fillPiece(int maxPieceNum);
  107. void addRequests();
  108. void detectMessageFlooding();
  109. void checkActiveInteraction();
  110. public:
  111. DefaultBtInteractive():peer(0),
  112. btContext(0),
  113. peerStorage(0),
  114. pieceStorage(0),
  115. btRuntime(0),
  116. btMessageReceiver(0),
  117. dispatcher(0),
  118. btRequestFactory(0),
  119. peerConnection(0),
  120. logger(LogFactory::getInstance()),
  121. allowedFastSetSize(10),
  122. keepAliveInterval(120),
  123. maxDownloadSpeedLimit(0)
  124. {}
  125. virtual ~DefaultBtInteractive() {}
  126. virtual void initiateHandshake();
  127. virtual BtMessageHandle receiveHandshake(bool quickReply = false);
  128. virtual BtMessageHandle receiveAndSendHandshake();
  129. virtual void doPostHandshakeProcessing();
  130. virtual void doInteractionProcessing();
  131. virtual void cancelAllPiece();
  132. virtual void sendPendingMessage();
  133. void receiveMessages();
  134. virtual int32_t countPendingMessage() {
  135. return dispatcher->countMessageInQueue();
  136. }
  137. virtual bool isSendingMessageInProgress() {
  138. return dispatcher->isSendingInProgress();
  139. }
  140. void setCuid(int32_t cuid) {
  141. this->cuid = cuid;
  142. }
  143. void setPeer(const PeerHandle& peer) {
  144. this->peer = peer;
  145. }
  146. void setBtContext(const BtContextHandle& btContext) {
  147. this->btContext = btContext;
  148. this->peerStorage = PEER_STORAGE(btContext);
  149. this->pieceStorage = PIECE_STORAGE(btContext);
  150. this->btRuntime = BT_RUNTIME(btContext);
  151. }
  152. void setBtMessageReceiver(const BtMessageReceiverWeakHandle& receiver) {
  153. this->btMessageReceiver = receiver;
  154. }
  155. void setDispatcher(const BtMessageDispatcherWeakHandle& dispatcher) {
  156. this->dispatcher = dispatcher;
  157. }
  158. void setBtRequestFactory(const BtRequestFactoryWeakHandle& factory) {
  159. this->btRequestFactory = factory;
  160. }
  161. void setPeerConnection(const PeerConnectionWeakHandle& peerConnection) {
  162. this->peerConnection = peerConnection;
  163. }
  164. void setKeepAliveInterval(int32_t keepAliveInterval) {
  165. this->keepAliveInterval = keepAliveInterval;
  166. }
  167. void setMaxDownloadSpeedLimit(int32_t maxDownloadSpeedLimit) {
  168. this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
  169. }
  170. void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
  171. this->messageFactory = factory;
  172. }
  173. };
  174. typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
  175. #endif // _D_DEFAULT_BT_INTERACTIVE_H_