PeerInteraction.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 "PeerInteraction.h"
  23. #include "LogFactory.h"
  24. #include "DlAbortEx.h"
  25. #include "KeepAliveMessage.h"
  26. #include "PeerMessageUtil.h"
  27. #include "Util.h"
  28. #include <netinet/in.h>
  29. PeerInteraction::PeerInteraction(int cuid,
  30. const PeerHandle& peer,
  31. const SocketHandle& socket,
  32. const Option* op,
  33. TorrentMan* torrentMan)
  34. :cuid(cuid),
  35. uploadLimit(0),
  36. torrentMan(torrentMan),
  37. peer(peer),
  38. quickReplied(false) {
  39. peerConnection = new PeerConnection(cuid, socket, op);
  40. logger = LogFactory::getInstance();
  41. }
  42. PeerInteraction::~PeerInteraction() {
  43. delete peerConnection;
  44. }
  45. bool PeerInteraction::isSendingMessageInProgress() const {
  46. if(messageQueue.size() > 0) {
  47. const PeerMessageHandle& peerMessage = messageQueue.front();
  48. if(peerMessage->isInProgress()) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. void PeerInteraction::sendMessages(int uploadSpeed) {
  55. MessageQueue tempQueue;
  56. while(messageQueue.size() > 0) {
  57. PeerMessageHandle msg = messageQueue.front();
  58. messageQueue.pop_front();
  59. if(uploadLimit != 0 && uploadLimit*1024 <= uploadSpeed &&
  60. msg->getId() == PieceMessage::ID && !msg->isInProgress()) {
  61. //!((PieceMessage*)msg)->isPendingCountMax()) {
  62. //((PieceMessage*)msg)->incrementPendingCount();
  63. tempQueue.push_back(msg);
  64. } else {
  65. msg->send();
  66. if(msg->isInProgress()) {
  67. messageQueue.push_front(msg);
  68. break;
  69. }
  70. }
  71. }
  72. copy(tempQueue.begin(), tempQueue.end(), back_inserter(messageQueue));
  73. }
  74. void PeerInteraction::addMessage(const PeerMessageHandle& peerMessage) {
  75. messageQueue.push_back(peerMessage);
  76. if(peerMessage->getId() == RequestMessage::ID) {
  77. RequestMessage* requestMessage = (RequestMessage*)peerMessage.get();
  78. RequestSlot requestSlot(requestMessage->getIndex(),
  79. requestMessage->getBegin(),
  80. requestMessage->getLength(),
  81. requestMessage->getBlockIndex());
  82. requestSlots.push_back(requestSlot);
  83. }
  84. }
  85. void PeerInteraction::rejectAllPieceMessageInQueue() {
  86. MessageQueue tempQueue;
  87. for(MessageQueue::iterator itr = messageQueue.begin();
  88. itr != messageQueue.end();) {
  89. // Don't delete piece message which is in the allowed fast set.
  90. if((*itr)->getId() == PieceMessage::ID && !(*itr)->isInProgress()
  91. && !isInFastSet(((PieceMessage*)(*itr).get())->getIndex())) {
  92. PieceMessage* pieceMessage = (PieceMessage*)(*itr).get();
  93. logger->debug("CUID#%d - Reject piece message in queue because"
  94. " peer has been choked. index=%d, begin=%d, length=%d",
  95. cuid,
  96. pieceMessage->getIndex(),
  97. pieceMessage->getBegin(),
  98. pieceMessage->getBlockLength());
  99. if(peer->isFastExtensionEnabled()) {
  100. tempQueue.push_back(createRejectMessage(pieceMessage->getIndex(),
  101. pieceMessage->getBegin(),
  102. pieceMessage->getBlockLength()));
  103. }
  104. itr = messageQueue.erase(itr);
  105. } else {
  106. itr++;
  107. }
  108. }
  109. copy(tempQueue.begin(), tempQueue.end(), back_inserter(messageQueue));
  110. }
  111. void PeerInteraction::rejectPieceMessageInQueue(int index, int begin, int length) {
  112. MessageQueue tempQueue;
  113. for(MessageQueue::iterator itr = messageQueue.begin();
  114. itr != messageQueue.end();) {
  115. if((*itr)->getId() == PieceMessage::ID && !(*itr)->isInProgress()) {
  116. PieceMessage* pieceMessage = (PieceMessage*)(*itr).get();
  117. if(pieceMessage->getIndex() == index &&
  118. pieceMessage->getBegin() == begin &&
  119. pieceMessage->getBlockLength() == length) {
  120. logger->debug("CUID#%d - Reject piece message in queue because cancel"
  121. " message received. index=%d, begin=%d, length=%d",
  122. cuid, index, begin, length);
  123. itr = messageQueue.erase(itr);
  124. if(peer->isFastExtensionEnabled()) {
  125. tempQueue.push_back(createRejectMessage(index, begin, length));
  126. }
  127. } else {
  128. itr++;
  129. }
  130. } else {
  131. itr++;
  132. }
  133. }
  134. copy(tempQueue.begin(), tempQueue.end(), back_inserter(messageQueue));
  135. }
  136. void PeerInteraction::onChoked() {
  137. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end();) {
  138. Piece& piece = *itr;
  139. if(!peer->isInFastSet(piece.getIndex())) {
  140. abortPiece(piece);
  141. itr = pieces.erase(itr);
  142. } else {
  143. itr++;
  144. }
  145. }
  146. }
  147. void PeerInteraction::abortAllPieces() {
  148. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end();) {
  149. abortPiece(*itr);
  150. itr = pieces.erase(itr);
  151. }
  152. }
  153. void PeerInteraction::abortPiece(Piece& piece) {
  154. if(!Piece::isNull(piece)) {
  155. for(MessageQueue::iterator itr = messageQueue.begin();
  156. itr != messageQueue.end();) {
  157. if((*itr)->getId() == RequestMessage::ID &&
  158. !(*itr)->isInProgress() &&
  159. ((RequestMessage*)(*itr).get())->getIndex() == piece.getIndex()) {
  160. itr = messageQueue.erase(itr);
  161. } else {
  162. itr++;
  163. }
  164. }
  165. for(RequestSlots::iterator itr = requestSlots.begin();
  166. itr != requestSlots.end();) {
  167. if(itr->getIndex() == piece.getIndex()) {
  168. logger->debug("CUID#%d - Deleting request slot blockIndex=%d"
  169. " because piece was canceled",
  170. cuid,
  171. itr->getBlockIndex());
  172. piece.cancelBlock(itr->getBlockIndex());
  173. itr = requestSlots.erase(itr);
  174. } else {
  175. itr++;
  176. }
  177. }
  178. torrentMan->cancelPiece(piece);
  179. }
  180. }
  181. void PeerInteraction::deleteRequestSlot(const RequestSlot& requestSlot) {
  182. RequestSlots::iterator itr = find(requestSlots.begin(), requestSlots.end(),
  183. requestSlot);
  184. if(itr != requestSlots.end()) {
  185. requestSlots.erase(itr);
  186. }
  187. }
  188. void PeerInteraction::checkRequestSlot() {
  189. for(RequestSlots::iterator itr = requestSlots.begin();
  190. itr != requestSlots.end();) {
  191. RequestSlot& slot = *itr;
  192. if(slot.isTimeout(REQUEST_TIME_OUT)) {
  193. logger->debug("CUID#%d - Deleting request slot blockIndex=%d"
  194. " because of time out",
  195. cuid,
  196. slot.getBlockIndex());
  197. Piece& piece = getDownloadPiece(slot.getIndex());
  198. piece.cancelBlock(slot.getBlockIndex());
  199. itr = requestSlots.erase(itr);
  200. peer->snubbing = true;
  201. } else {
  202. Piece piece = getDownloadPiece(slot.getIndex());
  203. if(piece.hasBlock(slot.getBlockIndex()) ||
  204. torrentMan->hasPiece(piece.getIndex())) {
  205. logger->debug("CUID#%d - Deleting request slot blockIndex=%d because"
  206. " the block has been acquired.", cuid,
  207. slot.getBlockIndex());
  208. addMessage(createCancelMessage(slot.getIndex(),
  209. slot.getBegin(),
  210. slot.getLength()));
  211. itr = requestSlots.erase(itr);
  212. } else {
  213. itr++;
  214. }
  215. }
  216. }
  217. updatePiece();
  218. }
  219. bool PeerInteraction::isInRequestSlot(int index, int blockIndex) const {
  220. for(RequestSlots::const_iterator itr = requestSlots.begin();
  221. itr != requestSlots.end(); itr++) {
  222. const RequestSlot& slot = *itr;
  223. if(slot.getIndex() == index && slot.getBlockIndex() == blockIndex) {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. RequestSlot PeerInteraction::getCorrespondingRequestSlot(int index,
  230. int begin,
  231. int length) const {
  232. for(RequestSlots::const_iterator itr = requestSlots.begin();
  233. itr != requestSlots.end(); itr++) {
  234. const RequestSlot& slot = *itr;
  235. if(slot.getIndex() == index &&
  236. slot.getBegin() == begin &&
  237. slot.getLength() == length) {
  238. return slot;
  239. }
  240. }
  241. return RequestSlot::nullSlot;
  242. }
  243. int PeerInteraction::countMessageInQueue() const {
  244. return messageQueue.size();
  245. }
  246. int PeerInteraction::countRequestSlot() const {
  247. return requestSlots.size();
  248. }
  249. HandshakeMessageHandle PeerInteraction::receiveHandshake(bool quickReply) {
  250. char msg[HANDSHAKE_MESSAGE_LENGTH];
  251. int msgLength = HANDSHAKE_MESSAGE_LENGTH;
  252. bool retval = peerConnection->receiveHandshake(msg, msgLength);
  253. // To handle tracker's NAT-checking feature
  254. if(!quickReplied && quickReply && msgLength >= 48) {
  255. quickReplied = true;
  256. // check info_hash
  257. if(memcmp(torrentMan->getInfoHash(), &msg[28], INFO_HASH_LENGTH) == 0) {
  258. sendHandshake();
  259. }
  260. }
  261. if(!retval) {
  262. return NULL;
  263. }
  264. HandshakeMessageHandle handshakeMessage(createHandshakeMessage(msg, msgLength));
  265. handshakeMessage->check();
  266. if(handshakeMessage->isFastExtensionSupported()) {
  267. peer->setFastExtensionEnabled(true);
  268. logger->info("CUID#%d - Fast extension enabled.", cuid);
  269. }
  270. return handshakeMessage;
  271. }
  272. HandshakeMessageHandle PeerInteraction::createHandshakeMessage(const char* msg, int msgLength) {
  273. HandshakeMessage* message = HandshakeMessage::create(msg, msgLength);
  274. setPeerMessageCommonProperty(message);
  275. return message;
  276. }
  277. PeerMessageHandle PeerInteraction::receiveMessage() {
  278. char msg[MAX_PAYLOAD_LEN];
  279. int msgLength = 0;
  280. if(!peerConnection->receiveMessage(msg, msgLength)) {
  281. return NULL;
  282. }
  283. PeerMessageHandle peerMessage(createPeerMessage(msg, msgLength));
  284. peerMessage->check();
  285. return peerMessage;
  286. }
  287. PeerMessageHandle PeerInteraction::createPeerMessage(const char* msg, int msgLength) {
  288. PeerMessage* peerMessage;
  289. if(msgLength == 0) {
  290. // keep-alive
  291. peerMessage = new KeepAliveMessage();
  292. } else {
  293. int id = PeerMessageUtil::getId(msg);
  294. switch(id) {
  295. case ChokeMessage::ID:
  296. peerMessage = ChokeMessage::create(msg, msgLength);
  297. break;
  298. case UnchokeMessage::ID:
  299. peerMessage = UnchokeMessage::create(msg, msgLength);
  300. break;
  301. case InterestedMessage::ID:
  302. peerMessage = InterestedMessage::create(msg, msgLength);
  303. break;
  304. case NotInterestedMessage::ID:
  305. peerMessage = NotInterestedMessage::create(msg, msgLength);
  306. break;
  307. case HaveMessage::ID:
  308. peerMessage = HaveMessage::create(msg, msgLength);
  309. ((HaveMessage*)peerMessage)->setPieces(torrentMan->pieces);
  310. break;
  311. case BitfieldMessage::ID:
  312. peerMessage = BitfieldMessage::create(msg, msgLength);
  313. ((BitfieldMessage*)peerMessage)->setPieces(torrentMan->pieces);
  314. break;
  315. case RequestMessage::ID:
  316. peerMessage = RequestMessage::create(msg, msgLength);
  317. ((RequestMessage*)peerMessage)->setPieces(torrentMan->pieces);
  318. ((RequestMessage*)peerMessage)->setPieceLength(torrentMan->getPieceLength(((RequestMessage*)peerMessage)->getIndex()));
  319. break;
  320. case CancelMessage::ID:
  321. peerMessage = CancelMessage::create(msg, msgLength);
  322. ((CancelMessage*)peerMessage)->setPieces(torrentMan->pieces);
  323. ((CancelMessage*)peerMessage)->setPieceLength(torrentMan->getPieceLength(((CancelMessage*)peerMessage)->getIndex()));
  324. break;
  325. case PieceMessage::ID:
  326. peerMessage = PieceMessage::create(msg, msgLength);
  327. ((PieceMessage*)peerMessage)->setPieces(torrentMan->pieces);
  328. ((PieceMessage*)peerMessage)->setPieceLength(torrentMan->getPieceLength(((PieceMessage*)peerMessage)->getIndex()));
  329. break;
  330. case PortMessage::ID:
  331. peerMessage = PortMessage::create(msg, msgLength);
  332. break;
  333. case HaveAllMessage::ID:
  334. peerMessage = HaveAllMessage::create(msg, msgLength);
  335. break;
  336. case HaveNoneMessage::ID:
  337. peerMessage = HaveNoneMessage::create(msg, msgLength);
  338. break;
  339. case RejectMessage::ID:
  340. peerMessage = RejectMessage::create(msg, msgLength);
  341. ((RejectMessage*)peerMessage)->setPieces(torrentMan->pieces);
  342. ((RejectMessage*)peerMessage)->setPieceLength(torrentMan->getPieceLength(((RejectMessage*)peerMessage)->getIndex()));
  343. break;
  344. case SuggestPieceMessage::ID:
  345. peerMessage = SuggestPieceMessage::create(msg, msgLength);
  346. ((SuggestPieceMessage*)peerMessage)->setPieces(torrentMan->pieces);
  347. break;
  348. case AllowedFastMessage::ID:
  349. peerMessage = AllowedFastMessage::create(msg, msgLength);
  350. ((AllowedFastMessage*)peerMessage)->setPieces(torrentMan->pieces);
  351. break;
  352. default:
  353. throw new DlAbortEx("Invalid message id. id = %d", id);
  354. }
  355. }
  356. setPeerMessageCommonProperty(peerMessage);
  357. return peerMessage;
  358. }
  359. void PeerInteraction::syncPiece() {
  360. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  361. torrentMan->syncPiece(*itr);
  362. }
  363. }
  364. void PeerInteraction::updatePiece() {
  365. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  366. torrentMan->updatePiece(*itr);
  367. }
  368. }
  369. void PeerInteraction::getNewPieceAndSendInterest(int pieceNum) {
  370. if(pieces.empty() && !torrentMan->hasMissingPiece(peer)) {
  371. if(peer->amInterested) {
  372. logger->debug("CUID#%d - Not interested in the peer", cuid);
  373. addMessage(createNotInterestedMessage());
  374. }
  375. } else {
  376. if(peer->peerChoking) {
  377. onChoked();
  378. if(peer->isFastExtensionEnabled()) {
  379. while((int)pieces.size() < pieceNum) {
  380. Piece piece = torrentMan->getMissingFastPiece(peer);
  381. if(Piece::isNull(piece)) {
  382. break;
  383. } else {
  384. pieces.push_back(piece);
  385. }
  386. }
  387. }
  388. } else {
  389. while((int)pieces.size() < pieceNum) {
  390. Piece piece = torrentMan->getMissingPiece(peer);
  391. if(Piece::isNull(piece)) {
  392. break;
  393. } else {
  394. pieces.push_back(piece);
  395. }
  396. }
  397. }
  398. if(!peer->amInterested) {
  399. logger->debug("CUID#%d - Interested in the peer", cuid);
  400. addMessage(createInterestedMessage());
  401. }
  402. }
  403. }
  404. void PeerInteraction::addRequests() {
  405. // Abort downloading of completed piece.
  406. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end();) {
  407. Piece& piece = *itr;
  408. if(piece.pieceComplete()) {
  409. abortPiece(piece);
  410. itr = pieces.erase(itr);
  411. } else {
  412. itr++;
  413. }
  414. }
  415. int MAX_PENDING_REQUEST;
  416. if(peer->getLatency() < 500) {
  417. MAX_PENDING_REQUEST = 24;
  418. } else if(peer->getLatency() < 1500) {
  419. MAX_PENDING_REQUEST = 12;
  420. } else {
  421. MAX_PENDING_REQUEST = 6;
  422. }
  423. int pieceNum;
  424. if(torrentMan->isEndGame()) {
  425. pieceNum = 1;
  426. } else {
  427. int blocks = DIV_FLOOR(torrentMan->pieceLength, BLOCK_LENGTH);
  428. pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
  429. }
  430. getNewPieceAndSendInterest(pieceNum);
  431. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  432. Piece& piece = *itr;
  433. if(torrentMan->isEndGame()) {
  434. BlockIndexes missingBlockIndexes = piece.getAllMissingBlockIndexes();
  435. random_shuffle(missingBlockIndexes.begin(), missingBlockIndexes.end());
  436. int count = countRequestSlot();
  437. for(BlockIndexes::const_iterator bitr = missingBlockIndexes.begin();
  438. bitr != missingBlockIndexes.end() && count < MAX_PENDING_REQUEST;
  439. bitr++) {
  440. int blockIndex = *bitr;
  441. if(!isInRequestSlot(piece.getIndex(), blockIndex)) {
  442. addMessage(createRequestMessage(piece.getIndex(), blockIndex));
  443. count++;
  444. }
  445. }
  446. } else {
  447. while(countRequestSlot() < MAX_PENDING_REQUEST) {
  448. int blockIndex = piece.getMissingUnusedBlockIndex();
  449. if(blockIndex == -1) {
  450. break;
  451. }
  452. addMessage(createRequestMessage(piece.getIndex(), blockIndex));
  453. }
  454. }
  455. if(countRequestSlot() >= MAX_PENDING_REQUEST) {
  456. break;
  457. }
  458. }
  459. updatePiece();
  460. }
  461. void PeerInteraction::sendHandshake() {
  462. HandshakeMessage* handshake = new HandshakeMessage();
  463. memcpy(handshake->infoHash, torrentMan->getInfoHash(), INFO_HASH_LENGTH);
  464. memcpy(handshake->peerId, torrentMan->peerId.c_str(), PEER_ID_LENGTH);
  465. setPeerMessageCommonProperty(handshake);
  466. addMessage(handshake);
  467. sendMessages(0);
  468. }
  469. void PeerInteraction::sendBitfield() {
  470. if(peer->isFastExtensionEnabled()) {
  471. if(torrentMan->hasAllPieces()) {
  472. addMessage(createHaveAllMessage());
  473. } else if(torrentMan->getDownloadLength() > 0) {
  474. addMessage(createBitfieldMessage());
  475. } else {
  476. addMessage(createHaveNoneMessage());
  477. }
  478. } else {
  479. if(torrentMan->getDownloadLength() > 0) {
  480. addMessage(createBitfieldMessage());
  481. }
  482. }
  483. sendMessages(0);
  484. }
  485. void PeerInteraction::sendAllowedFast() {
  486. if(peer->isFastExtensionEnabled()) {
  487. Integers fastSet = Util::computeFastSet(peer->ipaddr, torrentMan->getInfoHash(),
  488. torrentMan->pieces, ALLOWED_FAST_SET_SIZE);
  489. for(Integers::const_iterator itr = fastSet.begin();
  490. itr != fastSet.end(); itr++) {
  491. addMessage(createAllowedFastMessage(*itr));
  492. }
  493. }
  494. }
  495. Piece& PeerInteraction::getDownloadPiece(int index) {
  496. for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  497. if(itr->getIndex() == index) {
  498. return *itr;
  499. }
  500. }
  501. throw new DlAbortEx("No such piece index=%d", index);
  502. }
  503. bool PeerInteraction::hasDownloadPiece(int index) const {
  504. for(Pieces::const_iterator itr = pieces.begin(); itr != pieces.end(); itr++) {
  505. if(itr->getIndex() == index) {
  506. return true;
  507. }
  508. }
  509. return false;
  510. }
  511. bool PeerInteraction::isInFastSet(int index) const {
  512. return find(fastSet.begin(), fastSet.end(), index) != fastSet.end();
  513. }
  514. void PeerInteraction::addFastSetIndex(int index) {
  515. if(!isInFastSet(index)) {
  516. fastSet.push_back(index);
  517. }
  518. }
  519. void PeerInteraction::setPeerMessageCommonProperty(PeerMessage* peerMessage) {
  520. peerMessage->setPeer(peer);
  521. peerMessage->setCuid(cuid);
  522. peerMessage->setPeerInteraction(this);
  523. }
  524. RequestMessage* PeerInteraction::createRequestMessage(int index, int blockIndex) {
  525. RequestMessage* msg = new RequestMessage();
  526. Piece piece = getDownloadPiece(index);
  527. msg->setIndex(piece.getIndex());
  528. msg->setBegin(blockIndex*piece.getBlockLength());
  529. msg->setLength(piece.getBlockLength(blockIndex));
  530. msg->setBlockIndex(blockIndex);
  531. setPeerMessageCommonProperty(msg);
  532. return msg;
  533. }
  534. CancelMessage* PeerInteraction::createCancelMessage(int index, int begin, int length) {
  535. CancelMessage* msg = new CancelMessage();
  536. msg->setIndex(index);
  537. msg->setBegin(begin);
  538. msg->setLength(length);
  539. setPeerMessageCommonProperty(msg);
  540. return msg;
  541. }
  542. PieceMessage* PeerInteraction::createPieceMessage(int index, int begin, int length) {
  543. PieceMessage* msg = new PieceMessage();
  544. msg->setIndex(index);
  545. msg->setBegin(begin);
  546. msg->setBlockLength(length);
  547. setPeerMessageCommonProperty(msg);
  548. return msg;
  549. }
  550. HaveMessage* PeerInteraction::createHaveMessage(int index) {
  551. HaveMessage* msg = new HaveMessage();
  552. msg->setIndex(index);
  553. setPeerMessageCommonProperty(msg);
  554. return msg;
  555. }
  556. ChokeMessage* PeerInteraction::createChokeMessage() {
  557. ChokeMessage* msg = new ChokeMessage();
  558. setPeerMessageCommonProperty(msg);
  559. return msg;
  560. }
  561. UnchokeMessage* PeerInteraction::createUnchokeMessage() {
  562. UnchokeMessage* msg = new UnchokeMessage();
  563. setPeerMessageCommonProperty(msg);
  564. return msg;
  565. }
  566. InterestedMessage* PeerInteraction::createInterestedMessage() {
  567. InterestedMessage* msg = new InterestedMessage();
  568. setPeerMessageCommonProperty(msg);
  569. return msg;
  570. }
  571. NotInterestedMessage* PeerInteraction::createNotInterestedMessage() {
  572. NotInterestedMessage* msg = new NotInterestedMessage();
  573. setPeerMessageCommonProperty(msg);
  574. return msg;
  575. }
  576. BitfieldMessage* PeerInteraction::createBitfieldMessage() {
  577. BitfieldMessage* msg = new BitfieldMessage();
  578. msg->setBitfield(getTorrentMan()->getBitfield(),
  579. getTorrentMan()->getBitfieldLength());
  580. setPeerMessageCommonProperty(msg);
  581. return msg;
  582. }
  583. KeepAliveMessage* PeerInteraction::createKeepAliveMessage() {
  584. KeepAliveMessage* msg = new KeepAliveMessage();
  585. setPeerMessageCommonProperty(msg);
  586. return msg;
  587. }
  588. HaveAllMessage* PeerInteraction::createHaveAllMessage() {
  589. HaveAllMessage* msg = new HaveAllMessage();
  590. setPeerMessageCommonProperty(msg);
  591. return msg;
  592. }
  593. HaveNoneMessage* PeerInteraction::createHaveNoneMessage() {
  594. HaveNoneMessage* msg = new HaveNoneMessage();
  595. setPeerMessageCommonProperty(msg);
  596. return msg;
  597. }
  598. RejectMessage* PeerInteraction::createRejectMessage(int index, int begin, int length) {
  599. RejectMessage* msg = new RejectMessage();
  600. msg->setIndex(index);
  601. msg->setBegin(begin);
  602. msg->setLength(length);
  603. setPeerMessageCommonProperty(msg);
  604. return msg;
  605. }
  606. AllowedFastMessage* PeerInteraction::createAllowedFastMessage(int index) {
  607. AllowedFastMessage* msg = new AllowedFastMessage();
  608. msg->setIndex(index);
  609. setPeerMessageCommonProperty(msg);
  610. return msg;
  611. }