2006-12-24 06:25:21 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "BtBitfieldMessage.h"
|
2009-03-12 15:54:43 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "PeerMessageUtil.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "DlAbortEx.h"
|
2007-01-25 16:47:29 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
2008-04-27 02:22:14 +00:00
|
|
|
#include "StringFormat.h"
|
2008-05-11 01:22:32 +00:00
|
|
|
#include "PieceStorage.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2009-03-12 15:54:43 +00:00
|
|
|
const std::string BtBitfieldMessage::NAME("bitfield");
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) {
|
2006-12-24 06:25:21 +00:00
|
|
|
if(this->bitfield == bitfield) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete [] this->bitfield;
|
|
|
|
|
|
|
|
this->bitfieldLength = bitfieldLength;
|
|
|
|
this->bitfield = new unsigned char[this->bitfieldLength];
|
|
|
|
memcpy(this->bitfield, bitfield, this->bitfieldLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
BtBitfieldMessageHandle
|
2008-03-09 12:24:01 +00:00
|
|
|
BtBitfieldMessage::create(const unsigned char* data, size_t dataLength)
|
2006-12-24 06:25:21 +00:00
|
|
|
{
|
2009-03-12 15:54:43 +00:00
|
|
|
PeerMessageUtil::assertPayloadLengthGreater(1,dataLength, NAME);
|
|
|
|
PeerMessageUtil::assertID(ID, data, NAME);
|
2008-04-20 00:50:22 +00:00
|
|
|
BtBitfieldMessageHandle message(new BtBitfieldMessage());
|
2006-12-24 06:25:21 +00:00
|
|
|
message->setBitfield((unsigned char*)data+1, dataLength-1);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtBitfieldMessage::doReceivedAction() {
|
2008-05-11 01:22:32 +00:00
|
|
|
pieceStorage->updatePieceStats(bitfield, bitfieldLength, peer->getBitfield());
|
2006-12-24 06:25:21 +00:00
|
|
|
peer->setBitfield(bitfield, bitfieldLength);
|
2009-07-06 15:22:06 +00:00
|
|
|
if(peer->isSeeder() && pieceStorage->downloadFinished()) {
|
|
|
|
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2006-12-24 15:55:59 +00:00
|
|
|
const unsigned char* BtBitfieldMessage::getMessage() {
|
2006-12-24 06:25:21 +00:00
|
|
|
if(!msg) {
|
|
|
|
/**
|
|
|
|
* len --- 1+bitfieldLength, 4bytes
|
|
|
|
* id --- 5, 1byte
|
|
|
|
* bitfield --- bitfield, len bytes
|
|
|
|
* total: 5+len bytes
|
|
|
|
*/
|
|
|
|
msgLength = 5+bitfieldLength;
|
2006-12-24 15:55:59 +00:00
|
|
|
msg = new unsigned char[msgLength];
|
2006-12-24 06:25:21 +00:00
|
|
|
PeerMessageUtil::createPeerMessageString(msg, msgLength,
|
|
|
|
1+bitfieldLength, ID);
|
|
|
|
memcpy(msg+5, bitfield, bitfieldLength);
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t BtBitfieldMessage::getMessageLength() {
|
2006-12-24 06:25:21 +00:00
|
|
|
getMessage();
|
|
|
|
return msgLength;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string BtBitfieldMessage::toString() const {
|
2009-06-06 12:33:07 +00:00
|
|
|
return strconcat(NAME, " ", Util::toHex(bitfield, bitfieldLength));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|