DefaultBtMessageDispatcher.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "DefaultBtMessageDispatcher.h"
  36. #include <algorithm>
  37. #include "prefs.h"
  38. #include "BtAbortOutstandingRequestEvent.h"
  39. #include "BtCancelSendingPieceEvent.h"
  40. #include "BtChokingEvent.h"
  41. #include "BtMessageFactory.h"
  42. #include "message.h"
  43. #include "DownloadContext.h"
  44. #include "PeerStorage.h"
  45. #include "PieceStorage.h"
  46. #include "BtMessage.h"
  47. #include "Peer.h"
  48. #include "Piece.h"
  49. #include "LogFactory.h"
  50. #include "Logger.h"
  51. #include "a2functional.h"
  52. #include "a2algo.h"
  53. #include "RequestGroupMan.h"
  54. #include "RequestGroup.h"
  55. #include "util.h"
  56. #include "fmt.h"
  57. #include "PeerConnection.h"
  58. #include "BtCancelMessage.h"
  59. namespace aria2 {
  60. DefaultBtMessageDispatcher::DefaultBtMessageDispatcher()
  61. : cuid_{0},
  62. downloadContext_{nullptr},
  63. peerConnection_{nullptr},
  64. messageFactory_{nullptr},
  65. requestGroupMan_{nullptr},
  66. requestTimeout_{0}
  67. {
  68. }
  69. DefaultBtMessageDispatcher::~DefaultBtMessageDispatcher()
  70. {
  71. A2_LOG_DEBUG("DefaultBtMessageDispatcher::deleted");
  72. }
  73. void DefaultBtMessageDispatcher::addMessageToQueue(
  74. std::unique_ptr<BtMessage> btMessage)
  75. {
  76. btMessage->onQueued();
  77. messageQueue_.push_back(std::move(btMessage));
  78. }
  79. void DefaultBtMessageDispatcher::sendMessagesInternal()
  80. {
  81. auto tempQueue = std::vector<std::unique_ptr<BtMessage>>{};
  82. while (!messageQueue_.empty()) {
  83. auto msg = std::move(messageQueue_.front());
  84. messageQueue_.pop_front();
  85. if (msg->isUploading()) {
  86. if (requestGroupMan_->doesOverallUploadSpeedExceed() ||
  87. downloadContext_->getOwnerRequestGroup()->doesUploadSpeedExceed()) {
  88. tempQueue.push_back(std::move(msg));
  89. continue;
  90. }
  91. }
  92. msg->send();
  93. }
  94. if (!tempQueue.empty()) {
  95. messageQueue_.insert(std::begin(messageQueue_),
  96. std::make_move_iterator(std::begin(tempQueue)),
  97. std::make_move_iterator(std::end(tempQueue)));
  98. }
  99. }
  100. void DefaultBtMessageDispatcher::sendMessages()
  101. {
  102. if (peerConnection_->getBufferEntrySize() < A2_IOV_MAX) {
  103. sendMessagesInternal();
  104. }
  105. peerConnection_->sendPendingData();
  106. }
  107. namespace {
  108. std::vector<BtMessage*>
  109. toRawPointers(const std::deque<std::unique_ptr<BtMessage>>& v)
  110. {
  111. auto x = std::vector<BtMessage*>{};
  112. x.reserve(v.size());
  113. for (auto& i : v) {
  114. x.push_back(i.get());
  115. }
  116. return x;
  117. }
  118. } // namespace
  119. // Cancel sending piece message to peer.
  120. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(size_t index,
  121. int32_t begin,
  122. int32_t length)
  123. {
  124. BtCancelSendingPieceEvent event(index, begin, length);
  125. auto q = toRawPointers(messageQueue_);
  126. for (auto i : q) {
  127. i->onCancelSendingPieceEvent(event);
  128. }
  129. }
  130. // Cancel sending piece message to peer.
  131. // TODO Is this method really necessary?
  132. void DefaultBtMessageDispatcher::doCancelSendingPieceAction(
  133. const std::shared_ptr<Piece>& piece)
  134. {
  135. }
  136. namespace {
  137. void abortOutstandingRequest(const RequestSlot* slot,
  138. const std::shared_ptr<Piece>& piece, cuid_t cuid)
  139. {
  140. A2_LOG_DEBUG(fmt(MSG_DELETING_REQUEST_SLOT, cuid,
  141. static_cast<unsigned long>(slot->getIndex()),
  142. slot->getBegin(),
  143. static_cast<unsigned long>(slot->getBlockIndex())));
  144. piece->cancelBlock(slot->getBlockIndex());
  145. }
  146. } // namespace
  147. // localhost cancels outstanding download requests to the peer.
  148. void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(
  149. const std::shared_ptr<Piece>& piece)
  150. {
  151. for (auto& slot : requestSlots_) {
  152. if (slot->getIndex() == piece->getIndex()) {
  153. abortOutstandingRequest(slot.get(), piece, cuid_);
  154. }
  155. }
  156. requestSlots_.erase(
  157. std::remove_if(std::begin(requestSlots_), std::end(requestSlots_),
  158. [&](const std::unique_ptr<RequestSlot>& slot) {
  159. return slot->getIndex() == piece->getIndex();
  160. }),
  161. std::end(requestSlots_));
  162. BtAbortOutstandingRequestEvent event(piece);
  163. auto tempQueue = toRawPointers(messageQueue_);
  164. for (auto i : tempQueue) {
  165. i->onAbortOutstandingRequestEvent(event);
  166. }
  167. }
  168. // localhost received choke message from the peer.
  169. void DefaultBtMessageDispatcher::doChokedAction()
  170. {
  171. for (auto& slot : requestSlots_) {
  172. if (!peer_->isInPeerAllowedIndexSet(slot->getIndex())) {
  173. A2_LOG_DEBUG(fmt(MSG_DELETING_REQUEST_SLOT_CHOKED, cuid_,
  174. static_cast<unsigned long>(slot->getIndex()),
  175. slot->getBegin(),
  176. static_cast<unsigned long>(slot->getBlockIndex())));
  177. slot->getPiece()->cancelBlock(slot->getBlockIndex());
  178. }
  179. }
  180. requestSlots_.erase(
  181. std::remove_if(std::begin(requestSlots_), std::end(requestSlots_),
  182. [&](const std::unique_ptr<RequestSlot>& slot) {
  183. return !peer_->isInPeerAllowedIndexSet(slot->getIndex());
  184. }),
  185. std::end(requestSlots_));
  186. }
  187. // localhost dispatched choke message to the peer.
  188. void DefaultBtMessageDispatcher::doChokingAction()
  189. {
  190. BtChokingEvent event;
  191. auto tempQueue = toRawPointers(messageQueue_);
  192. for (auto i : tempQueue) {
  193. i->onChokingEvent(event);
  194. }
  195. }
  196. void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
  197. {
  198. for (auto& slot : requestSlots_) {
  199. if (slot->isTimeout(requestTimeout_)) {
  200. A2_LOG_DEBUG(fmt(MSG_DELETING_REQUEST_SLOT_TIMEOUT, cuid_,
  201. static_cast<unsigned long>(slot->getIndex()),
  202. slot->getBegin(),
  203. static_cast<unsigned long>(slot->getBlockIndex())));
  204. slot->getPiece()->cancelBlock(slot->getBlockIndex());
  205. peer_->snubbing(true);
  206. }
  207. else if (slot->getPiece()->hasBlock(slot->getBlockIndex())) {
  208. A2_LOG_DEBUG(fmt(MSG_DELETING_REQUEST_SLOT_ACQUIRED, cuid_,
  209. static_cast<unsigned long>(slot->getIndex()),
  210. slot->getBegin(),
  211. static_cast<unsigned long>(slot->getBlockIndex())));
  212. addMessageToQueue(messageFactory_->createCancelMessage(
  213. slot->getIndex(), slot->getBegin(), slot->getLength()));
  214. }
  215. }
  216. requestSlots_.erase(
  217. std::remove_if(std::begin(requestSlots_), std::end(requestSlots_),
  218. [&](const std::unique_ptr<RequestSlot>& slot) {
  219. return slot->isTimeout(requestTimeout_) ||
  220. slot->getPiece()->hasBlock(slot->getBlockIndex());
  221. }),
  222. std::end(requestSlots_));
  223. }
  224. bool DefaultBtMessageDispatcher::isSendingInProgress()
  225. {
  226. return peerConnection_->getBufferEntrySize();
  227. }
  228. bool DefaultBtMessageDispatcher::isOutstandingRequest(size_t index,
  229. size_t blockIndex)
  230. {
  231. for (auto& slot : requestSlots_) {
  232. if (slot->getIndex() == index && slot->getBlockIndex() == blockIndex) {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. const RequestSlot*
  239. DefaultBtMessageDispatcher::getOutstandingRequest(size_t index, int32_t begin,
  240. int32_t length)
  241. {
  242. for (auto& slot : requestSlots_) {
  243. if (slot->getIndex() == index && slot->getBegin() == begin &&
  244. slot->getLength() == length) {
  245. return slot.get();
  246. }
  247. }
  248. return nullptr;
  249. }
  250. void DefaultBtMessageDispatcher::removeOutstandingRequest(
  251. const RequestSlot* slot)
  252. {
  253. for (auto i = std::begin(requestSlots_), eoi = std::end(requestSlots_);
  254. i != eoi; ++i) {
  255. if (*(*i) == *slot) {
  256. abortOutstandingRequest((*i).get(), (*i)->getPiece(), cuid_);
  257. requestSlots_.erase(i);
  258. break;
  259. }
  260. }
  261. }
  262. void DefaultBtMessageDispatcher::addOutstandingRequest(
  263. std::unique_ptr<RequestSlot> slot)
  264. {
  265. requestSlots_.push_back(std::move(slot));
  266. }
  267. size_t DefaultBtMessageDispatcher::countOutstandingUpload()
  268. {
  269. return std::count_if(std::begin(messageQueue_), std::end(messageQueue_),
  270. std::mem_fn(&BtMessage::isUploading));
  271. }
  272. void DefaultBtMessageDispatcher::setPeer(const std::shared_ptr<Peer>& peer)
  273. {
  274. peer_ = peer;
  275. }
  276. void DefaultBtMessageDispatcher::setDownloadContext(
  277. DownloadContext* downloadContext)
  278. {
  279. downloadContext_ = downloadContext;
  280. }
  281. void DefaultBtMessageDispatcher::setBtMessageFactory(BtMessageFactory* factory)
  282. {
  283. messageFactory_ = factory;
  284. }
  285. void DefaultBtMessageDispatcher::setRequestGroupMan(RequestGroupMan* rgman)
  286. {
  287. requestGroupMan_ = rgman;
  288. }
  289. } // namespace aria2