PieceMessage.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "PieceMessage.h"
  23. #include "PeerMessageUtil.h"
  24. #include "PeerInteraction.h"
  25. #include "Util.h"
  26. #include "message.h"
  27. #include "DlAbortEx.h"
  28. void PieceMessage::setBlock(const char* block, int blockLength) {
  29. if(this->block != NULL) {
  30. delete [] this->block;
  31. }
  32. this->blockLength = blockLength;
  33. this->block = new char[this->blockLength];
  34. memcpy(this->block, block, this->blockLength);
  35. }
  36. PieceMessage* PieceMessage::create(const char* data, int dataLength) {
  37. if(dataLength <= 9) {
  38. throw new DlAbortEx("invalid payload size for %s, size = %d. It should be greater than %d", "piece", dataLength, 9);
  39. }
  40. int id = PeerMessageUtil::getId(data);
  41. if(id != ID) {
  42. throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
  43. id, "piece", ID);
  44. }
  45. PieceMessage* pieceMessage = new PieceMessage();
  46. pieceMessage->setIndex(PeerMessageUtil::getIntParam(data, 1));
  47. pieceMessage->setBegin(PeerMessageUtil::getIntParam(data, 5));
  48. pieceMessage->setBlock(data+9, dataLength-9);
  49. return pieceMessage;
  50. }
  51. void PieceMessage::receivedAction() {
  52. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  53. RequestSlot slot = peerInteraction->getCorrespondingRequestSlot(index,
  54. begin,
  55. blockLength);
  56. peer->addPeerUpload(blockLength);
  57. if(peerInteraction->hasDownloadPiece() &&
  58. !RequestSlot::isNull(slot)) {
  59. Piece& piece = peerInteraction->getDownloadPiece();
  60. long long int offset =
  61. ((long long int)index)*torrentMan->pieceLength+begin;
  62. logger->debug("CUID#%d - Writing the block length=%d, offset=%lld",
  63. cuid, blockLength, offset);
  64. torrentMan->diskAdaptor->writeData(block,
  65. blockLength,
  66. offset);
  67. piece.completeBlock(slot.getBlockIndex());
  68. peerInteraction->deleteRequestSlot(slot);
  69. torrentMan->updatePiece(piece);
  70. logger->debug("CUID#%d - Setting piece block index=%d",
  71. cuid, slot.getBlockIndex());
  72. torrentMan->addDeltaDownloadLength(blockLength);
  73. if(piece.pieceComplete()) {
  74. if(checkPieceHash(piece)) {
  75. onGotNewPiece(piece);
  76. } else {
  77. onGotWrongPiece(piece);
  78. }
  79. }
  80. }
  81. }
  82. const char* PieceMessage::getMessageHeader() {
  83. if(!inProgress) {
  84. /**
  85. * len --- 9+blockLength, 4bytes
  86. * id --- 7, 1byte
  87. * index --- index, 4bytes
  88. * begin --- begin, 4bytes
  89. * total: 13bytes
  90. */
  91. PeerMessageUtil::createPeerMessageString(msgHeader, sizeof(msgHeader),
  92. 9+blockLength, ID);
  93. PeerMessageUtil::setIntParam(&msgHeader[5], index);
  94. PeerMessageUtil::setIntParam(&msgHeader[9], begin);
  95. }
  96. return msgHeader;
  97. }
  98. int PieceMessage::getMessageHeaderLength() {
  99. return sizeof(msgHeader);
  100. }
  101. void PieceMessage::send() {
  102. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  103. PeerConnection* peerConnection = peerInteraction->getPeerConnection();
  104. if(!headerSent) {
  105. if(!inProgress) {
  106. logger->info(MSG_SEND_PEER_MESSAGE,
  107. cuid, peer->ipaddr.c_str(), peer->port,
  108. toString().c_str());
  109. getMessageHeader();
  110. leftDataLength = getMessageHeaderLength();
  111. inProgress = true;
  112. }
  113. int writtenLength
  114. = peerConnection->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
  115. leftDataLength);
  116. if(writtenLength == leftDataLength) {
  117. headerSent = true;
  118. leftDataLength = blockLength;
  119. } else {
  120. leftDataLength -= writtenLength;
  121. }
  122. }
  123. if(headerSent) {
  124. inProgress = false;
  125. int pieceLength = torrentMan->pieceLength;
  126. long long int pieceDataOffset =
  127. ((long long int)index)*pieceLength+begin+blockLength-leftDataLength;
  128. int writtenLength =
  129. sendPieceData(pieceDataOffset, leftDataLength);
  130. peer->addPeerDownload(writtenLength);
  131. torrentMan->addUploadLength(writtenLength);
  132. torrentMan->addDeltaUploadLength(writtenLength);
  133. if(writtenLength != leftDataLength) {
  134. inProgress = true;
  135. }
  136. leftDataLength -= writtenLength;
  137. }
  138. }
  139. int PieceMessage::sendPieceData(long long int offset, int length) const {
  140. int BUF_SIZE = 256;
  141. char buf[BUF_SIZE];
  142. int iteration = length/BUF_SIZE;
  143. int writtenLength = 0;
  144. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  145. PeerConnection* peerConnection = peerInteraction->getPeerConnection();
  146. for(int i = 0; i < iteration; i++) {
  147. if(torrentMan->diskAdaptor->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < BUF_SIZE) {
  148. throw new DlAbortEx("Failed to read data from disk.");
  149. }
  150. int ws = peerConnection->sendMessage(buf, BUF_SIZE);
  151. writtenLength += ws;
  152. if(ws != BUF_SIZE) {
  153. //logger->debug("CUID#%d - %d bytes written", cuid, writtenLength);
  154. return writtenLength;
  155. }
  156. }
  157. int rem = length%BUF_SIZE;
  158. if(rem > 0) {
  159. if(torrentMan->diskAdaptor->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
  160. throw new DlAbortEx("Failed to read data from disk.");
  161. }
  162. int ws = peerConnection->sendMessage(buf, rem);
  163. writtenLength += ws;
  164. }
  165. //logger->debug("CUID#%d - %d bytes written", cuid, writtenLength);
  166. return writtenLength;
  167. }
  168. void PieceMessage::check() const {
  169. PeerMessageUtil::checkIndex(index, pieces);
  170. PeerMessageUtil::checkBegin(begin, pieceLength);
  171. }
  172. string PieceMessage::toString() const {
  173. return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
  174. ", length="+Util::itos(blockLength);
  175. }
  176. bool PieceMessage::checkPieceHash(const Piece& piece) {
  177. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  178. long long int offset =
  179. ((long long int)piece.getIndex())*torrentMan->pieceLength;
  180. return torrentMan->diskAdaptor->sha1Sum(offset, piece.getLength()) ==
  181. torrentMan->getPieceHash(piece.getIndex());
  182. }
  183. void PieceMessage::onGotNewPiece(Piece& piece) {
  184. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  185. logger->info(MSG_GOT_NEW_PIECE, cuid, piece.getIndex());
  186. torrentMan->completePiece(piece);
  187. torrentMan->advertisePiece(cuid, piece.getIndex());
  188. piece = Piece::nullPiece;
  189. }
  190. void PieceMessage::onGotWrongPiece(Piece& piece) {
  191. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  192. logger->error(MSG_GOT_WRONG_PIECE, cuid, piece.getIndex());
  193. erasePieceOnDisk(piece);
  194. piece.clearAllBlock();
  195. torrentMan->updatePiece(piece);
  196. }
  197. void PieceMessage::erasePieceOnDisk(const Piece& piece) {
  198. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  199. int BUFSIZE = 4096;
  200. char buf[BUFSIZE];
  201. memset(buf, 0, BUFSIZE);
  202. long long int offset = ((long long int)piece.getIndex())*torrentMan->pieceLength;
  203. for(int i = 0; i < piece.getLength()/BUFSIZE; i++) {
  204. torrentMan->diskAdaptor->writeData(buf, BUFSIZE, offset);
  205. offset += BUFSIZE;
  206. }
  207. int r = piece.getLength()%BUFSIZE;
  208. if(r > 0) {
  209. torrentMan->diskAdaptor->writeData(buf, r, offset);
  210. }
  211. }