PieceMessage.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef _D_PIECE_MESSAGE_H_
  23. #define _D_PIECE_MESSAGE_H_
  24. #include "PeerMessage.h"
  25. #include "TorrentMan.h"
  26. class PieceMessage : public PeerMessage {
  27. private:
  28. int index;
  29. int begin;
  30. char* block;
  31. int blockLength;
  32. int leftDataLength;
  33. bool headerSent;
  34. int pendingCount;
  35. // for check
  36. int pieces;
  37. int pieceLength;
  38. char msgHeader[13];
  39. bool checkPieceHash(const Piece& piece);
  40. void onGotNewPiece(Piece& piece);
  41. void onGotWrongPiece(Piece& piece);
  42. void erasePieceOnDisk(const Piece& piece);
  43. int sendPieceData(long long int offset, int length) const;
  44. PeerMessageHandle createRejectMessage(int index, int begin, int blockLength) const;
  45. public:
  46. PieceMessage(int index = 0, int begin = 0, int blockLength = 0)
  47. :PeerMessage(),
  48. index(index),
  49. begin(begin),
  50. block(0),
  51. blockLength(blockLength),
  52. leftDataLength(0),
  53. headerSent(false),
  54. pendingCount(0),
  55. pieces(0),
  56. pieceLength(0)
  57. {
  58. uploading = true;
  59. }
  60. virtual ~PieceMessage() {
  61. if(block != NULL) {
  62. delete [] block;
  63. }
  64. }
  65. enum ID_t {
  66. ID = 7
  67. };
  68. int getIndex() const { return index; }
  69. void setIndex(int index) { this->index = index; }
  70. int getBegin() const { return begin; }
  71. void setBegin(int begin) { this->begin = begin; }
  72. const char* getBlock() const { return block; }
  73. void setBlock(const char* block, int blockLength);
  74. int getBlockLength() const { return blockLength; }
  75. void setBlockLength(int blockLength) { this->blockLength = blockLength; }
  76. void setPieces(int pieces) {
  77. this->pieces = pieces;
  78. }
  79. int getPieces() const { return pieces;}
  80. void setPieceLength(int pieceLength) {
  81. this->pieceLength = pieceLength;
  82. }
  83. int getPieceLength() const { return pieceLength;}
  84. void incrementPendingCount() { pendingCount++; }
  85. bool isPendingCountMax() const { return pendingCount > 2; }
  86. static PieceMessage* create(const char* data, int dataLength);
  87. virtual int getId() const { return ID; }
  88. virtual void receivedAction();
  89. virtual const char* getMessageHeader();
  90. virtual int getMessageHeaderLength();
  91. virtual void send();
  92. virtual void check() const;
  93. virtual string toString() const;
  94. virtual void onChoked();
  95. virtual void onCanceled(int index, int begin, int blockLength);
  96. };
  97. #endif // _D_PIECE_MESSAGE_H_