CancelMessage.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "CancelMessage.h"
  23. #include "PeerInteraction.h"
  24. #include "PeerMessageUtil.h"
  25. #include "Util.h"
  26. #include "DlAbortEx.h"
  27. CancelMessage* CancelMessage::create(const char* data, int dataLength) {
  28. if(dataLength != 13) {
  29. throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "cancel", dataLength, 13);
  30. }
  31. int id = PeerMessageUtil::getId(data);
  32. if(id != ID) {
  33. throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
  34. id, "cancel", ID);
  35. }
  36. CancelMessage* cancelMessage = new CancelMessage();
  37. cancelMessage->setIndex(PeerMessageUtil::getIntParam(data, 1));
  38. cancelMessage->setBegin(PeerMessageUtil::getIntParam(data, 5));
  39. cancelMessage->setLength(PeerMessageUtil::getIntParam(data, 9));
  40. return cancelMessage;
  41. }
  42. void CancelMessage::receivedAction() {
  43. peerInteraction->rejectPieceMessageInQueue(index, begin, length);
  44. }
  45. const char* CancelMessage::getMessage() {
  46. if(!inProgress) {
  47. /**
  48. * len --- 13, 4bytes
  49. * id --- 8, 1byte
  50. * index --- index, 4bytes
  51. * begin --- begin, 4bytes
  52. * length -- length, 4bytes
  53. * total: 17bytes
  54. */
  55. PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 13, ID);
  56. PeerMessageUtil::setIntParam(&msg[5], index);
  57. PeerMessageUtil::setIntParam(&msg[9], begin);
  58. PeerMessageUtil::setIntParam(&msg[13], length);
  59. }
  60. return msg;
  61. }
  62. int CancelMessage::getMessageLength() {
  63. return sizeof(msg);
  64. }
  65. void CancelMessage::check() const {
  66. PeerMessageUtil::checkIndex(index, pieces);
  67. PeerMessageUtil::checkBegin(begin, pieceLength);
  68. PeerMessageUtil::checkLength(length);
  69. PeerMessageUtil::checkRange(begin, length, pieceLength);
  70. }
  71. string CancelMessage::toString() const {
  72. return "cancel index="+Util::itos(index)+", begin="+Util::itos(begin)+
  73. ", length="+Util::itos(length);
  74. }