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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-12-24 06:25:21 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-09-29 14:52:42 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#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-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");
|
|
|
|
|
2010-06-11 11:48:22 +00:00
|
|
|
BtBitfieldMessage::BtBitfieldMessage():SimpleBtMessage(ID, NAME),
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_(0),
|
|
|
|
bitfieldLength_(0)
|
2010-06-11 11:48:22 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
BtBitfieldMessage::BtBitfieldMessage
|
|
|
|
(const unsigned char* bitfield, size_t bitfieldLength):
|
|
|
|
SimpleBtMessage(ID, NAME),
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_(0),
|
|
|
|
bitfieldLength_(0)
|
2010-06-11 11:48:22 +00:00
|
|
|
{
|
|
|
|
setBitfield(bitfield, bitfieldLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
BtBitfieldMessage::~BtBitfieldMessage()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
delete [] bitfield_;
|
2010-06-11 11:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtBitfieldMessage::setBitfield
|
|
|
|
(const unsigned char* bitfield, size_t bitfieldLength) {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(bitfield_ == bitfield) {
|
2006-12-24 06:25:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
delete [] bitfield_;
|
|
|
|
bitfieldLength_ = bitfieldLength;
|
|
|
|
bitfield_ = new unsigned char[bitfieldLength_];
|
|
|
|
memcpy(bitfield_, bitfield, bitfieldLength_);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-29 14:52:42 +00:00
|
|
|
bittorrent::assertPayloadLengthGreater(1,dataLength, NAME);
|
|
|
|
bittorrent::assertID(ID, data, NAME);
|
2008-04-20 00:50:22 +00:00
|
|
|
BtBitfieldMessageHandle message(new BtBitfieldMessage());
|
2009-11-15 12:55:50 +00:00
|
|
|
message->setBitfield(data+1, dataLength-1);
|
2006-12-24 06:25:21 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BtBitfieldMessage::doReceivedAction() {
|
2010-06-11 11:48:22 +00:00
|
|
|
if(isMetadataGetMode()) {
|
2009-11-22 13:33:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
getPieceStorage()->updatePieceStats(bitfield_, bitfieldLength_,
|
2010-06-11 11:48:22 +00:00
|
|
|
getPeer()->getBitfield());
|
2010-06-21 13:51:56 +00:00
|
|
|
getPeer()->setBitfield(bitfield_, bitfieldLength_);
|
2010-06-11 11:48:22 +00:00
|
|
|
if(getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
|
2009-07-06 15:22:06 +00:00
|
|
|
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* BtBitfieldMessage::createMessage() {
|
|
|
|
/**
|
|
|
|
* len --- 1+bitfieldLength, 4bytes
|
|
|
|
* id --- 5, 1byte
|
|
|
|
* bitfield --- bitfield, len bytes
|
|
|
|
* total: 5+len bytes
|
|
|
|
*/
|
2010-06-21 13:51:56 +00:00
|
|
|
const size_t msgLength = 5+bitfieldLength_;
|
2010-03-04 16:24:03 +00:00
|
|
|
unsigned char* msg = new unsigned char[msgLength];
|
2010-06-21 13:51:56 +00:00
|
|
|
bittorrent::createPeerMessageString(msg, msgLength, 1+bitfieldLength_, ID);
|
|
|
|
memcpy(msg+5, bitfield_, bitfieldLength_);
|
2006-12-24 06:25:21 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t BtBitfieldMessage::getMessageLength() {
|
2010-06-21 13:51:56 +00:00
|
|
|
return 5+bitfieldLength_;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string BtBitfieldMessage::toString() const {
|
2010-06-21 13:51:56 +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
|