mirror of https://github.com/aria2/aria2
2007-01-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/message.h: Added EX_INVALID_PAYLOAD_SIZE and EX_INVALID_BT_MESSAGE_ID. Following source files affected. * src/BtAllowedFastMessage.cc: * src/BtBitfieldMessage.cc * src/BtCancelMessage.cc * src/BtChokeMessage.cc * src/BtHaveAllMessage.cc * src/BtHaveMessage.cc * src/BtHaveNoneMessage.cc * src/BtInterestedMessage.cc * src/BtNotInterestedMessage.cc * src/BtPieceMessage.cc * src/BtPortMessage.cc * src/BtRejectMessage.cc * src/BtRequestMessage.cc * src/BtSuggestPieceMessage.cc * src/BtUnchokeMessage.cc * src/message.h: Added EX_INVALID_CHUNK_CHECKSUM. Following source files are affected. * src/ChunkChecksumValidator.cc * src/SegmentMan.ccpull/1/head
parent
139f46332e
commit
cd9e35bc63
25
ChangeLog
25
ChangeLog
|
@ -1,3 +1,28 @@
|
||||||
|
2007-01-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
|
* src/message.h: Added EX_INVALID_PAYLOAD_SIZE and
|
||||||
|
EX_INVALID_BT_MESSAGE_ID. Following source files affected.
|
||||||
|
* src/BtAllowedFastMessage.cc:
|
||||||
|
* src/BtBitfieldMessage.cc
|
||||||
|
* src/BtCancelMessage.cc
|
||||||
|
* src/BtChokeMessage.cc
|
||||||
|
* src/BtHaveAllMessage.cc
|
||||||
|
* src/BtHaveMessage.cc
|
||||||
|
* src/BtHaveNoneMessage.cc
|
||||||
|
* src/BtInterestedMessage.cc
|
||||||
|
* src/BtNotInterestedMessage.cc
|
||||||
|
* src/BtPieceMessage.cc
|
||||||
|
* src/BtPortMessage.cc
|
||||||
|
* src/BtRejectMessage.cc
|
||||||
|
* src/BtRequestMessage.cc
|
||||||
|
* src/BtSuggestPieceMessage.cc
|
||||||
|
* src/BtUnchokeMessage.cc
|
||||||
|
|
||||||
|
* src/message.h: Added EX_INVALID_CHUNK_CHECKSUM. Following source
|
||||||
|
files are affected.
|
||||||
|
* src/ChunkChecksumValidator.cc
|
||||||
|
* src/SegmentMan.cc
|
||||||
|
|
||||||
2007-01-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2007-01-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
To add chunk checksum validation:
|
To add chunk checksum validation:
|
||||||
|
|
2
TODO
2
TODO
|
@ -23,8 +23,8 @@
|
||||||
* Fix DefaultBtProgressInfoFile.cc: save(), load()
|
* Fix DefaultBtProgressInfoFile.cc: save(), load()
|
||||||
* remove blockIndex
|
* remove blockIndex
|
||||||
* Add an ability of seeding
|
* Add an ability of seeding
|
||||||
|
* Continue file allocation with existing file
|
||||||
|
|
||||||
* Stopping while piece hash checking and file allocation
|
* Stopping while piece hash checking and file allocation
|
||||||
* Stop download after selective download completes
|
* Stop download after selective download completes
|
||||||
* Remove -pg option in Makefile.am
|
* Remove -pg option in Makefile.am
|
||||||
* Continue file allocation with existing file
|
|
|
@ -55,7 +55,7 @@ AbstractDiskWriter::~AbstractDiskWriter()
|
||||||
closeFile();
|
closeFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::openFile(const string& filename, uint64_t totalLength) {
|
void AbstractDiskWriter::openFile(const string& filename, int64_t totalLength) {
|
||||||
File f(filename);
|
File f(filename);
|
||||||
if(f.exists()) {
|
if(f.exists()) {
|
||||||
openExistingFile(filename);
|
openExistingFile(filename);
|
||||||
|
@ -95,15 +95,15 @@ void AbstractDiskWriter::createFile(const string& filename, int32_t addFlags) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t AbstractDiskWriter::writeDataInternal(const char* data, uint32_t len) {
|
int32_t AbstractDiskWriter::writeDataInternal(const char* data, int32_t len) {
|
||||||
return write(fd, data, len);
|
return write(fd, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int AbstractDiskWriter::readDataInternal(char* data, uint32_t len) {
|
int32_t AbstractDiskWriter::readDataInternal(char* data, int32_t len) {
|
||||||
return read(fd, data, len);
|
return read(fd, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
string AbstractDiskWriter::messageDigest(int64_t offset, uint64_t length,
|
string AbstractDiskWriter::messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo)
|
const MessageDigestContext::DigestAlgo& algo)
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
|
@ -112,7 +112,7 @@ string AbstractDiskWriter::messageDigest(int64_t offset, uint64_t length,
|
||||||
|
|
||||||
int32_t BUFSIZE = 16*1024;
|
int32_t BUFSIZE = 16*1024;
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
for(uint64_t i = 0; i < length/BUFSIZE; i++) {
|
for(int64_t i = 0; i < length/BUFSIZE; i++) {
|
||||||
int32_t rs = readData(buf, BUFSIZE, offset);
|
int32_t rs = readData(buf, BUFSIZE, offset);
|
||||||
if(BUFSIZE != readData(buf, BUFSIZE, offset)) {
|
if(BUFSIZE != readData(buf, BUFSIZE, offset)) {
|
||||||
throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
|
throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
|
||||||
|
@ -143,14 +143,14 @@ void AbstractDiskWriter::seek(int64_t offset) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractDiskWriter::writeData(const char* data, uint32_t len, int64_t offset) {
|
void AbstractDiskWriter::writeData(const char* data, int32_t len, int64_t offset) {
|
||||||
seek(offset);
|
seek(offset);
|
||||||
if(writeDataInternal(data, len) < 0) {
|
if(writeDataInternal(data, len) < 0) {
|
||||||
throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
|
throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int AbstractDiskWriter::readData(char* data, uint32_t len, int64_t offset) {
|
int32_t AbstractDiskWriter::readData(char* data, int32_t len, int64_t offset) {
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
seek(offset);
|
seek(offset);
|
||||||
if((ret = readDataInternal(data, len)) < 0) {
|
if((ret = readDataInternal(data, len)) < 0) {
|
||||||
|
|
|
@ -49,8 +49,8 @@ protected:
|
||||||
void createFile(const string& filename, int32_t addFlags = 0);
|
void createFile(const string& filename, int32_t addFlags = 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int writeDataInternal(const char* data, uint32_t len);
|
int32_t writeDataInternal(const char* data, int32_t len);
|
||||||
int readDataInternal(char* data, uint32_t len);
|
int32_t readDataInternal(char* data, int32_t len);
|
||||||
|
|
||||||
void seek(int64_t offset);
|
void seek(int64_t offset);
|
||||||
|
|
||||||
|
@ -58,18 +58,18 @@ public:
|
||||||
AbstractDiskWriter();
|
AbstractDiskWriter();
|
||||||
virtual ~AbstractDiskWriter();
|
virtual ~AbstractDiskWriter();
|
||||||
|
|
||||||
virtual void openFile(const string& filename, uint64_t totalLength = 0);
|
virtual void openFile(const string& filename, int64_t totalLength = 0);
|
||||||
|
|
||||||
virtual void closeFile();
|
virtual void closeFile();
|
||||||
|
|
||||||
virtual void openExistingFile(const string& filename);
|
virtual void openExistingFile(const string& filename);
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo);
|
const MessageDigestContext::DigestAlgo& algo);
|
||||||
|
|
||||||
virtual void writeData(const char* data, uint32_t len, int64_t offset);
|
virtual void writeData(const char* data, int32_t len, int64_t offset);
|
||||||
|
|
||||||
virtual int readData(char* data, uint32_t len, int64_t offset);
|
virtual int32_t readData(char* data, int32_t len, int64_t offset);
|
||||||
|
|
||||||
void setFileAllocator(const FileAllocatorHandle& fileAllocator) {
|
void setFileAllocator(const FileAllocatorHandle& fileAllocator) {
|
||||||
this->fileAllocator = fileAllocator;
|
this->fileAllocator = fileAllocator;
|
||||||
|
|
|
@ -51,15 +51,15 @@ void AbstractSingleDiskAdaptor::openExistingFile() {
|
||||||
diskWriter->openExistingFile(getFilePath());
|
diskWriter->openExistingFile(getFilePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractSingleDiskAdaptor::writeData(const unsigned char* data, uint32_t len, int64_t offset) {
|
void AbstractSingleDiskAdaptor::writeData(const unsigned char* data, int32_t len, int64_t offset) {
|
||||||
diskWriter->writeData(data, len, offset);
|
diskWriter->writeData(data, len, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
int AbstractSingleDiskAdaptor::readData(unsigned char* data, uint32_t len, int64_t offset) {
|
int32_t AbstractSingleDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset) {
|
||||||
return diskWriter->readData(data, len, offset);
|
return diskWriter->readData(data, len, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
string AbstractSingleDiskAdaptor::messageDigest(int64_t offset, uint64_t length, const MessageDigestContext::DigestAlgo& algo) {
|
string AbstractSingleDiskAdaptor::messageDigest(int64_t offset, int64_t length, const MessageDigestContext::DigestAlgo& algo) {
|
||||||
return diskWriter->messageDigest(offset, length, algo);
|
return diskWriter->messageDigest(offset, length, algo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
class AbstractSingleDiskAdaptor : public DiskAdaptor {
|
class AbstractSingleDiskAdaptor : public DiskAdaptor {
|
||||||
protected:
|
protected:
|
||||||
DiskWriterHandle diskWriter;
|
DiskWriterHandle diskWriter;
|
||||||
uint64_t totalLength;
|
int64_t totalLength;
|
||||||
public:
|
public:
|
||||||
AbstractSingleDiskAdaptor():diskWriter(0), totalLength(0) {}
|
AbstractSingleDiskAdaptor():diskWriter(0), totalLength(0) {}
|
||||||
|
|
||||||
|
@ -55,12 +55,12 @@ public:
|
||||||
|
|
||||||
virtual void openExistingFile();
|
virtual void openExistingFile();
|
||||||
|
|
||||||
virtual void writeData(const unsigned char* data, uint32_t len,
|
virtual void writeData(const unsigned char* data, int32_t len,
|
||||||
int64_t offset);
|
int64_t offset);
|
||||||
|
|
||||||
virtual int readData(unsigned char* data, uint32_t len, int64_t offset);
|
virtual int32_t readData(unsigned char* data, int32_t len, int64_t offset);
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo);
|
const MessageDigestContext::DigestAlgo& algo);
|
||||||
|
|
||||||
virtual bool fileExists();
|
virtual bool fileExists();
|
||||||
|
@ -71,11 +71,11 @@ public:
|
||||||
|
|
||||||
DiskWriterHandle getDiskWriter() const { return diskWriter; }
|
DiskWriterHandle getDiskWriter() const { return diskWriter; }
|
||||||
|
|
||||||
void setTotalLength(const uint64_t& totalLength) {
|
void setTotalLength(const int64_t& totalLength) {
|
||||||
this->totalLength = totalLength;
|
this->totalLength = totalLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getTotalLength() const { return totalLength; }
|
int64_t getTotalLength() const { return totalLength; }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _D_ABSTRACT_SINGLE_DISK_ADAPTOR_H_
|
#endif // _D_ABSTRACT_SINGLE_DISK_ADAPTOR_H_
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
BitfieldMan::BitfieldMan(uint32_t blockLength, uint64_t totalLength)
|
BitfieldMan::BitfieldMan(int32_t blockLength, int64_t totalLength)
|
||||||
:blockLength(blockLength),
|
:blockLength(blockLength),
|
||||||
totalLength(totalLength),
|
totalLength(totalLength),
|
||||||
bitfield(0),
|
bitfield(0),
|
||||||
|
@ -104,19 +104,19 @@ BitfieldMan::~BitfieldMan() {
|
||||||
delete [] filterBitfield;
|
delete [] filterBitfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countSetBit(const unsigned char* bitfield, uint32_t len) const {
|
int32_t BitfieldMan::countSetBit(const unsigned char* bitfield, int32_t len) const {
|
||||||
uint32_t count = 0;
|
int32_t count = 0;
|
||||||
uint32_t size = sizeof(uint32_t);
|
int32_t size = sizeof(int32_t);
|
||||||
for(uint32_t i = 0; i < len/size; i++) {
|
for(int32_t i = 0; i < len/size; ++i) {
|
||||||
count += Util::countBit(*(uint32_t*)&bitfield[i*size]);
|
count += Util::countBit(*(uint32_t*)&bitfield[i*size]);
|
||||||
}
|
}
|
||||||
for(uint32_t i = len-len%size; i < len; i++) {
|
for(int32_t i = len-len%size; i < len; i++) {
|
||||||
count += Util::countBit((uint32_t)bitfield[i]);
|
count += Util::countBit((uint32_t)bitfield[i]);
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BitfieldMan::getNthBitIndex(const unsigned char bitfield, uint32_t nth) const {
|
int32_t BitfieldMan::getNthBitIndex(const unsigned char bitfield, int32_t nth) const {
|
||||||
int32_t index = -1;
|
int32_t index = -1;
|
||||||
for(int bs = 7; bs >= 0; bs--) {
|
for(int bs = 7; bs >= 0; bs--) {
|
||||||
unsigned char mask = 1 << bs;
|
unsigned char mask = 1 << bs;
|
||||||
|
@ -133,22 +133,22 @@ int32_t BitfieldMan::getNthBitIndex(const unsigned char bitfield, uint32_t nth)
|
||||||
|
|
||||||
int32_t
|
int32_t
|
||||||
BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
||||||
uint32_t bitfieldLength) const
|
int32_t bitfieldLength) const
|
||||||
{
|
{
|
||||||
uint32_t byte = (int32_t)(((double)bitfieldLength)*
|
int32_t byte = (int32_t)(((double)bitfieldLength)*
|
||||||
randomizer->getRandomNumber()/
|
randomizer->getRandomNumber()/
|
||||||
(randomizer->getMaxRandomNumber()+1.0));
|
(randomizer->getMaxRandomNumber()+1.0));
|
||||||
|
|
||||||
unsigned char lastMask = 0;
|
unsigned char lastMask = 0;
|
||||||
// the number of bytes in the last byte of bitfield
|
// the number of bytes in the last byte of bitfield
|
||||||
uint32_t lastByteLength = totalLength%(blockLength*8);
|
int32_t lastByteLength = totalLength%(blockLength*8);
|
||||||
// the number of block in the last byte of bitfield
|
// the number of block in the last byte of bitfield
|
||||||
uint32_t lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
|
int32_t lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
|
||||||
for(uint32_t i = 0; i < lastBlockCount; i++) {
|
for(int32_t i = 0; i < lastBlockCount; ++i) {
|
||||||
lastMask >>= 1;
|
lastMask >>= 1;
|
||||||
lastMask |= 0x80;
|
lastMask |= 0x80;
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char mask;
|
unsigned char mask;
|
||||||
if(byte == bitfieldLength-1) {
|
if(byte == bitfieldLength-1) {
|
||||||
mask = lastMask;
|
mask = lastMask;
|
||||||
|
@ -167,12 +167,12 @@ BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, uint32_t length) const {
|
bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int32_t length) const {
|
||||||
if(bitfieldLength != length) {
|
if(bitfieldLength != length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char temp = peerBitfield[i] & ~bitfield[i];
|
unsigned char temp = peerBitfield[i] & ~bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
temp &= filterBitfield[i];
|
temp &= filterBitfield[i];
|
||||||
|
@ -185,12 +185,12 @@ bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, uint32_t le
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
int32_t BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int32_t length) const {
|
||||||
if(bitfieldLength != length) {
|
if(bitfieldLength != length) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
tempBitfield[i] = peerBitfield[i] & ~bitfield[i];
|
tempBitfield[i] = peerBitfield[i] & ~bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
tempBitfield[i] &= filterBitfield[i];
|
tempBitfield[i] &= filterBitfield[i];
|
||||||
|
@ -201,12 +201,12 @@ int32_t BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, uint32_t
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
int32_t BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int32_t length) const {
|
||||||
if(bitfieldLength != length) {
|
if(bitfieldLength != length) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
tempBitfield[i] &= filterBitfield[i];
|
tempBitfield[i] &= filterBitfield[i];
|
||||||
|
@ -217,11 +217,11 @@ int32_t BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, ui
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
int32_t BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, int32_t length) const {
|
||||||
if(bitfieldLength != length) {
|
if(bitfieldLength != length) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char bit = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
unsigned char bit = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
bit &= filterBitfield[i];
|
bit &= filterBitfield[i];
|
||||||
|
@ -237,7 +237,7 @@ int32_t BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfiel
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t BitfieldMan::getFirstMissingUnusedIndex() const {
|
int32_t BitfieldMan::getFirstMissingUnusedIndex() const {
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char bit = ~bitfield[i] & ~useBitfield[i];
|
unsigned char bit = ~bitfield[i] & ~useBitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
bit &= filterBitfield[i];
|
bit &= filterBitfield[i];
|
||||||
|
@ -254,7 +254,7 @@ int32_t BitfieldMan::getFirstMissingUnusedIndex() const {
|
||||||
|
|
||||||
int32_t BitfieldMan::getMissingIndex() const {
|
int32_t BitfieldMan::getMissingIndex() const {
|
||||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
tempBitfield[i] = ~bitfield[i];
|
tempBitfield[i] = ~bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
tempBitfield[i] &= filterBitfield[i];
|
tempBitfield[i] &= filterBitfield[i];
|
||||||
|
@ -281,7 +281,7 @@ public:
|
||||||
Range(int32_t startIndex = 0, int32_t endIndex = 0):startIndex(startIndex),
|
Range(int32_t startIndex = 0, int32_t endIndex = 0):startIndex(startIndex),
|
||||||
endIndex(endIndex) {}
|
endIndex(endIndex) {}
|
||||||
|
|
||||||
uint32_t getSize() const {
|
int32_t getSize() const {
|
||||||
return endIndex-startIndex;
|
return endIndex-startIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,7 +317,7 @@ int32_t BitfieldMan::getEndIndex(int32_t index) const {
|
||||||
int32_t BitfieldMan::getSparseMissingUnusedIndex() const {
|
int32_t BitfieldMan::getSparseMissingUnusedIndex() const {
|
||||||
Range maxRange;
|
Range maxRange;
|
||||||
int32_t index = 0;
|
int32_t index = 0;
|
||||||
uint32_t blocks = countBlock();
|
int32_t blocks = countBlock();
|
||||||
Range currentRange;
|
Range currentRange;
|
||||||
while(index < (int32_t)blocks) {
|
while(index < (int32_t)blocks) {
|
||||||
currentRange.startIndex = getStartIndex(index);
|
currentRange.startIndex = getStartIndex(index);
|
||||||
|
@ -343,7 +343,7 @@ int32_t BitfieldMan::getSparseMissingUnusedIndex() const {
|
||||||
|
|
||||||
BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
||||||
BlockIndexes missingIndexes;
|
BlockIndexes missingIndexes;
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char bit = ~bitfield[i];
|
unsigned char bit = ~bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
bit &= filterBitfield[i];
|
bit &= filterBitfield[i];
|
||||||
|
@ -358,12 +358,12 @@ BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
||||||
return missingIndexes;
|
return missingIndexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, uint32_t peerBitfieldLength) const {
|
BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, int32_t peerBitfieldLength) const {
|
||||||
BlockIndexes missingIndexes;
|
BlockIndexes missingIndexes;
|
||||||
if(bitfieldLength != peerBitfieldLength) {
|
if(bitfieldLength != peerBitfieldLength) {
|
||||||
return missingIndexes;
|
return missingIndexes;
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
unsigned char bit = peerBitfield[i] & ~bitfield[i];
|
unsigned char bit = peerBitfield[i] & ~bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
bit &= filterBitfield[i];
|
bit &= filterBitfield[i];
|
||||||
|
@ -378,17 +378,17 @@ BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield
|
||||||
return missingIndexes;
|
return missingIndexes;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countMissingBlock() const {
|
int32_t BitfieldMan::countMissingBlock() const {
|
||||||
return cachedNumMissingBlock;
|
return cachedNumMissingBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countMissingBlockNow() const {
|
int32_t BitfieldMan::countMissingBlockNow() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
unsigned char* temp = new unsigned char[bitfieldLength];
|
unsigned char* temp = new unsigned char[bitfieldLength];
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
temp[i] = bitfield[i]&filterBitfield[i];
|
temp[i] = bitfield[i]&filterBitfield[i];
|
||||||
}
|
}
|
||||||
uint32_t count = countSetBit(filterBitfield, bitfieldLength)-
|
int32_t count = countSetBit(filterBitfield, bitfieldLength)-
|
||||||
countSetBit(temp, bitfieldLength);
|
countSetBit(temp, bitfieldLength);
|
||||||
delete [] temp;
|
delete [] temp;
|
||||||
return count;
|
return count;
|
||||||
|
@ -397,7 +397,7 @@ uint32_t BitfieldMan::countMissingBlockNow() const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countBlock() const {
|
int32_t BitfieldMan::countBlock() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
return cachedNumFilteredBlock;
|
return cachedNumFilteredBlock;
|
||||||
} else {
|
} else {
|
||||||
|
@ -405,7 +405,7 @@ uint32_t BitfieldMan::countBlock() const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countFilteredBlockNow() const {
|
int32_t BitfieldMan::countFilteredBlockNow() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
return countSetBit(filterBitfield, bitfieldLength);
|
return countSetBit(filterBitfield, bitfieldLength);
|
||||||
} else {
|
} else {
|
||||||
|
@ -446,14 +446,14 @@ bool BitfieldMan::unsetBit(int32_t index) {
|
||||||
|
|
||||||
bool BitfieldMan::isAllBitSet() const {
|
bool BitfieldMan::isAllBitSet() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
|
if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
for(uint32_t i = 0; i < bitfieldLength-1; i++) {
|
for(int32_t i = 0; i < bitfieldLength-1; ++i) {
|
||||||
if(bitfield[i] != 0xff) {
|
if(bitfield[i] != 0xff) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -480,7 +480,7 @@ bool BitfieldMan::isUseBitSet(int32_t index) const {
|
||||||
return isBitSetInternal(useBitfield, index);
|
return isBitSetInternal(useBitfield, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength) {
|
void BitfieldMan::setBitfield(const unsigned char* bitfield, int32_t bitfieldLength) {
|
||||||
if(this->bitfieldLength != bitfieldLength) {
|
if(this->bitfieldLength != bitfieldLength) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -495,7 +495,7 @@ void BitfieldMan::clearAllBit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllBit() {
|
void BitfieldMan::setAllBit() {
|
||||||
for(uint32_t i = 0; i < blocks; i++) {
|
for(int32_t i = 0; i < blocks; ++i) {
|
||||||
setBitInternal(bitfield, i, true);
|
setBitInternal(bitfield, i, true);
|
||||||
}
|
}
|
||||||
updateCache();
|
updateCache();
|
||||||
|
@ -507,7 +507,7 @@ void BitfieldMan::clearAllUseBit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllUseBit() {
|
void BitfieldMan::setAllUseBit() {
|
||||||
for(uint32_t i = 0; i < blocks; i++) {
|
for(int32_t i = 0; i < blocks; ++i) {
|
||||||
setBitInternal(useBitfield, i, true);
|
setBitInternal(useBitfield, i, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -516,7 +516,7 @@ bool BitfieldMan::setFilterBit(int32_t index) {
|
||||||
return setBitInternal(filterBitfield, index, true);
|
return setBitInternal(filterBitfield, index, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::addFilter(int64_t offset, uint64_t length) {
|
void BitfieldMan::addFilter(int64_t offset, int64_t length) {
|
||||||
if(!filterBitfield) {
|
if(!filterBitfield) {
|
||||||
filterBitfield = new unsigned char[bitfieldLength];
|
filterBitfield = new unsigned char[bitfieldLength];
|
||||||
memset(filterBitfield, 0, bitfieldLength);
|
memset(filterBitfield, 0, bitfieldLength);
|
||||||
|
@ -552,29 +552,29 @@ bool BitfieldMan::isFilterEnabled() const {
|
||||||
return filterEnabled;
|
return filterEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredTotalLength() const {
|
int64_t BitfieldMan::getFilteredTotalLength() const {
|
||||||
return cachedFilteredTotalLength;
|
return cachedFilteredTotalLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredTotalLengthNow() const {
|
int64_t BitfieldMan::getFilteredTotalLengthNow() const {
|
||||||
if(!filterBitfield) {
|
if(!filterBitfield) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
uint32_t filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
|
int32_t filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
|
||||||
if(filteredBlocks == 0) {
|
if(filteredBlocks == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(isBitSetInternal(filterBitfield, blocks-1)) {
|
if(isBitSetInternal(filterBitfield, blocks-1)) {
|
||||||
return ((uint64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
|
return ((int64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
|
||||||
} else {
|
} else {
|
||||||
return ((uint64_t)filteredBlocks)*blockLength;
|
return ((int64_t)filteredBlocks)*blockLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
int64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||||
unsigned char* temp = new unsigned char[bitfieldLength];
|
unsigned char* temp = new unsigned char[bitfieldLength];
|
||||||
if(useFilter) {
|
if(useFilter) {
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(int32_t i = 0; i < bitfieldLength; ++i) {
|
||||||
temp[i] = bitfield[i];
|
temp[i] = bitfield[i];
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
temp[i] &= filterBitfield[i];
|
temp[i] &= filterBitfield[i];
|
||||||
|
@ -583,34 +583,34 @@ uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||||
} else {
|
} else {
|
||||||
memcpy(temp, bitfield, bitfieldLength);
|
memcpy(temp, bitfield, bitfieldLength);
|
||||||
}
|
}
|
||||||
uint32_t completedBlocks = countSetBit(temp, bitfieldLength);
|
int32_t completedBlocks = countSetBit(temp, bitfieldLength);
|
||||||
uint64_t completedLength = 0;
|
int64_t completedLength = 0;
|
||||||
if(completedBlocks == 0) {
|
if(completedBlocks == 0) {
|
||||||
completedLength = 0;
|
completedLength = 0;
|
||||||
} else {
|
} else {
|
||||||
if(isBitSetInternal(temp, blocks-1)) {
|
if(isBitSetInternal(temp, blocks-1)) {
|
||||||
completedLength = ((uint64_t)completedBlocks-1)*blockLength+getLastBlockLength();
|
completedLength = ((int64_t)completedBlocks-1)*blockLength+getLastBlockLength();
|
||||||
} else {
|
} else {
|
||||||
completedLength = ((uint64_t)completedBlocks)*blockLength;
|
completedLength = ((int64_t)completedBlocks)*blockLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete [] temp;
|
delete [] temp;
|
||||||
return completedLength;
|
return completedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getCompletedLength() const {
|
int64_t BitfieldMan::getCompletedLength() const {
|
||||||
return cachedCompletedLength;
|
return cachedCompletedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getCompletedLengthNow() const {
|
int64_t BitfieldMan::getCompletedLengthNow() const {
|
||||||
return getCompletedLength(false);
|
return getCompletedLength(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredCompletedLength() const {
|
int64_t BitfieldMan::getFilteredCompletedLength() const {
|
||||||
return cachedFilteredComletedLength;
|
return cachedFilteredComletedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredCompletedLengthNow() const {
|
int64_t BitfieldMan::getFilteredCompletedLengthNow() const {
|
||||||
return getCompletedLength(true);
|
return getCompletedLength(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,26 +43,26 @@ typedef deque<int> BlockIndexes;
|
||||||
|
|
||||||
class BitfieldMan {
|
class BitfieldMan {
|
||||||
private:
|
private:
|
||||||
uint32_t blockLength;
|
int32_t blockLength;
|
||||||
uint64_t totalLength;
|
int64_t totalLength;
|
||||||
unsigned char* bitfield;
|
unsigned char* bitfield;
|
||||||
unsigned char* useBitfield;
|
unsigned char* useBitfield;
|
||||||
unsigned char* filterBitfield;
|
unsigned char* filterBitfield;
|
||||||
uint32_t bitfieldLength;
|
int32_t bitfieldLength;
|
||||||
uint32_t blocks;
|
int32_t blocks;
|
||||||
bool filterEnabled;
|
bool filterEnabled;
|
||||||
RandomizerHandle randomizer;
|
RandomizerHandle randomizer;
|
||||||
|
|
||||||
// for caching
|
// for caching
|
||||||
uint32_t cachedNumMissingBlock;
|
int32_t cachedNumMissingBlock;
|
||||||
uint32_t cachedNumFilteredBlock;
|
int32_t cachedNumFilteredBlock;
|
||||||
uint64_t cachedCompletedLength;
|
int64_t cachedCompletedLength;
|
||||||
uint64_t cachedFilteredComletedLength;
|
int64_t cachedFilteredComletedLength;
|
||||||
uint64_t cachedFilteredTotalLength;
|
int64_t cachedFilteredTotalLength;
|
||||||
|
|
||||||
uint32_t countSetBit(const unsigned char* bitfield, uint32_t len) const;
|
int32_t countSetBit(const unsigned char* bitfield, int32_t len) const;
|
||||||
int32_t getNthBitIndex(const unsigned char bit, uint32_t nth) const;
|
int32_t getNthBitIndex(const unsigned char bit, int32_t nth) const;
|
||||||
int32_t getMissingIndexRandomly(const unsigned char* bitfield, uint32_t len) const;
|
int32_t getMissingIndexRandomly(const unsigned char* bitfield, int32_t len) const;
|
||||||
bool isBitSetInternal(const unsigned char* bitfield, int32_t index) const;
|
bool isBitSetInternal(const unsigned char* bitfield, int32_t index) const;
|
||||||
bool setBitInternal(unsigned char* bitfield, int32_t index, bool on);
|
bool setBitInternal(unsigned char* bitfield, int32_t index, bool on);
|
||||||
bool setFilterBit(int32_t index);
|
bool setFilterBit(int32_t index);
|
||||||
|
@ -70,9 +70,9 @@ private:
|
||||||
int32_t getStartIndex(int32_t index) const;
|
int32_t getStartIndex(int32_t index) const;
|
||||||
int32_t getEndIndex(int32_t index) const;
|
int32_t getEndIndex(int32_t index) const;
|
||||||
|
|
||||||
uint64_t getCompletedLength(bool useFilter) const;
|
int64_t getCompletedLength(bool useFilter) const;
|
||||||
public:
|
public:
|
||||||
BitfieldMan(uint32_t blockLength, uint64_t totalLength);
|
BitfieldMan(int32_t blockLength, int64_t totalLength);
|
||||||
BitfieldMan(const BitfieldMan& bitfieldMan);
|
BitfieldMan(const BitfieldMan& bitfieldMan);
|
||||||
~BitfieldMan();
|
~BitfieldMan();
|
||||||
|
|
||||||
|
@ -105,13 +105,13 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getBlockLength() const { return blockLength; }
|
int32_t getBlockLength() const { return blockLength; }
|
||||||
|
|
||||||
uint32_t getLastBlockLength() const {
|
int32_t getLastBlockLength() const {
|
||||||
return totalLength-blockLength*(blocks-1);
|
return totalLength-blockLength*(blocks-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getBlockLength(int32_t index) const {
|
int32_t getBlockLength(int32_t index) const {
|
||||||
if(index == (int32_t)(blocks-1)) {
|
if(index == (int32_t)(blocks-1)) {
|
||||||
return getLastBlockLength();
|
return getLastBlockLength();
|
||||||
} else if(0 <= index && index < (int32_t)(blocks-1)) {
|
} else if(0 <= index && index < (int32_t)(blocks-1)) {
|
||||||
|
@ -121,16 +121,16 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getTotalLength() const { return totalLength; }
|
int64_t getTotalLength() const { return totalLength; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
bool hasMissingPiece(const unsigned char* bitfield, uint32_t len) const;
|
bool hasMissingPiece(const unsigned char* bitfield, int32_t len) const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
int32_t getMissingIndex(const unsigned char* bitfield, uint32_t len) const;
|
int32_t getMissingIndex(const unsigned char* bitfield, int32_t len) const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
|
@ -138,7 +138,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
int32_t getFirstMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
|
int32_t getFirstMissingUnusedIndex(const unsigned char* bitfield, int32_t len) const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
|
@ -146,7 +146,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
int32_t getMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
|
int32_t getMissingUnusedIndex(const unsigned char* bitfield, int32_t len) const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
|
@ -162,15 +162,15 @@ public:
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, uint32_t len) const;
|
BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, int32_t len) const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countMissingBlock() const;
|
int32_t countMissingBlock() const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countMissingBlockNow() const;
|
int32_t countMissingBlockNow() const;
|
||||||
|
|
||||||
bool setUseBit(int32_t index);
|
bool setUseBit(int32_t index);
|
||||||
bool unsetUseBit(int32_t index);
|
bool unsetUseBit(int32_t index);
|
||||||
|
@ -187,20 +187,20 @@ public:
|
||||||
bool isAllBitSet() const;
|
bool isAllBitSet() const;
|
||||||
|
|
||||||
const unsigned char* getBitfield() const { return bitfield; }
|
const unsigned char* getBitfield() const { return bitfield; }
|
||||||
uint32_t getBitfieldLength() const { return bitfieldLength; }
|
int32_t getBitfieldLength() const { return bitfieldLength; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countBlock() const;
|
int32_t countBlock() const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countFilteredBlockNow() const;
|
int32_t countFilteredBlockNow() const;
|
||||||
|
|
||||||
int32_t getMaxIndex() const { return blocks-1; }
|
int32_t getMaxIndex() const { return blocks-1; }
|
||||||
|
|
||||||
void setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength);
|
void setBitfield(const unsigned char* bitfield, int32_t bitfieldLength);
|
||||||
|
|
||||||
void clearAllBit();
|
void clearAllBit();
|
||||||
void setAllBit();
|
void setAllBit();
|
||||||
|
@ -208,7 +208,7 @@ public:
|
||||||
void clearAllUseBit();
|
void clearAllUseBit();
|
||||||
void setAllUseBit();
|
void setAllUseBit();
|
||||||
|
|
||||||
void addFilter(int64_t offset, uint64_t length);
|
void addFilter(int64_t offset, int64_t length);
|
||||||
/**
|
/**
|
||||||
* Clears filter and disables filter
|
* Clears filter and disables filter
|
||||||
*/
|
*/
|
||||||
|
@ -220,24 +220,24 @@ public:
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredTotalLength() const;
|
int64_t getFilteredTotalLength() const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredTotalLengthNow() const;
|
int64_t getFilteredTotalLengthNow() const;
|
||||||
|
|
||||||
uint64_t getCompletedLength() const;
|
int64_t getCompletedLength() const;
|
||||||
|
|
||||||
uint64_t getCompletedLengthNow() const;
|
int64_t getCompletedLengthNow() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredCompletedLength() const;
|
int64_t getFilteredCompletedLength() const;
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredCompletedLengthNow() const;
|
int64_t getFilteredCompletedLengthNow() const;
|
||||||
|
|
||||||
void setRandomizer(const RandomizerHandle& randomizer) {
|
void setRandomizer(const RandomizerHandle& randomizer) {
|
||||||
this->randomizer = randomizer;
|
this->randomizer = randomizer;
|
||||||
|
|
|
@ -36,15 +36,15 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 5) {
|
if(dataLength != 5) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "allowed fast", dataLength, 5);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "allowed fast", dataLength, 5);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "allowed fast", ID);
|
||||||
id, "allowed fast", ID);
|
|
||||||
}
|
}
|
||||||
BtAllowedFastMessageHandle message = new BtAllowedFastMessage();
|
BtAllowedFastMessageHandle message = new BtAllowedFastMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -59,7 +59,7 @@ void BtAllowedFastMessage::doReceivedAction() {
|
||||||
peer->addPeerAllowedIndex(index);
|
peer->addPeerAllowedIndex(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtAllowedFastMessage::MESSAGE_LENGTH = 9;
|
int32_t BtAllowedFastMessage::MESSAGE_LENGTH = 9;
|
||||||
|
|
||||||
const unsigned char* BtAllowedFastMessage::getMessage() {
|
const unsigned char* BtAllowedFastMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -76,7 +76,7 @@ const unsigned char* BtAllowedFastMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtAllowedFastMessage::getMessageLength() {
|
int32_t BtAllowedFastMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtAllowedFastMessage(int32_t index = 0)
|
BtAllowedFastMessage(int32_t index = 0)
|
||||||
:SimpleBtMessage(),
|
:SimpleBtMessage(),
|
||||||
|
@ -57,22 +57,22 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 17;
|
static const int8_t ID = 17;
|
||||||
|
|
||||||
void setIndex(int32_t index) {
|
void setIndex(int32_t index) {
|
||||||
this->index = index;
|
this->index = index;
|
||||||
}
|
}
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
|
|
||||||
static BtAllowedFastMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtAllowedFastMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -41,10 +41,10 @@
|
||||||
class BtAllowedFastMessageValidator : public BtMessageValidator {
|
class BtAllowedFastMessageValidator : public BtMessageValidator {
|
||||||
private:
|
private:
|
||||||
const BtAllowedFastMessage* message;
|
const BtAllowedFastMessage* message;
|
||||||
uint32_t numPiece;
|
int32_t numPiece;
|
||||||
public:
|
public:
|
||||||
BtAllowedFastMessageValidator(const BtAllowedFastMessage* message,
|
BtAllowedFastMessageValidator(const BtAllowedFastMessage* message,
|
||||||
uint32_t numPiece):
|
int32_t numPiece):
|
||||||
message(message),
|
message(message),
|
||||||
numPiece(numPiece) {}
|
numPiece(numPiece) {}
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,9 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength) {
|
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, int32_t bitfieldLength) {
|
||||||
if(this->bitfield == bitfield) {
|
if(this->bitfield == bitfield) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -49,15 +50,14 @@ void BtBitfieldMessage::setBitfield(const unsigned char* bitfield, uint32_t bitf
|
||||||
}
|
}
|
||||||
|
|
||||||
BtBitfieldMessageHandle
|
BtBitfieldMessageHandle
|
||||||
BtBitfieldMessage::create(const unsigned char* data, uint32_t dataLength)
|
BtBitfieldMessage::create(const unsigned char* data, int32_t dataLength)
|
||||||
{
|
{
|
||||||
if(dataLength <= 1) {
|
if(dataLength <= 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be greater than %d", "bitfield", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "bitfield", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "bitfield", ID);
|
||||||
id, "bitfield", ID);
|
|
||||||
}
|
}
|
||||||
BtBitfieldMessageHandle message = new BtBitfieldMessage();
|
BtBitfieldMessageHandle message = new BtBitfieldMessage();
|
||||||
message->setBitfield((unsigned char*)data+1, dataLength-1);
|
message->setBitfield((unsigned char*)data+1, dataLength-1);
|
||||||
|
@ -85,7 +85,7 @@ const unsigned char* BtBitfieldMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtBitfieldMessage::getMessageLength() {
|
int32_t BtBitfieldMessage::getMessageLength() {
|
||||||
getMessage();
|
getMessage();
|
||||||
return msgLength;
|
return msgLength;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,9 +44,9 @@ typedef SharedHandle<BtBitfieldMessage> BtBitfieldMessageHandle;
|
||||||
class BtBitfieldMessage : public SimpleBtMessage {
|
class BtBitfieldMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* bitfield;
|
unsigned char* bitfield;
|
||||||
uint32_t bitfieldLength;
|
int32_t bitfieldLength;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
uint32_t msgLength;
|
int32_t msgLength;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
bitfield = 0;
|
bitfield = 0;
|
||||||
|
@ -61,7 +61,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
BtBitfieldMessage(const unsigned char* bitfield,
|
BtBitfieldMessage(const unsigned char* bitfield,
|
||||||
uint32_t bitfieldLength):SimpleBtMessage()
|
int32_t bitfieldLength):SimpleBtMessage()
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
setBitfield(bitfield, bitfieldLength);
|
setBitfield(bitfield, bitfieldLength);
|
||||||
|
@ -72,23 +72,23 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 5;
|
static const int8_t ID = 5;
|
||||||
|
|
||||||
void setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength);
|
void setBitfield(const unsigned char* bitfield, int32_t bitfieldLength);
|
||||||
|
|
||||||
const unsigned char* getBitfield() const { return bitfield; }
|
const unsigned char* getBitfield() const { return bitfield; }
|
||||||
|
|
||||||
uint32_t getBitfieldLength() const { return bitfieldLength; }
|
int32_t getBitfieldLength() const { return bitfieldLength; }
|
||||||
|
|
||||||
static BtBitfieldMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtBitfieldMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,15 +36,15 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 13) {
|
if(dataLength != 13) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "cancel", dataLength, 13);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "cancel", dataLength, 13);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "cancel", ID);
|
||||||
id, "cancel", ID);
|
|
||||||
}
|
}
|
||||||
BtCancelMessageHandle message = new BtCancelMessage();
|
BtCancelMessageHandle message = new BtCancelMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -57,7 +57,7 @@ void BtCancelMessage::doReceivedAction() {
|
||||||
dispatcher->doCancelSendingPieceAction(index, begin, length);
|
dispatcher->doCancelSendingPieceAction(index, begin, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtCancelMessage::MESSAGE_LENGTH = 17;
|
int32_t BtCancelMessage::MESSAGE_LENGTH = 17;
|
||||||
|
|
||||||
const unsigned char* BtCancelMessage::getMessage() {
|
const unsigned char* BtCancelMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -78,11 +78,11 @@ const unsigned char* BtCancelMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtCancelMessage::getMessageLength() {
|
int32_t BtCancelMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
string BtCancelMessage::toString() const {
|
string BtCancelMessage::toString() const {
|
||||||
return "cancel index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
return "cancel index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||||
", length="+Util::uitos(length);
|
", length="+Util::itos(length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#define _D_BT_CANCEL_MESSAGE_H_
|
#define _D_BT_CANCEL_MESSAGE_H_
|
||||||
|
|
||||||
#include "SimpleBtMessage.h"
|
#include "SimpleBtMessage.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
class BtCancelMessage;
|
class BtCancelMessage;
|
||||||
|
|
||||||
|
@ -45,12 +46,12 @@ class BtCancelMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtCancelMessage(int32_t index = 0, int32_t begin = 0, uint32_t length = 0)
|
BtCancelMessage(int32_t index = 0, int32_t begin = 0, int32_t length = 0)
|
||||||
:SimpleBtMessage(),
|
:SimpleBtMessage(),
|
||||||
index(index),
|
index(index),
|
||||||
begin(begin),
|
begin(begin),
|
||||||
|
@ -61,7 +62,7 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 8;
|
static const int8_t ID = 8;
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
|
|
||||||
|
@ -71,19 +72,19 @@ public:
|
||||||
|
|
||||||
void setBegin(int32_t begin) { this->begin = begin; }
|
void setBegin(int32_t begin) { this->begin = begin; }
|
||||||
|
|
||||||
uint32_t getLength() const { return length; }
|
int32_t getLength() const { return length; }
|
||||||
|
|
||||||
void setLength(uint32_t length) { this->length = length; }
|
void setLength(int32_t length) { this->length = length; }
|
||||||
|
|
||||||
static BtCancelMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtCancelMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,12 +41,12 @@
|
||||||
class BtCancelMessageValidator : public BtMessageValidator {
|
class BtCancelMessageValidator : public BtMessageValidator {
|
||||||
private:
|
private:
|
||||||
const BtCancelMessage* message;
|
const BtCancelMessage* message;
|
||||||
uint32_t numPiece;
|
int32_t numPiece;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
public:
|
public:
|
||||||
BtCancelMessageValidator(const BtCancelMessage* message,
|
BtCancelMessageValidator(const BtCancelMessage* message,
|
||||||
uint32_t numPiece,
|
int32_t numPiece,
|
||||||
uint32_t pieceLength):
|
int32_t pieceLength):
|
||||||
message(message),
|
message(message),
|
||||||
numPiece(numPiece),
|
numPiece(numPiece),
|
||||||
pieceLength(pieceLength) {}
|
pieceLength(pieceLength) {}
|
||||||
|
|
|
@ -41,9 +41,9 @@ class BtCancelSendingPieceEvent : public BtEvent {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
public:
|
public:
|
||||||
BtCancelSendingPieceEvent(int32_t index, int32_t begin, uint32_t length):
|
BtCancelSendingPieceEvent(int32_t index, int32_t begin, int32_t length):
|
||||||
index(index), begin(begin), length(length) {}
|
index(index), begin(begin), length(length) {}
|
||||||
|
|
||||||
virtual ~BtCancelSendingPieceEvent() {}
|
virtual ~BtCancelSendingPieceEvent() {}
|
||||||
|
@ -64,11 +64,11 @@ public:
|
||||||
return begin;
|
return begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLength(uint32_t length) {
|
void setLength(int32_t length) {
|
||||||
this->length = length;
|
this->length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getLength() const {
|
int32_t getLength() const {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtChokeMessage.h"
|
#include "BtChokeMessage.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "choke", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "choke", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "choke", ID);
|
||||||
id, "choke", ID);
|
|
||||||
}
|
}
|
||||||
BtChokeMessageHandle chokeMessage = new BtChokeMessage();
|
BtChokeMessageHandle chokeMessage = new BtChokeMessage();
|
||||||
return chokeMessage;
|
return chokeMessage;
|
||||||
|
@ -59,7 +59,7 @@ bool BtChokeMessage::sendPredicate() const {
|
||||||
return !peer->amChoking;
|
return !peer->amChoking;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtChokeMessage::MESSAGE_LENGTH = 5;
|
int32_t BtChokeMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtChokeMessage::getMessage() {
|
const unsigned char* BtChokeMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -74,7 +74,7 @@ const unsigned char* BtChokeMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtChokeMessage::getMessageLength() {
|
int32_t BtChokeMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtChokeMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtChokeMessage():SimpleBtMessage(), msg(0) {}
|
BtChokeMessage():SimpleBtMessage(), msg(0) {}
|
||||||
|
|
||||||
|
@ -53,19 +53,19 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 0;
|
static const int8_t ID = 0;
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
static BtChokeMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtChokeMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual bool sendPredicate() const;
|
virtual bool sendPredicate() const;
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ void BtHandshakeMessage::init() {
|
||||||
this->reserved[7] |= 0x04;
|
this->reserved[7] |= 0x04;
|
||||||
}
|
}
|
||||||
|
|
||||||
BtHandshakeMessageHandle BtHandshakeMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtHandshakeMessageHandle BtHandshakeMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
BtHandshakeMessageHandle message = new BtHandshakeMessage();
|
BtHandshakeMessageHandle message = new BtHandshakeMessage();
|
||||||
message->pstrlen = data[0];
|
message->pstrlen = data[0];
|
||||||
memcpy(message->pstr, &data[1], PSTR_LENGTH);
|
memcpy(message->pstr, &data[1], PSTR_LENGTH);
|
||||||
|
@ -85,7 +85,7 @@ const unsigned char* BtHandshakeMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHandshakeMessage::getMessageLength() {
|
int32_t BtHandshakeMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,12 +43,12 @@ typedef SharedHandle<BtHandshakeMessage> BtHandshakeMessageHandle;
|
||||||
|
|
||||||
class BtHandshakeMessage : public SimpleBtMessage {
|
class BtHandshakeMessage : public SimpleBtMessage {
|
||||||
public:
|
public:
|
||||||
static const uint32_t PSTR_LENGTH = 19;
|
static const int32_t PSTR_LENGTH = 19;
|
||||||
static const unsigned char* BT_PSTR;
|
static const unsigned char* BT_PSTR;
|
||||||
static const uint32_t RESERVED_LENGTH = 8;
|
static const int32_t RESERVED_LENGTH = 8;
|
||||||
static const uint32_t MESSAGE_LENGTH = 68;
|
static const int32_t MESSAGE_LENGTH = 68;
|
||||||
private:
|
private:
|
||||||
uint8_t pstrlen;
|
int8_t pstrlen;
|
||||||
unsigned char* pstr;
|
unsigned char* pstr;
|
||||||
unsigned char* reserved;
|
unsigned char* reserved;
|
||||||
unsigned char* infoHash;
|
unsigned char* infoHash;
|
||||||
|
@ -63,7 +63,7 @@ public:
|
||||||
*/
|
*/
|
||||||
BtHandshakeMessage(const unsigned char* infoHash, const unsigned char* peerId);
|
BtHandshakeMessage(const unsigned char* infoHash, const unsigned char* peerId);
|
||||||
|
|
||||||
static BtHandshakeMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtHandshakeMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual ~BtHandshakeMessage() {
|
virtual ~BtHandshakeMessage() {
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
|
@ -73,21 +73,21 @@ public:
|
||||||
delete [] peerId;
|
delete [] peerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = UINT8_MAX;
|
static const int8_t ID = INT8_MAX;
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction() {};
|
virtual void doReceivedAction() {};
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
bool isFastExtensionSupported() const;
|
bool isFastExtensionSupported() const;
|
||||||
|
|
||||||
uint8_t getPstrlen() const {
|
int8_t getPstrlen() const {
|
||||||
return pstrlen;
|
return pstrlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtHaveAllMessage.h"
|
#include "BtHaveAllMessage.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtHaveAllMessageHandle BtHaveAllMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtHaveAllMessageHandle BtHaveAllMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "have all", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "have all", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "have all", ID);
|
||||||
id, "have all", ID);
|
|
||||||
}
|
}
|
||||||
BtHaveAllMessageHandle message = new BtHaveAllMessage();
|
BtHaveAllMessageHandle message = new BtHaveAllMessage();
|
||||||
return message;
|
return message;
|
||||||
|
@ -57,7 +57,7 @@ void BtHaveAllMessage::doReceivedAction() {
|
||||||
peer->setAllBitfield();
|
peer->setAllBitfield();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveAllMessage::MESSAGE_LENGTH = 5;
|
int32_t BtHaveAllMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtHaveAllMessage::getMessage() {
|
const unsigned char* BtHaveAllMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -72,7 +72,7 @@ const unsigned char* BtHaveAllMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveAllMessage::getMessageLength() {
|
int32_t BtHaveAllMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtHaveAllMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtHaveAllMessage():msg(0) {}
|
BtHaveAllMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -53,17 +53,17 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 14;
|
static const int8_t ID = 14;
|
||||||
|
|
||||||
static BtHaveAllMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtHaveAllMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,15 +36,15 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtHaveMessageHandle BtHaveMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtHaveMessageHandle BtHaveMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 5) {
|
if(dataLength != 5) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "have", dataLength, 5);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "have", dataLength, 5);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "have", ID);
|
||||||
id, "have", ID);
|
|
||||||
}
|
}
|
||||||
BtHaveMessageHandle message = new BtHaveMessage();
|
BtHaveMessageHandle message = new BtHaveMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -59,7 +59,7 @@ bool BtHaveMessage::sendPredicate() const {
|
||||||
return !peer->hasPiece(index);
|
return !peer->hasPiece(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveMessage::MESSAGE_LENGTH = 9;
|
int32_t BtHaveMessage::MESSAGE_LENGTH = 9;
|
||||||
|
|
||||||
const unsigned char* BtHaveMessage::getMessage() {
|
const unsigned char* BtHaveMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -76,7 +76,7 @@ const unsigned char* BtHaveMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveMessage::getMessageLength() {
|
int32_t BtHaveMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtHaveMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtHaveMessage(int32_t index = 0):index(index), msg(0) {}
|
BtHaveMessage(int32_t index = 0):index(index), msg(0) {}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 4;
|
static const int8_t ID = 4;
|
||||||
|
|
||||||
void setIndex(int32_t index) {
|
void setIndex(int32_t index) {
|
||||||
this->index = index;
|
this->index = index;
|
||||||
|
@ -61,15 +61,15 @@ public:
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
|
|
||||||
static BtHaveMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtHaveMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual bool sendPredicate() const;
|
virtual bool sendPredicate() const;
|
||||||
|
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtHaveNoneMessage.h"
|
#include "BtHaveNoneMessage.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtHaveNoneMessageHandle BtHaveNoneMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtHaveNoneMessageHandle BtHaveNoneMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "have none", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "have none", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "have none", ID);
|
||||||
id, "have none", ID);
|
|
||||||
}
|
}
|
||||||
BtHaveNoneMessageHandle message = new BtHaveNoneMessage();
|
BtHaveNoneMessageHandle message = new BtHaveNoneMessage();
|
||||||
return message;
|
return message;
|
||||||
|
@ -56,7 +56,7 @@ void BtHaveNoneMessage::doReceivedAction() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveNoneMessage::MESSAGE_LENGTH = 5;
|
int32_t BtHaveNoneMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtHaveNoneMessage::getMessage() {
|
const unsigned char* BtHaveNoneMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -71,7 +71,7 @@ const unsigned char* BtHaveNoneMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtHaveNoneMessage::getMessageLength() {
|
int32_t BtHaveNoneMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtHaveNoneMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtHaveNoneMessage():msg(0) {}
|
BtHaveNoneMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -53,17 +53,17 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 15;
|
static const int8_t ID = 15;
|
||||||
|
|
||||||
static BtHaveNoneMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtHaveNoneMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -56,7 +56,7 @@ public:
|
||||||
|
|
||||||
virtual void sendPendingMessage() = 0;
|
virtual void sendPendingMessage() = 0;
|
||||||
|
|
||||||
virtual uint32_t countPendingMessage() = 0;
|
virtual int32_t countPendingMessage() = 0;
|
||||||
|
|
||||||
virtual bool isSendingMessageInProgress() = 0;
|
virtual bool isSendingMessageInProgress() = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtInterestedMessage.h"
|
#include "BtInterestedMessage.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "interested", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "interested", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "interested", ID);
|
||||||
id, "interested", ID);
|
|
||||||
}
|
}
|
||||||
BtInterestedMessageHandle message = new BtInterestedMessage();
|
BtInterestedMessageHandle message = new BtInterestedMessage();
|
||||||
return message;
|
return message;
|
||||||
|
@ -57,7 +57,7 @@ bool BtInterestedMessage::sendPredicate() const {
|
||||||
return !peer->amInterested;
|
return !peer->amInterested;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtInterestedMessage::MESSAGE_LENGTH = 5;
|
int32_t BtInterestedMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtInterestedMessage::getMessage() {
|
const unsigned char* BtInterestedMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -72,7 +72,7 @@ const unsigned char* BtInterestedMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtInterestedMessage::getMessageLength() {
|
int32_t BtInterestedMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtInterestedMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtInterestedMessage():msg(0) {}
|
BtInterestedMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -53,17 +53,17 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 2;
|
static const int8_t ID = 2;
|
||||||
|
|
||||||
static BtInterestedMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtInterestedMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "BtKeepAliveMessage.h"
|
#include "BtKeepAliveMessage.h"
|
||||||
|
|
||||||
uint32_t BtKeepAliveMessage::MESSAGE_LENGTH = 4;
|
int32_t BtKeepAliveMessage::MESSAGE_LENGTH = 4;
|
||||||
|
|
||||||
const unsigned char* BtKeepAliveMessage::getMessage() {
|
const unsigned char* BtKeepAliveMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -48,6 +48,6 @@ const unsigned char* BtKeepAliveMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtKeepAliveMessage::getMessageLength() {
|
int32_t BtKeepAliveMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtKeepAliveMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtKeepAliveMessage():msg(0) {}
|
BtKeepAliveMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -53,15 +53,15 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 99;
|
static const int8_t ID = 99;
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction() {}
|
virtual void doReceivedAction() {}
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const {
|
virtual string toString() const {
|
||||||
return "keep alive";
|
return "keep alive";
|
||||||
|
|
|
@ -50,7 +50,7 @@ public:
|
||||||
|
|
||||||
virtual bool isUploading() = 0;
|
virtual bool isUploading() = 0;
|
||||||
|
|
||||||
virtual uint8_t getId() = 0;
|
virtual int8_t getId() = 0;
|
||||||
|
|
||||||
virtual void doReceivedAction() = 0;
|
virtual void doReceivedAction() = 0;
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ public:
|
||||||
|
|
||||||
virtual void sendMessages() = 0;
|
virtual void sendMessages() = 0;
|
||||||
|
|
||||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length) = 0;
|
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length) = 0;
|
||||||
|
|
||||||
virtual void doCancelSendingPieceAction(const PieceHandle& piece) = 0;
|
virtual void doCancelSendingPieceAction(const PieceHandle& piece) = 0;
|
||||||
|
|
||||||
|
@ -64,13 +64,13 @@ public:
|
||||||
|
|
||||||
virtual bool isSendingInProgress() = 0;
|
virtual bool isSendingInProgress() = 0;
|
||||||
|
|
||||||
virtual uint32_t countMessageInQueue() = 0;
|
virtual int32_t countMessageInQueue() = 0;
|
||||||
|
|
||||||
virtual uint32_t countOutstandingRequest() = 0;
|
virtual int32_t countOutstandingRequest() = 0;
|
||||||
|
|
||||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex) = 0;
|
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex) = 0;
|
||||||
|
|
||||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, uint32_t length) = 0;
|
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, int32_t length) = 0;
|
||||||
|
|
||||||
virtual void removeOutstandingRequest(const RequestSlot& slot) = 0;
|
virtual void removeOutstandingRequest(const RequestSlot& slot) = 0;
|
||||||
|
|
||||||
|
|
|
@ -44,10 +44,10 @@ public:
|
||||||
virtual ~BtMessageFactory() {}
|
virtual ~BtMessageFactory() {}
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createBtMessage(const unsigned char* msg, uint32_t msgLength) = 0;
|
createBtMessage(const unsigned char* msg, int32_t msgLength) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createHandshakeMessage(const unsigned char* msg, uint32_t msgLength) = 0;
|
createHandshakeMessage(const unsigned char* msg, int32_t msgLength) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createHandshakeMessage(const unsigned char* infoHash,
|
createHandshakeMessage(const unsigned char* infoHash,
|
||||||
|
@ -57,10 +57,10 @@ public:
|
||||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex) = 0;
|
createRequestMessage(const PieceHandle& piece, int32_t blockIndex) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
createCancelMessage(int32_t index, int32_t begin, int32_t length) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
createPieceMessage(int32_t index, int32_t begin, int32_t length) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle createHaveMessage(int32_t index) = 0;
|
virtual BtMessageHandle createHaveMessage(int32_t index) = 0;
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ public:
|
||||||
virtual BtMessageHandle createHaveNoneMessage() = 0;
|
virtual BtMessageHandle createHaveNoneMessage() = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
createRejectMessage(int32_t index, int32_t begin, int32_t length) = 0;
|
||||||
|
|
||||||
virtual BtMessageHandle createAllowedFastMessage(int32_t index) = 0;
|
virtual BtMessageHandle createAllowedFastMessage(int32_t index) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtNotInterestedMessage.h"
|
#include "BtNotInterestedMessage.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "not interested", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "not interested", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "not interested", ID);
|
||||||
id, "not interested", ID);
|
|
||||||
}
|
}
|
||||||
BtNotInterestedMessageHandle message = new BtNotInterestedMessage();
|
BtNotInterestedMessageHandle message = new BtNotInterestedMessage();
|
||||||
return message;
|
return message;
|
||||||
|
@ -57,7 +57,7 @@ bool BtNotInterestedMessage::sendPredicate() const {
|
||||||
return peer->amInterested;
|
return peer->amInterested;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtNotInterestedMessage::MESSAGE_LENGTH = 5;
|
int32_t BtNotInterestedMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtNotInterestedMessage::getMessage() {
|
const unsigned char* BtNotInterestedMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -72,7 +72,7 @@ const unsigned char* BtNotInterestedMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtNotInterestedMessage::getMessageLength() {
|
int32_t BtNotInterestedMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtNotInterestedMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtNotInterestedMessage():msg(0) {}
|
BtNotInterestedMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -53,17 +53,17 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 3;
|
static const int8_t ID = 3;
|
||||||
|
|
||||||
static BtNotInterestedMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtNotInterestedMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -40,21 +40,20 @@
|
||||||
#include "BtChokingEvent.h"
|
#include "BtChokingEvent.h"
|
||||||
#include "BtCancelSendingPieceEvent.h"
|
#include "BtCancelSendingPieceEvent.h"
|
||||||
|
|
||||||
void BtPieceMessage::setBlock(const unsigned char* block, uint32_t blockLength) {
|
void BtPieceMessage::setBlock(const unsigned char* block, int32_t blockLength) {
|
||||||
delete [] this->block;
|
delete [] this->block;
|
||||||
this->blockLength = blockLength;
|
this->blockLength = blockLength;
|
||||||
this->block = new unsigned char[this->blockLength];
|
this->block = new unsigned char[this->blockLength];
|
||||||
memcpy(this->block, block, this->blockLength);
|
memcpy(this->block, block, this->blockLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength <= 9) {
|
if(dataLength <= 9) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be greater than %d", "piece", dataLength, 9);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "piece", dataLength, 9);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID);
|
||||||
id, "piece", ID);
|
|
||||||
}
|
}
|
||||||
BtPieceMessageHandle message = new BtPieceMessage();
|
BtPieceMessageHandle message = new BtPieceMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -74,7 +73,7 @@ void BtPieceMessage::doReceivedAction() {
|
||||||
PieceHandle piece = pieceStorage->getPiece(index);
|
PieceHandle piece = pieceStorage->getPiece(index);
|
||||||
int64_t offset =
|
int64_t offset =
|
||||||
((int64_t)index)*btContext->getPieceLength()+begin;
|
((int64_t)index)*btContext->getPieceLength()+begin;
|
||||||
logger->debug("CUID#%d - Piece received. index=%d, begin=%d, length=%u, offset=%llu, blockIndex=%u",
|
logger->debug("CUID#%d - Piece received. index=%d, begin=%d, length=%d, offset=%llu, blockIndex=%d",
|
||||||
cuid, index, begin, blockLength, offset, slot.getBlockIndex());
|
cuid, index, begin, blockLength, offset, slot.getBlockIndex());
|
||||||
pieceStorage->getDiskAdaptor()->writeData(block,
|
pieceStorage->getDiskAdaptor()->writeData(block,
|
||||||
blockLength,
|
blockLength,
|
||||||
|
@ -95,7 +94,7 @@ void BtPieceMessage::doReceivedAction() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtPieceMessage::MESSAGE_HEADER_LENGTH = 13;
|
int32_t BtPieceMessage::MESSAGE_HEADER_LENGTH = 13;
|
||||||
|
|
||||||
const unsigned char* BtPieceMessage::getMessageHeader() {
|
const unsigned char* BtPieceMessage::getMessageHeader() {
|
||||||
if(!msgHeader) {
|
if(!msgHeader) {
|
||||||
|
@ -115,7 +114,7 @@ const unsigned char* BtPieceMessage::getMessageHeader() {
|
||||||
return msgHeader;
|
return msgHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtPieceMessage::getMessageHeaderLength() {
|
int32_t BtPieceMessage::getMessageHeaderLength() {
|
||||||
return MESSAGE_HEADER_LENGTH;
|
return MESSAGE_HEADER_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +131,7 @@ void BtPieceMessage::send() {
|
||||||
leftDataLength = getMessageHeaderLength();
|
leftDataLength = getMessageHeaderLength();
|
||||||
sendingInProgress = true;
|
sendingInProgress = true;
|
||||||
}
|
}
|
||||||
uint32_t writtenLength
|
int32_t writtenLength
|
||||||
= peerConnection->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
|
= peerConnection->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
|
||||||
leftDataLength);
|
leftDataLength);
|
||||||
if(writtenLength == leftDataLength) {
|
if(writtenLength == leftDataLength) {
|
||||||
|
@ -146,7 +145,7 @@ void BtPieceMessage::send() {
|
||||||
sendingInProgress = false;
|
sendingInProgress = false;
|
||||||
int64_t pieceDataOffset =
|
int64_t pieceDataOffset =
|
||||||
((int64_t)index)*btContext->getPieceLength()+begin+blockLength-leftDataLength;
|
((int64_t)index)*btContext->getPieceLength()+begin+blockLength-leftDataLength;
|
||||||
uint32_t writtenLength =
|
int32_t writtenLength =
|
||||||
sendPieceData(pieceDataOffset, leftDataLength);
|
sendPieceData(pieceDataOffset, leftDataLength);
|
||||||
peer->updateUploadLength(writtenLength);
|
peer->updateUploadLength(writtenLength);
|
||||||
if(writtenLength < leftDataLength) {
|
if(writtenLength < leftDataLength) {
|
||||||
|
@ -156,16 +155,16 @@ void BtPieceMessage::send() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
int32_t BtPieceMessage::sendPieceData(int64_t offset, int32_t length) const {
|
||||||
uint32_t BUF_SIZE = 256;
|
int32_t BUF_SIZE = 256;
|
||||||
unsigned char buf[BUF_SIZE];
|
unsigned char buf[BUF_SIZE];
|
||||||
int32_t iteration = length/BUF_SIZE;
|
int32_t iteration = length/BUF_SIZE;
|
||||||
uint32_t writtenLength = 0;
|
int32_t writtenLength = 0;
|
||||||
for(int32_t i = 0; i < iteration; i++) {
|
for(int32_t i = 0; i < iteration; i++) {
|
||||||
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < (int32_t)BUF_SIZE) {
|
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < BUF_SIZE) {
|
||||||
throw new DlAbortEx("Failed to read data from disk.");
|
throw new DlAbortEx("Failed to read data from disk.");
|
||||||
}
|
}
|
||||||
uint32_t ws = peerConnection->sendMessage(buf, BUF_SIZE);
|
int32_t ws = peerConnection->sendMessage(buf, BUF_SIZE);
|
||||||
writtenLength += ws;
|
writtenLength += ws;
|
||||||
if(ws != BUF_SIZE) {
|
if(ws != BUF_SIZE) {
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
|
@ -177,7 +176,7 @@ uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
||||||
if(pieceStorage->getDiskAdaptor()->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
|
if(pieceStorage->getDiskAdaptor()->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
|
||||||
throw new DlAbortEx("Failed to read data from disk.");
|
throw new DlAbortEx("Failed to read data from disk.");
|
||||||
}
|
}
|
||||||
uint32_t ws = peerConnection->sendMessage(buf, rem);
|
int32_t ws = peerConnection->sendMessage(buf, rem);
|
||||||
writtenLength += ws;
|
writtenLength += ws;
|
||||||
}
|
}
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
|
@ -185,7 +184,7 @@ uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
||||||
|
|
||||||
string BtPieceMessage::toString() const {
|
string BtPieceMessage::toString() const {
|
||||||
return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||||
", length="+Util::uitos(blockLength);
|
", length="+Util::itos(blockLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BtPieceMessage::checkPieceHash(const PieceHandle& piece) {
|
bool BtPieceMessage::checkPieceHash(const PieceHandle& piece) {
|
||||||
|
@ -271,7 +270,7 @@ void BtPieceMessage::handleCancelSendingPieceEvent(const BtEventHandle& event) {
|
||||||
begin == intEvent->getBegin() &&
|
begin == intEvent->getBegin() &&
|
||||||
blockLength == intEvent->getLength()) {
|
blockLength == intEvent->getLength()) {
|
||||||
logger->debug("CUID#%d - Reject piece message in queue because cancel"
|
logger->debug("CUID#%d - Reject piece message in queue because cancel"
|
||||||
" message received. index=%d, begin=%d, length=%u",
|
" message received. index=%d, begin=%d, length=%d",
|
||||||
cuid, index, begin, blockLength);
|
cuid, index, begin, blockLength);
|
||||||
if(peer->isFastExtensionEnabled()) {
|
if(peer->isFastExtensionEnabled()) {
|
||||||
BtMessageHandle rej = messageFactory->createRejectMessage(index,
|
BtMessageHandle rej = messageFactory->createRejectMessage(index,
|
||||||
|
|
|
@ -49,13 +49,13 @@ class BtPieceMessage : public AbstractBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t blockLength;
|
int32_t blockLength;
|
||||||
unsigned char* block;
|
unsigned char* block;
|
||||||
uint32_t leftDataLength;
|
int32_t leftDataLength;
|
||||||
bool headerSent;
|
bool headerSent;
|
||||||
unsigned char* msgHeader;
|
unsigned char* msgHeader;
|
||||||
|
|
||||||
static uint32_t MESSAGE_HEADER_LENGTH;
|
static int32_t MESSAGE_HEADER_LENGTH;
|
||||||
|
|
||||||
bool checkPieceHash(const PieceHandle& piece);
|
bool checkPieceHash(const PieceHandle& piece);
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ private:
|
||||||
|
|
||||||
void erasePieceOnDisk(const PieceHandle& piece);
|
void erasePieceOnDisk(const PieceHandle& piece);
|
||||||
|
|
||||||
uint32_t sendPieceData(int64_t offset, uint32_t length) const;
|
int32_t sendPieceData(int64_t offset, int32_t length) const;
|
||||||
|
|
||||||
class BtChokingEventListener : public AbstractBtEventListener {
|
class BtChokingEventListener : public AbstractBtEventListener {
|
||||||
private:
|
private:
|
||||||
|
@ -93,7 +93,7 @@ private:
|
||||||
|
|
||||||
typedef SharedHandle<BtCancelSendingPieceEventListener> BtCancelSendingPieceEventListenerHandle;
|
typedef SharedHandle<BtCancelSendingPieceEventListener> BtCancelSendingPieceEventListenerHandle;
|
||||||
public:
|
public:
|
||||||
BtPieceMessage(int32_t index = 0, int32_t begin = 0, uint32_t blockLength = 0)
|
BtPieceMessage(int32_t index = 0, int32_t begin = 0, int32_t blockLength = 0)
|
||||||
:index(index),
|
:index(index),
|
||||||
begin(begin),
|
begin(begin),
|
||||||
blockLength(blockLength),
|
blockLength(blockLength),
|
||||||
|
@ -112,7 +112,7 @@ public:
|
||||||
delete [] block;
|
delete [] block;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 7;
|
static const int8_t ID = 7;
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
|
|
||||||
|
@ -124,21 +124,21 @@ public:
|
||||||
|
|
||||||
const unsigned char* getBlock() const { return block; }
|
const unsigned char* getBlock() const { return block; }
|
||||||
|
|
||||||
void setBlock(const unsigned char* block, uint32_t blockLength);
|
void setBlock(const unsigned char* block, int32_t blockLength);
|
||||||
|
|
||||||
uint32_t getBlockLength() const { return blockLength; }
|
int32_t getBlockLength() const { return blockLength; }
|
||||||
|
|
||||||
void setBlockLength(uint32_t blockLength) { this->blockLength = blockLength; }
|
void setBlockLength(int32_t blockLength) { this->blockLength = blockLength; }
|
||||||
|
|
||||||
static BtPieceMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtPieceMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
const unsigned char* getMessageHeader();
|
const unsigned char* getMessageHeader();
|
||||||
|
|
||||||
uint32_t getMessageHeaderLength();
|
int32_t getMessageHeaderLength();
|
||||||
|
|
||||||
virtual void send();
|
virtual void send();
|
||||||
|
|
||||||
|
|
|
@ -41,12 +41,12 @@
|
||||||
class BtPieceMessageValidator : public BtMessageValidator {
|
class BtPieceMessageValidator : public BtMessageValidator {
|
||||||
private:
|
private:
|
||||||
const BtPieceMessage* message;
|
const BtPieceMessage* message;
|
||||||
uint32_t numPiece;
|
int32_t numPiece;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
public:
|
public:
|
||||||
BtPieceMessageValidator(const BtPieceMessage* message,
|
BtPieceMessageValidator(const BtPieceMessage* message,
|
||||||
uint32_t numPiece,
|
int32_t numPiece,
|
||||||
uint32_t pieceLength):
|
int32_t pieceLength):
|
||||||
message(message),
|
message(message),
|
||||||
numPiece(numPiece),
|
numPiece(numPiece),
|
||||||
pieceLength(pieceLength) {}
|
pieceLength(pieceLength) {}
|
||||||
|
|
|
@ -36,15 +36,15 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtPortMessageHandle BtPortMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtPortMessageHandle BtPortMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 3) {
|
if(dataLength != 3) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "port", dataLength, 3);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "port", dataLength, 3);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "piece", ID);
|
||||||
id, "piece", ID);
|
|
||||||
}
|
}
|
||||||
BtPortMessageHandle message = new BtPortMessage();
|
BtPortMessageHandle message = new BtPortMessage();
|
||||||
message->setPort(PeerMessageUtil::getShortIntParam(data, 1));
|
message->setPort(PeerMessageUtil::getShortIntParam(data, 1));
|
||||||
|
@ -52,5 +52,5 @@ BtPortMessageHandle BtPortMessage::create(const unsigned char* data, uint32_t da
|
||||||
}
|
}
|
||||||
|
|
||||||
string BtPortMessage::toString() const {
|
string BtPortMessage::toString() const {
|
||||||
return "port port="+Util::uitos(port);
|
return "port port="+Util::itos(port);
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,21 +43,21 @@ typedef SharedHandle<BtPortMessage> BtPortMessageHandle;
|
||||||
|
|
||||||
class BtPortMessage : public AbstractBtMessage {
|
class BtPortMessage : public AbstractBtMessage {
|
||||||
private:
|
private:
|
||||||
uint16_t port;
|
int16_t port;
|
||||||
public:
|
public:
|
||||||
BtPortMessage(uint16_t port = 0):port(port) {}
|
BtPortMessage(int16_t port = 0):port(port) {}
|
||||||
|
|
||||||
virtual ~BtPortMessage() {}
|
virtual ~BtPortMessage() {}
|
||||||
|
|
||||||
static const uint8_t ID = 9;
|
static const int8_t ID = 9;
|
||||||
|
|
||||||
uint16_t getPort() const { return port; }
|
int16_t getPort() const { return port; }
|
||||||
|
|
||||||
void setPort(uint16_t port) { this->port = port; }
|
void setPort(int16_t port) { this->port = port; }
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
static BtPortMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtPortMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual void doReceivedAction() {
|
virtual void doReceivedAction() {
|
||||||
logger->info("DHT is not supported yet.");
|
logger->info("DHT is not supported yet.");
|
||||||
|
|
|
@ -36,15 +36,15 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtRejectMessageHandle BtRejectMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtRejectMessageHandle BtRejectMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 13) {
|
if(dataLength != 13) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "reject", dataLength, 13);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "reject", dataLength, 13);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "reject", ID);
|
||||||
id, "reject", ID);
|
|
||||||
}
|
}
|
||||||
BtRejectMessageHandle message = new BtRejectMessage();
|
BtRejectMessageHandle message = new BtRejectMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -69,7 +69,7 @@ void BtRejectMessage::doReceivedAction() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtRejectMessage::MESSAGE_LENGTH = 17;
|
int32_t BtRejectMessage::MESSAGE_LENGTH = 17;
|
||||||
|
|
||||||
const unsigned char* BtRejectMessage::getMessage() {
|
const unsigned char* BtRejectMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -90,11 +90,11 @@ const unsigned char* BtRejectMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtRejectMessage::getMessageLength() {
|
int32_t BtRejectMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
string BtRejectMessage::toString() const {
|
string BtRejectMessage::toString() const {
|
||||||
return "reject index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
return "reject index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||||
", length="+Util::uitos(length);
|
", length="+Util::itos(length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,11 +45,11 @@ class BtRejectMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtRejectMessage(int32_t index = 0, int32_t begin = 0, uint32_t length = 0)
|
BtRejectMessage(int32_t index = 0, int32_t begin = 0, int32_t length = 0)
|
||||||
:index(index),
|
:index(index),
|
||||||
begin(begin),
|
begin(begin),
|
||||||
length(length),
|
length(length),
|
||||||
|
@ -59,7 +59,7 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 16;
|
static const int8_t ID = 16;
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
void setIndex(int32_t index) { this->index = index; }
|
void setIndex(int32_t index) { this->index = index; }
|
||||||
|
@ -67,18 +67,18 @@ public:
|
||||||
int32_t getBegin() const { return begin; }
|
int32_t getBegin() const { return begin; }
|
||||||
void setBegin(int32_t begin) { this->begin = begin; }
|
void setBegin(int32_t begin) { this->begin = begin; }
|
||||||
|
|
||||||
uint32_t getLength() const { return length; }
|
int32_t getLength() const { return length; }
|
||||||
void setLength(uint32_t length) { this->length = length; }
|
void setLength(int32_t length) { this->length = length; }
|
||||||
|
|
||||||
static BtRejectMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtRejectMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,12 +41,12 @@
|
||||||
class BtRejectMessageValidator : public BtMessageValidator {
|
class BtRejectMessageValidator : public BtMessageValidator {
|
||||||
private:
|
private:
|
||||||
const BtRejectMessage* message;
|
const BtRejectMessage* message;
|
||||||
uint32_t numPiece;
|
int32_t numPiece;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
public:
|
public:
|
||||||
BtRejectMessageValidator(const BtRejectMessage* message,
|
BtRejectMessageValidator(const BtRejectMessage* message,
|
||||||
uint32_t numPiece,
|
int32_t numPiece,
|
||||||
uint32_t pieceLength):
|
int32_t pieceLength):
|
||||||
message(message),
|
message(message),
|
||||||
numPiece(numPiece),
|
numPiece(numPiece),
|
||||||
pieceLength(pieceLength) {}
|
pieceLength(pieceLength) {}
|
||||||
|
|
|
@ -60,13 +60,13 @@ public:
|
||||||
* addTargetPiece() and returns them.
|
* addTargetPiece() and returns them.
|
||||||
* The number of objects returned is capped by max.
|
* The number of objects returned is capped by max.
|
||||||
*/
|
*/
|
||||||
virtual BtMessages createRequestMessages(uint32_t max) = 0;
|
virtual BtMessages createRequestMessages(int32_t max) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this method in end game mode.
|
* Use this method in end game mode.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
virtual BtMessages createRequestMessagesOnEndGame(uint32_t max) = 0;
|
virtual BtMessages createRequestMessagesOnEndGame(int32_t max) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<BtRequestFactory> BtRequestFactoryHandle;
|
typedef SharedHandle<BtRequestFactory> BtRequestFactoryHandle;
|
||||||
|
|
|
@ -37,15 +37,15 @@
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
#include "BtAbortOutstandingRequestEvent.h"
|
#include "BtAbortOutstandingRequestEvent.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 13) {
|
if(dataLength != 13) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "request", dataLength, 13);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "request", dataLength, 13);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "request", ID);
|
||||||
id, "request", ID);
|
|
||||||
}
|
}
|
||||||
BtRequestMessageHandle message = new BtRequestMessage();
|
BtRequestMessageHandle message = new BtRequestMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
|
@ -72,7 +72,7 @@ void BtRequestMessage::doReceivedAction() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtRequestMessage::MESSAGE_LENGTH = 17;
|
int32_t BtRequestMessage::MESSAGE_LENGTH = 17;
|
||||||
|
|
||||||
const unsigned char* BtRequestMessage::getMessage() {
|
const unsigned char* BtRequestMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -93,13 +93,13 @@ const unsigned char* BtRequestMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtRequestMessage::getMessageLength() {
|
int32_t BtRequestMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
string BtRequestMessage::toString() const {
|
string BtRequestMessage::toString() const {
|
||||||
return "request index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
return "request index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||||
", length="+Util::uitos(length);
|
", length="+Util::itos(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtRequestMessage::onQueued() {
|
void BtRequestMessage::onQueued() {
|
||||||
|
|
|
@ -48,11 +48,11 @@ class BtRequestMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
int32_t blockIndex;
|
int32_t blockIndex;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
|
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
|
|
||||||
class BtAbortOutstandingRequestEventListener : public AbstractBtEventListener {
|
class BtAbortOutstandingRequestEventListener : public AbstractBtEventListener {
|
||||||
private:
|
private:
|
||||||
|
@ -69,7 +69,7 @@ private:
|
||||||
public:
|
public:
|
||||||
BtRequestMessage(int32_t index = 0,
|
BtRequestMessage(int32_t index = 0,
|
||||||
int32_t begin = 0,
|
int32_t begin = 0,
|
||||||
uint32_t length = 0,
|
int32_t length = 0,
|
||||||
int32_t blockIndex = 0)
|
int32_t blockIndex = 0)
|
||||||
:index(index),
|
:index(index),
|
||||||
begin(begin),
|
begin(begin),
|
||||||
|
@ -84,7 +84,7 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 6;
|
static const int8_t ID = 6;
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
void setIndex(int32_t index) { this->index = index; }
|
void setIndex(int32_t index) { this->index = index; }
|
||||||
|
@ -92,21 +92,21 @@ public:
|
||||||
int32_t getBegin() const { return begin; }
|
int32_t getBegin() const { return begin; }
|
||||||
void setBegin(int32_t begin) { this->begin = begin; }
|
void setBegin(int32_t begin) { this->begin = begin; }
|
||||||
|
|
||||||
uint32_t getLength() const { return length; }
|
int32_t getLength() const { return length; }
|
||||||
void setLength(uint32_t length) { this->length = length; }
|
void setLength(int32_t length) { this->length = length; }
|
||||||
|
|
||||||
int32_t getBlockIndex() const { return blockIndex; }
|
int32_t getBlockIndex() const { return blockIndex; }
|
||||||
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||||
|
|
||||||
static BtRequestMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtRequestMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -41,12 +41,12 @@
|
||||||
class BtRequestMessageValidator : public BtMessageValidator {
|
class BtRequestMessageValidator : public BtMessageValidator {
|
||||||
private:
|
private:
|
||||||
const BtRequestMessage* message;
|
const BtRequestMessage* message;
|
||||||
uint32_t numPiece;
|
int32_t numPiece;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
public:
|
public:
|
||||||
BtRequestMessageValidator(const BtRequestMessage* message,
|
BtRequestMessageValidator(const BtRequestMessage* message,
|
||||||
uint32_t numPiece,
|
int32_t numPiece,
|
||||||
uint32_t pieceLength):
|
int32_t pieceLength):
|
||||||
message(message),
|
message(message),
|
||||||
numPiece(numPiece),
|
numPiece(numPiece),
|
||||||
pieceLength(pieceLength) {}
|
pieceLength(pieceLength) {}
|
||||||
|
|
|
@ -36,22 +36,22 @@
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 5) {
|
if(dataLength != 5) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "suggest piece", dataLength, 5);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "suggest piece", dataLength, 5);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "suggest piece", ID);
|
||||||
id, "suggest piece", ID);
|
|
||||||
}
|
}
|
||||||
BtSuggestPieceMessageHandle message = new BtSuggestPieceMessage();
|
BtSuggestPieceMessageHandle message = new BtSuggestPieceMessage();
|
||||||
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
message->setIndex(PeerMessageUtil::getIntParam(data, 1));
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtSuggestPieceMessage::MESSAGE_LENGTH = 9;
|
int32_t BtSuggestPieceMessage::MESSAGE_LENGTH = 9;
|
||||||
|
|
||||||
const unsigned char* BtSuggestPieceMessage::getMessage() {
|
const unsigned char* BtSuggestPieceMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -68,7 +68,7 @@ const unsigned char* BtSuggestPieceMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtSuggestPieceMessage::getMessageLength() {
|
int32_t BtSuggestPieceMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class BtSuggestPieceMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
int32_t index;
|
int32_t index;
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtSuggestPieceMessage():index(0), msg(0) {}
|
BtSuggestPieceMessage():index(0), msg(0) {}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 13;
|
static const int8_t ID = 13;
|
||||||
|
|
||||||
void setIndex(int32_t index) {
|
void setIndex(int32_t index) {
|
||||||
this->index = index;
|
this->index = index;
|
||||||
|
@ -61,9 +61,9 @@ public:
|
||||||
|
|
||||||
int32_t getIndex() const { return index; }
|
int32_t getIndex() const { return index; }
|
||||||
|
|
||||||
static BtSuggestPieceMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtSuggestPieceMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction() {
|
virtual void doReceivedAction() {
|
||||||
// TODO Current implementation ignores this message.
|
// TODO Current implementation ignores this message.
|
||||||
|
@ -71,7 +71,7 @@ public:
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,15 +35,15 @@
|
||||||
#include "BtUnchokeMessage.h"
|
#include "BtUnchokeMessage.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, uint32_t dataLength) {
|
BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, int32_t dataLength) {
|
||||||
if(dataLength != 1) {
|
if(dataLength != 1) {
|
||||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "unchoke", dataLength, 1);
|
throw new DlAbortEx(EX_INVALID_PAYLOAD_SIZE, "unchoke", dataLength, 1);
|
||||||
}
|
}
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
if(id != ID) {
|
if(id != ID) {
|
||||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "unchoke", ID);
|
||||||
id, "unchoke", ID);
|
|
||||||
}
|
}
|
||||||
BtUnchokeMessageHandle message = new BtUnchokeMessage();
|
BtUnchokeMessageHandle message = new BtUnchokeMessage();
|
||||||
return message;
|
return message;
|
||||||
|
@ -57,7 +57,7 @@ bool BtUnchokeMessage::sendPredicate() const {
|
||||||
return peer->amChoking;
|
return peer->amChoking;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtUnchokeMessage::MESSAGE_LENGTH = 5;
|
int32_t BtUnchokeMessage::MESSAGE_LENGTH = 5;
|
||||||
|
|
||||||
const unsigned char* BtUnchokeMessage::getMessage() {
|
const unsigned char* BtUnchokeMessage::getMessage() {
|
||||||
if(!msg) {
|
if(!msg) {
|
||||||
|
@ -72,7 +72,7 @@ const unsigned char* BtUnchokeMessage::getMessage() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtUnchokeMessage::getMessageLength() {
|
int32_t BtUnchokeMessage::getMessageLength() {
|
||||||
return MESSAGE_LENGTH;
|
return MESSAGE_LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ typedef SharedHandle<BtUnchokeMessage> BtUnchokeMessageHandle;
|
||||||
class BtUnchokeMessage : public SimpleBtMessage {
|
class BtUnchokeMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
unsigned char* msg;
|
unsigned char* msg;
|
||||||
static uint32_t MESSAGE_LENGTH;
|
static int32_t MESSAGE_LENGTH;
|
||||||
public:
|
public:
|
||||||
BtUnchokeMessage():msg(0) {}
|
BtUnchokeMessage():msg(0) {}
|
||||||
|
|
||||||
|
@ -52,17 +52,17 @@ public:
|
||||||
delete [] msg;
|
delete [] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint8_t ID = 1;
|
static const int8_t ID = 1;
|
||||||
|
|
||||||
static BtUnchokeMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
static BtUnchokeMessageHandle create(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
virtual uint8_t getId() { return ID; }
|
virtual int8_t getId() { return ID; }
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual const unsigned char* getMessage();
|
virtual const unsigned char* getMessage();
|
||||||
|
|
||||||
virtual uint32_t getMessageLength();
|
virtual int32_t getMessageLength();
|
||||||
|
|
||||||
virtual string toString() const;
|
virtual string toString() const;
|
||||||
|
|
||||||
|
|
|
@ -54,13 +54,13 @@ void ByteArrayDiskWriter::init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ByteArrayDiskWriter::initAndOpenFile(const string& filename,
|
void ByteArrayDiskWriter::initAndOpenFile(const string& filename,
|
||||||
uint64_t totalLength) {
|
int64_t totalLength) {
|
||||||
clear();
|
clear();
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ByteArrayDiskWriter::openFile(const string& filename,
|
void ByteArrayDiskWriter::openFile(const string& filename,
|
||||||
uint64_t totalLength) {
|
int64_t totalLength) {
|
||||||
initAndOpenFile(filename);
|
initAndOpenFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ void ByteArrayDiskWriter::openExistingFile(const string& filename) {
|
||||||
openFile(filename);
|
openFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ByteArrayDiskWriter::writeData(const char* data, uint32_t dataLength, int64_t position) {
|
void ByteArrayDiskWriter::writeData(const char* data, int32_t dataLength, int64_t position) {
|
||||||
if(bufLength+dataLength >= maxBufLength) {
|
if(bufLength+dataLength >= maxBufLength) {
|
||||||
maxBufLength = Util::expandBuffer(&buf, bufLength, bufLength+dataLength);
|
maxBufLength = Util::expandBuffer(&buf, bufLength, bufLength+dataLength);
|
||||||
}
|
}
|
||||||
|
@ -80,11 +80,11 @@ void ByteArrayDiskWriter::writeData(const char* data, uint32_t dataLength, int64
|
||||||
bufLength += dataLength;
|
bufLength += dataLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ByteArrayDiskWriter::readData(char* data, uint32_t len, int64_t position) {
|
int ByteArrayDiskWriter::readData(char* data, int32_t len, int64_t position) {
|
||||||
if(position >= bufLength) {
|
if(position >= bufLength) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
uint32_t readLength;
|
int32_t readLength;
|
||||||
if(position+len <= bufLength) {
|
if(position+len <= bufLength) {
|
||||||
readLength = len;
|
readLength = len;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -40,8 +40,8 @@
|
||||||
class ByteArrayDiskWriter : public DiskWriter {
|
class ByteArrayDiskWriter : public DiskWriter {
|
||||||
private:
|
private:
|
||||||
char* buf;
|
char* buf;
|
||||||
uint32_t maxBufLength;
|
int32_t maxBufLength;
|
||||||
uint32_t bufLength;
|
int32_t bufLength;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void clear();
|
void clear();
|
||||||
|
@ -49,19 +49,19 @@ public:
|
||||||
ByteArrayDiskWriter();
|
ByteArrayDiskWriter();
|
||||||
virtual ~ByteArrayDiskWriter();
|
virtual ~ByteArrayDiskWriter();
|
||||||
|
|
||||||
virtual void initAndOpenFile(const string& filename, uint64_t totalLength = 0);
|
virtual void initAndOpenFile(const string& filename, int64_t totalLength = 0);
|
||||||
|
|
||||||
virtual void openFile(const string& filename, uint64_t totalLength = 0);
|
virtual void openFile(const string& filename, int64_t totalLength = 0);
|
||||||
|
|
||||||
virtual void closeFile();
|
virtual void closeFile();
|
||||||
|
|
||||||
virtual void openExistingFile(const string& filename);
|
virtual void openExistingFile(const string& filename);
|
||||||
|
|
||||||
// position is ignored
|
// position is ignored
|
||||||
virtual void writeData(const char* data, uint32_t len, int64_t position = 0);
|
virtual void writeData(const char* data, int32_t len, int64_t position = 0);
|
||||||
virtual int readData(char* data, uint32_t len, int64_t position);
|
virtual int readData(char* data, int32_t len, int64_t position);
|
||||||
// not implemented yet
|
// not implemented yet
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo) { return ""; }
|
const MessageDigestContext::DigestAlgo& algo) { return ""; }
|
||||||
|
|
||||||
const char* getByteArray() const {
|
const char* getByteArray() const {
|
||||||
|
|
|
@ -36,18 +36,20 @@
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
#include "TimeA2.h"
|
#include "TimeA2.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
void ChunkChecksumValidator::validateSameLengthChecksum(BitfieldMan* bitfieldMan,
|
void ChunkChecksumValidator::validateSameLengthChecksum(BitfieldMan* bitfieldMan,
|
||||||
int32_t index,
|
int32_t index,
|
||||||
const string& expectedChecksum,
|
const string& expectedChecksum,
|
||||||
uint32_t dataLength,
|
int32_t dataLength,
|
||||||
uint32_t checksumLength)
|
int32_t checksumLength)
|
||||||
{
|
{
|
||||||
int64_t offset = index*checksumLength;
|
int64_t offset = ((int64_t)index)*checksumLength;
|
||||||
string actualChecksum = diskWriter->messageDigest(offset, dataLength, algo);
|
string actualChecksum = diskWriter->messageDigest(offset, dataLength, algo);
|
||||||
if(actualChecksum != expectedChecksum) {
|
if(actualChecksum != expectedChecksum) {
|
||||||
logger->error("Chunk checksum validation failed. checksumIndex=%d, offset=%lld, length=%u, expected=%s, actual=%s",
|
logger->error(EX_INVALID_CHUNK_CHECKSUM,
|
||||||
index, offset, dataLength, expectedChecksum.c_str(), actualChecksum.c_str());
|
index, offset, dataLength,
|
||||||
|
expectedChecksum.c_str(), actualChecksum.c_str());
|
||||||
bitfieldMan->unsetBit(index);
|
bitfieldMan->unsetBit(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,10 +57,10 @@ void ChunkChecksumValidator::validateSameLengthChecksum(BitfieldMan* bitfieldMan
|
||||||
void ChunkChecksumValidator::validateDifferentLengthChecksum(BitfieldMan* bitfieldMan,
|
void ChunkChecksumValidator::validateDifferentLengthChecksum(BitfieldMan* bitfieldMan,
|
||||||
int32_t index,
|
int32_t index,
|
||||||
const string& expectedChecksum,
|
const string& expectedChecksum,
|
||||||
uint32_t dataLength,
|
int32_t dataLength,
|
||||||
uint32_t checksumLength)
|
int32_t checksumLength)
|
||||||
{
|
{
|
||||||
int64_t offset = index*checksumLength;
|
int64_t offset = ((int64_t)index)*checksumLength;
|
||||||
int32_t startIndex;
|
int32_t startIndex;
|
||||||
int32_t endIndex;
|
int32_t endIndex;
|
||||||
Util::indexRange(startIndex, endIndex, offset,
|
Util::indexRange(startIndex, endIndex, offset,
|
||||||
|
@ -67,7 +69,7 @@ void ChunkChecksumValidator::validateDifferentLengthChecksum(BitfieldMan* bitfie
|
||||||
string actualChecksum = diskWriter->messageDigest(offset, dataLength, algo);
|
string actualChecksum = diskWriter->messageDigest(offset, dataLength, algo);
|
||||||
if(expectedChecksum != actualChecksum) {
|
if(expectedChecksum != actualChecksum) {
|
||||||
// wrong checksum
|
// wrong checksum
|
||||||
logger->error("Chunk checksum validation failed. checksumIndex=%d, offset=%lld, length=%u, expected=%s, actual=%s",
|
logger->error(EX_INVALID_CHUNK_CHECKSUM,
|
||||||
index, offset, dataLength,
|
index, offset, dataLength,
|
||||||
expectedChecksum.c_str(), actualChecksum.c_str());
|
expectedChecksum.c_str(), actualChecksum.c_str());
|
||||||
bitfieldMan->unsetBitRange(startIndex, endIndex);
|
bitfieldMan->unsetBitRange(startIndex, endIndex);
|
||||||
|
@ -77,18 +79,19 @@ void ChunkChecksumValidator::validateDifferentLengthChecksum(BitfieldMan* bitfie
|
||||||
|
|
||||||
void ChunkChecksumValidator::validate(BitfieldMan* bitfieldMan,
|
void ChunkChecksumValidator::validate(BitfieldMan* bitfieldMan,
|
||||||
const Strings& checksums,
|
const Strings& checksums,
|
||||||
uint32_t checksumLength)
|
int32_t checksumLength)
|
||||||
{
|
{
|
||||||
// We assume file is already opened using DiskWriter::open or openExistingFile.
|
// We assume file is already opened using DiskWriter::open or openExistingFile.
|
||||||
if(checksumLength*checksums.size() < bitfieldMan->getTotalLength()) {
|
if(((int64_t)checksumLength*checksums.size()) < bitfieldMan->getTotalLength()) {
|
||||||
// insufficient checksums.
|
// insufficient checksums.
|
||||||
logger->error("Insufficient checksums. checksumLength=%u, numChecksum=%u",
|
logger->error("Insufficient checksums. checksumLength=%d, numChecksum=%d",
|
||||||
checksumLength, checksums.size());
|
checksumLength, checksums.size());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint32_t x = bitfieldMan->getTotalLength()/checksumLength;
|
assert(bitfieldMan->getTotalLength()/checksumLength <= INT32_MAX);
|
||||||
uint32_t r = bitfieldMan->getTotalLength()%checksumLength;
|
int32_t x = bitfieldMan->getTotalLength()/checksumLength;
|
||||||
void (ChunkChecksumValidator::*f)(BitfieldMan*, int32_t, const string&, uint32_t, uint32_t);
|
int32_t r = bitfieldMan->getTotalLength()%checksumLength;
|
||||||
|
void (ChunkChecksumValidator::*f)(BitfieldMan*, int32_t, const string&, int32_t, int32_t);
|
||||||
|
|
||||||
if(checksumLength == bitfieldMan->getBlockLength()) {
|
if(checksumLength == bitfieldMan->getBlockLength()) {
|
||||||
f = &ChunkChecksumValidator::validateSameLengthChecksum;
|
f = &ChunkChecksumValidator::validateSameLengthChecksum;
|
||||||
|
@ -101,7 +104,7 @@ void ChunkChecksumValidator::validate(BitfieldMan* bitfieldMan,
|
||||||
fileAllocationMonitor->setCurrentValue(0);
|
fileAllocationMonitor->setCurrentValue(0);
|
||||||
fileAllocationMonitor->showProgress();
|
fileAllocationMonitor->showProgress();
|
||||||
Time cp;
|
Time cp;
|
||||||
for(uint32_t i = 0; i < x; ++i) {
|
for(int32_t i = 0; i < x; ++i) {
|
||||||
(this->*f)(bitfieldMan, i, checksums.at(i), checksumLength, checksumLength);
|
(this->*f)(bitfieldMan, i, checksums.at(i), checksumLength, checksumLength);
|
||||||
if(cp.elapsedInMillis(500)) {
|
if(cp.elapsedInMillis(500)) {
|
||||||
fileAllocationMonitor->setCurrentValue(i*checksumLength);
|
fileAllocationMonitor->setCurrentValue(i*checksumLength);
|
||||||
|
|
|
@ -56,14 +56,14 @@ private:
|
||||||
void validateSameLengthChecksum(BitfieldMan* bitfieldMan,
|
void validateSameLengthChecksum(BitfieldMan* bitfieldMan,
|
||||||
int32_t index,
|
int32_t index,
|
||||||
const string& expectedChecksum,
|
const string& expectedChecksum,
|
||||||
uint32_t thisLength,
|
int32_t thisLength,
|
||||||
uint32_t checksumLength);
|
int32_t checksumLength);
|
||||||
|
|
||||||
void validateDifferentLengthChecksum(BitfieldMan* bitfieldMan,
|
void validateDifferentLengthChecksum(BitfieldMan* bitfieldMan,
|
||||||
int32_t index,
|
int32_t index,
|
||||||
const string& expectedChecksum,
|
const string& expectedChecksum,
|
||||||
uint32_t thisLength,
|
int32_t thisLength,
|
||||||
uint32_t checksumLength);
|
int32_t checksumLength);
|
||||||
public:
|
public:
|
||||||
ChunkChecksumValidator():
|
ChunkChecksumValidator():
|
||||||
diskWriter(0),
|
diskWriter(0),
|
||||||
|
@ -76,7 +76,7 @@ public:
|
||||||
|
|
||||||
void validate(BitfieldMan* bitfieldMan,
|
void validate(BitfieldMan* bitfieldMan,
|
||||||
const Strings& checksums,
|
const Strings& checksums,
|
||||||
uint32_t checksumLength);
|
int32_t checksumLength);
|
||||||
|
|
||||||
void setDiskWriter(const DiskWriterHandle& diskWriter) {
|
void setDiskWriter(const DiskWriterHandle& diskWriter) {
|
||||||
this->diskWriter = diskWriter;
|
this->diskWriter = diskWriter;
|
||||||
|
|
|
@ -36,23 +36,24 @@
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
void ConsoleFileAllocationMonitor::showProgress() {
|
void ConsoleFileAllocationMonitor::showProgress() {
|
||||||
uint32_t progressPercentage = (uint32_t)(((current-min)*1.0/(max-min))*100);
|
int32_t progressPercentage = (int32_t)(((current-min)*1.0/(max-min))*100);
|
||||||
uint32_t numOfStar = progressPercentage/10*2;
|
int32_t numOfStar = progressPercentage/10*2;
|
||||||
|
|
||||||
cout << "\r ";
|
cout << "\r ";
|
||||||
cout << "\r";
|
cout << "\r";
|
||||||
cout << "|";
|
cout << "|";
|
||||||
for(uint32_t i = 0; i < numOfStar; i++) {
|
for(int32_t i = 0; i < numOfStar; ++i) {
|
||||||
cout << "*";
|
cout << "*";
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < 20-numOfStar; i++) {
|
for(int32_t i = 0; i < 20-numOfStar; ++i) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
cout << "|";
|
cout << "|";
|
||||||
cout << progressPercentage << "%";
|
cout << progressPercentage << "%";
|
||||||
cout << "(";
|
cout << "(";
|
||||||
cout << Util::ullitos(current, true) << "/" << Util::ullitos(max, true);
|
cout << Util::ullitos(current, true) << "/" << Util::ullitos(max, true);
|
||||||
cout << ") done";
|
cout << ")";
|
||||||
cout << flush;
|
cout << flush;
|
||||||
// |******************* | 95%(1,333,3256/1,553,3232 bytes) done
|
// Example,
|
||||||
|
// |******************* | 95%(1,333,3256/1,553,3232 bytes)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,9 @@
|
||||||
class ConsoleFileAllocationMonitor : public FileAllocationMonitor {
|
class ConsoleFileAllocationMonitor : public FileAllocationMonitor {
|
||||||
private:
|
private:
|
||||||
string filename;
|
string filename;
|
||||||
uint64_t min;
|
int64_t min;
|
||||||
uint64_t max;
|
int64_t max;
|
||||||
uint64_t current;
|
int64_t current;
|
||||||
public:
|
public:
|
||||||
ConsoleFileAllocationMonitor():min(0), max(0), current(0) {}
|
ConsoleFileAllocationMonitor():min(0), max(0), current(0) {}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ public:
|
||||||
this->filename = filename;
|
this->filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void setMinValue(const uint64_t& min) {
|
virtual void setMinValue(const int64_t& min) {
|
||||||
if(max < min) {
|
if(max < min) {
|
||||||
this->min = max;
|
this->min = max;
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,11 +60,11 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getMinValue() const {
|
int64_t getMinValue() const {
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void setMaxValue(const uint64_t& max) {
|
virtual void setMaxValue(const int64_t& max) {
|
||||||
if(max < min) {
|
if(max < min) {
|
||||||
this->max = min;
|
this->max = min;
|
||||||
} else {
|
} else {
|
||||||
|
@ -72,11 +72,11 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getMaxValue() const {
|
int64_t getMaxValue() const {
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void setCurrentValue(const uint64_t& current) {
|
virtual void setCurrentValue(const int64_t& current) {
|
||||||
if(current > max) {
|
if(current > max) {
|
||||||
this->current = max;
|
this->current = max;
|
||||||
} else {
|
} else {
|
||||||
|
@ -84,7 +84,7 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t getCurrentValue() const {
|
int64_t getCurrentValue() const {
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ void CopyDiskAdaptor::onDownloadComplete() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CopyDiskAdaptor::fixFilename() {
|
void CopyDiskAdaptor::fixFilename() {
|
||||||
long long int offset = 0;
|
int64_t offset = 0;
|
||||||
for(FileEntries::iterator itr = fileEntries.begin();
|
for(FileEntries::iterator itr = fileEntries.begin();
|
||||||
itr != fileEntries.end(); itr++) {
|
itr != fileEntries.end(); itr++) {
|
||||||
if(!(*itr)->isExtracted() && (*itr)->isRequested()) {
|
if(!(*itr)->isExtracted() && (*itr)->isRequested()) {
|
||||||
|
|
|
@ -105,8 +105,8 @@ void DefaultBtContext::extractFileEntries(Dictionary* infoDic,
|
||||||
// TODO use dynamic_cast
|
// TODO use dynamic_cast
|
||||||
List* files = (List*)infoDic->get("files");
|
List* files = (List*)infoDic->get("files");
|
||||||
if(files) {
|
if(files) {
|
||||||
long long int length = 0;
|
int64_t length = 0;
|
||||||
long long int offset = 0;
|
int64_t offset = 0;
|
||||||
// multi-file mode
|
// multi-file mode
|
||||||
fileMode = BtContext::MULTI;
|
fileMode = BtContext::MULTI;
|
||||||
const MetaList& metaList = files->getList();
|
const MetaList& metaList = files->getList();
|
||||||
|
@ -120,7 +120,7 @@ void DefaultBtContext::extractFileEntries(Dictionary* infoDic,
|
||||||
List* pathList = (List*)fileDic->get("path");
|
List* pathList = (List*)fileDic->get("path");
|
||||||
const MetaList& paths = pathList->getList();
|
const MetaList& paths = pathList->getList();
|
||||||
string path;
|
string path;
|
||||||
for(int i = 0; i < (int)paths.size()-1; i++) {
|
for(int32_t i = 0; i < (int32_t)paths.size()-1; i++) {
|
||||||
Data* subpath = (Data*)paths.at(i);
|
Data* subpath = (Data*)paths.at(i);
|
||||||
path += subpath->toString()+"/";
|
path += subpath->toString()+"/";
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,7 +220,7 @@ void DefaultBtInteractive::fillPiece(int maxPieceNum) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtInteractive::addRequests() {
|
void DefaultBtInteractive::addRequests() {
|
||||||
uint32_t MAX_PENDING_REQUEST;
|
int32_t MAX_PENDING_REQUEST;
|
||||||
if(peer->getLatency() < 500) {
|
if(peer->getLatency() < 500) {
|
||||||
MAX_PENDING_REQUEST = 24;
|
MAX_PENDING_REQUEST = 24;
|
||||||
} else if(peer->getLatency() < 1500) {
|
} else if(peer->getLatency() < 1500) {
|
||||||
|
@ -228,20 +228,19 @@ void DefaultBtInteractive::addRequests() {
|
||||||
} else {
|
} else {
|
||||||
MAX_PENDING_REQUEST = 6;
|
MAX_PENDING_REQUEST = 6;
|
||||||
}
|
}
|
||||||
uint32_t pieceNum;
|
int32_t pieceNum;
|
||||||
if(pieceStorage->isEndGame()) {
|
if(pieceStorage->isEndGame()) {
|
||||||
pieceNum = 1;
|
pieceNum = 1;
|
||||||
} else {
|
} else {
|
||||||
uint32_t blocks = DIV_FLOOR(btContext->getPieceLength(), BLOCK_LENGTH);
|
int32_t blocks = DIV_FLOOR(btContext->getPieceLength(), BLOCK_LENGTH);
|
||||||
pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
|
pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
|
||||||
}
|
}
|
||||||
fillPiece(pieceNum);
|
fillPiece(pieceNum);
|
||||||
|
|
||||||
uint32_t reqNumToCreate =
|
int32_t reqNumToCreate =
|
||||||
MAX_PENDING_REQUEST <= dispatcher->countOutstandingRequest() ?
|
MAX_PENDING_REQUEST <= dispatcher->countOutstandingRequest() ?
|
||||||
0 : MAX_PENDING_REQUEST-dispatcher->countOutstandingRequest();
|
0 : MAX_PENDING_REQUEST-dispatcher->countOutstandingRequest();
|
||||||
if(reqNumToCreate > 0) {
|
if(reqNumToCreate > 0) {
|
||||||
//logger->debug("CUID#%d - %u requets to go.", cuid, reqNumToCreate);
|
|
||||||
BtMessages requests;
|
BtMessages requests;
|
||||||
if(pieceStorage->isEndGame()) {
|
if(pieceStorage->isEndGame()) {
|
||||||
requests = btRequestFactory->createRequestMessagesOnEndGame(reqNumToCreate);
|
requests = btRequestFactory->createRequestMessagesOnEndGame(reqNumToCreate);
|
||||||
|
|
|
@ -51,28 +51,28 @@
|
||||||
|
|
||||||
class FloodingStat {
|
class FloodingStat {
|
||||||
private:
|
private:
|
||||||
uint32_t chokeUnchokeCount;
|
int32_t chokeUnchokeCount;
|
||||||
uint32_t keepAliveCount;
|
int32_t keepAliveCount;
|
||||||
public:
|
public:
|
||||||
FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
|
FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
|
||||||
|
|
||||||
void incChokeUnchokeCount() {
|
void incChokeUnchokeCount() {
|
||||||
if(chokeUnchokeCount < UINT32_MAX) {
|
if(chokeUnchokeCount < INT32_MAX) {
|
||||||
chokeUnchokeCount++;
|
chokeUnchokeCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void incKeepAliveCount() {
|
void incKeepAliveCount() {
|
||||||
if(keepAliveCount < UINT32_MAX) {
|
if(keepAliveCount < INT32_MAX) {
|
||||||
keepAliveCount++;
|
keepAliveCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getChokeUnchokeCount() const {
|
int32_t getChokeUnchokeCount() const {
|
||||||
return chokeUnchokeCount;
|
return chokeUnchokeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getKeepAliveCount() const {
|
int32_t getKeepAliveCount() const {
|
||||||
return keepAliveCount;
|
return keepAliveCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,15 +95,15 @@ private:
|
||||||
PeerConnectionWeakHandle peerConnection;
|
PeerConnectionWeakHandle peerConnection;
|
||||||
BtMessageFactoryWeakHandle messageFactory;
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
uint32_t allowedFastSetSize;
|
int32_t allowedFastSetSize;
|
||||||
Time haveCheckPoint;
|
Time haveCheckPoint;
|
||||||
Time keepAliveCheckPoint;
|
Time keepAliveCheckPoint;
|
||||||
Time floodingCheckPoint;
|
Time floodingCheckPoint;
|
||||||
FloodingStat floodingStat;
|
FloodingStat floodingStat;
|
||||||
uint32_t keepAliveInterval;
|
int32_t keepAliveInterval;
|
||||||
uint32_t maxDownloadSpeedLimit;
|
int32_t maxDownloadSpeedLimit;
|
||||||
|
|
||||||
static const uint32_t FLOODING_CHECK_INTERVAL = 5;
|
static const int32_t FLOODING_CHECK_INTERVAL = 5;
|
||||||
|
|
||||||
void addBitfieldMessageToQueue();
|
void addBitfieldMessageToQueue();
|
||||||
void addAllowedFastMessageToQueue();
|
void addAllowedFastMessageToQueue();
|
||||||
|
@ -147,7 +147,7 @@ public:
|
||||||
|
|
||||||
void receiveMessages();
|
void receiveMessages();
|
||||||
|
|
||||||
virtual uint32_t countPendingMessage() {
|
virtual int32_t countPendingMessage() {
|
||||||
return dispatcher->countMessageInQueue();
|
return dispatcher->countMessageInQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,11 +185,11 @@ public:
|
||||||
this->peerConnection = peerConnection;
|
this->peerConnection = peerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setKeepAliveInterval(uint32_t keepAliveInterval) {
|
void setKeepAliveInterval(int32_t keepAliveInterval) {
|
||||||
this->keepAliveInterval = keepAliveInterval;
|
this->keepAliveInterval = keepAliveInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMaxDownloadSpeedLimit(uint32_t maxDownloadSpeedLimit) {
|
void setMaxDownloadSpeedLimit(int32_t maxDownloadSpeedLimit) {
|
||||||
this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
|
this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ void DefaultBtMessageDispatcher::sendMessages() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cancel sending piece message to peer.
|
// Cancel sending piece message to peer.
|
||||||
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length)
|
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length)
|
||||||
{
|
{
|
||||||
BtCancelSendingPieceEventHandle event =
|
BtCancelSendingPieceEventHandle event =
|
||||||
new BtCancelSendingPieceEvent(index, begin, length);
|
new BtCancelSendingPieceEvent(index, begin, length);
|
||||||
|
@ -199,7 +199,7 @@ bool DefaultBtMessageDispatcher::isSendingInProgress()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t DefaultBtMessageDispatcher::countOutstandingRequest()
|
int32_t DefaultBtMessageDispatcher::countOutstandingRequest()
|
||||||
{
|
{
|
||||||
return requestSlots.size();
|
return requestSlots.size();
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,8 +56,8 @@ private:
|
||||||
PieceStorageHandle pieceStorage;
|
PieceStorageHandle pieceStorage;
|
||||||
BtMessageFactoryWeakHandle messageFactory;
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
uint32_t maxUploadSpeedLimit;
|
int32_t maxUploadSpeedLimit;
|
||||||
uint32_t requestTimeout;
|
int32_t requestTimeout;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
public:
|
public:
|
||||||
DefaultBtMessageDispatcher():
|
DefaultBtMessageDispatcher():
|
||||||
|
@ -78,7 +78,7 @@ public:
|
||||||
|
|
||||||
virtual void sendMessages();
|
virtual void sendMessages();
|
||||||
|
|
||||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length);
|
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, int32_t length);
|
||||||
|
|
||||||
virtual void doCancelSendingPieceAction(const PieceHandle& piece);
|
virtual void doCancelSendingPieceAction(const PieceHandle& piece);
|
||||||
|
|
||||||
|
@ -92,15 +92,15 @@ public:
|
||||||
|
|
||||||
virtual bool isSendingInProgress();
|
virtual bool isSendingInProgress();
|
||||||
|
|
||||||
virtual uint32_t countMessageInQueue() {
|
virtual int32_t countMessageInQueue() {
|
||||||
return messageQueue.size();
|
return messageQueue.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual uint32_t countOutstandingRequest();
|
virtual int32_t countOutstandingRequest();
|
||||||
|
|
||||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex);
|
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex);
|
||||||
|
|
||||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, uint32_t length) {
|
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, int32_t length) {
|
||||||
for(RequestSlots::iterator itr = requestSlots.begin();
|
for(RequestSlots::iterator itr = requestSlots.begin();
|
||||||
itr != requestSlots.end(); itr++) {
|
itr != requestSlots.end(); itr++) {
|
||||||
if(itr->getIndex() == index &&
|
if(itr->getIndex() == index &&
|
||||||
|
@ -146,11 +146,11 @@ public:
|
||||||
this->cuid = cuid;
|
this->cuid = cuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMaxUploadSpeedLimit(uint32_t maxUploadSpeedLimit) {
|
void setMaxUploadSpeedLimit(int32_t maxUploadSpeedLimit) {
|
||||||
this->maxUploadSpeedLimit = maxUploadSpeedLimit;
|
this->maxUploadSpeedLimit = maxUploadSpeedLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setRequestTimeout(uint32_t requestTimeout) {
|
void setRequestTimeout(int32_t requestTimeout) {
|
||||||
this->requestTimeout = requestTimeout;
|
this->requestTimeout = requestTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,14 +63,14 @@
|
||||||
#include "BtHandshakeMessageValidator.h"
|
#include "BtHandshakeMessageValidator.h"
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
DefaultBtMessageFactory::createBtMessage(const unsigned char* data, uint32_t dataLength)
|
DefaultBtMessageFactory::createBtMessage(const unsigned char* data, int32_t dataLength)
|
||||||
{
|
{
|
||||||
AbstractBtMessageHandle msg(0);
|
AbstractBtMessageHandle msg(0);
|
||||||
if(dataLength == 0) {
|
if(dataLength == 0) {
|
||||||
// keep-alive
|
// keep-alive
|
||||||
msg = new BtKeepAliveMessage();
|
msg = new BtKeepAliveMessage();
|
||||||
} else {
|
} else {
|
||||||
uint8_t id = PeerMessageUtil::getId(data);
|
int8_t id = PeerMessageUtil::getId(data);
|
||||||
switch(id) {
|
switch(id) {
|
||||||
case BtChokeMessage::ID:
|
case BtChokeMessage::ID:
|
||||||
msg = BtChokeMessage::create(data, dataLength);
|
msg = BtChokeMessage::create(data, dataLength);
|
||||||
|
@ -162,7 +162,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, uint32_t dat
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw new DlAbortEx("Invalid message id. id = %u", id);
|
throw new DlAbortEx("Invalid message ID. id=%d", id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setCommonProperty(msg);
|
setCommonProperty(msg);
|
||||||
|
@ -180,7 +180,7 @@ void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& m
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data, uint32_t dataLength)
|
DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data, int32_t dataLength)
|
||||||
{
|
{
|
||||||
BtHandshakeMessageHandle msg = BtHandshakeMessage::create(data, dataLength);
|
BtHandshakeMessageHandle msg = BtHandshakeMessage::create(data, dataLength);
|
||||||
BtMessageValidatorHandle validator =
|
BtMessageValidatorHandle validator =
|
||||||
|
@ -222,7 +222,7 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, int32_t
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
DefaultBtMessageFactory::createCancelMessage(int32_t index, int32_t begin, uint32_t length)
|
DefaultBtMessageFactory::createCancelMessage(int32_t index, int32_t begin, int32_t length)
|
||||||
{
|
{
|
||||||
BtCancelMessageHandle msg = new BtCancelMessage(index, begin, length);
|
BtCancelMessageHandle msg = new BtCancelMessage(index, begin, length);
|
||||||
BtMessageValidatorHandle validator =
|
BtMessageValidatorHandle validator =
|
||||||
|
@ -235,7 +235,7 @@ DefaultBtMessageFactory::createCancelMessage(int32_t index, int32_t begin, uint3
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
DefaultBtMessageFactory::createPieceMessage(int32_t index, int32_t begin, uint32_t length)
|
DefaultBtMessageFactory::createPieceMessage(int32_t index, int32_t begin, int32_t length)
|
||||||
{
|
{
|
||||||
BtPieceMessageHandle msg = new BtPieceMessage(index, begin, length);
|
BtPieceMessageHandle msg = new BtPieceMessage(index, begin, length);
|
||||||
BtMessageValidatorHandle validator =
|
BtMessageValidatorHandle validator =
|
||||||
|
@ -326,7 +326,7 @@ DefaultBtMessageFactory::createHaveNoneMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
DefaultBtMessageFactory::createRejectMessage(int32_t index, int32_t begin, uint32_t length)
|
DefaultBtMessageFactory::createRejectMessage(int32_t index, int32_t begin, int32_t length)
|
||||||
{
|
{
|
||||||
BtRejectMessageHandle msg = new BtRejectMessage(index, begin, length);
|
BtRejectMessageHandle msg = new BtRejectMessage(index, begin, length);
|
||||||
BtMessageValidatorHandle validator =
|
BtMessageValidatorHandle validator =
|
||||||
|
|
|
@ -69,10 +69,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createBtMessage(const unsigned char* msg, uint32_t msgLength);
|
createBtMessage(const unsigned char* msg, int32_t msgLength);
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createHandshakeMessage(const unsigned char* msg, uint32_t msgLength);
|
createHandshakeMessage(const unsigned char* msg, int32_t msgLength);
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createHandshakeMessage(const unsigned char* infoHash,
|
createHandshakeMessage(const unsigned char* infoHash,
|
||||||
|
@ -82,10 +82,10 @@ public:
|
||||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex);
|
createRequestMessage(const PieceHandle& piece, int32_t blockIndex);
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length);
|
createCancelMessage(int32_t index, int32_t begin, int32_t length);
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length);
|
createPieceMessage(int32_t index, int32_t begin, int32_t length);
|
||||||
|
|
||||||
virtual BtMessageHandle createHaveMessage(int32_t index);
|
virtual BtMessageHandle createHaveMessage(int32_t index);
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ public:
|
||||||
virtual BtMessageHandle createHaveNoneMessage();
|
virtual BtMessageHandle createHaveNoneMessage();
|
||||||
|
|
||||||
virtual BtMessageHandle
|
virtual BtMessageHandle
|
||||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length);
|
createRejectMessage(int32_t index, int32_t begin, int32_t length);
|
||||||
|
|
||||||
virtual BtMessageHandle createAllowedFastMessage(int32_t index);
|
virtual BtMessageHandle createAllowedFastMessage(int32_t index);
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
|
|
||||||
BtMessageHandle DefaultBtMessageReceiver::receiveHandshake(bool quickReply) {
|
BtMessageHandle DefaultBtMessageReceiver::receiveHandshake(bool quickReply) {
|
||||||
unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
|
unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
|
||||||
uint32_t dataLength = sizeof(data);
|
int32_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH;
|
||||||
bool retval = peerConnection->receiveHandshake(data, dataLength);
|
bool retval = peerConnection->receiveHandshake(data, dataLength);
|
||||||
// To handle tracker's NAT-checking feature
|
// To handle tracker's NAT-checking feature
|
||||||
if(!handshakeSent && quickReply && dataLength >= 48) {
|
if(!handshakeSent && quickReply && dataLength >= 48) {
|
||||||
|
@ -77,7 +77,7 @@ void DefaultBtMessageReceiver::sendHandshake() {
|
||||||
|
|
||||||
BtMessageHandle DefaultBtMessageReceiver::receiveMessage() {
|
BtMessageHandle DefaultBtMessageReceiver::receiveMessage() {
|
||||||
unsigned char data[MAX_PAYLOAD_LEN];
|
unsigned char data[MAX_PAYLOAD_LEN];
|
||||||
uint32_t dataLength = 0;
|
int32_t dataLength = 0;
|
||||||
if(!peerConnection->receiveMessage(data, dataLength)) {
|
if(!peerConnection->receiveMessage(data, dataLength)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,7 @@ void DefaultBtRequestFactory::removeAllTargetPiece() {
|
||||||
pieces.clear();
|
pieces.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessages DefaultBtRequestFactory::createRequestMessages(uint32_t max) {
|
BtMessages DefaultBtRequestFactory::createRequestMessages(int32_t max) {
|
||||||
BtMessages requests;
|
BtMessages requests;
|
||||||
for(Pieces::iterator itr = pieces.begin();
|
for(Pieces::iterator itr = pieces.begin();
|
||||||
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
||||||
|
@ -90,7 +90,7 @@ BtMessages DefaultBtRequestFactory::createRequestMessages(uint32_t max) {
|
||||||
return requests;
|
return requests;
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(uint32_t max) {
|
BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(int32_t max) {
|
||||||
BtMessages requests;
|
BtMessages requests;
|
||||||
for(Pieces::iterator itr = pieces.begin();
|
for(Pieces::iterator itr = pieces.begin();
|
||||||
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
||||||
|
|
|
@ -85,9 +85,9 @@ public:
|
||||||
|
|
||||||
virtual void doChokedAction();
|
virtual void doChokedAction();
|
||||||
|
|
||||||
virtual BtMessages createRequestMessages(uint32_t max);
|
virtual BtMessages createRequestMessages(int32_t max);
|
||||||
|
|
||||||
virtual BtMessages createRequestMessagesOnEndGame(uint32_t max);
|
virtual BtMessages createRequestMessagesOnEndGame(int32_t max);
|
||||||
|
|
||||||
Pieces& getTargetPieces() {
|
Pieces& getTargetPieces() {
|
||||||
return pieces;
|
return pieces;
|
||||||
|
|
|
@ -46,7 +46,7 @@ DefaultDiskWriter::DefaultDiskWriter():AbstractDiskWriter() {}
|
||||||
DefaultDiskWriter::~DefaultDiskWriter() {}
|
DefaultDiskWriter::~DefaultDiskWriter() {}
|
||||||
|
|
||||||
void DefaultDiskWriter::initAndOpenFile(const string& filename,
|
void DefaultDiskWriter::initAndOpenFile(const string& filename,
|
||||||
uint64_t totalLength)
|
int64_t totalLength)
|
||||||
{
|
{
|
||||||
createFile(filename);
|
createFile(filename);
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -45,7 +45,7 @@ public:
|
||||||
virtual ~DefaultDiskWriter();
|
virtual ~DefaultDiskWriter();
|
||||||
|
|
||||||
virtual void initAndOpenFile(const string& filename,
|
virtual void initAndOpenFile(const string& filename,
|
||||||
uint64_t totalLength = 0);
|
int64_t totalLength = 0);
|
||||||
|
|
||||||
static DefaultDiskWriter* createNewDiskWriter(const Option* option);
|
static DefaultDiskWriter* createNewDiskWriter(const Option* option);
|
||||||
};
|
};
|
||||||
|
|
|
@ -71,7 +71,7 @@ private:
|
||||||
BitfieldMan* bitfieldMan;
|
BitfieldMan* bitfieldMan;
|
||||||
DiskAdaptorHandle diskAdaptor;
|
DiskAdaptorHandle diskAdaptor;
|
||||||
Pieces usedPieces;
|
Pieces usedPieces;
|
||||||
uint32_t endGamePieceNum;
|
int32_t endGamePieceNum;
|
||||||
Logger* logger;
|
Logger* logger;
|
||||||
const Option* option;
|
const Option* option;
|
||||||
Haves haves;
|
Haves haves;
|
||||||
|
@ -127,11 +127,11 @@ public:
|
||||||
|
|
||||||
virtual const unsigned char* getBitfield();
|
virtual const unsigned char* getBitfield();
|
||||||
|
|
||||||
void setEndGamePieceNum(uint32_t num) {
|
void setEndGamePieceNum(int32_t num) {
|
||||||
endGamePieceNum = num;
|
endGamePieceNum = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getEndGamePieceNum() const {
|
int32_t getEndGamePieceNum() const {
|
||||||
return endGamePieceNum;
|
return endGamePieceNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,11 +57,11 @@ public:
|
||||||
|
|
||||||
virtual void initAndOpenFile() = 0;
|
virtual void initAndOpenFile() = 0;
|
||||||
|
|
||||||
virtual void writeData(const unsigned char* data, uint32_t len, int64_t offset) = 0;
|
virtual void writeData(const unsigned char* data, int32_t len, int64_t offset) = 0;
|
||||||
|
|
||||||
virtual int readData(unsigned char* data, uint32_t len, int64_t offset) = 0;
|
virtual int32_t readData(unsigned char* data, int32_t len, int64_t offset) = 0;
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo) = 0;
|
const MessageDigestContext::DigestAlgo& algo) = 0;
|
||||||
|
|
||||||
virtual void onDownloadComplete() = 0;
|
virtual void onDownloadComplete() = 0;
|
||||||
|
|
|
@ -47,12 +47,12 @@ public:
|
||||||
|
|
||||||
virtual ~DiskAdaptorWriter() {}
|
virtual ~DiskAdaptorWriter() {}
|
||||||
|
|
||||||
virtual void initAndOpenFile(const string& filename, uint64_t totalLength = 0)
|
virtual void initAndOpenFile(const string& filename, int64_t totalLength = 0)
|
||||||
{
|
{
|
||||||
diskAdaptor->initAndOpenFile();
|
diskAdaptor->initAndOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void openFile(const string& filename, uint64_t totalLength = 0)
|
virtual void openFile(const string& filename, int64_t totalLength = 0)
|
||||||
{
|
{
|
||||||
diskAdaptor->openFile();
|
diskAdaptor->openFile();
|
||||||
}
|
}
|
||||||
|
@ -67,17 +67,17 @@ public:
|
||||||
diskAdaptor->openExistingFile();
|
diskAdaptor->openExistingFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void writeData(const char* data, uint32_t len, int64_t position = 0)
|
virtual void writeData(const char* data, int32_t len, int64_t position = 0)
|
||||||
{
|
{
|
||||||
diskAdaptor->writeData((const unsigned char*)data, len, position);
|
diskAdaptor->writeData((const unsigned char*)data, len, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual int readData(char* data, uint32_t len, int64_t position)
|
virtual int32_t readData(char* data, int32_t len, int64_t position)
|
||||||
{
|
{
|
||||||
return diskAdaptor->readData((unsigned char*)data, len, position);
|
return diskAdaptor->readData((unsigned char*)data, len, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo)
|
const MessageDigestContext::DigestAlgo& algo)
|
||||||
{
|
{
|
||||||
return diskAdaptor->messageDigest(offset, length, algo);
|
return diskAdaptor->messageDigest(offset, length, algo);
|
||||||
|
|
|
@ -55,9 +55,9 @@ public:
|
||||||
* If the file exists, then it is truncated to 0 length.
|
* If the file exists, then it is truncated to 0 length.
|
||||||
* @param filename the file name to be opened.
|
* @param filename the file name to be opened.
|
||||||
*/
|
*/
|
||||||
virtual void initAndOpenFile(const string& filename, uint64_t totalLength = 0) = 0;
|
virtual void initAndOpenFile(const string& filename, int64_t totalLength = 0) = 0;
|
||||||
|
|
||||||
virtual void openFile(const string& filename, uint64_t totalLength = 0) = 0;
|
virtual void openFile(const string& filename, int64_t totalLength = 0) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this output stream.
|
* Closes this output stream.
|
||||||
|
@ -81,18 +81,18 @@ public:
|
||||||
* @param len the number of bytes to write
|
* @param len the number of bytes to write
|
||||||
* @param position the offset of this binary stream
|
* @param position the offset of this binary stream
|
||||||
*/
|
*/
|
||||||
virtual void writeData(const char* data, uint32_t len, int64_t position = 0) = 0;
|
virtual void writeData(const char* data, int32_t len, int64_t position = 0) = 0;
|
||||||
virtual void writeData(const unsigned char* data, uint32_t len, int64_t position = 0)
|
virtual void writeData(const unsigned char* data, int32_t len, int64_t position = 0)
|
||||||
{
|
{
|
||||||
writeData((const char*)data, len, position);
|
writeData((const char*)data, len, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual int readData(char* data, uint32_t len, int64_t position) = 0;
|
virtual int readData(char* data, int32_t len, int64_t position) = 0;
|
||||||
virtual int readData(unsigned char* data, uint32_t len, int64_t position) {
|
virtual int readData(unsigned char* data, int32_t len, int64_t position) {
|
||||||
return readData((char*)data, len, position);
|
return readData((char*)data, len, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo) = 0;
|
const MessageDigestContext::DigestAlgo& algo) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,7 @@ bool DownloadCommand::executeInternal(Segment& segment) {
|
||||||
}
|
}
|
||||||
// calculate downloading speed
|
// calculate downloading speed
|
||||||
if(peerStat->getDownloadStartTime().elapsed(startupIdleTime)) {
|
if(peerStat->getDownloadStartTime().elapsed(startupIdleTime)) {
|
||||||
uint32_t nowSpeed = peerStat->calculateDownloadSpeed();
|
int32_t nowSpeed = peerStat->calculateDownloadSpeed();
|
||||||
if(lowestDownloadSpeedLimit > 0 && nowSpeed <= lowestDownloadSpeedLimit) {
|
if(lowestDownloadSpeedLimit > 0 && nowSpeed <= lowestDownloadSpeedLimit) {
|
||||||
throw new DlAbortEx("CUID#%d - Too slow Downloading speed: %d <= %d(B/s)",
|
throw new DlAbortEx("CUID#%d - Too slow Downloading speed: %d <= %d(B/s)",
|
||||||
cuid,
|
cuid,
|
||||||
|
|
|
@ -45,9 +45,9 @@ using namespace std;
|
||||||
class DownloadCommand : public AbstractCommand {
|
class DownloadCommand : public AbstractCommand {
|
||||||
private:
|
private:
|
||||||
long long int lastSize;
|
long long int lastSize;
|
||||||
uint32_t maxDownloadSpeedLimit;
|
int32_t maxDownloadSpeedLimit;
|
||||||
uint32_t startupIdleTime;
|
int32_t startupIdleTime;
|
||||||
uint32_t lowestDownloadSpeedLimit;
|
int32_t lowestDownloadSpeedLimit;
|
||||||
PeerStatHandle peerStat;
|
PeerStatHandle peerStat;
|
||||||
protected:
|
protected:
|
||||||
bool executeInternal(Segment& segment);
|
bool executeInternal(Segment& segment);
|
||||||
|
@ -62,15 +62,15 @@ public:
|
||||||
|
|
||||||
string transferEncoding;
|
string transferEncoding;
|
||||||
|
|
||||||
void setMaxDownloadSpeedLimit(uint32_t maxDownloadSpeedLimit) {
|
void setMaxDownloadSpeedLimit(int32_t maxDownloadSpeedLimit) {
|
||||||
this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
|
this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setStartupIdleTime(uint32_t startupIdleTime) {
|
void setStartupIdleTime(int32_t startupIdleTime) {
|
||||||
this->startupIdleTime = startupIdleTime;
|
this->startupIdleTime = startupIdleTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLowestDownloadSpeedLimit(uint32_t lowestDownloadSpeedLimit) {
|
void setLowestDownloadSpeedLimit(int32_t lowestDownloadSpeedLimit) {
|
||||||
this->lowestDownloadSpeedLimit = lowestDownloadSpeedLimit;
|
this->lowestDownloadSpeedLimit = lowestDownloadSpeedLimit;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
|
|
||||||
#include "FileProgressMonitor.h"
|
#include "FileProgressMonitor.h"
|
||||||
|
|
||||||
typedef FileProgressMonitor<uint64_t> FileAllocationMonitor;
|
typedef FileProgressMonitor<int64_t> FileAllocationMonitor;
|
||||||
typedef SharedHandle<FileAllocationMonitor> FileAllocationMonitorHandle;
|
typedef SharedHandle<FileAllocationMonitor> FileAllocationMonitorHandle;
|
||||||
|
|
||||||
class FileAllocationMonitorFactory;
|
class FileAllocationMonitorFactory;
|
||||||
|
|
|
@ -40,21 +40,21 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
void FileAllocator::allocate(int fd, uint64_t totalLength)
|
void FileAllocator::allocate(int fd, int64_t totalLength)
|
||||||
{
|
{
|
||||||
if(0 != lseek(fd, 0, SEEK_SET)) {
|
if(0 != lseek(fd, 0, SEEK_SET)) {
|
||||||
throw new DlAbortEx("Seek failed: %s", strerror(errno));
|
throw new DlAbortEx("Seek failed: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
uint32_t bufSize = 4096;
|
int32_t bufSize = 4096;
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
memset(buf, 0, bufSize);
|
memset(buf, 0, bufSize);
|
||||||
uint64_t x = (totalLength+bufSize-1)/bufSize;
|
int64_t x = (totalLength+bufSize-1)/bufSize;
|
||||||
fileAllocationMonitor->setMinValue(0);
|
fileAllocationMonitor->setMinValue(0);
|
||||||
fileAllocationMonitor->setMaxValue(totalLength);
|
fileAllocationMonitor->setMaxValue(totalLength);
|
||||||
fileAllocationMonitor->setCurrentValue(0);
|
fileAllocationMonitor->setCurrentValue(0);
|
||||||
fileAllocationMonitor->showProgress();
|
fileAllocationMonitor->showProgress();
|
||||||
Time cp;
|
Time cp;
|
||||||
for(uint64_t i = 0; i < x; i++) {
|
for(int64_t i = 0; i < x; ++i) {
|
||||||
if(write(fd, buf, bufSize) < 0) {
|
if(write(fd, buf, bufSize) < 0) {
|
||||||
throw new DlAbortEx("Allocation failed: %s", strerror(errno));
|
throw new DlAbortEx("Allocation failed: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ public:
|
||||||
|
|
||||||
~FileAllocator() {}
|
~FileAllocator() {}
|
||||||
|
|
||||||
void allocate(int fd, uint64_t totalLength);
|
void allocate(int fd, int64_t totalLength);
|
||||||
|
|
||||||
void setFileAllocationMonitor(const FileAllocationMonitorHandle& monitor)
|
void setFileAllocationMonitor(const FileAllocationMonitorHandle& monitor)
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
class MetalinkChunkChecksum {
|
class MetalinkChunkChecksum {
|
||||||
public:
|
public:
|
||||||
MessageDigestContext::DigestAlgo digestAlgo;
|
MessageDigestContext::DigestAlgo digestAlgo;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
Strings pieceHashes;
|
Strings pieceHashes;
|
||||||
public:
|
public:
|
||||||
MetalinkChunkChecksum():pieceLength(0) {}
|
MetalinkChunkChecksum():pieceLength(0) {}
|
||||||
|
|
|
@ -47,7 +47,7 @@ public:
|
||||||
string version;
|
string version;
|
||||||
string language;
|
string language;
|
||||||
string os;
|
string os;
|
||||||
uint64_t size;
|
int64_t size;
|
||||||
Checksum checksum;
|
Checksum checksum;
|
||||||
public:
|
public:
|
||||||
MetalinkResources resources;
|
MetalinkResources resources;
|
||||||
|
|
|
@ -102,16 +102,16 @@ void MultiDiskAdaptor::onDownloadComplete() {
|
||||||
openFile();
|
openFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MultiDiskAdaptor::writeData(const unsigned char* data, uint32_t len,
|
void MultiDiskAdaptor::writeData(const unsigned char* data, int32_t len,
|
||||||
int64_t offset)
|
int64_t offset)
|
||||||
{
|
{
|
||||||
int64_t fileOffset = offset;
|
int64_t fileOffset = offset;
|
||||||
bool writing = false;
|
bool writing = false;
|
||||||
uint32_t rem = len;
|
int32_t rem = len;
|
||||||
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
||||||
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
||||||
if(isInRange(*itr, offset) || writing) {
|
if(isInRange(*itr, offset) || writing) {
|
||||||
uint32_t writeLength = calculateLength(*itr, fileOffset, rem);
|
int32_t writeLength = calculateLength(*itr, fileOffset, rem);
|
||||||
(*itr)->getDiskWriter()->writeData(data+(len-rem), writeLength, fileOffset);
|
(*itr)->getDiskWriter()->writeData(data+(len-rem), writeLength, fileOffset);
|
||||||
rem -= writeLength;
|
rem -= writeLength;
|
||||||
writing = true;
|
writing = true;
|
||||||
|
@ -132,11 +132,11 @@ bool MultiDiskAdaptor::isInRange(const DiskWriterEntryHandle entry,
|
||||||
offset < entry->getFileEntry()->getOffset()+entry->getFileEntry()->getLength();
|
offset < entry->getFileEntry()->getOffset()+entry->getFileEntry()->getLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t MultiDiskAdaptor::calculateLength(const DiskWriterEntryHandle entry,
|
int32_t MultiDiskAdaptor::calculateLength(const DiskWriterEntryHandle entry,
|
||||||
int64_t fileOffset,
|
int64_t fileOffset,
|
||||||
uint32_t rem) const
|
int32_t rem) const
|
||||||
{
|
{
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
if(entry->getFileEntry()->getLength() < fileOffset+rem) {
|
if(entry->getFileEntry()->getLength() < fileOffset+rem) {
|
||||||
length = entry->getFileEntry()->getLength()-fileOffset;
|
length = entry->getFileEntry()->getLength()-fileOffset;
|
||||||
} else {
|
} else {
|
||||||
|
@ -145,16 +145,16 @@ uint32_t MultiDiskAdaptor::calculateLength(const DiskWriterEntryHandle entry,
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MultiDiskAdaptor::readData(unsigned char* data, uint32_t len, int64_t offset)
|
int MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset)
|
||||||
{
|
{
|
||||||
int64_t fileOffset = offset;
|
int64_t fileOffset = offset;
|
||||||
bool reading = false;
|
bool reading = false;
|
||||||
uint32_t rem = len;
|
int32_t rem = len;
|
||||||
uint32_t totalReadLength = 0;
|
int32_t totalReadLength = 0;
|
||||||
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
||||||
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
||||||
if(isInRange(*itr, offset) || reading) {
|
if(isInRange(*itr, offset) || reading) {
|
||||||
uint32_t readLength = calculateLength((*itr), fileOffset, rem);
|
int32_t readLength = calculateLength((*itr), fileOffset, rem);
|
||||||
totalReadLength += (*itr)->getDiskWriter()->readData(data+(len-rem), readLength, fileOffset);
|
totalReadLength += (*itr)->getDiskWriter()->readData(data+(len-rem), readLength, fileOffset);
|
||||||
rem -= readLength;
|
rem -= readLength;
|
||||||
reading = true;
|
reading = true;
|
||||||
|
@ -171,18 +171,18 @@ int MultiDiskAdaptor::readData(unsigned char* data, uint32_t len, int64_t offset
|
||||||
|
|
||||||
void MultiDiskAdaptor::hashUpdate(MessageDigestContext& ctx,
|
void MultiDiskAdaptor::hashUpdate(MessageDigestContext& ctx,
|
||||||
const DiskWriterEntryHandle& entry,
|
const DiskWriterEntryHandle& entry,
|
||||||
int64_t offset, uint64_t length)
|
int64_t offset, int64_t length)
|
||||||
{
|
{
|
||||||
uint32_t BUFSIZE = 16*1024;
|
int32_t BUFSIZE = 16*1024;
|
||||||
unsigned char buf[BUFSIZE];
|
unsigned char buf[BUFSIZE];
|
||||||
for(uint64_t i = 0; i < length/BUFSIZE; i++) {
|
for(int64_t i = 0; i < length/BUFSIZE; i++) {
|
||||||
if((int32_t)BUFSIZE != entry->getDiskWriter()->readData(buf, BUFSIZE, offset)) {
|
if(BUFSIZE != entry->getDiskWriter()->readData(buf, BUFSIZE, offset)) {
|
||||||
throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
|
throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
|
||||||
}
|
}
|
||||||
ctx.digestUpdate(buf, BUFSIZE);
|
ctx.digestUpdate(buf, BUFSIZE);
|
||||||
offset += BUFSIZE;
|
offset += BUFSIZE;
|
||||||
}
|
}
|
||||||
uint32_t r = length%BUFSIZE;
|
int32_t r = length%BUFSIZE;
|
||||||
if(r > 0) {
|
if(r > 0) {
|
||||||
if((int32_t)r != entry->getDiskWriter()->readData(buf, r, offset)) {
|
if((int32_t)r != entry->getDiskWriter()->readData(buf, r, offset)) {
|
||||||
throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
|
throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
|
||||||
|
@ -191,18 +191,18 @@ void MultiDiskAdaptor::hashUpdate(MessageDigestContext& ctx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string MultiDiskAdaptor::messageDigest(int64_t offset, uint64_t length,
|
string MultiDiskAdaptor::messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo) {
|
const MessageDigestContext::DigestAlgo& algo) {
|
||||||
int64_t fileOffset = offset;
|
int64_t fileOffset = offset;
|
||||||
bool reading = false;
|
bool reading = false;
|
||||||
uint32_t rem = length;
|
int32_t rem = length;
|
||||||
MessageDigestContext ctx(algo);
|
MessageDigestContext ctx(algo);
|
||||||
ctx.digestInit();
|
ctx.digestInit();
|
||||||
|
|
||||||
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
|
||||||
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
itr != diskWriterEntries.end() && rem != 0; itr++) {
|
||||||
if(isInRange(*itr, offset) || reading) {
|
if(isInRange(*itr, offset) || reading) {
|
||||||
uint32_t readLength = calculateLength((*itr), fileOffset, rem);
|
int32_t readLength = calculateLength((*itr), fileOffset, rem);
|
||||||
hashUpdate(ctx, *itr, fileOffset, readLength);
|
hashUpdate(ctx, *itr, fileOffset, readLength);
|
||||||
rem -= readLength;
|
rem -= readLength;
|
||||||
reading = true;
|
reading = true;
|
||||||
|
|
|
@ -101,7 +101,7 @@ typedef deque<DiskWriterEntryHandle> DiskWriterEntries;
|
||||||
class MultiDiskAdaptor : public DiskAdaptor {
|
class MultiDiskAdaptor : public DiskAdaptor {
|
||||||
private:
|
private:
|
||||||
string topDir;
|
string topDir;
|
||||||
uint32_t pieceLength;
|
int32_t pieceLength;
|
||||||
DiskWriterEntries diskWriterEntries;
|
DiskWriterEntries diskWriterEntries;
|
||||||
const Option* option;
|
const Option* option;
|
||||||
|
|
||||||
|
@ -111,13 +111,13 @@ private:
|
||||||
|
|
||||||
bool isInRange(const DiskWriterEntryHandle entry, int64_t offset) const;
|
bool isInRange(const DiskWriterEntryHandle entry, int64_t offset) const;
|
||||||
|
|
||||||
uint32_t calculateLength(const DiskWriterEntryHandle entry,
|
int32_t calculateLength(const DiskWriterEntryHandle entry,
|
||||||
int64_t fileOffset,
|
int64_t fileOffset,
|
||||||
uint32_t rem) const;
|
int32_t rem) const;
|
||||||
|
|
||||||
void hashUpdate(MessageDigestContext& ctx,
|
void hashUpdate(MessageDigestContext& ctx,
|
||||||
const DiskWriterEntryHandle& entry,
|
const DiskWriterEntryHandle& entry,
|
||||||
int64_t offset, uint64_t length);
|
int64_t offset, int64_t length);
|
||||||
|
|
||||||
string getTopDirPath() const;
|
string getTopDirPath() const;
|
||||||
public:
|
public:
|
||||||
|
@ -137,12 +137,12 @@ public:
|
||||||
|
|
||||||
virtual void onDownloadComplete();
|
virtual void onDownloadComplete();
|
||||||
|
|
||||||
virtual void writeData(const unsigned char* data, uint32_t len,
|
virtual void writeData(const unsigned char* data, int32_t len,
|
||||||
int64_t offset);
|
int64_t offset);
|
||||||
|
|
||||||
virtual int readData(unsigned char* data, uint32_t len, int64_t offset);
|
virtual int readData(unsigned char* data, int32_t len, int64_t offset);
|
||||||
|
|
||||||
virtual string messageDigest(int64_t offset, uint64_t length,
|
virtual string messageDigest(int64_t offset, int64_t length,
|
||||||
const MessageDigestContext::DigestAlgo& algo);
|
const MessageDigestContext::DigestAlgo& algo);
|
||||||
|
|
||||||
virtual bool fileExists();
|
virtual bool fileExists();
|
||||||
|
@ -159,11 +159,11 @@ public:
|
||||||
return topDir;
|
return topDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPieceLength(uint32_t pieceLength) {
|
void setPieceLength(int32_t pieceLength) {
|
||||||
this->pieceLength = pieceLength;
|
this->pieceLength = pieceLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getPieceLength() const {
|
int32_t getPieceLength() const {
|
||||||
return pieceLength;
|
return pieceLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,11 +45,11 @@ public:
|
||||||
|
|
||||||
virtual void setFilename(const string& filename) {}
|
virtual void setFilename(const string& filename) {}
|
||||||
|
|
||||||
virtual void setMinValue(const uint64_t& min) {}
|
virtual void setMinValue(const int64_t& min) {}
|
||||||
|
|
||||||
virtual void setMaxValue(const uint64_t& max) {}
|
virtual void setMaxValue(const int64_t& max) {}
|
||||||
|
|
||||||
virtual void setCurrentValue(const uint64_t& current) {}
|
virtual void setCurrentValue(const int64_t& current) {}
|
||||||
|
|
||||||
virtual void showProgress() {}
|
virtual void showProgress() {}
|
||||||
|
|
||||||
|
|
|
@ -56,8 +56,8 @@ PeerConnection::PeerConnection(int32_t cuid,
|
||||||
|
|
||||||
PeerConnection::~PeerConnection() {}
|
PeerConnection::~PeerConnection() {}
|
||||||
|
|
||||||
uint32_t PeerConnection::sendMessage(const unsigned char* data, uint32_t dataLength) {
|
int32_t PeerConnection::sendMessage(const unsigned char* data, int32_t dataLength) {
|
||||||
uint32_t writtenLength = 0;
|
int32_t writtenLength = 0;
|
||||||
if(socket->isWritable(0)) {
|
if(socket->isWritable(0)) {
|
||||||
// TODO fix this
|
// TODO fix this
|
||||||
socket->writeData((const char*)data, dataLength);
|
socket->writeData((const char*)data, dataLength);
|
||||||
|
@ -66,14 +66,14 @@ uint32_t PeerConnection::sendMessage(const unsigned char* data, uint32_t dataLen
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::receiveMessage(unsigned char* data, uint32_t& dataLength) {
|
bool PeerConnection::receiveMessage(unsigned char* data, int32_t& dataLength) {
|
||||||
if(!socket->isReadable(0)) {
|
if(!socket->isReadable(0)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(resbufLength == 0 && lenbufLength != 4) {
|
if(resbufLength == 0 && lenbufLength != 4) {
|
||||||
// read payload size, 4-byte integer
|
// read payload size, 4-byte integer
|
||||||
uint32_t remain = 4-lenbufLength;
|
int32_t remain = 4-lenbufLength;
|
||||||
uint32_t temp = remain;
|
int32_t temp = remain;
|
||||||
// TODO fix this
|
// TODO fix this
|
||||||
socket->readData((char*)lenbuf+lenbufLength, (int&)temp);
|
socket->readData((char*)lenbuf+lenbufLength, (int&)temp);
|
||||||
if(temp == 0) {
|
if(temp == 0) {
|
||||||
|
@ -86,7 +86,7 @@ bool PeerConnection::receiveMessage(unsigned char* data, uint32_t& dataLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//payloadLen = ntohl(nPayloadLen);
|
//payloadLen = ntohl(nPayloadLen);
|
||||||
uint32_t payloadLength = ntohl(*((uint32_t*)lenbuf));
|
int32_t payloadLength = ntohl(*((int32_t*)lenbuf));
|
||||||
if(payloadLength > MAX_PAYLOAD_LEN || payloadLength < 0) {
|
if(payloadLength > MAX_PAYLOAD_LEN || payloadLength < 0) {
|
||||||
throw new DlAbortEx("max payload length exceeded or invalid. length = %d",
|
throw new DlAbortEx("max payload length exceeded or invalid. length = %d",
|
||||||
payloadLength);
|
payloadLength);
|
||||||
|
@ -97,7 +97,7 @@ bool PeerConnection::receiveMessage(unsigned char* data, uint32_t& dataLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// we have currentPayloadLen-resbufLen bytes to read
|
// we have currentPayloadLen-resbufLen bytes to read
|
||||||
uint32_t remaining = currentPayloadLength-resbufLength;
|
int32_t remaining = currentPayloadLength-resbufLength;
|
||||||
if(remaining > 0) {
|
if(remaining > 0) {
|
||||||
socket->readData((char*)resbuf+resbufLength, (int&)remaining);
|
socket->readData((char*)resbuf+resbufLength, (int&)remaining);
|
||||||
if(remaining == 0) {
|
if(remaining == 0) {
|
||||||
|
@ -118,13 +118,13 @@ bool PeerConnection::receiveMessage(unsigned char* data, uint32_t& dataLength) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::receiveHandshake(unsigned char* data, uint32_t& dataLength) {
|
bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength) {
|
||||||
if(!socket->isReadable(0)) {
|
if(!socket->isReadable(0)) {
|
||||||
dataLength = 0;
|
dataLength = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint32_t remain = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
|
int32_t remain = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
|
||||||
uint32_t temp = remain;
|
int32_t temp = remain;
|
||||||
socket->readData((char*)resbuf+resbufLength, (int&)temp);
|
socket->readData((char*)resbuf+resbufLength, (int&)temp);
|
||||||
if(temp == 0) {
|
if(temp == 0) {
|
||||||
// we got EOF
|
// we got EOF
|
||||||
|
@ -138,7 +138,7 @@ bool PeerConnection::receiveHandshake(unsigned char* data, uint32_t& dataLength)
|
||||||
}
|
}
|
||||||
resbufLength += temp;
|
resbufLength += temp;
|
||||||
// we got whole handshake payload
|
// we got whole handshake payload
|
||||||
uint32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
|
int32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
|
||||||
memcpy(data, resbuf, writeLength);
|
memcpy(data, resbuf, writeLength);
|
||||||
dataLength = writeLength;
|
dataLength = writeLength;
|
||||||
if(retval) {
|
if(retval) {
|
||||||
|
|
|
@ -52,19 +52,19 @@ private:
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
|
|
||||||
char resbuf[MAX_PAYLOAD_LEN];
|
char resbuf[MAX_PAYLOAD_LEN];
|
||||||
uint32_t resbufLength;
|
int32_t resbufLength;
|
||||||
uint32_t currentPayloadLength;
|
int32_t currentPayloadLength;
|
||||||
unsigned char lenbuf[4];
|
unsigned char lenbuf[4];
|
||||||
uint32_t lenbufLength;
|
int32_t lenbufLength;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PeerConnection(int32_t cuid, const SocketHandle& socket, const Option* op);
|
PeerConnection(int32_t cuid, const SocketHandle& socket, const Option* op);
|
||||||
~PeerConnection();
|
~PeerConnection();
|
||||||
|
|
||||||
// Returns the number of bytes written
|
// Returns the number of bytes written
|
||||||
uint32_t sendMessage(const unsigned char* data, uint32_t dataLength);
|
int32_t sendMessage(const unsigned char* data, int32_t dataLength);
|
||||||
|
|
||||||
bool receiveMessage(unsigned char* data, uint32_t& dataLength);
|
bool receiveMessage(unsigned char* data, int32_t& dataLength);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if a handshake message is fully received, otherwise returns
|
* Returns true if a handshake message is fully received, otherwise returns
|
||||||
|
@ -72,7 +72,7 @@ public:
|
||||||
* In both cases, 'msg' is filled with received bytes and the filled length
|
* In both cases, 'msg' is filled with received bytes and the filled length
|
||||||
* is assigned to 'length'.
|
* is assigned to 'length'.
|
||||||
*/
|
*/
|
||||||
bool receiveHandshake(unsigned char* data, uint32_t& dataLength);
|
bool receiveHandshake(unsigned char* data, int32_t& dataLength);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
||||||
|
|
|
@ -42,7 +42,7 @@ class PeerInteractionCommand : public PeerAbstractCommand {
|
||||||
private:
|
private:
|
||||||
int sequence;
|
int sequence;
|
||||||
BtInteractiveHandle btInteractive;
|
BtInteractiveHandle btInteractive;
|
||||||
uint32_t maxDownloadSpeedLimit;
|
int32_t maxDownloadSpeedLimit;
|
||||||
protected:
|
protected:
|
||||||
virtual bool executeInternal();
|
virtual bool executeInternal();
|
||||||
virtual bool prepareForRetry(int wait);
|
virtual bool prepareForRetry(int wait);
|
||||||
|
|
|
@ -37,89 +37,89 @@
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
uint8_t PeerMessageUtil::getId(const unsigned char* msg) {
|
int8_t PeerMessageUtil::getId(const unsigned char* msg) {
|
||||||
return msg[0];
|
return msg[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t PeerMessageUtil::getIntParam(const unsigned char* msg, int32_t offset) {
|
int32_t PeerMessageUtil::getIntParam(const unsigned char* msg, int32_t offset) {
|
||||||
uint32_t nParam;
|
int32_t nParam;
|
||||||
memcpy(&nParam, msg+offset, sizeof(uint32_t));
|
memcpy(&nParam, msg+offset, sizeof(int32_t));
|
||||||
return ntohl(nParam);
|
return ntohl(nParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, int32_t offset) {
|
int16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, int32_t offset) {
|
||||||
uint16_t nParam;
|
int16_t nParam;
|
||||||
memcpy(&nParam, msg+offset, sizeof(uint16_t));
|
memcpy(&nParam, msg+offset, sizeof(int16_t));
|
||||||
return ntohs(nParam);
|
return ntohs(nParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::checkIndex(int32_t index, uint32_t pieces) {
|
void PeerMessageUtil::checkIndex(int32_t index, int32_t pieces) {
|
||||||
if(!(0 <= index && index < (int32_t)pieces)) {
|
if(!(0 <= index && index < (int32_t)pieces)) {
|
||||||
throw new DlAbortEx("invalid index = %u", index);
|
throw new DlAbortEx("Invalid index: %d", index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::checkBegin(int32_t begin, uint32_t pieceLength) {
|
void PeerMessageUtil::checkBegin(int32_t begin, int32_t pieceLength) {
|
||||||
if(!(0 <= begin && begin < (int32_t)pieceLength)) {
|
if(!(0 <= begin && begin < (int32_t)pieceLength)) {
|
||||||
throw new DlAbortEx("invalid begin = %u", begin);
|
throw new DlAbortEx("Invalid begin: %d", begin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::checkLength(uint32_t length) {
|
void PeerMessageUtil::checkLength(int32_t length) {
|
||||||
if(length > MAX_BLOCK_LENGTH) {
|
if(length > MAX_BLOCK_LENGTH) {
|
||||||
throw new DlAbortEx("too large length %u > %dKB", length,
|
throw new DlAbortEx("Length too long: %d > %dKB", length,
|
||||||
MAX_BLOCK_LENGTH/1024);
|
MAX_BLOCK_LENGTH/1024);
|
||||||
}
|
}
|
||||||
if(length <= 0) {
|
if(length <= 0) {
|
||||||
throw new DlAbortEx("invalid length %u", length);
|
throw new DlAbortEx("Invalid length: %d", length);
|
||||||
}
|
}
|
||||||
if(!Util::isPowerOf(length, 2)) {
|
if(!Util::isPowerOf(length, 2)) {
|
||||||
throw new DlAbortEx("invalid length %u, which is not power of 2",
|
throw new DlAbortEx("Invalid length: %d It is not power of 2",
|
||||||
length);
|
length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::checkRange(int32_t begin, uint32_t length, uint32_t pieceLength) {
|
void PeerMessageUtil::checkRange(int32_t begin, int32_t length, int32_t pieceLength) {
|
||||||
if(!(0 <= begin && 0 < length)) {
|
if(!(0 <= begin && 0 < length)) {
|
||||||
throw new DlAbortEx("invalid range, begin = %u, length = %u",
|
throw new DlAbortEx("Invalid range: begin=%d, length=%d",
|
||||||
begin, length);
|
begin, length);
|
||||||
}
|
}
|
||||||
uint32_t end = begin+length;
|
int32_t end = begin+length;
|
||||||
if(!(0 < end && end <= pieceLength)) {
|
if(!(0 < end && end <= pieceLength)) {
|
||||||
throw new DlAbortEx("invalid range, begin = %u, length = %u",
|
throw new DlAbortEx("Invalid range: begin=%d, length=%d",
|
||||||
begin, length);
|
begin, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
|
void PeerMessageUtil::checkBitfield(const unsigned char* bitfield,
|
||||||
uint32_t bitfieldLength,
|
int32_t bitfieldLength,
|
||||||
uint32_t pieces) {
|
int32_t pieces) {
|
||||||
if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) {
|
if(!(bitfieldLength == BITFIELD_LEN_FROM_PIECES(pieces))) {
|
||||||
throw new DlAbortEx("invalid bitfield length = %d",
|
throw new DlAbortEx("Invalid bitfield length: %d",
|
||||||
bitfieldLength);
|
bitfieldLength);
|
||||||
}
|
}
|
||||||
char lastbyte = bitfield[bitfieldLength-1];
|
char lastbyte = bitfield[bitfieldLength-1];
|
||||||
for(uint32_t i = 0; i < 8-pieces%8 && pieces%8 != 0; i++) {
|
for(int32_t i = 0; i < 8-pieces%8 && pieces%8 != 0; ++i) {
|
||||||
if(!(((lastbyte >> i) & 1) == 0)) {
|
if(!(((lastbyte >> i) & 1) == 0)) {
|
||||||
throw new DlAbortEx("invalid bitfield");
|
throw new DlAbortEx("Invalid bitfield");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::setIntParam(unsigned char* dest, uint32_t param) {
|
void PeerMessageUtil::setIntParam(unsigned char* dest, int32_t param) {
|
||||||
uint32_t nParam = htonl(param);
|
int32_t nParam = htonl(param);
|
||||||
memcpy(dest, &nParam, sizeof(uint32_t));
|
memcpy(dest, &nParam, sizeof(int32_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::setShortIntParam(unsigned char* dest, uint16_t param) {
|
void PeerMessageUtil::setShortIntParam(unsigned char* dest, int16_t param) {
|
||||||
uint16_t nParam = htons(param);
|
int16_t nParam = htons(param);
|
||||||
memcpy(dest, &nParam, sizeof(uint16_t));
|
memcpy(dest, &nParam, sizeof(int16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
|
void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
|
||||||
uint32_t msgLength,
|
int32_t msgLength,
|
||||||
uint32_t payloadLength,
|
int32_t payloadLength,
|
||||||
uint8_t messageId) {
|
int8_t messageId) {
|
||||||
assert(msgLength >= 5);
|
assert(msgLength >= 5);
|
||||||
memset(msg, 0, msgLength);
|
memset(msg, 0, msgLength);
|
||||||
setIntParam(msg, payloadLength);
|
setIntParam(msg, payloadLength);
|
||||||
|
|
|
@ -43,28 +43,28 @@ class PeerMessageUtil {
|
||||||
private:
|
private:
|
||||||
PeerMessageUtil() {}
|
PeerMessageUtil() {}
|
||||||
public:
|
public:
|
||||||
static uint32_t getIntParam(const unsigned char* msg, int32_t offset);
|
static int32_t getIntParam(const unsigned char* msg, int32_t offset);
|
||||||
|
|
||||||
static uint16_t getShortIntParam(const unsigned char* msg, int32_t offset);
|
static int16_t getShortIntParam(const unsigned char* msg, int32_t offset);
|
||||||
|
|
||||||
static void setIntParam(unsigned char* dest, uint32_t param);
|
static void setIntParam(unsigned char* dest, int32_t param);
|
||||||
|
|
||||||
static void setShortIntParam(unsigned char* dest, uint16_t param);
|
static void setShortIntParam(unsigned char* dest, int16_t param);
|
||||||
|
|
||||||
static uint8_t getId(const unsigned char* msg);
|
static int8_t getId(const unsigned char* msg);
|
||||||
|
|
||||||
static void checkIndex(int32_t index, uint32_t pieces);
|
static void checkIndex(int32_t index, int32_t pieces);
|
||||||
static void checkBegin(int32_t begin, uint32_t pieceLength);
|
static void checkBegin(int32_t begin, int32_t pieceLength);
|
||||||
static void checkLength(uint32_t length);
|
static void checkLength(int32_t length);
|
||||||
static void checkRange(int32_t begin, uint32_t length, uint32_t pieceLength);
|
static void checkRange(int32_t begin, int32_t length, int32_t pieceLength);
|
||||||
static void checkBitfield(const unsigned char* bitfield,
|
static void checkBitfield(const unsigned char* bitfield,
|
||||||
uint32_t bitfieldLength,
|
int32_t bitfieldLength,
|
||||||
uint32_t pieces);
|
int32_t pieces);
|
||||||
|
|
||||||
static void createPeerMessageString(unsigned char* msg,
|
static void createPeerMessageString(unsigned char* msg,
|
||||||
uint32_t msgLength,
|
int32_t msgLength,
|
||||||
uint32_t payloadLength,
|
int32_t payloadLength,
|
||||||
uint8_t messageId);
|
int8_t messageId);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _D_PEER_MESSAGE_UTIL_H_
|
#endif // _D_PEER_MESSAGE_UTIL_H_
|
||||||
|
|
|
@ -40,45 +40,45 @@
|
||||||
|
|
||||||
class TransferStat {
|
class TransferStat {
|
||||||
public:
|
public:
|
||||||
uint32_t downloadSpeed;
|
int32_t downloadSpeed;
|
||||||
uint32_t uploadSpeed;
|
int32_t uploadSpeed;
|
||||||
uint64_t sessionDownloadLength;
|
int64_t sessionDownloadLength;
|
||||||
uint64_t sessionUploadLength;
|
int64_t sessionUploadLength;
|
||||||
public:
|
public:
|
||||||
TransferStat():downloadSpeed(0), uploadSpeed(0),
|
TransferStat():downloadSpeed(0), uploadSpeed(0),
|
||||||
sessionDownloadLength(0), sessionUploadLength(0) {}
|
sessionDownloadLength(0), sessionUploadLength(0) {}
|
||||||
|
|
||||||
uint32_t getDownloadSpeed() const {
|
int32_t getDownloadSpeed() const {
|
||||||
return downloadSpeed;
|
return downloadSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDownloadSpeed(uint32_t s) { downloadSpeed = s; }
|
void setDownloadSpeed(int32_t s) { downloadSpeed = s; }
|
||||||
|
|
||||||
uint32_t getUploadSpeed() const {
|
int32_t getUploadSpeed() const {
|
||||||
return uploadSpeed;
|
return uploadSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUploadSpeed(uint32_t s) { uploadSpeed = s; }
|
void setUploadSpeed(int32_t s) { uploadSpeed = s; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes downloaded since the program started.
|
* Returns the number of bytes downloaded since the program started.
|
||||||
* This is not the total number of bytes downloaded.
|
* This is not the total number of bytes downloaded.
|
||||||
*/
|
*/
|
||||||
uint64_t getSessionDownloadLength() const {
|
int64_t getSessionDownloadLength() const {
|
||||||
return sessionDownloadLength;
|
return sessionDownloadLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSessionDownloadLength(uint64_t s) { sessionDownloadLength = s; }
|
void setSessionDownloadLength(int64_t s) { sessionDownloadLength = s; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes uploaded since the program started.
|
* Returns the number of bytes uploaded since the program started.
|
||||||
* This is not the total number of bytes uploaded.
|
* This is not the total number of bytes uploaded.
|
||||||
*/
|
*/
|
||||||
uint64_t getSessionUploadLength() const {
|
int64_t getSessionUploadLength() const {
|
||||||
return sessionUploadLength;
|
return sessionUploadLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSessionUploadLength(uint64_t s) { sessionUploadLength = s; }
|
void setSessionUploadLength(int64_t s) { sessionUploadLength = s; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class PeerStorage {
|
class PeerStorage {
|
||||||
|
|
|
@ -35,7 +35,7 @@
|
||||||
#include "RequestSlot.h"
|
#include "RequestSlot.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
RequestSlot::RequestSlot(int32_t index, int32_t begin, uint32_t length, int32_t blockIndex)
|
RequestSlot::RequestSlot(int32_t index, int32_t begin, int32_t length, int32_t blockIndex)
|
||||||
:index(index), begin(begin), length(length), blockIndex(blockIndex) {}
|
:index(index), begin(begin), length(length), blockIndex(blockIndex) {}
|
||||||
|
|
||||||
RequestSlot::RequestSlot(const RequestSlot& requestSlot) {
|
RequestSlot::RequestSlot(const RequestSlot& requestSlot) {
|
||||||
|
|
|
@ -43,11 +43,11 @@ private:
|
||||||
Time dispatchedTime;
|
Time dispatchedTime;
|
||||||
int32_t index;
|
int32_t index;
|
||||||
int32_t begin;
|
int32_t begin;
|
||||||
uint32_t length;
|
int32_t length;
|
||||||
int32_t blockIndex;
|
int32_t blockIndex;
|
||||||
void copy(const RequestSlot& requestSlot);
|
void copy(const RequestSlot& requestSlot);
|
||||||
public:
|
public:
|
||||||
RequestSlot(int32_t index, int32_t begin, uint32_t length, int32_t blockIndex);
|
RequestSlot(int32_t index, int32_t begin, int32_t length, int32_t blockIndex);
|
||||||
RequestSlot(const RequestSlot& requestSlot);
|
RequestSlot(const RequestSlot& requestSlot);
|
||||||
~RequestSlot() {}
|
~RequestSlot() {}
|
||||||
|
|
||||||
|
@ -76,8 +76,8 @@ public:
|
||||||
int32_t getBegin() const { return begin; }
|
int32_t getBegin() const { return begin; }
|
||||||
void setBegin(int32_t begin) { this->begin = begin; }
|
void setBegin(int32_t begin) { this->begin = begin; }
|
||||||
|
|
||||||
uint32_t getLength() const { return length; }
|
int32_t getLength() const { return length; }
|
||||||
void setLength(uint32_t length) { this->length = length; }
|
void setLength(int32_t length) { this->length = length; }
|
||||||
|
|
||||||
int32_t getBlockIndex() const { return blockIndex; }
|
int32_t getBlockIndex() const { return blockIndex; }
|
||||||
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||||
|
|
|
@ -432,7 +432,7 @@ void SegmentMan::registerPeerStat(const PeerStatHandle& peerStat) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t SegmentMan::calculateDownloadSpeed() const {
|
int32_t SegmentMan::calculateDownloadSpeed() const {
|
||||||
int speed = 0;
|
int speed = 0;
|
||||||
for(PeerStats::const_iterator itr = peerStats.begin();
|
for(PeerStats::const_iterator itr = peerStats.begin();
|
||||||
itr != peerStats.end(); itr++) {
|
itr != peerStats.end(); itr++) {
|
||||||
|
@ -473,7 +473,7 @@ void SegmentMan::checkIntegrity()
|
||||||
|
|
||||||
bool SegmentMan::isChunkChecksumValidationReady() const {
|
bool SegmentMan::isChunkChecksumValidationReady() const {
|
||||||
return bitfield &&
|
return bitfield &&
|
||||||
pieceHashes.size()*chunkHashLength == bitfield->getBlockLength()*(bitfield->getMaxIndex()+1);
|
((int64_t)pieceHashes.size())*chunkHashLength == ((int64_t)bitfield->getBlockLength())*(bitfield->getMaxIndex()+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SegmentMan::tryChunkChecksumValidation(const Segment& segment)
|
void SegmentMan::tryChunkChecksumValidation(const Segment& segment)
|
||||||
|
@ -499,7 +499,7 @@ void SegmentMan::tryChunkChecksumValidation(const Segment& segment)
|
||||||
logger->debug("No chunk to verify.");
|
logger->debug("No chunk to verify.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int64_t hashOffset = hashStartIndex*chunkHashLength;
|
int64_t hashOffset = ((int64_t)hashStartIndex)*chunkHashLength;
|
||||||
int32_t startIndex;
|
int32_t startIndex;
|
||||||
int32_t endIndex;
|
int32_t endIndex;
|
||||||
Util::indexRange(startIndex, endIndex,
|
Util::indexRange(startIndex, endIndex,
|
||||||
|
@ -509,15 +509,15 @@ void SegmentMan::tryChunkChecksumValidation(const Segment& segment)
|
||||||
logger->debug("startIndex=%d, endIndex=%d", startIndex, endIndex);
|
logger->debug("startIndex=%d, endIndex=%d", startIndex, endIndex);
|
||||||
if(bitfield->isBitRangeSet(startIndex, endIndex)) {
|
if(bitfield->isBitRangeSet(startIndex, endIndex)) {
|
||||||
for(int32_t index = hashStartIndex; index <= hashEndIndex; ++index) {
|
for(int32_t index = hashStartIndex; index <= hashEndIndex; ++index) {
|
||||||
int64_t offset = index*chunkHashLength;
|
int64_t offset = ((int64_t)index)*chunkHashLength;
|
||||||
uint32_t dataLength =
|
int32_t dataLength =
|
||||||
offset+chunkHashLength <= totalSize ? chunkHashLength : totalSize-offset;
|
offset+chunkHashLength <= totalSize ? chunkHashLength : totalSize-offset;
|
||||||
string actualChecksum = diskWriter->messageDigest(offset, dataLength, digestAlgo);
|
string actualChecksum = diskWriter->messageDigest(offset, dataLength, digestAlgo);
|
||||||
string expectedChecksum = pieceHashes.at(index);
|
string expectedChecksum = pieceHashes.at(index);
|
||||||
if(expectedChecksum == actualChecksum) {
|
if(expectedChecksum == actualChecksum) {
|
||||||
logger->info("Chunk checksum validation succeeded.");
|
logger->info("Good chunk checksum.");
|
||||||
} else {
|
} else {
|
||||||
logger->error("Chunk checksum validation failed. checksumIndex=%d, offset=%lld, length=%u, expected=%s, actual=%s",
|
logger->error(EX_INVALID_CHUNK_CHECKSUM,
|
||||||
index, offset, dataLength,
|
index, offset, dataLength,
|
||||||
expectedChecksum.c_str(), actualChecksum.c_str());
|
expectedChecksum.c_str(), actualChecksum.c_str());
|
||||||
logger->info("Unset bit from %d to %d(inclusive)", startIndex, endIndex);
|
logger->info("Unset bit from %d to %d(inclusive)", startIndex, endIndex);
|
||||||
|
|
|
@ -158,7 +158,7 @@ public:
|
||||||
Requests reserved;
|
Requests reserved;
|
||||||
|
|
||||||
Strings pieceHashes;
|
Strings pieceHashes;
|
||||||
uint32_t chunkHashLength;
|
int32_t chunkHashLength;
|
||||||
MessageDigestContext::DigestAlgo digestAlgo;
|
MessageDigestContext::DigestAlgo digestAlgo;
|
||||||
|
|
||||||
SegmentMan();
|
SegmentMan();
|
||||||
|
@ -275,7 +275,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* Returns current download speed in bytes per sec.
|
* Returns current download speed in bytes per sec.
|
||||||
*/
|
*/
|
||||||
uint32_t calculateDownloadSpeed() const;
|
int32_t calculateDownloadSpeed() const;
|
||||||
|
|
||||||
bool fileExists();
|
bool fileExists();
|
||||||
|
|
||||||
|
|
|
@ -41,11 +41,11 @@ class RefCount {
|
||||||
public:
|
public:
|
||||||
RefCount():totalRefCount(0), strongRefCount(0) {}
|
RefCount():totalRefCount(0), strongRefCount(0) {}
|
||||||
|
|
||||||
RefCount(uint32_t totalRefCount, uint32_t strongRefCount)
|
RefCount(int32_t totalRefCount, int32_t strongRefCount)
|
||||||
:totalRefCount(totalRefCount), strongRefCount(strongRefCount) {}
|
:totalRefCount(totalRefCount), strongRefCount(strongRefCount) {}
|
||||||
|
|
||||||
uint32_t totalRefCount;
|
int32_t totalRefCount;
|
||||||
uint32_t strongRefCount;
|
int32_t strongRefCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -49,7 +49,7 @@ public:
|
||||||
|
|
||||||
virtual const unsigned char* getMessage() = 0;
|
virtual const unsigned char* getMessage() = 0;
|
||||||
|
|
||||||
virtual uint32_t getMessageLength() = 0;
|
virtual int32_t getMessageLength() = 0;
|
||||||
|
|
||||||
virtual void onSendComplete() {};
|
virtual void onSendComplete() {};
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ class HeadResult {
|
||||||
public:
|
public:
|
||||||
HeadResult():totalLength(0) {}
|
HeadResult():totalLength(0) {}
|
||||||
string filename;
|
string filename;
|
||||||
uint64_t totalLength;
|
int64_t totalLength;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& o, const HeadResult& hr);
|
std::ostream& operator<<(std::ostream& o, const HeadResult& hr);
|
||||||
|
@ -51,7 +51,7 @@ private:
|
||||||
Strings urls;
|
Strings urls;
|
||||||
int maxConnections;
|
int maxConnections;
|
||||||
MessageDigestContext::DigestAlgo digestAlgo;
|
MessageDigestContext::DigestAlgo digestAlgo;
|
||||||
uint32_t chunkChecksumLength;
|
int32_t chunkChecksumLength;
|
||||||
Strings chunkChecksums;
|
Strings chunkChecksums;
|
||||||
|
|
||||||
RequestInfo* createNextRequestInfo() const;
|
RequestInfo* createNextRequestInfo() const;
|
||||||
|
@ -76,7 +76,7 @@ public:
|
||||||
this->digestAlgo = algo;
|
this->digestAlgo = algo;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setChunkChecksumLength(uint32_t chunkChecksumLength) {
|
void setChunkChecksumLength(int32_t chunkChecksumLength) {
|
||||||
this->chunkChecksumLength = chunkChecksumLength;
|
this->chunkChecksumLength = chunkChecksumLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/Util.cc
12
src/Util.cc
|
@ -571,7 +571,7 @@ static int nbits[] = {
|
||||||
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t Util::countBit(uint32_t n) {
|
int32_t Util::countBit(uint32_t n) {
|
||||||
return
|
return
|
||||||
nbits[n&0xffu]+
|
nbits[n&0xffu]+
|
||||||
nbits[(n >> 8)&0xffu]+
|
nbits[(n >> 8)&0xffu]+
|
||||||
|
@ -638,9 +638,13 @@ void Util::setGlobalSignalHandler(int signal, void (*handler)(int), int flags) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Util::indexRange(int32_t& startIndex, int32_t& endIndex,
|
void Util::indexRange(int32_t& startIndex, int32_t& endIndex,
|
||||||
int64_t offset, uint32_t srcLength, uint32_t destLength)
|
int64_t offset, int32_t srcLength, int32_t destLength)
|
||||||
{
|
{
|
||||||
startIndex = offset/destLength;
|
int64_t _startIndex = offset/destLength;
|
||||||
endIndex = (offset+srcLength-1)/destLength;
|
int64_t _endIndex = (offset+srcLength-1)/destLength;
|
||||||
|
assert(_startIndex <= INT32_MAX);
|
||||||
|
assert(_endIndex <= INT32_MAX);
|
||||||
|
startIndex = _startIndex;
|
||||||
|
endIndex = _endIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue