DefaultBtInteractive.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. #include "DefaultBtInteractive.h"
  36. #include <cstring>
  37. #include <vector>
  38. #include "prefs.h"
  39. #include "message.h"
  40. #include "BtHandshakeMessage.h"
  41. #include "util.h"
  42. #include "BtKeepAliveMessage.h"
  43. #include "BtChokeMessage.h"
  44. #include "BtUnchokeMessage.h"
  45. #include "BtRequestMessage.h"
  46. #include "BtPieceMessage.h"
  47. #include "BtPortMessage.h"
  48. #include "BtInterestedMessage.h"
  49. #include "BtNotInterestedMessage.h"
  50. #include "BtHaveMessage.h"
  51. #include "BtHaveAllMessage.h"
  52. #include "BtBitfieldMessage.h"
  53. #include "BtHaveNoneMessage.h"
  54. #include "BtAllowedFastMessage.h"
  55. #include "DlAbortEx.h"
  56. #include "BtExtendedMessage.h"
  57. #include "HandshakeExtensionMessage.h"
  58. #include "UTPexExtensionMessage.h"
  59. #include "DefaultExtensionMessageFactory.h"
  60. #include "ExtensionMessageRegistry.h"
  61. #include "DHTNode.h"
  62. #include "Peer.h"
  63. #include "Piece.h"
  64. #include "DownloadContext.h"
  65. #include "PieceStorage.h"
  66. #include "PeerStorage.h"
  67. #include "BtRuntime.h"
  68. #include "BtMessageReceiver.h"
  69. #include "BtMessageDispatcher.h"
  70. #include "BtMessageFactory.h"
  71. #include "BtRequestFactory.h"
  72. #include "PeerConnection.h"
  73. #include "Logger.h"
  74. #include "LogFactory.h"
  75. #include "fmt.h"
  76. #include "RequestGroup.h"
  77. #include "RequestGroupMan.h"
  78. #include "bittorrent_helper.h"
  79. #include "UTMetadataRequestFactory.h"
  80. #include "UTMetadataRequestTracker.h"
  81. #include "wallclock.h"
  82. namespace aria2 {
  83. DefaultBtInteractive::DefaultBtInteractive
  84. (const std::shared_ptr<DownloadContext>& downloadContext,
  85. const std::shared_ptr<Peer>& peer)
  86. : cuid_(0),
  87. downloadContext_(downloadContext),
  88. peer_(peer),
  89. metadataGetMode_(false),
  90. localNode_(nullptr),
  91. allowedFastSetSize_(10),
  92. haveTimer_(global::wallclock()),
  93. keepAliveTimer_(global::wallclock()),
  94. floodingTimer_(global::wallclock()),
  95. inactiveTimer_(global::wallclock()),
  96. pexTimer_(global::wallclock()),
  97. perSecTimer_(global::wallclock()),
  98. keepAliveInterval_(120),
  99. utPexEnabled_(false),
  100. dhtEnabled_(false),
  101. numReceivedMessage_(0),
  102. maxOutstandingRequest_(DEFAULT_MAX_OUTSTANDING_REQUEST),
  103. requestGroupMan_(nullptr),
  104. tcpPort_(0),
  105. haveLastSent_(global::wallclock())
  106. {}
  107. DefaultBtInteractive::~DefaultBtInteractive() {}
  108. void DefaultBtInteractive::initiateHandshake() {
  109. dispatcher_->addMessageToQueue
  110. (messageFactory_->createHandshakeMessage
  111. (bittorrent::getInfoHash(downloadContext_),
  112. bittorrent::getStaticPeerId()));
  113. dispatcher_->sendMessages();
  114. }
  115. std::unique_ptr<BtHandshakeMessage> DefaultBtInteractive::receiveHandshake
  116. (bool quickReply) {
  117. auto message = btMessageReceiver_->receiveHandshake(quickReply);
  118. if(!message) {
  119. return nullptr;
  120. }
  121. if(memcmp(message->getPeerId(), bittorrent::getStaticPeerId(),
  122. PEER_ID_LENGTH) == 0) {
  123. throw DL_ABORT_EX
  124. (fmt("CUID#%" PRId64 " - Drop connection from the same Peer ID",
  125. cuid_));
  126. }
  127. for(auto& peer : peerStorage_->getUsedPeers()) {
  128. if(peer->isActive() &&
  129. memcmp(peer->getPeerId(), message->getPeerId(), PEER_ID_LENGTH) == 0) {
  130. throw DL_ABORT_EX
  131. (fmt("CUID#%" PRId64 " - Same Peer ID has been already seen.",
  132. cuid_));
  133. }
  134. }
  135. peer_->setPeerId(message->getPeerId());
  136. if(message->isFastExtensionSupported()) {
  137. peer_->setFastExtensionEnabled(true);
  138. A2_LOG_INFO(fmt(MSG_FAST_EXTENSION_ENABLED, cuid_));
  139. }
  140. if(message->isExtendedMessagingEnabled()) {
  141. peer_->setExtendedMessagingEnabled(true);
  142. if(!utPexEnabled_) {
  143. extensionMessageRegistry_->removeExtension
  144. (ExtensionMessageRegistry::UT_PEX);
  145. }
  146. A2_LOG_INFO(fmt(MSG_EXTENDED_MESSAGING_ENABLED, cuid_));
  147. }
  148. if(message->isDHTEnabled()) {
  149. peer_->setDHTEnabled(true);
  150. A2_LOG_INFO(fmt(MSG_DHT_ENABLED_PEER, cuid_));
  151. }
  152. A2_LOG_INFO(fmt(MSG_RECEIVE_PEER_MESSAGE, cuid_,
  153. peer_->getIPAddress().c_str(), peer_->getPort(),
  154. message->toString().c_str()));
  155. return message;
  156. }
  157. std::unique_ptr<BtHandshakeMessage>
  158. DefaultBtInteractive::receiveAndSendHandshake()
  159. {
  160. return receiveHandshake(true);
  161. }
  162. void DefaultBtInteractive::doPostHandshakeProcessing() {
  163. // Set time 0 to haveTimer to cache http/ftp download piece completion
  164. haveTimer_.reset(0);
  165. keepAliveTimer_ = global::wallclock();
  166. floodingTimer_ = global::wallclock();
  167. pexTimer_.reset(0);
  168. if(peer_->isExtendedMessagingEnabled()) {
  169. addHandshakeExtendedMessageToQueue();
  170. }
  171. if(!metadataGetMode_) {
  172. addBitfieldMessageToQueue();
  173. }
  174. if(peer_->isDHTEnabled() && dhtEnabled_) {
  175. addPortMessageToQueue();
  176. }
  177. if(!metadataGetMode_) {
  178. addAllowedFastMessageToQueue();
  179. }
  180. sendPendingMessage();
  181. }
  182. void DefaultBtInteractive::addPortMessageToQueue()
  183. {
  184. dispatcher_->addMessageToQueue
  185. (messageFactory_->createPortMessage(localNode_->getPort()));
  186. }
  187. void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
  188. {
  189. auto m = make_unique<HandshakeExtensionMessage>();
  190. m->setClientVersion("aria2/" PACKAGE_VERSION);
  191. m->setTCPPort(tcpPort_);
  192. m->setExtensions(extensionMessageRegistry_->getExtensions());
  193. auto attrs = bittorrent::getTorrentAttrs(downloadContext_);
  194. if(!attrs->metadata.empty()) {
  195. m->setMetadataSize(attrs->metadataSize);
  196. }
  197. dispatcher_->addMessageToQueue
  198. (messageFactory_->createBtExtendedMessage(std::move(m)));
  199. }
  200. void DefaultBtInteractive::addBitfieldMessageToQueue() {
  201. if(peer_->isFastExtensionEnabled()) {
  202. if(pieceStorage_->allDownloadFinished()) {
  203. dispatcher_->addMessageToQueue(messageFactory_->createHaveAllMessage());
  204. } else if(pieceStorage_->getCompletedLength() > 0) {
  205. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  206. } else {
  207. dispatcher_->addMessageToQueue(messageFactory_->createHaveNoneMessage());
  208. }
  209. } else {
  210. if(pieceStorage_->getCompletedLength() > 0) {
  211. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  212. }
  213. }
  214. }
  215. void DefaultBtInteractive::addAllowedFastMessageToQueue() {
  216. if(peer_->isFastExtensionEnabled()) {
  217. auto fastSet =
  218. bittorrent::computeFastSet(peer_->getIPAddress(),
  219. downloadContext_->getNumPieces(),
  220. bittorrent::getInfoHash(downloadContext_),
  221. allowedFastSetSize_);
  222. for(std::vector<size_t>::const_iterator itr = fastSet.begin(),
  223. eoi = fastSet.end(); itr != eoi; ++itr) {
  224. dispatcher_->addMessageToQueue
  225. (messageFactory_->createAllowedFastMessage(*itr));
  226. }
  227. }
  228. }
  229. void DefaultBtInteractive::decideChoking() {
  230. if(peer_->shouldBeChoking()) {
  231. if(!peer_->amChoking()) {
  232. dispatcher_->addMessageToQueue(messageFactory_->createChokeMessage());
  233. }
  234. } else {
  235. if(peer_->amChoking()) {
  236. dispatcher_->addMessageToQueue(messageFactory_->createUnchokeMessage());
  237. }
  238. }
  239. }
  240. void DefaultBtInteractive::checkHave() {
  241. const size_t MIN_HAVE_PACK_SIZE = 20;
  242. const time_t MAX_HAVE_DELAY_SEC = 10;
  243. pieceStorage_->getAdvertisedPieceIndexes(haveIndexes_, cuid_, haveTimer_);
  244. haveTimer_ = global::wallclock();
  245. if(haveIndexes_.size() >= MIN_HAVE_PACK_SIZE) {
  246. if(peer_->isFastExtensionEnabled() &&
  247. pieceStorage_->allDownloadFinished()) {
  248. dispatcher_->addMessageToQueue(messageFactory_->createHaveAllMessage());
  249. } else {
  250. dispatcher_->addMessageToQueue(messageFactory_->createBitfieldMessage());
  251. }
  252. haveIndexes_.clear();
  253. } else {
  254. if(haveIndexes_.size() >= MIN_HAVE_PACK_SIZE ||
  255. haveLastSent_.difference(global::wallclock()) >= MAX_HAVE_DELAY_SEC) {
  256. haveLastSent_ = global::wallclock();
  257. for(std::vector<size_t>::const_iterator itr = haveIndexes_.begin(),
  258. eoi = haveIndexes_.end(); itr != eoi; ++itr) {
  259. dispatcher_->addMessageToQueue(messageFactory_->
  260. createHaveMessage(*itr));
  261. }
  262. haveIndexes_.clear();
  263. }
  264. }
  265. }
  266. void DefaultBtInteractive::sendKeepAlive() {
  267. if(keepAliveTimer_.difference(global::wallclock()) >= keepAliveInterval_) {
  268. dispatcher_->addMessageToQueue(messageFactory_->createKeepAliveMessage());
  269. dispatcher_->sendMessages();
  270. keepAliveTimer_ = global::wallclock();
  271. }
  272. }
  273. size_t DefaultBtInteractive::receiveMessages() {
  274. size_t countOldOutstandingRequest = dispatcher_->countOutstandingRequest();
  275. size_t msgcount = 0;
  276. while(1) {
  277. if(requestGroupMan_->doesOverallDownloadSpeedExceed() ||
  278. downloadContext_->getOwnerRequestGroup()->doesDownloadSpeedExceed()) {
  279. break;
  280. }
  281. auto message = btMessageReceiver_->receiveMessage();
  282. if(!message) {
  283. break;
  284. }
  285. ++msgcount;
  286. A2_LOG_INFO(fmt(MSG_RECEIVE_PEER_MESSAGE,
  287. cuid_,
  288. peer_->getIPAddress().c_str(), peer_->getPort(),
  289. message->toString().c_str()));
  290. message->doReceivedAction();
  291. switch(message->getId()) {
  292. case BtChokeMessage::ID:
  293. if(!peer_->peerChoking()) {
  294. floodingStat_.incChokeUnchokeCount();
  295. }
  296. break;
  297. case BtUnchokeMessage::ID:
  298. if(peer_->peerChoking()) {
  299. floodingStat_.incChokeUnchokeCount();
  300. }
  301. break;
  302. case BtRequestMessage::ID:
  303. case BtPieceMessage::ID:
  304. inactiveTimer_ = global::wallclock();
  305. break;
  306. case BtKeepAliveMessage::ID:
  307. floodingStat_.incKeepAliveCount();
  308. break;
  309. }
  310. }
  311. if(!pieceStorage_->isEndGame() &&
  312. countOldOutstandingRequest > dispatcher_->countOutstandingRequest() &&
  313. (countOldOutstandingRequest - dispatcher_->countOutstandingRequest())*4 >=
  314. maxOutstandingRequest_) {
  315. maxOutstandingRequest_ =
  316. std::min((size_t)UB_MAX_OUTSTANDING_REQUEST,
  317. maxOutstandingRequest_*2);
  318. }
  319. return msgcount;
  320. }
  321. void DefaultBtInteractive::decideInterest() {
  322. if(pieceStorage_->hasMissingPiece(peer_)) {
  323. if(!peer_->amInterested()) {
  324. A2_LOG_DEBUG(fmt(MSG_PEER_INTERESTED, cuid_));
  325. dispatcher_->
  326. addMessageToQueue(messageFactory_->createInterestedMessage());
  327. }
  328. } else {
  329. if(peer_->amInterested()) {
  330. A2_LOG_DEBUG(fmt(MSG_PEER_NOT_INTERESTED, cuid_));
  331. dispatcher_->
  332. addMessageToQueue(messageFactory_->createNotInterestedMessage());
  333. }
  334. }
  335. }
  336. void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) {
  337. if(pieceStorage_->hasMissingPiece(peer_)) {
  338. size_t numMissingBlock = btRequestFactory_->countMissingBlock();
  339. if(numMissingBlock >= maxMissingBlock) {
  340. return;
  341. }
  342. size_t diffMissingBlock = maxMissingBlock-numMissingBlock;
  343. std::vector<std::shared_ptr<Piece> > pieces;
  344. if(peer_->peerChoking()) {
  345. if(peer_->isFastExtensionEnabled()) {
  346. if(pieceStorage_->isEndGame()) {
  347. pieceStorage_->getMissingFastPiece
  348. (pieces, diffMissingBlock, peer_,
  349. btRequestFactory_->getTargetPieceIndexes(), cuid_);
  350. } else {
  351. pieces.reserve(diffMissingBlock);
  352. pieceStorage_->getMissingFastPiece
  353. (pieces, diffMissingBlock, peer_, cuid_);
  354. }
  355. }
  356. } else {
  357. if(pieceStorage_->isEndGame()) {
  358. pieceStorage_->getMissingPiece
  359. (pieces, diffMissingBlock, peer_,
  360. btRequestFactory_->getTargetPieceIndexes(), cuid_);
  361. } else {
  362. pieces.reserve(diffMissingBlock);
  363. pieceStorage_->getMissingPiece(pieces, diffMissingBlock, peer_, cuid_);
  364. }
  365. }
  366. for(std::vector<std::shared_ptr<Piece> >::const_iterator i =
  367. pieces.begin(), eoi = pieces.end(); i != eoi; ++i) {
  368. btRequestFactory_->addTargetPiece(*i);
  369. }
  370. }
  371. }
  372. void DefaultBtInteractive::addRequests() {
  373. if(!pieceStorage_->isEndGame() && !pieceStorage_->hasMissingUnusedPiece()) {
  374. pieceStorage_->enterEndGame();
  375. }
  376. fillPiece(maxOutstandingRequest_);
  377. size_t reqNumToCreate =
  378. maxOutstandingRequest_ <= dispatcher_->countOutstandingRequest() ?
  379. 0 : maxOutstandingRequest_-dispatcher_->countOutstandingRequest();
  380. if(reqNumToCreate > 0) {
  381. auto requests =
  382. btRequestFactory_->createRequestMessages(reqNumToCreate,
  383. pieceStorage_->isEndGame());
  384. for(auto& i : requests) {
  385. dispatcher_->addMessageToQueue(std::move(i));
  386. }
  387. }
  388. }
  389. void DefaultBtInteractive::cancelAllPiece() {
  390. btRequestFactory_->removeAllTargetPiece();
  391. if(metadataGetMode_ && downloadContext_->getTotalLength() > 0) {
  392. std::vector<size_t> metadataRequests =
  393. utMetadataRequestTracker_->getAllTrackedIndex();
  394. for(std::vector<size_t>::const_iterator i = metadataRequests.begin(),
  395. eoi = metadataRequests.end(); i != eoi; ++i) {
  396. A2_LOG_DEBUG(fmt("Cancel metadata: piece=%lu",
  397. static_cast<unsigned long>(*i)));
  398. pieceStorage_->cancelPiece(pieceStorage_->getPiece(*i), cuid_);
  399. }
  400. }
  401. }
  402. void DefaultBtInteractive::sendPendingMessage() {
  403. dispatcher_->sendMessages();
  404. }
  405. void DefaultBtInteractive::detectMessageFlooding() {
  406. if(floodingTimer_.
  407. difference(global::wallclock()) >= FLOODING_CHECK_INTERVAL) {
  408. if(floodingStat_.getChokeUnchokeCount() >= 2 ||
  409. floodingStat_.getKeepAliveCount() >= 2) {
  410. throw DL_ABORT_EX(EX_FLOODING_DETECTED);
  411. } else {
  412. floodingStat_.reset();
  413. }
  414. floodingTimer_ = global::wallclock();
  415. }
  416. }
  417. void DefaultBtInteractive::checkActiveInteraction()
  418. {
  419. time_t inactiveTime = inactiveTimer_.difference(global::wallclock());
  420. // To allow aria2 to accept mutially interested peer, disconnect uninterested
  421. // peer.
  422. {
  423. const time_t interval = 30;
  424. if(!peer_->amInterested() && !peer_->peerInterested() &&
  425. inactiveTime >= interval) {
  426. peer_->setDisconnectedGracefully(true);
  427. // TODO change the message
  428. throw DL_ABORT_EX
  429. (fmt("Disconnect peer because we are not interested each other"
  430. " after %ld second(s).",
  431. static_cast<long int>(interval)));
  432. }
  433. }
  434. // Since the peers which are *just* connected and do nothing to improve
  435. // mutual download progress are completely waste of resources, those peers
  436. // are disconnected in a certain time period.
  437. {
  438. const time_t interval = 60;
  439. if(inactiveTime >= interval) {
  440. peer_->setDisconnectedGracefully(true);
  441. throw DL_ABORT_EX
  442. (fmt(EX_DROP_INACTIVE_CONNECTION,
  443. static_cast<long int>(interval)));
  444. }
  445. }
  446. // If both of us are seeders, drop connection.
  447. if(peer_->isSeeder() && pieceStorage_->downloadFinished()) {
  448. throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
  449. }
  450. }
  451. void DefaultBtInteractive::addPeerExchangeMessage()
  452. {
  453. if(pexTimer_.difference(global::wallclock()) >=
  454. UTPexExtensionMessage::DEFAULT_INTERVAL) {
  455. auto m = make_unique<UTPexExtensionMessage>
  456. (peer_->getExtensionMessageID(ExtensionMessageRegistry::UT_PEX));
  457. auto& usedPeers = peerStorage_->getUsedPeers();
  458. for(auto i = std::begin(usedPeers), eoi = std::end(usedPeers);
  459. i != eoi && !m->freshPeersAreFull(); ++i) {
  460. if((*i)->isActive() && peer_->getIPAddress() != (*i)->getIPAddress()) {
  461. m->addFreshPeer(*i);
  462. }
  463. }
  464. auto& droppedPeers = peerStorage_->getDroppedPeers();
  465. for(auto i = std::begin(droppedPeers), eoi = std::end(droppedPeers);
  466. i != eoi && !m->droppedPeersAreFull(); ++i) {
  467. if(peer_->getIPAddress() != (*i)->getIPAddress()) {
  468. m->addDroppedPeer(*i);
  469. }
  470. }
  471. dispatcher_->addMessageToQueue
  472. (messageFactory_->createBtExtendedMessage(std::move(m)));
  473. pexTimer_ = global::wallclock();
  474. }
  475. }
  476. void DefaultBtInteractive::doInteractionProcessing() {
  477. if(metadataGetMode_) {
  478. sendKeepAlive();
  479. numReceivedMessage_ = receiveMessages();
  480. // PieceStorage is re-initialized with metadata_size in
  481. // HandshakeExtensionMessage::doReceivedAction().
  482. pieceStorage_ =
  483. downloadContext_->getOwnerRequestGroup()->getPieceStorage();
  484. if(peer_->getExtensionMessageID(ExtensionMessageRegistry::UT_METADATA) &&
  485. downloadContext_->getTotalLength() > 0) {
  486. size_t num = utMetadataRequestTracker_->avail();
  487. if(num > 0) {
  488. auto requests =
  489. utMetadataRequestFactory_->create(num, pieceStorage_.get());
  490. for(auto& i : requests) {
  491. dispatcher_->addMessageToQueue(std::move(i));
  492. }
  493. }
  494. if(perSecTimer_.difference(global::wallclock()) >= 1) {
  495. perSecTimer_ = global::wallclock();
  496. // Drop timeout request after queuing message to give a chance
  497. // to other connection to request piece.
  498. auto indexes = utMetadataRequestTracker_->removeTimeoutEntry();
  499. for(auto idx : indexes) {
  500. pieceStorage_->cancelPiece(pieceStorage_->getPiece(idx), cuid_);
  501. }
  502. }
  503. if(pieceStorage_->downloadFinished()) {
  504. downloadContext_->getOwnerRequestGroup()->setForceHaltRequested
  505. (true, RequestGroup::NONE);
  506. }
  507. }
  508. } else {
  509. checkActiveInteraction();
  510. decideChoking();
  511. detectMessageFlooding();
  512. if(perSecTimer_.difference(global::wallclock()) >= 1) {
  513. perSecTimer_ = global::wallclock();
  514. dispatcher_->checkRequestSlotAndDoNecessaryThing();
  515. }
  516. checkHave();
  517. sendKeepAlive();
  518. numReceivedMessage_ = receiveMessages();
  519. btRequestFactory_->removeCompletedPiece();
  520. decideInterest();
  521. if(!pieceStorage_->downloadFinished()) {
  522. addRequests();
  523. }
  524. }
  525. if(peer_->getExtensionMessageID(ExtensionMessageRegistry::UT_PEX) &&
  526. utPexEnabled_) {
  527. addPeerExchangeMessage();
  528. }
  529. sendPendingMessage();
  530. }
  531. void DefaultBtInteractive::setLocalNode(DHTNode* node)
  532. {
  533. localNode_ = node;
  534. }
  535. size_t DefaultBtInteractive::countPendingMessage()
  536. {
  537. return dispatcher_->countMessageInQueue();
  538. }
  539. bool DefaultBtInteractive::isSendingMessageInProgress()
  540. {
  541. return dispatcher_->isSendingInProgress();
  542. }
  543. size_t DefaultBtInteractive::countReceivedMessageInIteration() const
  544. {
  545. return numReceivedMessage_;
  546. }
  547. size_t DefaultBtInteractive::countOutstandingRequest()
  548. {
  549. if(metadataGetMode_) {
  550. return utMetadataRequestTracker_->count();
  551. } else {
  552. return dispatcher_->countOutstandingRequest();
  553. }
  554. }
  555. void DefaultBtInteractive::setBtRuntime
  556. (const std::shared_ptr<BtRuntime>& btRuntime)
  557. {
  558. btRuntime_ = btRuntime;
  559. }
  560. void DefaultBtInteractive::setPieceStorage
  561. (const std::shared_ptr<PieceStorage>& pieceStorage)
  562. {
  563. pieceStorage_ = pieceStorage;
  564. }
  565. void DefaultBtInteractive::setPeerStorage
  566. (const std::shared_ptr<PeerStorage>& peerStorage)
  567. {
  568. peerStorage_ = peerStorage;
  569. }
  570. void DefaultBtInteractive::setPeer(const std::shared_ptr<Peer>& peer)
  571. {
  572. peer_ = peer;
  573. }
  574. void DefaultBtInteractive::setBtMessageReceiver
  575. (std::unique_ptr<BtMessageReceiver> receiver)
  576. {
  577. btMessageReceiver_ = std::move(receiver);
  578. }
  579. void DefaultBtInteractive::setDispatcher
  580. (std::unique_ptr<BtMessageDispatcher> dispatcher)
  581. {
  582. dispatcher_ = std::move(dispatcher);
  583. }
  584. void DefaultBtInteractive::setBtRequestFactory
  585. (std::unique_ptr<BtRequestFactory> factory)
  586. {
  587. btRequestFactory_ = std::move(factory);
  588. }
  589. void DefaultBtInteractive::setPeerConnection
  590. (std::unique_ptr<PeerConnection> peerConnection)
  591. {
  592. peerConnection_ = std::move(peerConnection);
  593. }
  594. void DefaultBtInteractive::setExtensionMessageFactory
  595. (std::unique_ptr<ExtensionMessageFactory> factory)
  596. {
  597. extensionMessageFactory_ = std::move(factory);
  598. }
  599. void DefaultBtInteractive::setBtMessageFactory
  600. (std::unique_ptr<BtMessageFactory> factory)
  601. {
  602. messageFactory_ = std::move(factory);
  603. }
  604. void DefaultBtInteractive::setRequestGroupMan(RequestGroupMan* rgman)
  605. {
  606. requestGroupMan_ = rgman;
  607. }
  608. void DefaultBtInteractive::setExtensionMessageRegistry
  609. (std::unique_ptr<ExtensionMessageRegistry> registry)
  610. {
  611. extensionMessageRegistry_ = std::move(registry);
  612. }
  613. void DefaultBtInteractive::setUTMetadataRequestTracker
  614. (std::unique_ptr<UTMetadataRequestTracker> tracker)
  615. {
  616. utMetadataRequestTracker_ = std::move(tracker);
  617. }
  618. void DefaultBtInteractive::setUTMetadataRequestFactory
  619. (std::unique_ptr<UTMetadataRequestFactory> factory)
  620. {
  621. utMetadataRequestFactory_ = std::move(factory);
  622. }
  623. } // namespace aria2