/* */ #ifndef _D_BT_PIECE_MESSAGE_H_ #define _D_BT_PIECE_MESSAGE_H_ #include "AbstractBtMessage.h" namespace aria2 { class Piece; class BtPieceMessage; class DownloadContext; typedef SharedHandle BtPieceMessageHandle; class BtPieceMessage : public AbstractBtMessage { private: size_t index; uint32_t begin; uint32_t blockLength; unsigned char* block; unsigned char* msgHeader; SharedHandle _downloadContext; static size_t MESSAGE_HEADER_LENGTH; bool checkPieceHash(const SharedHandle& piece); void onNewPiece(const SharedHandle& piece); void onWrongPiece(const SharedHandle& piece); void erasePieceOnDisk(const SharedHandle& piece); size_t sendPieceData(off_t offset, size_t length) const; public: BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0) :AbstractBtMessage(ID, NAME), index(index), begin(begin), blockLength(blockLength), block(0), msgHeader(0) { uploading = true; } virtual ~BtPieceMessage() { delete [] msgHeader; delete [] block; } static const uint8_t ID = 7; static const std::string NAME; size_t getIndex() const { return index; } void setIndex(size_t index) { this->index = index; } uint32_t getBegin() const { return begin; } void setBegin(uint32_t begin) { this->begin = begin; } const unsigned char* getBlock() const { return block; } void setBlock(const unsigned char* block, size_t blockLength); size_t getBlockLength() const { return blockLength; } void setBlockLength(size_t blockLength) { this->blockLength = blockLength; } void setDownloadContext(const SharedHandle& downloadContext); static BtPieceMessageHandle create(const unsigned char* data, size_t dataLength); virtual void doReceivedAction(); const unsigned char* getMessageHeader(); size_t getMessageHeaderLength(); virtual void send(); virtual std::string toString() const; virtual void onChokingEvent(const BtChokingEvent& event); virtual void onCancelSendingPieceEvent (const BtCancelSendingPieceEvent& event); }; } // namespace aria2 #endif // _D_BT_PIECE_MESSAGE_H_