/* */ #include "BtSuggestPieceMessage.h" #include "PeerMessageUtil.h" #include "Util.h" #include "DlAbortEx.h" BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, uint32_t dataLength) { if(dataLength != 5) { throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "suggest piece", dataLength, 5); } uint8_t id = PeerMessageUtil::getId(data); if(id != ID) { throw new DlAbortEx("invalid ID=%u for %s. It should be %d.", id, "suggest piece", ID); } BtSuggestPieceMessageHandle message = new BtSuggestPieceMessage(); message->setIndex(PeerMessageUtil::getIntParam(data, 1)); return message; } uint32_t BtSuggestPieceMessage::MESSAGE_LENGTH = 9; const unsigned char* BtSuggestPieceMessage::getMessage() { if(!msg) { /** * len --- 5, 4bytes * id --- 13, 1byte * piece index --- index, 4bytes * total: 9bytes */ msg = new unsigned char[MESSAGE_LENGTH]; PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID); PeerMessageUtil::setIntParam(&msg[5], index); } return msg; } uint32_t BtSuggestPieceMessage::getMessageLength() { return MESSAGE_LENGTH; } string BtSuggestPieceMessage::toString() const { return "suggest piece index="+Util::itos(index); }