PieceMessage.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(!RequestSlot::isNull(slot) &&
  58. peerInteraction->hasDownloadPiece(slot.getIndex())) {
  59. peer->snubbing = false;
  60. //logger->debug("CUID#%d - Latency=%d", cuid, slot.getLatencyInMillis());
  61. peer->updateLatency(slot.getLatencyInMillis());
  62. Piece& piece = peerInteraction->getDownloadPiece(slot.getIndex());
  63. long long int offset =
  64. ((long long int)index)*torrentMan->pieceLength+begin;
  65. logger->debug("CUID#%d - Writing the block length=%d, offset=%lld",
  66. cuid, blockLength, offset);
  67. torrentMan->diskAdaptor->writeData(block,
  68. blockLength,
  69. offset);
  70. piece.completeBlock(slot.getBlockIndex());
  71. peerInteraction->deleteRequestSlot(slot);
  72. torrentMan->updatePiece(piece);
  73. logger->debug("CUID#%d - Setting piece block index=%d",
  74. cuid, slot.getBlockIndex());
  75. torrentMan->addDeltaDownloadLength(blockLength);
  76. if(piece.pieceComplete()) {
  77. if(checkPieceHash(piece)) {
  78. onGotNewPiece(piece);
  79. } else {
  80. onGotWrongPiece(piece);
  81. peerInteraction->abortPiece(piece);
  82. }
  83. }
  84. }
  85. }
  86. const char* PieceMessage::getMessageHeader() {
  87. if(!inProgress) {
  88. /**
  89. * len --- 9+blockLength, 4bytes
  90. * id --- 7, 1byte
  91. * index --- index, 4bytes
  92. * begin --- begin, 4bytes
  93. * total: 13bytes
  94. */
  95. PeerMessageUtil::createPeerMessageString(msgHeader, sizeof(msgHeader),
  96. 9+blockLength, ID);
  97. PeerMessageUtil::setIntParam(&msgHeader[5], index);
  98. PeerMessageUtil::setIntParam(&msgHeader[9], begin);
  99. }
  100. return msgHeader;
  101. }
  102. int PieceMessage::getMessageHeaderLength() {
  103. return sizeof(msgHeader);
  104. }
  105. void PieceMessage::send() {
  106. if(invalidate) {
  107. return;
  108. }
  109. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  110. PeerConnection* peerConnection = peerInteraction->getPeerConnection();
  111. if(!headerSent) {
  112. if(!inProgress) {
  113. logger->info(MSG_SEND_PEER_MESSAGE,
  114. cuid, peer->ipaddr.c_str(), peer->port,
  115. toString().c_str());
  116. getMessageHeader();
  117. leftDataLength = getMessageHeaderLength();
  118. inProgress = true;
  119. }
  120. int writtenLength
  121. = peerConnection->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
  122. leftDataLength);
  123. if(writtenLength == leftDataLength) {
  124. headerSent = true;
  125. leftDataLength = blockLength;
  126. } else {
  127. leftDataLength -= writtenLength;
  128. }
  129. }
  130. if(headerSent) {
  131. inProgress = false;
  132. int pieceLength = torrentMan->pieceLength;
  133. long long int pieceDataOffset =
  134. ((long long int)index)*pieceLength+begin+blockLength-leftDataLength;
  135. int writtenLength =
  136. sendPieceData(pieceDataOffset, leftDataLength);
  137. peer->addPeerDownload(writtenLength);
  138. torrentMan->addUploadLength(writtenLength);
  139. torrentMan->addDeltaUploadLength(writtenLength);
  140. if(writtenLength != leftDataLength) {
  141. inProgress = true;
  142. }
  143. leftDataLength -= writtenLength;
  144. }
  145. }
  146. int PieceMessage::sendPieceData(long long int offset, int length) const {
  147. int BUF_SIZE = 256;
  148. char buf[BUF_SIZE];
  149. int iteration = length/BUF_SIZE;
  150. int writtenLength = 0;
  151. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  152. PeerConnection* peerConnection = peerInteraction->getPeerConnection();
  153. for(int i = 0; i < iteration; i++) {
  154. if(torrentMan->diskAdaptor->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < BUF_SIZE) {
  155. throw new DlAbortEx("Failed to read data from disk.");
  156. }
  157. int ws = peerConnection->sendMessage(buf, BUF_SIZE);
  158. writtenLength += ws;
  159. if(ws != BUF_SIZE) {
  160. //logger->debug("CUID#%d - %d bytes written", cuid, writtenLength);
  161. return writtenLength;
  162. }
  163. }
  164. int rem = length%BUF_SIZE;
  165. if(rem > 0) {
  166. if(torrentMan->diskAdaptor->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
  167. throw new DlAbortEx("Failed to read data from disk.");
  168. }
  169. int ws = peerConnection->sendMessage(buf, rem);
  170. writtenLength += ws;
  171. }
  172. //logger->debug("CUID#%d - %d bytes written", cuid, writtenLength);
  173. return writtenLength;
  174. }
  175. void PieceMessage::check() const {
  176. PeerMessageUtil::checkIndex(index, pieces);
  177. PeerMessageUtil::checkBegin(begin, pieceLength);
  178. }
  179. string PieceMessage::toString() const {
  180. return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
  181. ", length="+Util::itos(blockLength);
  182. }
  183. bool PieceMessage::checkPieceHash(const Piece& piece) {
  184. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  185. long long int offset =
  186. ((long long int)piece.getIndex())*torrentMan->pieceLength;
  187. return torrentMan->diskAdaptor->sha1Sum(offset, piece.getLength()) ==
  188. torrentMan->getPieceHash(piece.getIndex());
  189. }
  190. void PieceMessage::onGotNewPiece(Piece& piece) {
  191. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  192. logger->info(MSG_GOT_NEW_PIECE, cuid, piece.getIndex());
  193. torrentMan->completePiece(piece);
  194. torrentMan->advertisePiece(cuid, piece.getIndex());
  195. }
  196. void PieceMessage::onGotWrongPiece(Piece& piece) {
  197. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  198. logger->error(MSG_GOT_WRONG_PIECE, cuid, piece.getIndex());
  199. erasePieceOnDisk(piece);
  200. piece.clearAllBlock();
  201. torrentMan->updatePiece(piece);
  202. }
  203. void PieceMessage::erasePieceOnDisk(const Piece& piece) {
  204. TorrentMan* torrentMan = peerInteraction->getTorrentMan();
  205. int BUFSIZE = 4096;
  206. char buf[BUFSIZE];
  207. memset(buf, 0, BUFSIZE);
  208. long long int offset = ((long long int)piece.getIndex())*torrentMan->pieceLength;
  209. for(int i = 0; i < piece.getLength()/BUFSIZE; i++) {
  210. torrentMan->diskAdaptor->writeData(buf, BUFSIZE, offset);
  211. offset += BUFSIZE;
  212. }
  213. int r = piece.getLength()%BUFSIZE;
  214. if(r > 0) {
  215. torrentMan->diskAdaptor->writeData(buf, r, offset);
  216. }
  217. }
  218. PeerMessageHandle PieceMessage::createRejectMessage(int index,
  219. int begin,
  220. int blockLength) const {
  221. return peerInteraction->getPeerMessageFactory()->
  222. createRejectMessage(index,
  223. begin,
  224. blockLength);
  225. }
  226. void PieceMessage::onChoked() {
  227. if(!invalidate &&
  228. !inProgress &&
  229. !peerInteraction->isInFastSet(index)) {
  230. logger->debug("CUID#%d - Reject piece message in queue because"
  231. " the peer has been choked. index=%d, begin=%d, length=%d",
  232. cuid,
  233. index,
  234. begin,
  235. blockLength);
  236. if(peer->isFastExtensionEnabled()) {
  237. peerInteraction->addMessage(createRejectMessage(index,
  238. begin,
  239. blockLength));
  240. }
  241. invalidate = true;
  242. }
  243. }
  244. void PieceMessage::onCanceled(int index, int begin, int blockLength) {
  245. if(!invalidate &&
  246. !inProgress &&
  247. this->index == index &&
  248. this->begin == begin &&
  249. this->blockLength == blockLength) {
  250. logger->debug("CUID#%d - Reject piece message in queue because cancel"
  251. " message received. index=%d, begin=%d, length=%d",
  252. cuid, index, begin, blockLength);
  253. if(peer->isFastExtensionEnabled()) {
  254. peerInteraction->addMessage(createRejectMessage(index,
  255. begin,
  256. blockLength));
  257. }
  258. invalidate = true;
  259. }
  260. }