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