BtRequestMessage.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "BtRequestMessage.h"
  36. #include "PeerMessageUtil.h"
  37. #include "Util.h"
  38. #include "DlAbortEx.h"
  39. #include "BtAbortOutstandingRequestEvent.h"
  40. #include "message.h"
  41. #include "Peer.h"
  42. #include "Piece.h"
  43. #include "BtContext.h"
  44. #include "PieceStorage.h"
  45. #include "BtMessageDispatcher.h"
  46. #include "BtMessageFactory.h"
  47. #include "StringFormat.h"
  48. namespace aria2 {
  49. BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, size_t dataLength) {
  50. if(dataLength != 13) {
  51. throw DlAbortEx
  52. (StringFormat(EX_INVALID_PAYLOAD_SIZE, "request", dataLength, 13).str());
  53. }
  54. uint8_t id = PeerMessageUtil::getId(data);
  55. if(id != ID) {
  56. throw DlAbortEx
  57. (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "request", ID).str());
  58. }
  59. BtRequestMessageHandle message(new BtRequestMessage());
  60. message->setIndex(PeerMessageUtil::getIntParam(data, 1));
  61. message->setBegin(PeerMessageUtil::getIntParam(data, 5));
  62. message->setLength(PeerMessageUtil::getIntParam(data, 9));
  63. return message;
  64. }
  65. void BtRequestMessage::doReceivedAction() {
  66. if(pieceStorage->hasPiece(index) &&
  67. (!peer->amChoking() ||
  68. (peer->amChoking() && peer->isInAmAllowedIndexSet(index)))) {
  69. BtMessageHandle msg = messageFactory->createPieceMessage(index,
  70. begin,
  71. length);
  72. dispatcher->addMessageToQueue(msg);
  73. } else {
  74. if(peer->isFastExtensionEnabled()) {
  75. BtMessageHandle msg = messageFactory->createRejectMessage(index,
  76. begin,
  77. length);
  78. dispatcher->addMessageToQueue(msg);
  79. }
  80. }
  81. }
  82. size_t BtRequestMessage::MESSAGE_LENGTH = 17;
  83. const unsigned char* BtRequestMessage::getMessage() {
  84. if(!msg) {
  85. /**
  86. * len --- 13, 4bytes
  87. * id --- 6, 1byte
  88. * index --- index, 4bytes
  89. * begin --- begin, 4bytes
  90. * length --- length, 4bytes
  91. * total: 17bytes
  92. */
  93. msg = new unsigned char[MESSAGE_LENGTH];
  94. PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
  95. PeerMessageUtil::setIntParam(&msg[5], index);
  96. PeerMessageUtil::setIntParam(&msg[9], begin);
  97. PeerMessageUtil::setIntParam(&msg[13], length);
  98. }
  99. return msg;
  100. }
  101. size_t BtRequestMessage::getMessageLength() {
  102. return MESSAGE_LENGTH;
  103. }
  104. std::string BtRequestMessage::toString() const {
  105. return "request index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
  106. ", length="+Util::uitos(length);
  107. }
  108. void BtRequestMessage::onQueued() {
  109. RequestSlot requestSlot(index, begin, length, blockIndex, pieceStorage->getPiece(index));
  110. dispatcher->addOutstandingRequest(requestSlot);
  111. }
  112. bool BtRequestMessage::BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) {
  113. BtAbortOutstandingRequestEvent* intEvent = dynamic_cast<BtAbortOutstandingRequestEvent*>(event.get());
  114. return intEvent != 0;
  115. }
  116. void BtRequestMessage::BtAbortOutstandingRequestEventListener::handleEventInternal(const BtEventHandle& event) {
  117. message->handleAbortOutstandingRequestEvent(event);
  118. }
  119. void BtRequestMessage::handleAbortOutstandingRequestEvent(const BtEventHandle& event) {
  120. BtAbortOutstandingRequestEvent* intEvent = (BtAbortOutstandingRequestEvent*)event.get();
  121. if(index == intEvent->getPiece()->getIndex() &&
  122. !invalidate &&
  123. !sendingInProgress) {
  124. invalidate = true;
  125. }
  126. }
  127. } // namespace aria2