mirror of https://github.com/aria2/aria2
2009-03-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Refactored BtMessage subclasses. BtCancelMessage, BtRequestMessage and BtRejectMessage are derived from RangeBtMessage class. BtAllowedFastMessage, BtHaveMessage and BtSuggestPieceMessage are derived from IndexBtMessage class. BtChokeMessage, BtHaveAllMessage, BtHaveNoneMessage, BtInterestedMessage, BtNotInterestedMessage and BtUnchokeMessage are derived from ZeroBtMessage class. * src/AbstractBtMessage.cc * src/AbstractBtMessage.h * src/BtAllowedFastMessage.cc * src/BtAllowedFastMessage.h * src/BtAllowedFastMessageValidator.h: Removed * src/BtBitfieldMessage.cc * src/BtBitfieldMessage.h * src/BtCancelMessage.cc * src/BtCancelMessage.h * src/BtCancelMessageValidator.h: Removed * src/BtChokeMessage.cc * src/BtChokeMessage.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtHandshakeMessage.cc * src/BtHandshakeMessage.h * src/BtHaveAllMessage.cc * src/BtHaveAllMessage.h * src/BtHaveMessage.cc * src/BtHaveMessage.h * src/BtHaveMessageValidator.h: Removed * src/BtHaveNoneMessage.cc * src/BtHaveNoneMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtKeepAliveMessage.cc * src/BtKeepAliveMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtPieceMessage.cc * src/BtPieceMessage.h * src/BtPortMessage.cc * src/BtPortMessage.h * src/BtRejectMessage.cc * src/BtRejectMessage.h * src/BtRejectMessageValidator.h: Removed * src/BtRequestMessage.cc * src/BtRequestMessage.h * src/BtRequestMessageValidator.h: Removed * src/BtSuggestPieceMessage.cc * src/BtSuggestPieceMessage.h * src/BtSuggestPieceMessageValidator.h: Removed * src/BtUnchokeMessage.cc * src/BtUnchokeMessage.h * src/DefaultBtMessageFactory.cc * src/IndexBtMessage.cc * src/IndexBtMessage.h * src/IndexBtMessageValidator.h * src/Makefile.am * src/PeerMessageUtil.cc * src/PeerMessageUtil.h * src/RangeBtMessage.cc * src/RangeBtMessage.h * src/RangeBtMessageValidator.h * src/SimpleBtMessage.cc * src/SimpleBtMessage.h * src/ZeroBtMessage.cc * src/ZeroBtMessage.h * test/BtRequestMessageTest.ccpull/1/head
parent
371ef954d5
commit
15582ed8cd
69
ChangeLog
69
ChangeLog
|
@ -1,3 +1,72 @@
|
|||
2009-03-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Refactored BtMessage subclasses. BtCancelMessage, BtRequestMessage
|
||||
and BtRejectMessage are derived from RangeBtMessage class.
|
||||
BtAllowedFastMessage, BtHaveMessage and BtSuggestPieceMessage are
|
||||
derived from IndexBtMessage class. BtChokeMessage,
|
||||
BtHaveAllMessage, BtHaveNoneMessage, BtInterestedMessage,
|
||||
BtNotInterestedMessage and BtUnchokeMessage are derived from
|
||||
ZeroBtMessage class.
|
||||
* src/AbstractBtMessage.cc
|
||||
* src/AbstractBtMessage.h
|
||||
* src/BtAllowedFastMessage.cc
|
||||
* src/BtAllowedFastMessage.h
|
||||
* src/BtAllowedFastMessageValidator.h: Removed
|
||||
* src/BtBitfieldMessage.cc
|
||||
* src/BtBitfieldMessage.h
|
||||
* src/BtCancelMessage.cc
|
||||
* src/BtCancelMessage.h
|
||||
* src/BtCancelMessageValidator.h: Removed
|
||||
* src/BtChokeMessage.cc
|
||||
* src/BtChokeMessage.h
|
||||
* src/BtExtendedMessage.cc
|
||||
* src/BtExtendedMessage.h
|
||||
* src/BtHandshakeMessage.cc
|
||||
* src/BtHandshakeMessage.h
|
||||
* src/BtHaveAllMessage.cc
|
||||
* src/BtHaveAllMessage.h
|
||||
* src/BtHaveMessage.cc
|
||||
* src/BtHaveMessage.h
|
||||
* src/BtHaveMessageValidator.h: Removed
|
||||
* src/BtHaveNoneMessage.cc
|
||||
* src/BtHaveNoneMessage.h
|
||||
* src/BtInterestedMessage.cc
|
||||
* src/BtInterestedMessage.h
|
||||
* src/BtKeepAliveMessage.cc
|
||||
* src/BtKeepAliveMessage.h
|
||||
* src/BtNotInterestedMessage.cc
|
||||
* src/BtNotInterestedMessage.h
|
||||
* src/BtPieceMessage.cc
|
||||
* src/BtPieceMessage.h
|
||||
* src/BtPortMessage.cc
|
||||
* src/BtPortMessage.h
|
||||
* src/BtRejectMessage.cc
|
||||
* src/BtRejectMessage.h
|
||||
* src/BtRejectMessageValidator.h: Removed
|
||||
* src/BtRequestMessage.cc
|
||||
* src/BtRequestMessage.h
|
||||
* src/BtRequestMessageValidator.h: Removed
|
||||
* src/BtSuggestPieceMessage.cc
|
||||
* src/BtSuggestPieceMessage.h
|
||||
* src/BtSuggestPieceMessageValidator.h: Removed
|
||||
* src/BtUnchokeMessage.cc
|
||||
* src/BtUnchokeMessage.h
|
||||
* src/DefaultBtMessageFactory.cc
|
||||
* src/IndexBtMessage.cc
|
||||
* src/IndexBtMessage.h
|
||||
* src/IndexBtMessageValidator.h
|
||||
* src/Makefile.am
|
||||
* src/PeerMessageUtil.cc
|
||||
* src/PeerMessageUtil.h
|
||||
* src/RangeBtMessage.cc
|
||||
* src/RangeBtMessage.h
|
||||
* src/RangeBtMessageValidator.h
|
||||
* src/SimpleBtMessage.cc
|
||||
* src/SimpleBtMessage.h
|
||||
* src/ZeroBtMessage.cc
|
||||
* src/ZeroBtMessage.h
|
||||
* test/BtRequestMessageTest.cc
|
||||
|
||||
2009-03-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Reverted usage_text.h to r1093 to avoid to lose translated
|
||||
|
|
|
@ -43,12 +43,13 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
AbstractBtMessage::AbstractBtMessage(uint8_t id):
|
||||
AbstractBtMessage::AbstractBtMessage(uint8_t id, const std::string& name):
|
||||
BtMessage(id),
|
||||
sendingInProgress(false),
|
||||
invalidate(false),
|
||||
uploading(false),
|
||||
cuid(0),
|
||||
_name(name),
|
||||
logger(LogFactory::getInstance())
|
||||
{}
|
||||
|
||||
|
@ -132,4 +133,9 @@ void AbstractBtMessage::setBtRequestFactory(const WeakHandle<BtRequestFactory>&
|
|||
this->requestFactory = factory;
|
||||
}
|
||||
|
||||
const std::string& AbstractBtMessage::getName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -58,6 +58,8 @@ protected:
|
|||
bool uploading;
|
||||
int32_t cuid;
|
||||
|
||||
std::string _name;
|
||||
|
||||
SharedHandle<BtContext> btContext;
|
||||
|
||||
SharedHandle<PieceStorage> pieceStorage;
|
||||
|
@ -78,7 +80,7 @@ protected:
|
|||
|
||||
Logger* logger;
|
||||
public:
|
||||
AbstractBtMessage(uint8_t id);
|
||||
AbstractBtMessage(uint8_t id, const std::string& name);
|
||||
|
||||
virtual ~AbstractBtMessage();
|
||||
|
||||
|
@ -145,6 +147,8 @@ public:
|
|||
void setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory);
|
||||
|
||||
void setBtRequestFactory(const WeakHandle<BtRequestFactory>& factory);
|
||||
|
||||
const std::string& getName() const;
|
||||
};
|
||||
|
||||
typedef SharedHandle<AbstractBtMessage> AbstractBtMessageHandle;
|
||||
|
|
|
@ -33,28 +33,18 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtAllowedFastMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "allowed fast", dataLength, 5).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "allowed fast", ID).str());
|
||||
}
|
||||
BtAllowedFastMessageHandle message(new BtAllowedFastMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
return message;
|
||||
const std::string BtAllowedFastMessage::NAME("allowed fast");
|
||||
|
||||
SharedHandle<BtAllowedFastMessage> BtAllowedFastMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return IndexBtMessage::create<BtAllowedFastMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtAllowedFastMessage::doReceivedAction() {
|
||||
|
@ -63,34 +53,11 @@ void BtAllowedFastMessage::doReceivedAction() {
|
|||
(StringFormat("%s received while fast extension is disabled",
|
||||
toString().c_str()).str());
|
||||
}
|
||||
peer->addPeerAllowedIndex(index);
|
||||
}
|
||||
|
||||
const unsigned char* BtAllowedFastMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
* id --- 17, 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;
|
||||
}
|
||||
|
||||
size_t BtAllowedFastMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
peer->addPeerAllowedIndex(getIndex());
|
||||
}
|
||||
|
||||
void BtAllowedFastMessage::onSendComplete() {
|
||||
peer->addAmAllowedIndex(index);
|
||||
}
|
||||
|
||||
std::string BtAllowedFastMessage::toString() const {
|
||||
return "allowed fast index="+Util::itos(index);
|
||||
peer->addAmAllowedIndex(getIndex());
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_ALLOWED_FAST_MESSAGE_H_
|
||||
#define _D_BT_ALLOWED_FAST_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "IndexBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,39 +43,20 @@ class BtAllowedFastMessage;
|
|||
|
||||
typedef SharedHandle<BtAllowedFastMessage> BtAllowedFastMessageHandle;
|
||||
|
||||
class BtAllowedFastMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 9;
|
||||
class BtAllowedFastMessage : public IndexBtMessage {
|
||||
public:
|
||||
BtAllowedFastMessage(size_t index = 0)
|
||||
:SimpleBtMessage(ID),
|
||||
index(index),
|
||||
msg(0) {}
|
||||
|
||||
virtual ~BtAllowedFastMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
:IndexBtMessage(ID, NAME, index) {}
|
||||
|
||||
static const uint8_t ID = 17;
|
||||
|
||||
void setIndex(size_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
size_t getIndex() const { return index; }
|
||||
static const std::string NAME;
|
||||
|
||||
static BtAllowedFastMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtAllowedFastMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
virtual void onSendComplete();
|
||||
|
||||
};
|
||||
|
|
|
@ -33,6 +33,9 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtBitfieldMessage.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
|
@ -40,10 +43,11 @@
|
|||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
#include "PieceStorage.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtBitfieldMessage::NAME("bitfield");
|
||||
|
||||
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) {
|
||||
if(this->bitfield == bitfield) {
|
||||
return;
|
||||
|
@ -58,15 +62,8 @@ void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, size_t bitfie
|
|||
BtBitfieldMessageHandle
|
||||
BtBitfieldMessage::create(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
if(dataLength <= 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "bitfield", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "bitfield", ID).str());
|
||||
}
|
||||
PeerMessageUtil::assertPayloadLengthGreater(1,dataLength, NAME);
|
||||
PeerMessageUtil::assertID(ID, data, NAME);
|
||||
BtBitfieldMessageHandle message(new BtBitfieldMessage());
|
||||
message->setBitfield((unsigned char*)data+1, dataLength-1);
|
||||
return message;
|
||||
|
@ -100,7 +97,7 @@ size_t BtBitfieldMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
std::string BtBitfieldMessage::toString() const {
|
||||
return "bitfield "+Util::toHex(bitfield, bitfieldLength);
|
||||
return NAME+" "+Util::toHex(bitfield, bitfieldLength);
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -57,13 +57,13 @@ private:
|
|||
msgLength = 0;
|
||||
}
|
||||
public:
|
||||
BtBitfieldMessage():SimpleBtMessage(ID)
|
||||
BtBitfieldMessage():SimpleBtMessage(ID, NAME)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
BtBitfieldMessage(const unsigned char* bitfield,
|
||||
size_t bitfieldLength):SimpleBtMessage(ID)
|
||||
size_t bitfieldLength):SimpleBtMessage(ID, NAME)
|
||||
{
|
||||
init();
|
||||
setBitfield(bitfield, bitfieldLength);
|
||||
|
@ -76,6 +76,8 @@ public:
|
|||
|
||||
static const uint8_t ID = 5;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
|
||||
|
||||
const unsigned char* getBitfield() const { return bitfield; }
|
||||
|
|
|
@ -33,62 +33,21 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtCancelMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "BtMessageDispatcher.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "cancel", dataLength, 13).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "cancel", ID).str());
|
||||
}
|
||||
BtCancelMessageHandle message(new BtCancelMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
|
||||
message->setLength(PeerMessageUtil::getIntParam(data, 9));
|
||||
return message;
|
||||
const std::string BtCancelMessage::NAME("cancel");
|
||||
|
||||
SharedHandle<BtCancelMessage> BtCancelMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return RangeBtMessage::create<BtCancelMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtCancelMessage::doReceivedAction() {
|
||||
dispatcher->doCancelSendingPieceAction(index, begin, length);
|
||||
}
|
||||
|
||||
const unsigned char* BtCancelMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
* id --- 8, 1byte
|
||||
* index --- index, 4bytes
|
||||
* begin --- begin, 4bytes
|
||||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
PeerMessageUtil::setIntParam(&msg[13], length);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtCancelMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtCancelMessage::toString() const {
|
||||
return "cancel index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
void BtCancelMessage::doReceivedAction()
|
||||
{
|
||||
dispatcher->doCancelSendingPieceAction(getIndex(), getBegin(), getLength());
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_CANCEL_MESSAGE_H_
|
||||
#define _D_BT_CANCEL_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "RangeBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,49 +43,18 @@ class BtCancelMessage;
|
|||
|
||||
typedef SharedHandle<BtCancelMessage> BtCancelMessageHandle;
|
||||
|
||||
class BtCancelMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
uint32_t begin;
|
||||
size_t length;
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 17;
|
||||
class BtCancelMessage : public RangeBtMessage {
|
||||
public:
|
||||
BtCancelMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0)
|
||||
:SimpleBtMessage(ID),
|
||||
index(index),
|
||||
begin(begin),
|
||||
length(length),
|
||||
msg(0) {}
|
||||
|
||||
virtual ~BtCancelMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
:RangeBtMessage(ID, NAME, index, begin, length) {}
|
||||
|
||||
static const int8_t ID = 8;
|
||||
|
||||
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; }
|
||||
|
||||
size_t getLength() const { return length; }
|
||||
|
||||
void setLength(size_t length) { this->length = length; }
|
||||
static const std::string NAME;
|
||||
|
||||
static BtCancelMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -33,65 +33,36 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtChokeMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "BtMessageDispatcher.h"
|
||||
#include "BtRequestFactory.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "choke", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "choke", ID).str());
|
||||
}
|
||||
BtChokeMessageHandle chokeMessage(new BtChokeMessage());
|
||||
return chokeMessage;
|
||||
const std::string BtChokeMessage::NAME("choke");
|
||||
|
||||
SharedHandle<BtChokeMessage> BtChokeMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtChokeMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtChokeMessage::doReceivedAction() {
|
||||
void BtChokeMessage::doReceivedAction()
|
||||
{
|
||||
peer->peerChoking(true);
|
||||
dispatcher->doChokedAction();
|
||||
requestFactory->doChokedAction();
|
||||
}
|
||||
|
||||
bool BtChokeMessage::sendPredicate() const {
|
||||
bool BtChokeMessage::sendPredicate() const
|
||||
{
|
||||
return !peer->amChoking();
|
||||
}
|
||||
|
||||
const unsigned char* BtChokeMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 0, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtChokeMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
void BtChokeMessage::onSendComplete() {
|
||||
void BtChokeMessage::onSendComplete()
|
||||
{
|
||||
peer->amChoking(true);
|
||||
dispatcher->doChokingAction();
|
||||
}
|
||||
|
||||
std::string BtChokeMessage::toString() const {
|
||||
static const std::string CHOKE("choke");
|
||||
return CHOKE;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_CHOKE_MESSAGE_H_
|
||||
#define _D_BT_CHOKE_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,29 +43,18 @@ class BtChokeMessage;
|
|||
|
||||
typedef SharedHandle<BtChokeMessage> BtChokeMessageHandle;
|
||||
|
||||
class BtChokeMessage : public SimpleBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
class BtChokeMessage : public ZeroBtMessage {
|
||||
public:
|
||||
BtChokeMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtChokeMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtChokeMessage():ZeroBtMessage(ID, NAME) {}
|
||||
|
||||
static const uint8_t ID = 0;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
static BtChokeMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtChokeMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual bool sendPredicate() const;
|
||||
|
||||
|
|
|
@ -47,9 +47,11 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtExtendedMessage::NAME("extended");
|
||||
|
||||
BtExtendedMessage::BtExtendedMessage
|
||||
(const ExtensionMessageHandle& extensionMessage):
|
||||
SimpleBtMessage(ID),
|
||||
SimpleBtMessage(ID, NAME),
|
||||
_extensionMessage(extensionMessage),
|
||||
_msg(0),
|
||||
_msgLength(0)
|
||||
|
@ -90,7 +92,7 @@ bool BtExtendedMessage::sendPredicate() const
|
|||
}
|
||||
|
||||
std::string BtExtendedMessage::toString() const {
|
||||
return "extended "+_extensionMessage->toString();
|
||||
return NAME+" "+_extensionMessage->toString();
|
||||
}
|
||||
|
||||
BtExtendedMessageHandle
|
||||
|
@ -98,15 +100,8 @@ BtExtendedMessage::create(const SharedHandle<ExtensionMessageFactory>& factory,
|
|||
const PeerHandle& peer,
|
||||
const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
if(dataLength < 2) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID).str());
|
||||
}
|
||||
PeerMessageUtil::assertPayloadLengthGreater(1, dataLength, NAME);
|
||||
PeerMessageUtil::assertID(ID, data, NAME);
|
||||
assert(!factory.isNull());
|
||||
ExtensionMessageHandle extmsg = factory->createMessage(data+1,
|
||||
dataLength-1);
|
||||
|
|
|
@ -60,6 +60,8 @@ public:
|
|||
|
||||
static const uint8_t ID = 20;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
static BtExtendedMessageHandle create
|
||||
(const SharedHandle<ExtensionMessageFactory>& factory,
|
||||
const SharedHandle<Peer>& peer,
|
||||
|
|
|
@ -40,16 +40,18 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtHandshakeMessage::NAME("handshake");
|
||||
|
||||
const unsigned char* BtHandshakeMessage::BT_PSTR = (const unsigned char*)"BitTorrent protocol";
|
||||
|
||||
BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID)
|
||||
BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID, NAME)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash,
|
||||
const unsigned char* peerId):
|
||||
SimpleBtMessage(ID)
|
||||
SimpleBtMessage(ID, NAME)
|
||||
{
|
||||
init();
|
||||
memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH);
|
||||
|
@ -100,7 +102,7 @@ size_t BtHandshakeMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
std::string BtHandshakeMessage::toString() const {
|
||||
return "handshake peerId="+
|
||||
return NAME+" peerId="+
|
||||
Util::urlencode(peerId, PEER_ID_LENGTH)+
|
||||
", reserved="+Util::toHex(reserved, RESERVED_LENGTH);
|
||||
}
|
||||
|
|
|
@ -74,6 +74,8 @@ public:
|
|||
|
||||
static const uint8_t ID = INT8_MAX;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
virtual void doReceivedAction() {};
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
|
|
@ -34,59 +34,31 @@
|
|||
/* copyright --> */
|
||||
#include "BtHaveAllMessage.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
#include "PieceStorage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtHaveAllMessageHandle BtHaveAllMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "have all", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have all", ID).str());
|
||||
}
|
||||
BtHaveAllMessageHandle message(new BtHaveAllMessage());
|
||||
return message;
|
||||
const std::string BtHaveAllMessage::NAME("have all");
|
||||
|
||||
SharedHandle<BtHaveAllMessage> BtHaveAllMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtHaveAllMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtHaveAllMessage::doReceivedAction() {
|
||||
void BtHaveAllMessage::doReceivedAction()
|
||||
{
|
||||
if(!peer->isFastExtensionEnabled()) {
|
||||
throw DlAbortEx
|
||||
(StringFormat("%s received while fast extension is disabled",
|
||||
toString().c_str()).str());
|
||||
}
|
||||
pieceStorage->subtractPieceStats(peer->getBitfield(), peer->getBitfieldLength());
|
||||
pieceStorage->subtractPieceStats(peer->getBitfield(),
|
||||
peer->getBitfieldLength());
|
||||
peer->setAllBitfield();
|
||||
pieceStorage->addPieceStats(peer->getBitfield(), peer->getBitfieldLength());
|
||||
}
|
||||
|
||||
const unsigned char* BtHaveAllMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 14, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtHaveAllMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtHaveAllMessage::toString() const {
|
||||
static const std::string HAVE_ALL("have all");
|
||||
return HAVE_ALL;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_HAVE_ALL_MESSAGE_H_
|
||||
#define _D_BT_HAVE_ALL_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,29 +43,18 @@ class BtHaveAllMessage;
|
|||
|
||||
typedef SharedHandle<BtHaveAllMessage> BtHaveAllMessageHandle;
|
||||
|
||||
class BtHaveAllMessage : public SimpleBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
class BtHaveAllMessage : public ZeroBtMessage {
|
||||
public:
|
||||
BtHaveAllMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtHaveAllMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtHaveAllMessage():ZeroBtMessage(ID, NAME) {}
|
||||
|
||||
static const uint8_t ID = 14;
|
||||
|
||||
static BtHaveAllMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static const std::string NAME;
|
||||
|
||||
static SharedHandle<BtHaveAllMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -33,57 +33,23 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtHaveMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
#include "PieceStorage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtHaveMessageHandle BtHaveMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "have", dataLength, 5).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have", ID).str());
|
||||
}
|
||||
BtHaveMessageHandle message(new BtHaveMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
return message;
|
||||
const std::string BtHaveMessage::NAME("have");
|
||||
|
||||
SharedHandle<BtHaveMessage> BtHaveMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return IndexBtMessage::create<BtHaveMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtHaveMessage::doReceivedAction() {
|
||||
peer->updateBitfield(index, 1);
|
||||
pieceStorage->addPieceStats(index);
|
||||
}
|
||||
|
||||
const unsigned char* BtHaveMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
* id --- 4, 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;
|
||||
}
|
||||
|
||||
size_t BtHaveMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtHaveMessage::toString() const {
|
||||
return "have index="+Util::itos(index);
|
||||
void BtHaveMessage::doReceivedAction()
|
||||
{
|
||||
peer->updateBitfield(getIndex(), 1);
|
||||
pieceStorage->addPieceStats(getIndex());
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_HAVE_MESSAGE_H_
|
||||
#define _D_BT_HAVE_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "IndexBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,35 +43,18 @@ class BtHaveMessage;
|
|||
|
||||
typedef SharedHandle<BtHaveMessage> BtHaveMessageHandle;
|
||||
|
||||
class BtHaveMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
unsigned char* msg;
|
||||
static const size_t MESSAGE_LENGTH = 9;
|
||||
class BtHaveMessage : public IndexBtMessage {
|
||||
public:
|
||||
BtHaveMessage(size_t index = 0):SimpleBtMessage(ID), index(index), msg(0) {}
|
||||
|
||||
virtual ~BtHaveMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtHaveMessage(size_t index = 0):IndexBtMessage(ID, NAME, index) {}
|
||||
|
||||
static const uint8_t ID = 4;
|
||||
|
||||
void setIndex(size_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
static const std::string NAME;
|
||||
|
||||
size_t getIndex() const { return index; }
|
||||
|
||||
static BtHaveMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtHaveMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -34,28 +34,21 @@
|
|||
/* copyright --> */
|
||||
#include "BtHaveNoneMessage.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtHaveNoneMessageHandle BtHaveNoneMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "have none", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "have none", ID).str());
|
||||
}
|
||||
BtHaveNoneMessageHandle message(new BtHaveNoneMessage());
|
||||
return message;
|
||||
const std::string BtHaveNoneMessage::NAME("have none");
|
||||
|
||||
SharedHandle<BtHaveNoneMessage> BtHaveNoneMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtHaveNoneMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtHaveNoneMessage::doReceivedAction() {
|
||||
void BtHaveNoneMessage::doReceivedAction()
|
||||
{
|
||||
if(!peer->isFastExtensionEnabled()) {
|
||||
throw DlAbortEx
|
||||
(StringFormat("%s received while fast extension is disabled",
|
||||
|
@ -63,26 +56,4 @@ void BtHaveNoneMessage::doReceivedAction() {
|
|||
}
|
||||
}
|
||||
|
||||
const unsigned char* BtHaveNoneMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 15, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtHaveNoneMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtHaveNoneMessage::toString() const {
|
||||
static const std::string HAVE_NONE("have none");
|
||||
return HAVE_NONE;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_HAVE_NONE_MESSAGE_H_
|
||||
#define _D_BT_HAVE_NONE_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,29 +43,17 @@ class BtHaveNoneMessage;
|
|||
|
||||
typedef SharedHandle<BtHaveNoneMessage> BtHaveNoneMessageHandle;
|
||||
|
||||
class BtHaveNoneMessage : public SimpleBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
class BtHaveNoneMessage : public ZeroBtMessage {
|
||||
public:
|
||||
BtHaveNoneMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtHaveNoneMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
|
||||
BtHaveNoneMessage():ZeroBtMessage(ID, NAME) {}
|
||||
static const uint8_t ID = 15;
|
||||
|
||||
static BtHaveNoneMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static const std::string NAME;
|
||||
|
||||
static SharedHandle<BtHaveNoneMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -33,67 +33,36 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtInterestedMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "BtContext.h"
|
||||
#include "StringFormat.h"
|
||||
#include "PeerStorage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "interested", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "interested", ID).str());
|
||||
}
|
||||
BtInterestedMessageHandle message(new BtInterestedMessage());
|
||||
return message;
|
||||
const std::string BtInterestedMessage::NAME("interested");
|
||||
|
||||
SharedHandle<BtInterestedMessage> BtInterestedMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtInterestedMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtInterestedMessage::doReceivedAction() {
|
||||
void BtInterestedMessage::doReceivedAction()
|
||||
{
|
||||
peer->peerInterested(true);
|
||||
if(!peer->amChoking()) {
|
||||
_peerStorage->executeChoke();
|
||||
}
|
||||
}
|
||||
|
||||
bool BtInterestedMessage::sendPredicate() const {
|
||||
bool BtInterestedMessage::sendPredicate() const
|
||||
{
|
||||
return !peer->amInterested();
|
||||
}
|
||||
|
||||
const unsigned char* BtInterestedMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 2, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtInterestedMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
void BtInterestedMessage::onSendComplete() {
|
||||
peer->amInterested(true);
|
||||
}
|
||||
|
||||
std::string BtInterestedMessage::toString() const {
|
||||
static const std::string INTERESTED("interested");
|
||||
return INTERESTED;
|
||||
}
|
||||
|
||||
void BtInterestedMessage::setPeerStorage
|
||||
(const SharedHandle<PeerStorage>& peerStorage)
|
||||
{
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_INTERESTED_MESSAGE_H_
|
||||
#define _D_BT_INTERESTED_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -44,32 +44,21 @@ class BtInterestedMessage;
|
|||
|
||||
typedef SharedHandle<BtInterestedMessage> BtInterestedMessageHandle;
|
||||
|
||||
class BtInterestedMessage : public SimpleBtMessage {
|
||||
class BtInterestedMessage : public ZeroBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
||||
SharedHandle<PeerStorage> _peerStorage;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
public:
|
||||
BtInterestedMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtInterestedMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtInterestedMessage():ZeroBtMessage(ID, NAME) {}
|
||||
|
||||
static const uint8_t ID = 2;
|
||||
|
||||
static BtInterestedMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static const std::string NAME;
|
||||
|
||||
static SharedHandle<BtInterestedMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
virtual bool sendPredicate() const;
|
||||
|
||||
virtual void onSendComplete();
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtKeepAliveMessage::NAME("keep alive");
|
||||
|
||||
const unsigned char* BtKeepAliveMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
|
|
|
@ -49,7 +49,7 @@ private:
|
|||
|
||||
static const size_t MESSAGE_LENGTH = 4;
|
||||
public:
|
||||
BtKeepAliveMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
BtKeepAliveMessage():SimpleBtMessage(ID, NAME), msg(0) {}
|
||||
|
||||
virtual ~BtKeepAliveMessage() {
|
||||
delete [] msg;
|
||||
|
@ -57,6 +57,8 @@ public:
|
|||
|
||||
static const uint8_t ID = 99;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
virtual void doReceivedAction() {}
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
@ -64,7 +66,7 @@ public:
|
|||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const {
|
||||
return "keep alive";
|
||||
return NAME;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -33,67 +33,36 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtNotInterestedMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "BtContext.h"
|
||||
#include "PeerStorage.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "not interested", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "not interested", ID).str());
|
||||
}
|
||||
BtNotInterestedMessageHandle message(new BtNotInterestedMessage());
|
||||
return message;
|
||||
const std::string BtNotInterestedMessage::NAME("not interested");
|
||||
|
||||
SharedHandle<BtNotInterestedMessage> BtNotInterestedMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtNotInterestedMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtNotInterestedMessage::doReceivedAction() {
|
||||
void BtNotInterestedMessage::doReceivedAction()
|
||||
{
|
||||
peer->peerInterested(false);
|
||||
if(!peer->amChoking()) {
|
||||
_peerStorage->executeChoke();
|
||||
}
|
||||
}
|
||||
|
||||
bool BtNotInterestedMessage::sendPredicate() const {
|
||||
bool BtNotInterestedMessage::sendPredicate() const
|
||||
{
|
||||
return peer->amInterested();
|
||||
}
|
||||
|
||||
const unsigned char* BtNotInterestedMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 3, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtNotInterestedMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
void BtNotInterestedMessage::onSendComplete() {
|
||||
peer->amInterested(false);
|
||||
}
|
||||
|
||||
std::string BtNotInterestedMessage::toString() const {
|
||||
static const std::string NOT_INTERESTED("not interested");
|
||||
return NOT_INTERESTED;
|
||||
}
|
||||
|
||||
void BtNotInterestedMessage::setPeerStorage
|
||||
(const SharedHandle<PeerStorage>& peerStorage)
|
||||
{
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_NOT_INTERESTED_MESSAGE_H_
|
||||
#define _D_BT_NOT_INTERESTED_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -44,32 +44,21 @@ class BtNotInterestedMessage;
|
|||
|
||||
typedef SharedHandle<BtNotInterestedMessage> BtNotInterestedMessageHandle;
|
||||
|
||||
class BtNotInterestedMessage : public SimpleBtMessage {
|
||||
class BtNotInterestedMessage : public ZeroBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
||||
SharedHandle<PeerStorage> _peerStorage;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
public:
|
||||
BtNotInterestedMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtNotInterestedMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtNotInterestedMessage():ZeroBtMessage(ID, NAME) {}
|
||||
|
||||
static const uint8_t ID = 3;
|
||||
|
||||
static BtNotInterestedMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static const std::string NAME;
|
||||
|
||||
static SharedHandle<BtNotInterestedMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
virtual bool sendPredicate() const;
|
||||
|
||||
virtual void onSendComplete();
|
||||
|
|
|
@ -33,6 +33,11 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtPieceMessage.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "message.h"
|
||||
|
@ -51,12 +56,11 @@
|
|||
#include "BtRequestFactory.h"
|
||||
#include "PeerConnection.h"
|
||||
#include "StringFormat.h"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtPieceMessage::NAME("piece");
|
||||
|
||||
void BtPieceMessage::setBlock(const unsigned char* block, size_t blockLength) {
|
||||
delete [] this->block;
|
||||
this->blockLength = blockLength;
|
||||
|
@ -65,15 +69,8 @@ void BtPieceMessage::setBlock(const unsigned char* block, size_t blockLength) {
|
|||
}
|
||||
|
||||
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength <= 9) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "piece", dataLength, 9).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID).str());
|
||||
}
|
||||
PeerMessageUtil::assertPayloadLengthGreater(9, dataLength, NAME);
|
||||
PeerMessageUtil::assertID(ID, data, NAME);
|
||||
BtPieceMessageHandle message(new BtPieceMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
|
||||
|
@ -173,7 +170,7 @@ size_t BtPieceMessage::sendPieceData(off_t offset, size_t length) const {
|
|||
}
|
||||
|
||||
std::string BtPieceMessage::toString() const {
|
||||
return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
return NAME+" index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
", length="+Util::itos(blockLength);
|
||||
}
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ private:
|
|||
typedef SharedHandle<BtCancelSendingPieceEventListener> BtCancelSendingPieceEventListenerHandle;
|
||||
public:
|
||||
BtPieceMessage(size_t index = 0, uint32_t begin = 0, size_t blockLength = 0)
|
||||
:AbstractBtMessage(ID),
|
||||
:AbstractBtMessage(ID, NAME),
|
||||
index(index),
|
||||
begin(begin),
|
||||
blockLength(blockLength),
|
||||
|
@ -118,6 +118,8 @@ public:
|
|||
|
||||
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; }
|
||||
|
|
|
@ -48,8 +48,10 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
const std::string BtPortMessage::NAME("port");
|
||||
|
||||
BtPortMessage::BtPortMessage(uint16_t port):
|
||||
SimpleBtMessage(ID), _port(port), _msg(0) {}
|
||||
SimpleBtMessage(ID, NAME), _port(port), _msg(0) {}
|
||||
|
||||
BtPortMessage::~BtPortMessage()
|
||||
{
|
||||
|
@ -58,15 +60,8 @@ BtPortMessage::~BtPortMessage()
|
|||
|
||||
SharedHandle<BtPortMessage> BtPortMessage::create(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
if(dataLength != 3) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "port", dataLength, 3).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID).str());
|
||||
}
|
||||
PeerMessageUtil::assertPayloadLengthEqual(3, dataLength, NAME);
|
||||
PeerMessageUtil::assertID(ID, data, NAME);
|
||||
uint16_t port = PeerMessageUtil::getShortIntParam(data, 1);
|
||||
SharedHandle<BtPortMessage> message(new BtPortMessage(port));
|
||||
return message;
|
||||
|
@ -114,7 +109,7 @@ size_t BtPortMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
std::string BtPortMessage::toString() const {
|
||||
return "port port="+Util::uitos(_port);
|
||||
return NAME+" port="+Util::uitos(_port);
|
||||
}
|
||||
|
||||
void BtPortMessage::setLocalNode(const WeakHandle<DHTNode>& localNode)
|
||||
|
|
|
@ -64,6 +64,8 @@ public:
|
|||
|
||||
static const uint8_t ID = 9;
|
||||
|
||||
static const std::string NAME;
|
||||
|
||||
uint16_t getPort() const { return _port; }
|
||||
|
||||
static SharedHandle<BtPortMessage> create(const unsigned char* data, size_t dataLength);
|
||||
|
|
|
@ -33,10 +33,7 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtRejectMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "RequestSlot.h"
|
||||
#include "BtMessageDispatcher.h"
|
||||
|
@ -44,24 +41,16 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
BtRejectMessageHandle BtRejectMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "reject", dataLength, 13).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "reject", ID).str());
|
||||
}
|
||||
BtRejectMessageHandle message(new BtRejectMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
|
||||
message->setLength(PeerMessageUtil::getIntParam(data, 9));
|
||||
return message;
|
||||
const std::string BtRejectMessage::NAME("reject");
|
||||
|
||||
SharedHandle<BtRejectMessage> BtRejectMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return RangeBtMessage::create<BtRejectMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtRejectMessage::doReceivedAction() {
|
||||
void BtRejectMessage::doReceivedAction()
|
||||
{
|
||||
if(!peer->isFastExtensionEnabled()) {
|
||||
throw DlAbortEx
|
||||
(StringFormat("%s received while fast extension is disabled.",
|
||||
|
@ -69,7 +58,8 @@ void BtRejectMessage::doReceivedAction() {
|
|||
}
|
||||
// TODO Current implementation does not close a connection even if
|
||||
// a request for this reject message has never sent.
|
||||
RequestSlot slot = dispatcher->getOutstandingRequest(index, begin, length);
|
||||
RequestSlot slot =
|
||||
dispatcher->getOutstandingRequest(getIndex(), getBegin(), getLength());
|
||||
if(RequestSlot::isNull(slot)) {
|
||||
//throw DlAbortEx("reject recieved, but it is not in the request slots.");
|
||||
} else {
|
||||
|
@ -78,32 +68,4 @@ void BtRejectMessage::doReceivedAction() {
|
|||
|
||||
}
|
||||
|
||||
const unsigned char* BtRejectMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
* id --- 16, 1byte
|
||||
* index --- index, 4bytes
|
||||
* begin --- begin, 4bytes
|
||||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
PeerMessageUtil::setIntParam(&msg[13], length);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtRejectMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtRejectMessage::toString() const {
|
||||
return "reject index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_REJECT_MESSAGE_H_
|
||||
#define _D_BT_REJECT_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "RangeBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,45 +43,19 @@ class BtRejectMessage;
|
|||
|
||||
typedef SharedHandle<BtRejectMessage> BtRejectMessageHandle;
|
||||
|
||||
class BtRejectMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
uint32_t begin;
|
||||
size_t length;
|
||||
unsigned char* msg;
|
||||
static const size_t MESSAGE_LENGTH = 17;
|
||||
class BtRejectMessage : public RangeBtMessage {
|
||||
public:
|
||||
BtRejectMessage(size_t index = 0, uint32_t begin = 0, size_t length = 0)
|
||||
:SimpleBtMessage(ID),
|
||||
index(index),
|
||||
begin(begin),
|
||||
length(length),
|
||||
msg(0) {}
|
||||
|
||||
virtual ~BtRejectMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
:RangeBtMessage(ID, NAME, index, begin, length) {}
|
||||
|
||||
static const uint8_t ID = 16;
|
||||
|
||||
size_t getIndex() const { return index; }
|
||||
void setIndex(size_t index) { this->index = index; }
|
||||
static const std::string NAME;
|
||||
|
||||
uint32_t getBegin() const { return begin; }
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
|
||||
size_t getLength() const { return length; }
|
||||
void setLength(size_t length) { this->length = length; }
|
||||
|
||||
static BtRejectMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtRejectMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -33,101 +33,70 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtRequestMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "BtAbortOutstandingRequestEvent.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "Piece.h"
|
||||
#include "BtContext.h"
|
||||
#include "PieceStorage.h"
|
||||
#include "BtMessageDispatcher.h"
|
||||
#include "BtMessageFactory.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "request", dataLength, 13).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "request", ID).str());
|
||||
}
|
||||
BtRequestMessageHandle message(new BtRequestMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
|
||||
message->setLength(PeerMessageUtil::getIntParam(data, 9));
|
||||
return message;
|
||||
const std::string BtRequestMessage::NAME("request");
|
||||
|
||||
SharedHandle<BtRequestMessage> BtRequestMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return RangeBtMessage::create<BtRequestMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtRequestMessage::doReceivedAction() {
|
||||
if(pieceStorage->hasPiece(index) &&
|
||||
void BtRequestMessage::doReceivedAction()
|
||||
{
|
||||
if(pieceStorage->hasPiece(getIndex()) &&
|
||||
(!peer->amChoking() ||
|
||||
(peer->amChoking() && peer->isInAmAllowedIndexSet(index)))) {
|
||||
BtMessageHandle msg = messageFactory->createPieceMessage(index,
|
||||
begin,
|
||||
length);
|
||||
(peer->amChoking() && peer->isInAmAllowedIndexSet(getIndex())))) {
|
||||
BtMessageHandle msg = messageFactory->createPieceMessage(getIndex(),
|
||||
getBegin(),
|
||||
getLength());
|
||||
dispatcher->addMessageToQueue(msg);
|
||||
} else {
|
||||
if(peer->isFastExtensionEnabled()) {
|
||||
BtMessageHandle msg = messageFactory->createRejectMessage(index,
|
||||
begin,
|
||||
length);
|
||||
BtMessageHandle msg = messageFactory->createRejectMessage(getIndex(),
|
||||
getBegin(),
|
||||
getLength());
|
||||
dispatcher->addMessageToQueue(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const unsigned char* BtRequestMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
* id --- 6, 1byte
|
||||
* index --- index, 4bytes
|
||||
* begin --- begin, 4bytes
|
||||
* length --- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
PeerMessageUtil::setIntParam(&msg[13], length);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtRequestMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtRequestMessage::toString() const {
|
||||
return "request index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
}
|
||||
|
||||
void BtRequestMessage::onQueued() {
|
||||
RequestSlot requestSlot(index, begin, length, blockIndex, pieceStorage->getPiece(index));
|
||||
void BtRequestMessage::onQueued()
|
||||
{
|
||||
RequestSlot requestSlot(getIndex(), getBegin(), getLength(), _blockIndex,
|
||||
pieceStorage->getPiece(getIndex()));
|
||||
dispatcher->addOutstandingRequest(requestSlot);
|
||||
}
|
||||
|
||||
bool BtRequestMessage::BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) {
|
||||
BtAbortOutstandingRequestEvent* intEvent = dynamic_cast<BtAbortOutstandingRequestEvent*>(event.get());
|
||||
bool BtRequestMessage::
|
||||
BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event)
|
||||
{
|
||||
BtAbortOutstandingRequestEvent* intEvent =
|
||||
dynamic_cast<BtAbortOutstandingRequestEvent*>(event.get());
|
||||
return intEvent != 0;
|
||||
}
|
||||
|
||||
void BtRequestMessage::BtAbortOutstandingRequestEventListener::handleEventInternal(const BtEventHandle& event) {
|
||||
void BtRequestMessage::
|
||||
BtAbortOutstandingRequestEventListener::handleEventInternal
|
||||
(const BtEventHandle& event)
|
||||
{
|
||||
message->handleAbortOutstandingRequestEvent(event);
|
||||
}
|
||||
|
||||
void BtRequestMessage::handleAbortOutstandingRequestEvent(const BtEventHandle& event) {
|
||||
BtAbortOutstandingRequestEvent* intEvent = (BtAbortOutstandingRequestEvent*)event.get();
|
||||
if(index == intEvent->getPiece()->getIndex() &&
|
||||
void BtRequestMessage::
|
||||
handleAbortOutstandingRequestEvent(const BtEventHandle& event)
|
||||
{
|
||||
BtAbortOutstandingRequestEvent* intEvent =
|
||||
(BtAbortOutstandingRequestEvent*)event.get();
|
||||
if(getIndex() == intEvent->getPiece()->getIndex() &&
|
||||
!invalidate &&
|
||||
!sendingInProgress) {
|
||||
invalidate = true;
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_REQUEST_MESSAGE_H_
|
||||
#define _D_BT_REQUEST_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "RangeBtMessage.h"
|
||||
#include "AbstractBtEventListener.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
@ -44,75 +44,53 @@ class BtRequestMessage;
|
|||
|
||||
typedef SharedHandle<BtRequestMessage> BtRequestMessageHandle;
|
||||
|
||||
class BtRequestMessage : public SimpleBtMessage {
|
||||
class BtRequestMessage : public RangeBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
uint32_t begin;
|
||||
uint32_t length;
|
||||
size_t blockIndex;
|
||||
unsigned char* msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 17;
|
||||
size_t _blockIndex;
|
||||
|
||||
class BtAbortOutstandingRequestEventListener:public AbstractBtEventListener {
|
||||
private:
|
||||
BtRequestMessage* message;
|
||||
public:
|
||||
BtAbortOutstandingRequestEventListener(BtRequestMessage* message):message(message) {}
|
||||
BtAbortOutstandingRequestEventListener(BtRequestMessage* message):
|
||||
message(message) {}
|
||||
|
||||
virtual bool canHandle(const SharedHandle<BtEvent>& event);
|
||||
|
||||
virtual void handleEventInternal(const SharedHandle<BtEvent>& event);
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtAbortOutstandingRequestEventListener> BtAbortOutstandingRequestEventListenerHandle;
|
||||
typedef SharedHandle<BtAbortOutstandingRequestEventListener>
|
||||
BtAbortOutstandingRequestEventListenerHandle;
|
||||
public:
|
||||
BtRequestMessage(size_t index = 0,
|
||||
uint32_t begin = 0,
|
||||
uint32_t length = 0,
|
||||
size_t blockIndex = 0)
|
||||
:SimpleBtMessage(ID),
|
||||
index(index),
|
||||
begin(begin),
|
||||
length(length),
|
||||
blockIndex(blockIndex),
|
||||
msg(0)
|
||||
:RangeBtMessage(ID, NAME, index, begin, length),
|
||||
_blockIndex(blockIndex)
|
||||
{
|
||||
SharedHandle<BtEventListener> listener(new BtAbortOutstandingRequestEventListener(this));
|
||||
SharedHandle<BtEventListener> listener
|
||||
(new BtAbortOutstandingRequestEventListener(this));
|
||||
addEventListener(listener);
|
||||
}
|
||||
|
||||
virtual ~BtRequestMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
|
||||
static const uint8_t ID = 6;
|
||||
|
||||
size_t getIndex() const { return index; }
|
||||
void setIndex(size_t index) { this->index = index; }
|
||||
static const std::string NAME;
|
||||
|
||||
uint32_t getBegin() const { return begin; }
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
size_t getBlockIndex() const { return _blockIndex; }
|
||||
void setBlockIndex(size_t blockIndex) { _blockIndex = blockIndex; }
|
||||
|
||||
uint32_t getLength() const { return length; }
|
||||
void setLength(uint32_t length) { this->length = length; }
|
||||
|
||||
size_t getBlockIndex() const { return blockIndex; }
|
||||
void setBlockIndex(size_t blockIndex) { this->blockIndex = blockIndex; }
|
||||
|
||||
static BtRequestMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtRequestMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
virtual void onQueued();
|
||||
|
||||
virtual void handleAbortOutstandingRequestEvent(const SharedHandle<BtEvent>& event);
|
||||
virtual void handleAbortOutstandingRequestEvent
|
||||
(const SharedHandle<BtEvent>& event);
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
/* <!-- 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 --> */
|
||||
#ifndef _D_BT_REQUEST_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_REQUEST_MESSAGE_VALIDATOR_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtRequestMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtRequestMessageValidator : public BtMessageValidator {
|
||||
private:
|
||||
const BtRequestMessage* message;
|
||||
size_t numPiece;
|
||||
size_t pieceLength;
|
||||
public:
|
||||
BtRequestMessageValidator(const BtRequestMessage* message,
|
||||
size_t numPiece,
|
||||
size_t pieceLength):
|
||||
message(message),
|
||||
numPiece(numPiece),
|
||||
pieceLength(pieceLength) {}
|
||||
|
||||
virtual bool validate(Errors& error) {
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
PeerMessageUtil::checkBegin(message->getBegin(), pieceLength);
|
||||
PeerMessageUtil::checkLength(message->getLength());
|
||||
PeerMessageUtil::checkRange(message->getBegin(),
|
||||
message->getLength(),
|
||||
pieceLength);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtRequestMessageValidator> BtRequestMessageValidatorHandle;
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_REQUEST_MESSAGE_VALIDATOR_H_
|
|
@ -33,50 +33,15 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtSuggestPieceMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "Util.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "suggest piece", dataLength, 5).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "suggest piece", ID).str());
|
||||
}
|
||||
BtSuggestPieceMessageHandle message(new BtSuggestPieceMessage());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
return message;
|
||||
}
|
||||
const std::string BtSuggestPieceMessage::NAME("suggest piece");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
size_t BtSuggestPieceMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string BtSuggestPieceMessage::toString() const {
|
||||
return "suggest piece index="+Util::itos(index);
|
||||
SharedHandle<BtSuggestPieceMessage> BtSuggestPieceMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return IndexBtMessage::create<BtSuggestPieceMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_SUGGEST_PIECE_MESSAGE_H_
|
||||
#define _D_BT_SUGGEST_PIECE_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "IndexBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,37 +43,20 @@ class BtSuggestPieceMessage;
|
|||
|
||||
typedef SharedHandle<BtSuggestPieceMessage> BtSuggestPieceMessageHandle;
|
||||
|
||||
class BtSuggestPieceMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t index;
|
||||
unsigned char* msg;
|
||||
static const size_t MESSAGE_LENGTH = 9;
|
||||
class BtSuggestPieceMessage : public IndexBtMessage {
|
||||
public:
|
||||
BtSuggestPieceMessage():SimpleBtMessage(ID), index(0), msg(0) {}
|
||||
|
||||
virtual ~BtSuggestPieceMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtSuggestPieceMessage():IndexBtMessage(ID, NAME, 0) {}
|
||||
|
||||
static const uint8_t ID = 13;
|
||||
|
||||
void setIndex(size_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
static const std::string NAME;
|
||||
|
||||
size_t getIndex() const { return index; }
|
||||
|
||||
static BtSuggestPieceMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static SharedHandle<BtSuggestPieceMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction() {
|
||||
// TODO Current implementation ignores this message.
|
||||
}
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -33,60 +33,30 @@
|
|||
*/
|
||||
/* copyright --> */
|
||||
#include "BtUnchokeMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "DlAbortEx.h"
|
||||
#include "message.h"
|
||||
#include "Peer.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, size_t dataLength) {
|
||||
if(dataLength != 1) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, "unchoke", dataLength, 1).str());
|
||||
}
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "unchoke", ID).str());
|
||||
}
|
||||
BtUnchokeMessageHandle message(new BtUnchokeMessage());
|
||||
return message;
|
||||
const std::string BtUnchokeMessage::NAME("unchoke");
|
||||
|
||||
SharedHandle<BtUnchokeMessage> BtUnchokeMessage::create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
return ZeroBtMessage::create<BtUnchokeMessage>(data, dataLength);
|
||||
}
|
||||
|
||||
void BtUnchokeMessage::doReceivedAction() {
|
||||
void BtUnchokeMessage::doReceivedAction()
|
||||
{
|
||||
peer->peerChoking(false);
|
||||
}
|
||||
|
||||
bool BtUnchokeMessage::sendPredicate() const {
|
||||
bool BtUnchokeMessage::sendPredicate() const
|
||||
{
|
||||
return peer->amChoking();
|
||||
}
|
||||
|
||||
const unsigned char* BtUnchokeMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 1, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
size_t BtUnchokeMessage::getMessageLength() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
void BtUnchokeMessage::onSendComplete() {
|
||||
peer->amChoking(false);
|
||||
}
|
||||
|
||||
std::string BtUnchokeMessage::toString() const {
|
||||
static const std::string UNCHOKE("unchoke");
|
||||
return UNCHOKE;
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#ifndef _D_BT_UNCHOKE_MESSAGE_H_
|
||||
#define _D_BT_UNCHOKE_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -43,29 +43,22 @@ class BtUnchokeMessage;
|
|||
|
||||
typedef SharedHandle<BtUnchokeMessage> BtUnchokeMessageHandle;
|
||||
|
||||
class BtUnchokeMessage : public SimpleBtMessage {
|
||||
class BtUnchokeMessage : public ZeroBtMessage {
|
||||
private:
|
||||
unsigned char* msg;
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
public:
|
||||
BtUnchokeMessage():SimpleBtMessage(ID), msg(0) {}
|
||||
|
||||
virtual ~BtUnchokeMessage() {
|
||||
delete [] msg;
|
||||
}
|
||||
BtUnchokeMessage():ZeroBtMessage(ID, NAME) {}
|
||||
|
||||
static const uint8_t ID = 1;
|
||||
|
||||
static BtUnchokeMessageHandle create(const unsigned char* data, size_t dataLength);
|
||||
static const std::string NAME;
|
||||
|
||||
static SharedHandle<BtUnchokeMessage> create
|
||||
(const unsigned char* data, size_t dataLength);
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
virtual bool sendPredicate() const;
|
||||
|
||||
virtual void onSendComplete();
|
||||
|
|
|
@ -41,24 +41,20 @@
|
|||
#include "BtInterestedMessage.h"
|
||||
#include "BtNotInterestedMessage.h"
|
||||
#include "BtHaveMessage.h"
|
||||
#include "BtHaveMessageValidator.h"
|
||||
#include "BtBitfieldMessage.h"
|
||||
#include "BtBitfieldMessageValidator.h"
|
||||
#include "RangeBtMessageValidator.h"
|
||||
#include "IndexBtMessageValidator.h"
|
||||
#include "BtRequestMessage.h"
|
||||
#include "BtRequestMessageValidator.h"
|
||||
#include "BtCancelMessage.h"
|
||||
#include "BtCancelMessageValidator.h"
|
||||
#include "BtPieceMessage.h"
|
||||
#include "BtPieceMessageValidator.h"
|
||||
#include "BtPortMessage.h"
|
||||
#include "BtHaveAllMessage.h"
|
||||
#include "BtHaveNoneMessage.h"
|
||||
#include "BtRejectMessage.h"
|
||||
#include "BtRejectMessageValidator.h"
|
||||
#include "BtSuggestPieceMessage.h"
|
||||
#include "BtSuggestPieceMessageValidator.h"
|
||||
#include "BtAllowedFastMessage.h"
|
||||
#include "BtAllowedFastMessageValidator.h"
|
||||
#include "BtHandshakeMessage.h"
|
||||
#include "BtHandshakeMessageValidator.h"
|
||||
#include "BtExtendedMessage.h"
|
||||
|
@ -115,7 +111,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
|
|||
msg = BtHaveMessage::create(data, dataLength);
|
||||
{
|
||||
SharedHandle<BtMessageValidator> v
|
||||
(new BtHaveMessageValidator((BtHaveMessage*)msg.get(),
|
||||
(new IndexBtMessageValidator((BtHaveMessage*)msg.get(),
|
||||
btContext->getNumPieces()));
|
||||
msg->setBtMessageValidator(v);
|
||||
}
|
||||
|
@ -131,8 +127,9 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
|
|||
break;
|
||||
case BtRequestMessage::ID: {
|
||||
BtRequestMessageHandle temp = BtRequestMessage::create(data, dataLength);
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtRequestMessageValidator(temp.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(temp.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(temp->getIndex())));
|
||||
temp->setBtMessageValidator(validator);
|
||||
|
@ -141,8 +138,9 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
|
|||
}
|
||||
case BtCancelMessage::ID: {
|
||||
BtCancelMessageHandle temp = BtCancelMessage::create(data, dataLength);
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtCancelMessageValidator(temp.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(temp.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(temp->getIndex())));
|
||||
temp->setBtMessageValidator(validator);
|
||||
|
@ -167,8 +165,9 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
|
|||
break;
|
||||
case BtRejectMessage::ID: {
|
||||
BtRejectMessageHandle temp = BtRejectMessage::create(data, dataLength);
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtRejectMessageValidator(temp.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(temp.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(temp->getIndex())));
|
||||
temp->setBtMessageValidator(validator);
|
||||
|
@ -177,18 +176,16 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
|
|||
}
|
||||
case BtSuggestPieceMessage::ID: {
|
||||
BtSuggestPieceMessageHandle temp = BtSuggestPieceMessage::create(data, dataLength);
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtSuggestPieceMessageValidator(temp.get(),
|
||||
btContext->getNumPieces()));
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new IndexBtMessageValidator(temp.get(), btContext->getNumPieces()));
|
||||
temp->setBtMessageValidator(validator);
|
||||
msg = temp;
|
||||
break;
|
||||
}
|
||||
case BtAllowedFastMessage::ID: {
|
||||
BtAllowedFastMessageHandle temp = BtAllowedFastMessage::create(data, dataLength);
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtAllowedFastMessageValidator(temp.get(),
|
||||
btContext->getNumPieces()));
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new IndexBtMessageValidator(temp.get(), btContext->getNumPieces()));
|
||||
temp->setBtMessageValidator(validator);
|
||||
msg = temp;
|
||||
break;
|
||||
|
@ -264,8 +261,9 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, size_t b
|
|||
blockIndex*piece->getBlockLength(),
|
||||
piece->getBlockLength(blockIndex),
|
||||
blockIndex));
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtRequestMessageValidator(msg.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(msg.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(msg->getIndex())));
|
||||
msg->setBtMessageValidator(validator);
|
||||
|
@ -277,8 +275,9 @@ BtMessageHandle
|
|||
DefaultBtMessageFactory::createCancelMessage(size_t index, uint32_t begin, size_t length)
|
||||
{
|
||||
BtCancelMessageHandle msg(new BtCancelMessage(index, begin, length));
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtCancelMessageValidator(msg.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(msg.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(index)));
|
||||
msg->setBtMessageValidator(validator);
|
||||
|
@ -304,8 +303,7 @@ DefaultBtMessageFactory::createHaveMessage(size_t index)
|
|||
{
|
||||
BtHaveMessageHandle msg(new BtHaveMessage(index));
|
||||
SharedHandle<BtMessageValidator> v
|
||||
(new BtHaveMessageValidator(msg.get(),
|
||||
btContext->getNumPieces()));
|
||||
(new IndexBtMessageValidator(msg.get(), btContext->getNumPieces()));
|
||||
msg->setBtMessageValidator(v);
|
||||
setCommonProperty(msg);
|
||||
return msg;
|
||||
|
@ -385,8 +383,9 @@ BtMessageHandle
|
|||
DefaultBtMessageFactory::createRejectMessage(size_t index, uint32_t begin, size_t length)
|
||||
{
|
||||
BtRejectMessageHandle msg(new BtRejectMessage(index, begin, length));
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtRejectMessageValidator(msg.get(),
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new RangeBtMessageValidator
|
||||
(msg.get(),
|
||||
btContext->getNumPieces(),
|
||||
_pieceStorage->getPieceLength(index)));
|
||||
msg->setBtMessageValidator(validator);
|
||||
|
@ -398,9 +397,8 @@ BtMessageHandle
|
|||
DefaultBtMessageFactory::createAllowedFastMessage(size_t index)
|
||||
{
|
||||
BtAllowedFastMessageHandle msg(new BtAllowedFastMessage(index));
|
||||
BtMessageValidatorHandle validator
|
||||
(new BtAllowedFastMessageValidator(msg.get(),
|
||||
btContext->getNumPieces()));
|
||||
SharedHandle<BtMessageValidator> validator
|
||||
(new IndexBtMessageValidator(msg.get(), btContext->getNumPieces()));
|
||||
msg->setBtMessageValidator(validator);
|
||||
setCommonProperty(msg);
|
||||
return msg;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
||||
* Copyright (C) 2009 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
|
||||
|
@ -32,34 +32,35 @@
|
|||
* files in the program, then also delete it here.
|
||||
*/
|
||||
/* copyright --> */
|
||||
#ifndef _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtAllowedFastMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
#include "IndexBtMessage.h"
|
||||
#include "Util.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtAllowedFastMessageValidator : public BtMessageValidator {
|
||||
private:
|
||||
const BtAllowedFastMessage* message;
|
||||
size_t numPiece;
|
||||
public:
|
||||
BtAllowedFastMessageValidator(const BtAllowedFastMessage* message,
|
||||
size_t numPiece):
|
||||
message(message),
|
||||
numPiece(numPiece) {}
|
||||
|
||||
virtual bool validate(Errors& error) {
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
return true;
|
||||
const unsigned char* IndexBtMessage::getMessage()
|
||||
{
|
||||
if(!_msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
* id --- ?, 1byte
|
||||
* piece index --- index, 4bytes
|
||||
* total: 9bytes
|
||||
*/
|
||||
_msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 5, getId());
|
||||
PeerMessageUtil::setIntParam(&_msg[5], _index);
|
||||
}
|
||||
return _msg;
|
||||
}
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtAllowedFastMessageValidator> BtAllowedFastMessageValidatorHandle;
|
||||
size_t IndexBtMessage::getMessageLength()
|
||||
{
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string IndexBtMessage::toString() const
|
||||
{
|
||||
return getName()+" index="+Util::itos(_index);
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_ALLOWED_FAST_MESSAGE_VALIDATOR_H_
|
|
@ -2,7 +2,7 @@
|
|||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
||||
* Copyright (C) 2009 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
|
||||
|
@ -32,42 +32,52 @@
|
|||
* files in the program, then also delete it here.
|
||||
*/
|
||||
/* copyright --> */
|
||||
#ifndef _D_BT_REJECT_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_REJECT_MESSAGE_VALIDATOR_H_
|
||||
#ifndef _D_INDEX_BT_MESSAGE_H_
|
||||
#define _D_INDEX_BT_MESSAGE_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtRejectMessage.h"
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtRejectMessageValidator : public BtMessageValidator {
|
||||
class IndexBtMessage : public SimpleBtMessage {
|
||||
private:
|
||||
const BtRejectMessage* message;
|
||||
size_t numPiece;
|
||||
size_t pieceLength;
|
||||
public:
|
||||
BtRejectMessageValidator(const BtRejectMessage* message,
|
||||
size_t numPiece,
|
||||
size_t pieceLength):
|
||||
message(message),
|
||||
numPiece(numPiece),
|
||||
pieceLength(pieceLength) {}
|
||||
size_t _index;
|
||||
unsigned char* _msg;
|
||||
|
||||
virtual bool validate(Errors& error) {
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
PeerMessageUtil::checkBegin(message->getBegin(), pieceLength);
|
||||
PeerMessageUtil::checkLength(message->getLength());
|
||||
PeerMessageUtil::checkRange(message->getBegin(),
|
||||
message->getLength(),
|
||||
pieceLength);
|
||||
return true;
|
||||
static const size_t MESSAGE_LENGTH = 9;
|
||||
protected:
|
||||
template<typename T>
|
||||
static SharedHandle<T> create(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
PeerMessageUtil::assertPayloadLengthEqual(5, dataLength, T::NAME);
|
||||
PeerMessageUtil::assertID(T::ID, data, T::NAME);
|
||||
SharedHandle<T> message(new T());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
return message;
|
||||
}
|
||||
};
|
||||
public:
|
||||
IndexBtMessage(uint8_t id, const std::string& name, size_t index)
|
||||
:SimpleBtMessage(id, name),
|
||||
_index(index),
|
||||
_msg(0) {}
|
||||
|
||||
typedef SharedHandle<BtRejectMessageValidator> BtRejectMessageValidatorHandle;
|
||||
virtual ~IndexBtMessage()
|
||||
{
|
||||
delete [] _msg;
|
||||
}
|
||||
|
||||
void setIndex(size_t index) { _index = index; }
|
||||
|
||||
size_t getIndex() const { return _index; }
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_REJECT_MESSAGE_VALIDATOR_H_
|
||||
#endif // _D_INDEX_BT_MESSAGE_H_
|
|
@ -2,7 +2,7 @@
|
|||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
||||
* Copyright (C) 2009 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
|
||||
|
@ -32,28 +32,29 @@
|
|||
* files in the program, then also delete it here.
|
||||
*/
|
||||
/* copyright --> */
|
||||
#ifndef _D_BT_HAVE_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_HAVE_MESSAGE_VALIDATOR_H_
|
||||
#ifndef _D_INDEX_BT_VALIDATOR_H_
|
||||
#define _D_INDEX_BT_VALIDATOR_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtHaveMessage.h"
|
||||
#include "IndexBtMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtHaveMessageValidator : public BtMessageValidator {
|
||||
class IndexBtMessageValidator : public BtMessageValidator {
|
||||
private:
|
||||
const BtHaveMessage* message;
|
||||
size_t numPiece;
|
||||
const IndexBtMessage* _message;
|
||||
size_t _numPiece;
|
||||
public:
|
||||
BtHaveMessageValidator(const BtHaveMessage* message,
|
||||
IndexBtMessageValidator(const IndexBtMessage* message,
|
||||
size_t numPiece):
|
||||
message(message),
|
||||
numPiece(numPiece) {}
|
||||
_message(message),
|
||||
_numPiece(numPiece) {}
|
||||
|
||||
virtual bool validate(Errors& errors) {
|
||||
virtual bool validate(Errors& errors)
|
||||
{
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -61,4 +62,4 @@ public:
|
|||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_HAVE_MESSAGE_VALIDATOR_H_
|
||||
#endif // _D_INDEX_BT_MESSAGE_VALIDATOR_H_
|
|
@ -296,14 +296,8 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
|
|||
BtUnchokeMessage.cc BtUnchokeMessage.h\
|
||||
BtHandshakeMessage.cc BtHandshakeMessage.h\
|
||||
BtMessageValidator.h\
|
||||
BtAllowedFastMessageValidator.h\
|
||||
BtBitfieldMessageValidator.h\
|
||||
BtCancelMessageValidator.h\
|
||||
BtHaveMessageValidator.h\
|
||||
BtPieceMessageValidator.h\
|
||||
BtRejectMessageValidator.h\
|
||||
BtRequestMessageValidator.h\
|
||||
BtSuggestPieceMessageValidator.h\
|
||||
BtHandshakeMessageValidator.h\
|
||||
BtMessageFactory.h\
|
||||
DefaultBtMessageFactory.cc DefaultBtMessageFactory.h\
|
||||
|
@ -414,7 +408,11 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
|
|||
LibsslDHKeyExchange.h\
|
||||
BtConstants.h\
|
||||
BtLeecherStateChoke.cc BtLeecherStateChoke.h\
|
||||
BtSeederStateChoke.cc BtSeederStateChoke.h
|
||||
BtSeederStateChoke.cc BtSeederStateChoke.h\
|
||||
RangeBtMessage.cc RangeBtMessage.h\
|
||||
IndexBtMessage.cc IndexBtMessage.h\
|
||||
ZeroBtMessage.cc ZeroBtMessage.h\
|
||||
RangeBtMessageValidator.h
|
||||
endif # ENABLE_BITTORRENT
|
||||
|
||||
if ENABLE_METALINK
|
||||
|
|
|
@ -100,14 +100,8 @@ bin_PROGRAMS = aria2c$(EXEEXT)
|
|||
@ENABLE_BITTORRENT_TRUE@ BtUnchokeMessage.cc BtUnchokeMessage.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtHandshakeMessage.cc BtHandshakeMessage.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtAllowedFastMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtBitfieldMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtCancelMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtHaveMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtPieceMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtRejectMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtRequestMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtSuggestPieceMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtHandshakeMessageValidator.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtMessageFactory.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ DefaultBtMessageFactory.cc DefaultBtMessageFactory.h\
|
||||
|
@ -218,7 +212,11 @@ bin_PROGRAMS = aria2c$(EXEEXT)
|
|||
@ENABLE_BITTORRENT_TRUE@ LibsslDHKeyExchange.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtConstants.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtLeecherStateChoke.cc BtLeecherStateChoke.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.cc BtSeederStateChoke.h
|
||||
@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.cc BtSeederStateChoke.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ RangeBtMessage.cc RangeBtMessage.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ IndexBtMessage.cc IndexBtMessage.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ ZeroBtMessage.cc ZeroBtMessage.h\
|
||||
@ENABLE_BITTORRENT_TRUE@ RangeBtMessageValidator.h
|
||||
|
||||
@ENABLE_METALINK_TRUE@am__append_10 = Metalinker.cc Metalinker.h\
|
||||
@ENABLE_METALINK_TRUE@ MetalinkEntry.cc MetalinkEntry.h\
|
||||
|
@ -464,10 +462,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
|
|||
BtSuggestPieceMessage.cc BtSuggestPieceMessage.h \
|
||||
BtUnchokeMessage.cc BtUnchokeMessage.h BtHandshakeMessage.cc \
|
||||
BtHandshakeMessage.h BtMessageValidator.h \
|
||||
BtAllowedFastMessageValidator.h BtBitfieldMessageValidator.h \
|
||||
BtCancelMessageValidator.h BtHaveMessageValidator.h \
|
||||
BtPieceMessageValidator.h BtRejectMessageValidator.h \
|
||||
BtRequestMessageValidator.h BtSuggestPieceMessageValidator.h \
|
||||
BtBitfieldMessageValidator.h BtPieceMessageValidator.h \
|
||||
BtHandshakeMessageValidator.h BtMessageFactory.h \
|
||||
DefaultBtMessageFactory.cc DefaultBtMessageFactory.h \
|
||||
BtMessageDispatcher.h DefaultBtMessageDispatcher.cc \
|
||||
|
@ -544,9 +539,11 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
|
|||
LibsslARC4Decryptor.h LibsslARC4Encryptor.h \
|
||||
LibsslDHKeyExchange.h BtConstants.h BtLeecherStateChoke.cc \
|
||||
BtLeecherStateChoke.h BtSeederStateChoke.cc \
|
||||
BtSeederStateChoke.h Metalinker.cc Metalinker.h \
|
||||
MetalinkEntry.cc MetalinkEntry.h MetalinkResource.cc \
|
||||
MetalinkResource.h MetalinkProcessor.h \
|
||||
BtSeederStateChoke.h RangeBtMessage.cc RangeBtMessage.h \
|
||||
IndexBtMessage.cc IndexBtMessage.h ZeroBtMessage.cc \
|
||||
ZeroBtMessage.h RangeBtMessageValidator.h Metalinker.cc \
|
||||
Metalinker.h MetalinkEntry.cc MetalinkEntry.h \
|
||||
MetalinkResource.cc MetalinkResource.h MetalinkProcessor.h \
|
||||
MetalinkProcessorFactory.cc MetalinkProcessorFactory.h \
|
||||
MetalinkParserController.cc MetalinkParserController.h \
|
||||
MetalinkParserStateMachine.cc MetalinkParserStateMachine.h \
|
||||
|
@ -698,7 +695,10 @@ am__objects_2 =
|
|||
@ENABLE_BITTORRENT_TRUE@ ReceiverMSEHandshakeCommand.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ MSEHandshake.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ BtLeecherStateChoke.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.$(OBJEXT)
|
||||
@ENABLE_BITTORRENT_TRUE@ BtSeederStateChoke.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ RangeBtMessage.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ IndexBtMessage.$(OBJEXT) \
|
||||
@ENABLE_BITTORRENT_TRUE@ ZeroBtMessage.$(OBJEXT)
|
||||
@ENABLE_METALINK_TRUE@am__objects_10 = Metalinker.$(OBJEXT) \
|
||||
@ENABLE_METALINK_TRUE@ MetalinkEntry.$(OBJEXT) \
|
||||
@ENABLE_METALINK_TRUE@ MetalinkResource.$(OBJEXT) \
|
||||
|
@ -1427,6 +1427,7 @@ distclean-compile:
|
|||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpServerResponseCommand.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HttpSkipResponseCommand.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InOrderURISelector.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IndexBtMessage.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitialMetalinkParserState.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommand.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiateConnectionCommandFactory.Po@am__quote@
|
||||
|
@ -1484,6 +1485,7 @@ distclean-compile:
|
|||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PiecesMetalinkParserState.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Platform.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ProtocolDetector.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RangeBtMessage.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RarestPieceSelector.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RealtimeCommand.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ReceiverMSEHandshakeCommand.Po@am__quote@
|
||||
|
@ -1532,6 +1534,7 @@ distclean-compile:
|
|||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/VerificationMetalinkParserState.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/VersionMetalinkParserState.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/XML2SAXMetalinkProcessor.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ZeroBtMessage.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asctime_r.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bencode.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download_helper.Po@am__quote@
|
||||
|
|
|
@ -41,6 +41,8 @@
|
|||
#include "a2netcompat.h"
|
||||
#include "StringFormat.h"
|
||||
#include "BtConstants.h"
|
||||
#include "message.h"
|
||||
#include "StringFormat.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -180,4 +182,35 @@ PeerMessageUtil::unpackcompact(const unsigned char* compact)
|
|||
return std::pair<std::string, uint16_t>(host, port);
|
||||
}
|
||||
|
||||
|
||||
void PeerMessageUtil::assertPayloadLengthGreater
|
||||
(size_t threshold, size_t actual, const std::string& msgName)
|
||||
{
|
||||
if(actual <= threshold) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, msgName.c_str(), actual).str());
|
||||
}
|
||||
}
|
||||
|
||||
void PeerMessageUtil::assertPayloadLengthEqual
|
||||
(size_t expected, size_t actual, const std::string& msgName)
|
||||
{
|
||||
if(expected != actual) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_PAYLOAD_SIZE, msgName.c_str(),
|
||||
actual, expected).str());
|
||||
}
|
||||
}
|
||||
|
||||
void PeerMessageUtil::assertID
|
||||
(uint8_t expected, const unsigned char* data, const std::string& msgName)
|
||||
{
|
||||
uint8_t id = getId(data);
|
||||
if(expected != id) {
|
||||
throw DlAbortEx
|
||||
(StringFormat(EX_INVALID_BT_MESSAGE_ID, id, msgName.c_str(),
|
||||
expected).str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -79,6 +79,18 @@ public:
|
|||
static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port);
|
||||
|
||||
static std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
|
||||
|
||||
// Throws exception if threshold >= actual
|
||||
static void assertPayloadLengthGreater(size_t threshold, size_t actual,
|
||||
const std::string& msgName);
|
||||
|
||||
// Throws exception if expected != actual
|
||||
static void assertPayloadLengthEqual(size_t expected, size_t actual,
|
||||
const std::string& msgName);
|
||||
|
||||
// Throws exception if expected is not equal to id from data.
|
||||
static void assertID(uint8_t expected, const unsigned char* data,
|
||||
const std::string& msgName);
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/* <!-- copyright */
|
||||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2009 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 "RangeBtMessage.h"
|
||||
#include "Util.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
RangeBtMessage::RangeBtMessage(uint8_t id,
|
||||
const std::string& name,
|
||||
size_t index, uint32_t begin, size_t length)
|
||||
:SimpleBtMessage(id, name),
|
||||
_index(index),
|
||||
_begin(begin),
|
||||
_length(length),
|
||||
_msg(0) {}
|
||||
|
||||
RangeBtMessage::~RangeBtMessage()
|
||||
{
|
||||
delete [] _msg;
|
||||
}
|
||||
|
||||
const unsigned char* RangeBtMessage::getMessage()
|
||||
{
|
||||
if(!_msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
* id --- ?, 1byte
|
||||
* index --- index, 4bytes
|
||||
* begin --- begin, 4bytes
|
||||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
_msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 13, getId());
|
||||
PeerMessageUtil::setIntParam(&_msg[5], _index);
|
||||
PeerMessageUtil::setIntParam(&_msg[9], _begin);
|
||||
PeerMessageUtil::setIntParam(&_msg[13], _length);
|
||||
}
|
||||
return _msg;
|
||||
}
|
||||
|
||||
size_t RangeBtMessage::getMessageLength()
|
||||
{
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string RangeBtMessage::toString() const
|
||||
{
|
||||
return getName()+" index="+Util::uitos(_index)+", begin="+Util::uitos(_begin)+
|
||||
", length="+Util::uitos(_length);
|
||||
}
|
||||
|
||||
} // namespace aria2
|
|
@ -0,0 +1,91 @@
|
|||
/* <!-- copyright */
|
||||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2009 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 --> */
|
||||
#ifndef _D_RANGE_BT_MESSAGE_H_
|
||||
#define _D_RANGE_BT_MESSAGE_H_
|
||||
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class RangeBtMessage : public SimpleBtMessage {
|
||||
private:
|
||||
size_t _index;
|
||||
uint32_t _begin;
|
||||
size_t _length;
|
||||
unsigned char* _msg;
|
||||
|
||||
static const size_t MESSAGE_LENGTH = 17;
|
||||
protected:
|
||||
template<typename T>
|
||||
static SharedHandle<T> create
|
||||
(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
PeerMessageUtil::assertPayloadLengthEqual(13, dataLength, T::NAME);
|
||||
PeerMessageUtil::assertID(T::ID, data, T::NAME);
|
||||
SharedHandle<T> message(new T());
|
||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||
message->setBegin(PeerMessageUtil::getIntParam(data, 5));
|
||||
message->setLength(PeerMessageUtil::getIntParam(data, 9));
|
||||
return message;
|
||||
}
|
||||
public:
|
||||
RangeBtMessage(uint8_t id, const std::string& name,
|
||||
size_t index, uint32_t begin, size_t length);
|
||||
|
||||
virtual ~RangeBtMessage();
|
||||
|
||||
size_t getIndex() const { return _index; }
|
||||
|
||||
void setIndex(size_t index) { _index = index; }
|
||||
|
||||
uint32_t getBegin() const { return _begin; }
|
||||
|
||||
void setBegin(uint32_t begin) { _begin = begin; }
|
||||
|
||||
size_t getLength() const { return _length; }
|
||||
|
||||
void setLength(size_t length) { _length = length; }
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_RANGE_BT_MESSAGE_H_
|
|
@ -2,7 +2,7 @@
|
|||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
||||
* Copyright (C) 2009 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
|
||||
|
@ -32,35 +32,41 @@
|
|||
* files in the program, then also delete it here.
|
||||
*/
|
||||
/* copyright --> */
|
||||
#ifndef _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_
|
||||
#ifndef _D_RANGE_BT_MESSAGE_VALIDATOR_H_
|
||||
#define _D_RANGE_BT_MESSAGE_VALIDATOR_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtSuggestPieceMessage.h"
|
||||
#include "RangeBtMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtSuggestPieceMessageValidator : public BtMessageValidator {
|
||||
class RangeBtMessageValidator : public BtMessageValidator {
|
||||
private:
|
||||
const BtSuggestPieceMessage* message;
|
||||
size_t numPiece;
|
||||
const RangeBtMessage* _message;
|
||||
size_t _numPiece;
|
||||
size_t _pieceLength;
|
||||
public:
|
||||
BtSuggestPieceMessageValidator(const BtSuggestPieceMessage* message,
|
||||
size_t numPiece):
|
||||
message(message),
|
||||
numPiece(numPiece) {}
|
||||
RangeBtMessageValidator(const RangeBtMessage* message,
|
||||
size_t numPiece,
|
||||
size_t pieceLength):
|
||||
_message(message),
|
||||
_numPiece(numPiece),
|
||||
_pieceLength(pieceLength) {}
|
||||
|
||||
virtual bool validate(Errors& errors) {
|
||||
virtual bool validate(Errors& errors)
|
||||
{
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
PeerMessageUtil::checkIndex(_message->getIndex(), _numPiece);
|
||||
PeerMessageUtil::checkBegin(_message->getBegin(), _pieceLength);
|
||||
PeerMessageUtil::checkLength(_message->getLength());
|
||||
PeerMessageUtil::checkRange(_message->getBegin(),
|
||||
_message->getLength(),
|
||||
_pieceLength);
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtSuggestPieceMessageValidator> BtSuggestPieceMessageValidatorHandle;
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_SUGGEST_PIECE_MESSAGE_VALIDATOR_H_
|
||||
#endif // _D_RANGE_BT_MESSAGE_VALIDATOR_H_
|
|
@ -40,7 +40,8 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
SimpleBtMessage::SimpleBtMessage(uint8_t id):AbstractBtMessage(id) {}
|
||||
SimpleBtMessage::SimpleBtMessage(uint8_t id, const std::string& name):
|
||||
AbstractBtMessage(id, name) {}
|
||||
|
||||
SimpleBtMessage::~SimpleBtMessage() {}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace aria2 {
|
|||
|
||||
class SimpleBtMessage : public AbstractBtMessage {
|
||||
public:
|
||||
SimpleBtMessage(uint8_t id);
|
||||
SimpleBtMessage(uint8_t id, const std::string& name);
|
||||
|
||||
virtual ~SimpleBtMessage();
|
||||
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/* <!-- copyright */
|
||||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2009 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 "ZeroBtMessage.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
const unsigned char* ZeroBtMessage::getMessage()
|
||||
{
|
||||
if(!_msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- ?, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
_msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(_msg, MESSAGE_LENGTH, 1, getId());
|
||||
}
|
||||
return _msg;
|
||||
}
|
||||
|
||||
size_t ZeroBtMessage::getMessageLength()
|
||||
{
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
|
||||
std::string ZeroBtMessage::toString() const
|
||||
{
|
||||
return getName();
|
||||
}
|
||||
|
||||
} // namespace aria2
|
|
@ -2,7 +2,7 @@
|
|||
/*
|
||||
* aria2 - The high speed download utility
|
||||
*
|
||||
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
||||
* Copyright (C) 2009 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
|
||||
|
@ -32,41 +32,45 @@
|
|||
* files in the program, then also delete it here.
|
||||
*/
|
||||
/* copyright --> */
|
||||
#ifndef _D_BT_CANCEL_MESSAGE_VALIDATOR_H_
|
||||
#define _D_BT_CANCEL_MESSAGE_VALIDATOR_H_
|
||||
#ifndef _D_ZERO_BT_MESSAGE_H_
|
||||
#define _D_ZERO_BT_MESSAGE_H_
|
||||
|
||||
#include "BtMessageValidator.h"
|
||||
#include "BtCancelMessage.h"
|
||||
#include "SimpleBtMessage.h"
|
||||
#include "PeerMessageUtil.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class BtCancelMessageValidator : public BtMessageValidator {
|
||||
class ZeroBtMessage : public SimpleBtMessage {
|
||||
private:
|
||||
const BtCancelMessage* message;
|
||||
size_t numPiece;
|
||||
size_t pieceLength;
|
||||
public:
|
||||
BtCancelMessageValidator(const BtCancelMessage* message,
|
||||
size_t numPiece,
|
||||
size_t pieceLength):
|
||||
message(message),
|
||||
numPiece(numPiece),
|
||||
pieceLength(pieceLength) {}
|
||||
unsigned char* _msg;
|
||||
|
||||
virtual bool validate(Errors& error) {
|
||||
// TODO
|
||||
PeerMessageUtil::checkIndex(message->getIndex(), numPiece);
|
||||
PeerMessageUtil::checkBegin(message->getBegin(), pieceLength);
|
||||
PeerMessageUtil::checkLength(message->getLength());
|
||||
PeerMessageUtil::checkRange(message->getBegin(), message->getLength(),
|
||||
pieceLength);
|
||||
return true;
|
||||
static const size_t MESSAGE_LENGTH = 5;
|
||||
protected:
|
||||
template<typename T>
|
||||
static SharedHandle<T> create(const unsigned char* data, size_t dataLength)
|
||||
{
|
||||
PeerMessageUtil::assertPayloadLengthEqual(1, dataLength, T::NAME);
|
||||
PeerMessageUtil::assertID(T::ID, data, T::NAME);
|
||||
SharedHandle<T> message(new T());
|
||||
return message;
|
||||
}
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtCancelMessageValidator> BtCancelMessageValidatorHandle;
|
||||
public:
|
||||
ZeroBtMessage(uint8_t id, const std::string& name):
|
||||
SimpleBtMessage(id, name), _msg(0) {}
|
||||
|
||||
virtual ~ZeroBtMessage()
|
||||
{
|
||||
delete [] _msg;
|
||||
}
|
||||
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual size_t getMessageLength();
|
||||
|
||||
virtual std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace aria2
|
||||
|
||||
#endif // _D_BT_CANCEL_MESSAGE_VALIDATOR_H_
|
||||
#endif // _D_ZERO_BT_MESSAGE_H_
|
|
@ -14,7 +14,7 @@
|
|||
#include "Peer.h"
|
||||
#include "FileEntry.h"
|
||||
#include "BtHandshakeMessage.h"
|
||||
#include "BtRequestMessageValidator.h"
|
||||
#include "RangeBtMessageValidator.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -144,7 +144,7 @@ void BtRequestMessageTest::testCreate() {
|
|||
CPPUNIT_ASSERT_EQUAL((uint8_t)6, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1024, pm->getLength());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
@ -289,7 +289,7 @@ void BtRequestMessageTest::testValidate() {
|
|||
BtRequestMessage msg(0, 0, 16*1024);
|
||||
msg.setBtMessageValidator
|
||||
(SharedHandle<BtMessageValidator>
|
||||
(new BtRequestMessageValidator(&msg, 1024, 256*1024)));
|
||||
(new RangeBtMessageValidator(&msg, 1024, 256*1024)));
|
||||
std::deque<std::string> errors;
|
||||
|
||||
msg.validate(errors);
|
||||
|
@ -299,7 +299,7 @@ void BtRequestMessageTest::testValidate_lengthTooLong() {
|
|||
BtRequestMessage msg(0, 0, 16*1024+1);
|
||||
msg.setBtMessageValidator
|
||||
(SharedHandle<BtMessageValidator>
|
||||
(new BtRequestMessageValidator(&msg, 1024, 256*1024)));
|
||||
(new RangeBtMessageValidator(&msg, 1024, 256*1024)));
|
||||
std::deque<std::string> errors;
|
||||
try {
|
||||
msg.validate(errors);
|
||||
|
|
Loading…
Reference in New Issue