mirror of https://github.com/aria2/aria2
Refactor BitTorrent message buffer usage
parent
cc8375f0b0
commit
a7237c69f7
|
@ -97,7 +97,7 @@ void BtBitfieldMessage::doReceivedAction()
|
|||
}
|
||||
}
|
||||
|
||||
unsigned char* BtBitfieldMessage::createMessage()
|
||||
std::vector<unsigned char> BtBitfieldMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 1+bitfieldLength, 4bytes
|
||||
|
@ -106,9 +106,10 @@ unsigned char* BtBitfieldMessage::createMessage()
|
|||
* total: 5+bitfieldLength bytes
|
||||
*/
|
||||
const size_t msgLength = 5 + bitfieldLength_;
|
||||
auto msg = new unsigned char[msgLength];
|
||||
bittorrent::createPeerMessageString(msg, msgLength, 1 + bitfieldLength_, ID);
|
||||
memcpy(msg + 5, bitfield_.get(), bitfieldLength_);
|
||||
auto msg = std::vector<unsigned char>(msgLength);
|
||||
bittorrent::createPeerMessageString(msg.data(), msgLength,
|
||||
1 + bitfieldLength_, ID);
|
||||
memcpy(msg.data() + 5, bitfield_.get(), bitfieldLength_);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ public:
|
|||
|
||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ BtExtendedMessage::BtExtendedMessage(
|
|||
{
|
||||
}
|
||||
|
||||
unsigned char* BtExtendedMessage::createMessage()
|
||||
std::vector<unsigned char> BtExtendedMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 2+extpayload.length, 4bytes
|
||||
|
@ -69,10 +69,12 @@ unsigned char* BtExtendedMessage::createMessage()
|
|||
*/
|
||||
std::string payload = extensionMessage_->getPayload();
|
||||
msgLength_ = 6 + payload.size();
|
||||
auto msg = new unsigned char[msgLength_];
|
||||
bittorrent::createPeerMessageString(msg, msgLength_, 2 + payload.size(), ID);
|
||||
*(msg + 5) = extensionMessage_->getExtensionMessageID();
|
||||
memcpy(msg + 6, payload.data(), payload.size());
|
||||
auto msg = std::vector<unsigned char>(msgLength_);
|
||||
auto p = msg.data();
|
||||
bittorrent::createPeerMessageString(p, msgLength_, 2 + payload.size(), ID);
|
||||
p += 5;
|
||||
*p++ = extensionMessage_->getExtensionMessageID();
|
||||
std::copy(std::begin(payload), std::end(payload), p);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ public:
|
|||
|
||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -81,10 +81,10 @@ BtHandshakeMessage::create(const unsigned char* data, size_t dataLength)
|
|||
return msg;
|
||||
}
|
||||
|
||||
unsigned char* BtHandshakeMessage::createMessage()
|
||||
std::vector<unsigned char> BtHandshakeMessage::createMessage()
|
||||
{
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
auto dst = msg;
|
||||
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
auto dst = msg.data();
|
||||
*dst++ = pstrlen_;
|
||||
dst = std::copy(std::begin(pstr_), std::end(pstr_), dst);
|
||||
dst = std::copy(std::begin(reserved_), std::end(reserved_), dst);
|
||||
|
|
|
@ -79,7 +79,7 @@ public:
|
|||
|
||||
virtual void doReceivedAction() CXX11_OVERRIDE{};
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -39,15 +39,13 @@ namespace aria2 {
|
|||
|
||||
const char BtKeepAliveMessage::NAME[] = "keep alive";
|
||||
|
||||
unsigned char* BtKeepAliveMessage::createMessage()
|
||||
std::vector<unsigned char> BtKeepAliveMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 0, 4bytes
|
||||
* total: 4bytes
|
||||
*/
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
memset(msg, 0, MESSAGE_LENGTH);
|
||||
return msg;
|
||||
return std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
}
|
||||
|
||||
size_t BtKeepAliveMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
||||
|
|
|
@ -52,7 +52,7 @@ public:
|
|||
|
||||
virtual void doReceivedAction() CXX11_OVERRIDE {}
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -213,17 +213,16 @@ void BtPieceMessage::send()
|
|||
void BtPieceMessage::pushPieceData(int64_t offset, int32_t length) const
|
||||
{
|
||||
assert(length <= static_cast<int32_t>(16_k));
|
||||
auto buf = make_unique<unsigned char[]>(length + MESSAGE_HEADER_LENGTH);
|
||||
createMessageHeader(buf.get());
|
||||
auto buf = std::vector<unsigned char>(length + MESSAGE_HEADER_LENGTH);
|
||||
createMessageHeader(buf.data());
|
||||
ssize_t r;
|
||||
r = getPieceStorage()->getDiskAdaptor()->readData(
|
||||
buf.get() + MESSAGE_HEADER_LENGTH, length, offset);
|
||||
buf.data() + MESSAGE_HEADER_LENGTH, length, offset);
|
||||
if (r == length) {
|
||||
const auto& peer = getPeer();
|
||||
getPeerConnection()->pushBytes(
|
||||
buf.release(), length + MESSAGE_HEADER_LENGTH,
|
||||
make_unique<PieceSendUpdate>(downloadContext_, peer,
|
||||
MESSAGE_HEADER_LENGTH));
|
||||
std::move(buf), make_unique<PieceSendUpdate>(downloadContext_, peer,
|
||||
MESSAGE_HEADER_LENGTH));
|
||||
peer->updateUploadSpeed(length);
|
||||
downloadContext_->updateUploadSpeed(length);
|
||||
}
|
||||
|
|
|
@ -100,7 +100,7 @@ void BtPortMessage::doReceivedAction()
|
|||
}
|
||||
}
|
||||
|
||||
unsigned char* BtPortMessage::createMessage()
|
||||
std::vector<unsigned char> BtPortMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
|
@ -108,8 +108,8 @@ unsigned char* BtPortMessage::createMessage()
|
|||
* port --- port number, 2bytes
|
||||
* total: 7bytes
|
||||
*/
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 3, ID);
|
||||
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 3, ID);
|
||||
bittorrent::setShortIntParam(&msg[5], port_);
|
||||
return msg;
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ public:
|
|||
|
||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
|
||||
namespace aria2 {
|
||||
|
||||
unsigned char* IndexBtMessage::createMessage()
|
||||
std::vector<unsigned char> IndexBtMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
|
@ -47,8 +47,8 @@ unsigned char* IndexBtMessage::createMessage()
|
|||
* piece index --- index, 4bytes
|
||||
* total: 9bytes
|
||||
*/
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 5, getId());
|
||||
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 5, getId());
|
||||
bittorrent::setIntParam(&msg[5], index_);
|
||||
return msg;
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ public:
|
|||
|
||||
size_t getIndex() const { return index_; }
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ namespace {
|
|||
|
||||
const size_t MAX_PAD_LENGTH = 512;
|
||||
const size_t CRYPTO_BITFIELD_LENGTH = 4;
|
||||
const unsigned char VC[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
constexpr auto VC = std::array<unsigned char, MSEHandshake::VC_LENGTH>{};
|
||||
|
||||
const unsigned char* PRIME = reinterpret_cast<const unsigned char*>(
|
||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B"
|
||||
|
@ -123,13 +123,15 @@ void MSEHandshake::initEncryptionFacility(bool initiator)
|
|||
void MSEHandshake::sendPublicKey()
|
||||
{
|
||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending public key.", cuid_));
|
||||
auto buf = make_unique<unsigned char[]>(KEY_LENGTH + MAX_PAD_LENGTH);
|
||||
dh_->getPublicKey(buf.get(), KEY_LENGTH);
|
||||
auto buf = std::vector<unsigned char>(KEY_LENGTH + MAX_PAD_LENGTH);
|
||||
dh_->getPublicKey(buf.data(), KEY_LENGTH);
|
||||
|
||||
size_t padLength =
|
||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||
dh_->generateNonce(buf.get() + KEY_LENGTH, padLength);
|
||||
socketBuffer_.pushBytes(buf.release(), KEY_LENGTH + padLength);
|
||||
dh_->generateNonce(buf.data() + KEY_LENGTH, padLength);
|
||||
buf.resize(KEY_LENGTH + padLength);
|
||||
|
||||
socketBuffer_.pushBytes(std::move(buf));
|
||||
}
|
||||
|
||||
void MSEHandshake::read()
|
||||
|
@ -209,16 +211,16 @@ void MSEHandshake::initCipher(const unsigned char* infoHash)
|
|||
enc.init(peerCipherKey, sizeof(peerCipherKey));
|
||||
// discard first 1024 bytes ARC4 output.
|
||||
enc.encrypt(garbage.size(), garbage.data(), garbage.data());
|
||||
enc.encrypt(VC_LENGTH, initiatorVCMarker_, VC);
|
||||
enc.encrypt(VC_LENGTH, initiatorVCMarker_, VC.data());
|
||||
}
|
||||
}
|
||||
|
||||
// Given data is pushed to socketBuffer_ and data will be deleted by
|
||||
// socketBuffer_.
|
||||
void MSEHandshake::encryptAndSendData(unsigned char* data, size_t length)
|
||||
void MSEHandshake::encryptAndSendData(std::vector<unsigned char> data)
|
||||
{
|
||||
encryptor_->encrypt(length, data, data);
|
||||
socketBuffer_.pushBytes(data, length);
|
||||
encryptor_->encrypt(data.size(), data.data(), data.data());
|
||||
socketBuffer_.pushBytes(std::move(data));
|
||||
}
|
||||
|
||||
void MSEHandshake::createReq1Hash(unsigned char* md) const
|
||||
|
@ -264,27 +266,25 @@ void MSEHandshake::sendInitiatorStep2()
|
|||
{
|
||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending negotiation step2.", cuid_));
|
||||
// Assuming no exception
|
||||
auto md = make_unique<unsigned char[]>((size_t)20);
|
||||
createReq1Hash(md.get());
|
||||
socketBuffer_.pushBytes(md.release(), 20);
|
||||
auto md = std::vector<unsigned char>(20);
|
||||
createReq1Hash(md.data());
|
||||
socketBuffer_.pushBytes(std::move(md));
|
||||
// Assuming no exception
|
||||
md = make_unique<unsigned char[]>((size_t)20);
|
||||
createReq23Hash(md.get(), infoHash_);
|
||||
socketBuffer_.pushBytes(md.release(), 20);
|
||||
md = std::vector<unsigned char>(20);
|
||||
createReq23Hash(md.data(), infoHash_);
|
||||
socketBuffer_.pushBytes(std::move(md));
|
||||
// buffer is filled in this order:
|
||||
// VC(VC_LENGTH bytes),
|
||||
// crypto_provide(CRYPTO_BITFIELD_LENGTH bytes),
|
||||
// len(padC)(2 bytes),
|
||||
// padC(len(padC) bytes <= MAX_PAD_LENGTH),
|
||||
// len(IA)(2 bytes)
|
||||
auto buffer = make_unique<unsigned char[]>(
|
||||
40 + VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + MAX_PAD_LENGTH + 2);
|
||||
unsigned char* ptr = buffer.get();
|
||||
auto buffer = std::vector<unsigned char>(VC_LENGTH + CRYPTO_BITFIELD_LENGTH +
|
||||
2 + MAX_PAD_LENGTH + 2);
|
||||
auto ptr = std::begin(buffer);
|
||||
// VC
|
||||
memcpy(ptr, VC, sizeof(VC));
|
||||
ptr += sizeof(VC);
|
||||
ptr += VC_LENGTH;
|
||||
// crypto_provide
|
||||
memset(ptr, 0, CRYPTO_BITFIELD_LENGTH);
|
||||
if (!option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) &&
|
||||
option_->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
|
||||
ptr[3] = CRYPTO_PLAIN_TEXT;
|
||||
|
@ -294,24 +294,21 @@ void MSEHandshake::sendInitiatorStep2()
|
|||
// len(padC)
|
||||
uint16_t padCLength =
|
||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||
{
|
||||
uint16_t padCLengthBE = htons(padCLength);
|
||||
memcpy(ptr, &padCLengthBE, sizeof(padCLengthBE));
|
||||
}
|
||||
ptr += 2;
|
||||
uint16_t padCLengthBE = htons(padCLength);
|
||||
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&padCLengthBE),
|
||||
sizeof(padCLengthBE), ptr);
|
||||
// padC
|
||||
memset(ptr, 0, padCLength);
|
||||
ptr += padCLength;
|
||||
// len(IA)
|
||||
// currently, IA is zero-length.
|
||||
uint16_t iaLength = 0;
|
||||
{
|
||||
uint16_t iaLengthBE = htons(iaLength);
|
||||
memcpy(ptr, &iaLengthBE, sizeof(iaLengthBE));
|
||||
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&iaLengthBE),
|
||||
sizeof(iaLengthBE), ptr);
|
||||
}
|
||||
ptr += 2;
|
||||
size_t buflen = ptr - buffer.get();
|
||||
encryptAndSendData(buffer.release(), buflen);
|
||||
buffer.erase(ptr, std::end(buffer));
|
||||
encryptAndSendData(std::move(buffer));
|
||||
}
|
||||
|
||||
// This function reads exactly until the end of VC marker is reached.
|
||||
|
@ -494,8 +491,8 @@ bool MSEHandshake::receiveReceiverIA()
|
|||
wantRead_ = true;
|
||||
return false;
|
||||
}
|
||||
ia_ = make_unique<unsigned char[]>(iaLength_);
|
||||
decryptor_->encrypt(iaLength_, ia_.get(), rbuf_);
|
||||
ia_ = std::vector<unsigned char>(iaLength_);
|
||||
decryptor_->encrypt(iaLength_, ia_.data(), rbuf_);
|
||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - IA received.", cuid_));
|
||||
// shift rbuf_
|
||||
shiftBuffer(iaLength_);
|
||||
|
@ -509,27 +506,24 @@ void MSEHandshake::sendReceiverStep2()
|
|||
// cryptoSelect(CRYPTO_BITFIELD_LENGTH bytes),
|
||||
// len(padD)(2bytes),
|
||||
// padD(len(padD)bytes <= MAX_PAD_LENGTH)
|
||||
auto buffer = make_unique<unsigned char[]>(
|
||||
VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + MAX_PAD_LENGTH);
|
||||
unsigned char* ptr = buffer.get();
|
||||
auto buffer = std::vector<unsigned char>(VC_LENGTH + CRYPTO_BITFIELD_LENGTH +
|
||||
2 + MAX_PAD_LENGTH);
|
||||
auto ptr = std::begin(buffer);
|
||||
// VC
|
||||
memcpy(ptr, VC, sizeof(VC));
|
||||
ptr += sizeof(VC);
|
||||
ptr += VC_LENGTH;
|
||||
// crypto_select
|
||||
memset(ptr, 0, CRYPTO_BITFIELD_LENGTH);
|
||||
ptr[3] = negotiatedCryptoType_;
|
||||
ptr += CRYPTO_BITFIELD_LENGTH;
|
||||
// len(padD)
|
||||
uint16_t padDLength =
|
||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||
uint16_t padDLengthBE = htons(padDLength);
|
||||
memcpy(ptr, &padDLengthBE, sizeof(padDLengthBE));
|
||||
ptr += sizeof(padDLengthBE);
|
||||
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&padDLengthBE),
|
||||
sizeof(padDLengthBE), ptr);
|
||||
// padD, all zeroed
|
||||
memset(ptr, 0, padDLength);
|
||||
ptr += padDLength;
|
||||
size_t buflen = ptr - buffer.get();
|
||||
encryptAndSendData(buffer.release(), buflen);
|
||||
buffer.erase(ptr, std::end(buffer));
|
||||
encryptAndSendData(std::move(buffer));
|
||||
}
|
||||
|
||||
uint16_t MSEHandshake::verifyPadLength(const unsigned char* padlenbuf,
|
||||
|
@ -549,7 +543,7 @@ void MSEHandshake::verifyVC(unsigned char* vcbuf)
|
|||
{
|
||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Verifying VC.", cuid_));
|
||||
decryptor_->encrypt(VC_LENGTH, vcbuf, vcbuf);
|
||||
if (memcmp(VC, vcbuf, VC_LENGTH) != 0) {
|
||||
if (!std::equal(std::begin(VC), std::end(VC), vcbuf)) {
|
||||
throw DL_ABORT_EX(
|
||||
fmt("Invalid VC: %s", util::toHex(vcbuf, VC_LENGTH).c_str()));
|
||||
}
|
||||
|
|
|
@ -67,14 +67,15 @@ public:
|
|||
CRYPTO_ARC4 = 0x02u
|
||||
};
|
||||
|
||||
static constexpr size_t VC_LENGTH = 8U;
|
||||
|
||||
private:
|
||||
static const size_t PRIME_BITS = 768U;
|
||||
static const size_t KEY_LENGTH = (PRIME_BITS + 7U) / 8U;
|
||||
static const size_t VC_LENGTH = 8U;
|
||||
static constexpr size_t PRIME_BITS = 768U;
|
||||
static constexpr size_t KEY_LENGTH = (PRIME_BITS + 7U) / 8U;
|
||||
// The largest buffering occurs when receiver receives step2
|
||||
// handshake. We believe that IA is less than or equal to
|
||||
// BtHandshakeMessage::MESSAGE_LENGTH
|
||||
static const size_t MAX_BUFFER_LENGTH = 636U;
|
||||
static constexpr size_t MAX_BUFFER_LENGTH = 636U;
|
||||
|
||||
cuid_t cuid_;
|
||||
std::shared_ptr<SocketCore> socket_;
|
||||
|
@ -97,10 +98,10 @@ private:
|
|||
size_t markerIndex_;
|
||||
uint16_t padLength_;
|
||||
uint16_t iaLength_;
|
||||
std::unique_ptr<unsigned char[]> ia_;
|
||||
std::vector<unsigned char> ia_;
|
||||
std::unique_ptr<MessageDigest> sha1_;
|
||||
|
||||
void encryptAndSendData(unsigned char* data, size_t length);
|
||||
void encryptAndSendData(std::vector<unsigned char> data);
|
||||
|
||||
void createReq1Hash(unsigned char* md) const;
|
||||
|
||||
|
@ -171,7 +172,7 @@ public:
|
|||
void sendReceiverStep2();
|
||||
|
||||
// returns plain text IA
|
||||
const unsigned char* getIA() const { return ia_.get(); }
|
||||
const unsigned char* getIA() const { return ia_.data(); }
|
||||
|
||||
size_t getIALength() const { return iaLength_; }
|
||||
|
||||
|
|
|
@ -83,13 +83,13 @@ PeerConnection::PeerConnection(cuid_t cuid, const std::shared_ptr<Peer>& peer,
|
|||
|
||||
PeerConnection::~PeerConnection() {}
|
||||
|
||||
void PeerConnection::pushBytes(unsigned char* data, size_t len,
|
||||
void PeerConnection::pushBytes(std::vector<unsigned char> data,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||
{
|
||||
if (encryptionEnabled_) {
|
||||
encryptor_->encrypt(len, data, data);
|
||||
encryptor_->encrypt(data.size(), data.data(), data.data());
|
||||
}
|
||||
socketBuffer_.pushBytes(data, len, std::move(progressUpdate));
|
||||
socketBuffer_.pushBytes(std::move(data), std::move(progressUpdate));
|
||||
}
|
||||
|
||||
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength)
|
||||
|
|
|
@ -95,7 +95,7 @@ public:
|
|||
|
||||
// Pushes data into send buffer. After this call, this object gets
|
||||
// ownership of data, so caller must not delete or alter it.
|
||||
void pushBytes(unsigned char* data, size_t len,
|
||||
void pushBytes(std::vector<unsigned char> data,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate =
|
||||
std::unique_ptr<ProgressUpdate>{});
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ RangeBtMessage::RangeBtMessage(uint8_t id, const char* name, size_t index,
|
|||
{
|
||||
}
|
||||
|
||||
unsigned char* RangeBtMessage::createMessage()
|
||||
std::vector<unsigned char> RangeBtMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
|
@ -55,8 +55,8 @@ unsigned char* RangeBtMessage::createMessage()
|
|||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 13, getId());
|
||||
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 13, getId());
|
||||
bittorrent::setIntParam(&msg[5], index_);
|
||||
bittorrent::setIntParam(&msg[9], begin_);
|
||||
bittorrent::setIntParam(&msg[13], length_);
|
||||
|
|
|
@ -75,7 +75,7 @@ public:
|
|||
|
||||
void setLength(int32_t length) { length_ = length; }
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -56,11 +56,11 @@ void SimpleBtMessage::send()
|
|||
A2_LOG_INFO(fmt(MSG_SEND_PEER_MESSAGE, getCuid(),
|
||||
getPeer()->getIPAddress().c_str(), getPeer()->getPort(),
|
||||
toString().c_str()));
|
||||
unsigned char* msg = createMessage();
|
||||
auto msg = createMessage();
|
||||
size_t msgLength = getMessageLength();
|
||||
A2_LOG_DEBUG(
|
||||
fmt("msglength = %lu bytes", static_cast<unsigned long>(msgLength)));
|
||||
getPeerConnection()->pushBytes(msg, msgLength, getProgressUpdate());
|
||||
fmt("msglength = %lu bytes", static_cast<unsigned long>(msg.size())));
|
||||
getPeerConnection()->pushBytes(std::move(msg), getProgressUpdate());
|
||||
}
|
||||
|
||||
std::unique_ptr<ProgressUpdate> SimpleBtMessage::getProgressUpdate()
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
|
||||
#include "AbstractBtMessage.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
struct ProgressUpdate;
|
||||
|
@ -47,7 +49,7 @@ public:
|
|||
|
||||
virtual void send() CXX11_OVERRIDE;
|
||||
|
||||
virtual unsigned char* createMessage() = 0;
|
||||
virtual std::vector<unsigned char> createMessage() = 0;
|
||||
|
||||
virtual size_t getMessageLength() = 0;
|
||||
|
||||
|
|
|
@ -47,31 +47,34 @@
|
|||
namespace aria2 {
|
||||
|
||||
SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry(
|
||||
unsigned char* bytes, size_t length,
|
||||
std::vector<unsigned char> bytes,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||
: BufEntry(std::move(progressUpdate)), bytes_(bytes), length_(length)
|
||||
: BufEntry(std::move(progressUpdate)), bytes_(std::move(bytes))
|
||||
{
|
||||
}
|
||||
|
||||
SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() { delete[] bytes_; }
|
||||
SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() {}
|
||||
|
||||
ssize_t
|
||||
SocketBuffer::ByteArrayBufEntry::send(const std::shared_ptr<SocketCore>& socket,
|
||||
size_t offset)
|
||||
{
|
||||
return socket->writeData(bytes_ + offset, length_ - offset);
|
||||
return socket->writeData(bytes_.data() + offset, bytes_.size() - offset);
|
||||
}
|
||||
|
||||
bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
|
||||
{
|
||||
return length_ <= offset;
|
||||
return bytes_.size() <= offset;
|
||||
}
|
||||
|
||||
size_t SocketBuffer::ByteArrayBufEntry::getLength() const { return length_; }
|
||||
size_t SocketBuffer::ByteArrayBufEntry::getLength() const
|
||||
{
|
||||
return bytes_.size();
|
||||
}
|
||||
|
||||
const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
|
||||
{
|
||||
return bytes_;
|
||||
return bytes_.data();
|
||||
}
|
||||
|
||||
SocketBuffer::StringBufEntry::StringBufEntry(
|
||||
|
@ -106,12 +109,12 @@ SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> socket)
|
|||
|
||||
SocketBuffer::~SocketBuffer() {}
|
||||
|
||||
void SocketBuffer::pushBytes(unsigned char* bytes, size_t len,
|
||||
void SocketBuffer::pushBytes(std::vector<unsigned char> bytes,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||
{
|
||||
if (len > 0) {
|
||||
bufq_.push_back(
|
||||
make_unique<ByteArrayBufEntry>(bytes, len, std::move(progressUpdate)));
|
||||
if (!bytes.empty()) {
|
||||
bufq_.push_back(make_unique<ByteArrayBufEntry>(std::move(bytes),
|
||||
std::move(progressUpdate)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
#include <string>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
|
@ -77,7 +78,7 @@ private:
|
|||
|
||||
class ByteArrayBufEntry : public BufEntry {
|
||||
public:
|
||||
ByteArrayBufEntry(unsigned char* bytes, size_t length,
|
||||
ByteArrayBufEntry(std::vector<unsigned char> bytes,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate);
|
||||
virtual ~ByteArrayBufEntry();
|
||||
virtual ssize_t send(const std::shared_ptr<SocketCore>& socket,
|
||||
|
@ -87,8 +88,7 @@ private:
|
|||
virtual const unsigned char* getData() const CXX11_OVERRIDE;
|
||||
|
||||
private:
|
||||
unsigned char* bytes_;
|
||||
size_t length_;
|
||||
std::vector<unsigned char> bytes_;
|
||||
};
|
||||
|
||||
class StringBufEntry : public BufEntry {
|
||||
|
@ -123,13 +123,11 @@ public:
|
|||
SocketBuffer(const SocketBuffer&) = delete;
|
||||
SocketBuffer& operator=(const SocketBuffer&) = delete;
|
||||
|
||||
// Feeds data pointed by bytes with length len into queue. This
|
||||
// object gets ownership of bytes, so caller must not delete or
|
||||
// later bytes after this call. This function doesn't send data. If
|
||||
// Feeds |bytes| into queue. This function doesn't send data. If
|
||||
// progressUpdate is not null, its update() function will be called
|
||||
// each time the data is sent. It will be deleted by this object. It
|
||||
// can be null.
|
||||
void pushBytes(unsigned char* bytes, size_t len,
|
||||
void pushBytes(std::vector<unsigned char> bytes,
|
||||
std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
|
||||
|
||||
// Feeds data into queue. This function doesn't send data. If
|
||||
|
|
|
@ -42,15 +42,15 @@ ZeroBtMessage::ZeroBtMessage(uint8_t id, const char* name)
|
|||
{
|
||||
}
|
||||
|
||||
unsigned char* ZeroBtMessage::createMessage()
|
||||
std::vector<unsigned char> ZeroBtMessage::createMessage()
|
||||
{
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- ?, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 1, getId());
|
||||
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 1, getId());
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ protected:
|
|||
public:
|
||||
ZeroBtMessage(uint8_t id, const char* name);
|
||||
|
||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
||||
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||
|
||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
||||
|
||||
|
|
|
@ -70,9 +70,9 @@ void BtAllowedFastMessageTest::testCreateMessage()
|
|||
unsigned char data[9];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
|
||||
bittorrent::setIntParam(&data[5], 12345);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtAllowedFastMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -76,9 +76,9 @@ void BtBitfieldMessageTest::testCreateMessage()
|
|||
unsigned char data[5 + 2];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 3, 5);
|
||||
memcpy(&data[5], bitfield, sizeof(bitfield));
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 7) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)7, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)7, msg.getMessageLength());
|
||||
}
|
||||
|
||||
|
|
|
@ -95,9 +95,9 @@ void BtCancelMessageTest::testCreateMessage()
|
|||
bittorrent::setIntParam(&data[5], 12345);
|
||||
bittorrent::setIntParam(&data[9], 256);
|
||||
bittorrent::setIntParam(&data[13], 1_k);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtCancelMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -109,9 +109,9 @@ void BtChokeMessageTest::testCreateMessage()
|
|||
BtChokeMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 0);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtChokeMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -78,9 +78,9 @@ void BtExtendedMessageTest::testCreateMessage()
|
|||
bittorrent::createPeerMessageString(data, sizeof(data), 13, 20);
|
||||
*(data + 5) = extendedMessageID;
|
||||
memcpy(data + 6, payload.c_str(), payload.size());
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)17, msg.getMessageLength());
|
||||
}
|
||||
|
||||
|
|
|
@ -73,23 +73,23 @@ void BtHandshakeMessageTest::testCreate()
|
|||
|
||||
void BtHandshakeMessageTest::testCreateMessage()
|
||||
{
|
||||
unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||
unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
||||
constexpr unsigned char infoHash[] = {
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||
constexpr unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
||||
|
||||
std::shared_ptr<BtHandshakeMessage> msg(new BtHandshakeMessage());
|
||||
auto msg = std::make_shared<BtHandshakeMessage>();
|
||||
msg->setInfoHash(infoHash);
|
||||
msg->setPeerId(peerId);
|
||||
|
||||
unsigned char data[68];
|
||||
createHandshakeMessageData(data);
|
||||
unsigned char* rawmsg = msg->createMessage();
|
||||
auto rawmsg = msg->createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)68, rawmsg.size());
|
||||
CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
|
||||
util::toHex(rawmsg, 68));
|
||||
delete[] rawmsg;
|
||||
util::toHex(rawmsg.data(), 68));
|
||||
}
|
||||
|
||||
void BtHandshakeMessageTest::testToString()
|
||||
|
|
|
@ -65,9 +65,9 @@ void BtHaveAllMessageTest::testCreateMessage()
|
|||
BtHaveAllMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtHaveAllMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -71,9 +71,9 @@ void BtHaveMessageTest::testCreateMessage()
|
|||
unsigned char data[9];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 4);
|
||||
bittorrent::setIntParam(&data[5], 12345);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtHaveMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -62,9 +62,9 @@ void BtHaveNoneMessageTest::testCreateMessage()
|
|||
BtHaveNoneMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 15);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtHaveNoneMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -64,9 +64,9 @@ void BtInterestedMessageTest::testCreateMessage()
|
|||
BtInterestedMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtInterestedMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -28,9 +28,9 @@ void BtKeepAliveMessageTest::testCreateMessage()
|
|||
BtKeepAliveMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)99, message.getId());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)4, message.getMessageLength());
|
||||
unsigned char* rawmsg = message.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 4) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = message.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)4, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtKeepAliveMessageTest::testToString()
|
||||
|
|
|
@ -64,9 +64,9 @@ void BtNotInterestedMessageTest::testCreateMessage()
|
|||
BtNotInterestedMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtNotInterestedMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -99,9 +99,9 @@ void BtPortMessageTest::testCreateMessage()
|
|||
unsigned char data[7];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
|
||||
bittorrent::setShortIntParam(&data[5], 6881);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 7) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)7, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtPortMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -129,9 +129,9 @@ void BtRejectMessageTest::testCreateMessage()
|
|||
bittorrent::setIntParam(&data[5], 12345);
|
||||
bittorrent::setIntParam(&data[9], 256);
|
||||
bittorrent::setIntParam(&data[13], 1_k);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtRejectMessageTest::testDoReceivedAction()
|
||||
|
|
|
@ -151,9 +151,9 @@ void BtRequestMessageTest::testCreateMessage()
|
|||
bittorrent::setIntParam(&data[5], 12345);
|
||||
bittorrent::setIntParam(&data[9], 256);
|
||||
bittorrent::setIntParam(&data[13], 1_k);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking()
|
||||
|
|
|
@ -62,9 +62,9 @@ void BtSuggestPieceMessageTest::testCreateMessage()
|
|||
unsigned char data[9];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
|
||||
bittorrent::setIntParam(&data[5], 12345);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtSuggestPieceMessageTest::testToString()
|
||||
|
|
|
@ -64,9 +64,9 @@ void BtUnchokeMessageTest::testCreateMessage()
|
|||
BtUnchokeMessage msg;
|
||||
unsigned char data[5];
|
||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
|
||||
unsigned char* rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
||||
delete[] rawmsg;
|
||||
auto rawmsg = msg.createMessage();
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||
}
|
||||
|
||||
void BtUnchokeMessageTest::testDoReceivedAction()
|
||||
|
|
Loading…
Reference in New Issue