mirror of https://github.com/aria2/aria2
pull/1/head
parent
708b9f4a3d
commit
a413cb50f7
|
@ -100,7 +100,7 @@ public:
|
|||
this->uploading = uploading;
|
||||
}
|
||||
|
||||
virtual int32_t getId() {
|
||||
virtual uint8_t getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
#include "Util.h"
|
||||
#include <string.h>
|
||||
|
||||
BitfieldMan::BitfieldMan(int blockLength, long long int totalLength)
|
||||
BitfieldMan::BitfieldMan(uint32_t blockLength, uint64_t totalLength)
|
||||
:blockLength(blockLength), totalLength(totalLength), filterBitfield(0),
|
||||
filterEnabled(false), randomizer(0) {
|
||||
if(blockLength > 0 && totalLength > 0) {
|
||||
|
@ -71,25 +71,23 @@ BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan):randomizer(0) {
|
|||
BitfieldMan::~BitfieldMan() {
|
||||
delete [] bitfield;
|
||||
delete [] useBitfield;
|
||||
if(filterBitfield) {
|
||||
delete [] filterBitfield;
|
||||
}
|
||||
delete [] filterBitfield;
|
||||
}
|
||||
|
||||
int BitfieldMan::countSetBit(const unsigned char* bitfield, int len) const {
|
||||
int count = 0;
|
||||
int size = sizeof(unsigned int);
|
||||
for(int i = 0; i < len/size; i++) {
|
||||
count += Util::countBit(*(unsigned int*)&bitfield[i*size]);
|
||||
uint32_t BitfieldMan::countSetBit(const unsigned char* bitfield, uint32_t len) const {
|
||||
uint32_t count = 0;
|
||||
uint32_t size = sizeof(uint32_t);
|
||||
for(uint32_t i = 0; i < len/size; i++) {
|
||||
count += Util::countBit(*(uint32_t*)&bitfield[i*size]);
|
||||
}
|
||||
for(int i = len-len%size; i < len; i++) {
|
||||
count += Util::countBit((unsigned int)bitfield[i]);
|
||||
for(uint32_t i = len-len%size; i < len; i++) {
|
||||
count += Util::countBit((uint32_t)bitfield[i]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int BitfieldMan::getNthBitIndex(const unsigned char bitfield, int nth) const {
|
||||
int index = -1;
|
||||
int32_t BitfieldMan::getNthBitIndex(const unsigned char bitfield, uint32_t nth) const {
|
||||
int32_t index = -1;
|
||||
for(int bs = 7; bs >= 0; bs--) {
|
||||
unsigned char mask = 1 << bs;
|
||||
if(bitfield & mask) {
|
||||
|
@ -103,22 +101,23 @@ int BitfieldMan::getNthBitIndex(const unsigned char bitfield, int nth) const {
|
|||
return index;
|
||||
}
|
||||
|
||||
int
|
||||
int32_t
|
||||
BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
||||
int bitfieldLength) const
|
||||
uint32_t bitfieldLength) const
|
||||
{
|
||||
int byte = (int)(((double)bitfieldLength)*
|
||||
randomizer->getRandomNumber()/
|
||||
(randomizer->getMaxRandomNumber()+1.0));
|
||||
uint32_t byte = (int32_t)(((double)bitfieldLength)*
|
||||
randomizer->getRandomNumber()/
|
||||
(randomizer->getMaxRandomNumber()+1.0));
|
||||
|
||||
unsigned char lastMask = 0;
|
||||
int lastByteLength = totalLength%(blockLength*8);
|
||||
int lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
|
||||
for(int i = 0; i < lastBlockCount; i++) {
|
||||
uint32_t lastByteLength = totalLength%(blockLength*8);
|
||||
uint32_t lastBlockCount = DIV_FLOOR(lastByteLength, blockLength);
|
||||
// TODO test this
|
||||
for(uint32_t i = 0; i < lastBlockCount; i++) {
|
||||
lastMask >>= 1;
|
||||
lastMask |= 0x80;
|
||||
}
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char mask;
|
||||
if(byte == bitfieldLength-1) {
|
||||
mask = lastMask;
|
||||
|
@ -126,7 +125,7 @@ BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
|||
mask = 0xff;
|
||||
}
|
||||
if(bitfield[byte]&mask) {
|
||||
int index = byte*8+getNthBitIndex(bitfield[byte], 1);
|
||||
int32_t index = byte*8+getNthBitIndex(bitfield[byte], 1);
|
||||
return index;
|
||||
}
|
||||
byte++;
|
||||
|
@ -137,12 +136,12 @@ BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
|||
return -1;
|
||||
}
|
||||
|
||||
bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int length) const {
|
||||
bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, uint32_t length) const {
|
||||
if(bitfieldLength != length) {
|
||||
return false;
|
||||
}
|
||||
bool retval = false;
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char temp = peerBitfield[i] & ~bitfield[i];
|
||||
if(filterEnabled) {
|
||||
temp &= filterBitfield[i];
|
||||
|
@ -155,43 +154,43 @@ bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int length)
|
|||
return retval;
|
||||
}
|
||||
|
||||
int BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int length) const {
|
||||
int32_t BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
||||
if(bitfieldLength != length) {
|
||||
return -1;
|
||||
}
|
||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
tempBitfield[i] = peerBitfield[i] & ~bitfield[i];
|
||||
if(filterEnabled) {
|
||||
tempBitfield[i] &= filterBitfield[i];
|
||||
}
|
||||
}
|
||||
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
int32_t index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
delete [] tempBitfield;
|
||||
return index;
|
||||
}
|
||||
|
||||
int BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
|
||||
int32_t BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
||||
if(bitfieldLength != length) {
|
||||
return -1;
|
||||
}
|
||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
||||
if(filterEnabled) {
|
||||
tempBitfield[i] &= filterBitfield[i];
|
||||
}
|
||||
}
|
||||
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
int32_t index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
delete [] tempBitfield;
|
||||
return index;
|
||||
}
|
||||
|
||||
int BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
|
||||
int32_t BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, uint32_t length) const {
|
||||
if(bitfieldLength != length) {
|
||||
return -1;
|
||||
}
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char bit = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
||||
if(filterEnabled) {
|
||||
bit &= filterBitfield[i];
|
||||
|
@ -206,8 +205,8 @@ int BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, i
|
|||
return -1;
|
||||
}
|
||||
|
||||
int BitfieldMan::getFirstMissingUnusedIndex() const {
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
int32_t BitfieldMan::getFirstMissingUnusedIndex() const {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char bit = ~bitfield[i] & ~useBitfield[i];
|
||||
if(filterEnabled) {
|
||||
bit &= filterBitfield[i];
|
||||
|
@ -222,23 +221,23 @@ int BitfieldMan::getFirstMissingUnusedIndex() const {
|
|||
return -1;
|
||||
}
|
||||
|
||||
int BitfieldMan::getMissingIndex() const {
|
||||
int32_t BitfieldMan::getMissingIndex() const {
|
||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
tempBitfield[i] = ~bitfield[i];
|
||||
if(filterEnabled) {
|
||||
tempBitfield[i] &= filterBitfield[i];
|
||||
}
|
||||
}
|
||||
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
int32_t index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
||||
delete [] tempBitfield;
|
||||
return index;
|
||||
}
|
||||
|
||||
int BitfieldMan::getMissingUnusedIndex() const {
|
||||
int32_t BitfieldMan::getMissingUnusedIndex() const {
|
||||
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
||||
memset(tempBitfield, 0xff, bitfieldLength);
|
||||
int index = getMissingUnusedIndex(tempBitfield, bitfieldLength);
|
||||
int32_t index = getMissingUnusedIndex(tempBitfield, bitfieldLength);
|
||||
delete [] tempBitfield;
|
||||
return index;
|
||||
}
|
||||
|
@ -246,16 +245,16 @@ int BitfieldMan::getMissingUnusedIndex() const {
|
|||
// [startIndex, endIndex)
|
||||
class Range {
|
||||
public:
|
||||
int startIndex;
|
||||
int endIndex;
|
||||
Range(int startIndex = 0, int endIndex = 0):startIndex(startIndex),
|
||||
endIndex(endIndex) {}
|
||||
int32_t startIndex;
|
||||
int32_t endIndex;
|
||||
Range(int32_t startIndex = 0, int32_t endIndex = 0):startIndex(startIndex),
|
||||
endIndex(endIndex) {}
|
||||
|
||||
int getSize() const {
|
||||
uint32_t getSize() const {
|
||||
return endIndex-startIndex;
|
||||
}
|
||||
|
||||
int getMidIndex() const {
|
||||
int32_t getMidIndex() const {
|
||||
return (endIndex-startIndex)/2+startIndex;
|
||||
}
|
||||
|
||||
|
@ -264,32 +263,32 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
int BitfieldMan::getStartIndex(int index) const {
|
||||
while(index < blocks &&
|
||||
int32_t BitfieldMan::getStartIndex(int32_t index) const {
|
||||
while(index < (int32_t)blocks &&
|
||||
(isUseBitSet(index) || isBitSet(index))) {
|
||||
index++;
|
||||
}
|
||||
if(blocks <= index) {
|
||||
if((int32_t)blocks <= index) {
|
||||
return -1;
|
||||
} else {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
int BitfieldMan::getEndIndex(int index) const {
|
||||
while(index < blocks &&
|
||||
int32_t BitfieldMan::getEndIndex(int32_t index) const {
|
||||
while(index < (int32_t)blocks &&
|
||||
(!isUseBitSet(index) && !isBitSet(index))) {
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
int BitfieldMan::getSparseMissingUnusedIndex() const {
|
||||
int32_t BitfieldMan::getSparseMissingUnusedIndex() const {
|
||||
Range maxRange;
|
||||
int index = 0;
|
||||
int blocks = countBlock();
|
||||
int32_t index = 0;
|
||||
uint32_t blocks = countBlock();
|
||||
Range currentRange;
|
||||
while(index < blocks) {
|
||||
while(index < (int32_t)blocks) {
|
||||
currentRange.startIndex = getStartIndex(index);
|
||||
if(currentRange.startIndex == -1) {
|
||||
break;
|
||||
|
@ -313,7 +312,7 @@ int BitfieldMan::getSparseMissingUnusedIndex() const {
|
|||
|
||||
BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
||||
BlockIndexes missingIndexes;
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char bit = ~bitfield[i];
|
||||
if(filterEnabled) {
|
||||
bit &= filterBitfield[i];
|
||||
|
@ -328,12 +327,12 @@ BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
|||
return missingIndexes;
|
||||
}
|
||||
|
||||
BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, int peerBitfieldLength) const {
|
||||
BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, uint32_t peerBitfieldLength) const {
|
||||
BlockIndexes missingIndexes;
|
||||
if(bitfieldLength != peerBitfieldLength) {
|
||||
return missingIndexes;
|
||||
}
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
unsigned char bit = peerBitfield[i] & ~bitfield[i];
|
||||
if(filterEnabled) {
|
||||
bit &= filterBitfield[i];
|
||||
|
@ -348,13 +347,13 @@ BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield
|
|||
return missingIndexes;
|
||||
}
|
||||
|
||||
int BitfieldMan::countMissingBlock() const {
|
||||
uint32_t BitfieldMan::countMissingBlock() const {
|
||||
if(filterEnabled) {
|
||||
unsigned char* temp = new unsigned char[bitfieldLength];
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
temp[i] = bitfield[i]&filterBitfield[i];
|
||||
}
|
||||
int count = countSetBit(filterBitfield, bitfieldLength)-
|
||||
uint32_t count = countSetBit(filterBitfield, bitfieldLength)-
|
||||
countSetBit(temp, bitfieldLength);
|
||||
delete [] temp;
|
||||
return count;
|
||||
|
@ -363,7 +362,7 @@ int BitfieldMan::countMissingBlock() const {
|
|||
}
|
||||
}
|
||||
|
||||
int BitfieldMan::countBlock() const {
|
||||
uint32_t BitfieldMan::countBlock() const {
|
||||
if(filterEnabled) {
|
||||
return countSetBit(filterBitfield, bitfieldLength);
|
||||
} else {
|
||||
|
@ -371,8 +370,8 @@ int BitfieldMan::countBlock() const {
|
|||
}
|
||||
}
|
||||
|
||||
bool BitfieldMan::setBitInternal(unsigned char* bitfield, int index, bool on) {
|
||||
if(blocks <= index) { return false; }
|
||||
bool BitfieldMan::setBitInternal(unsigned char* bitfield, int32_t index, bool on) {
|
||||
if((int32_t)blocks <= index) { return false; }
|
||||
unsigned char mask = 128 >> index%8;
|
||||
if(on) {
|
||||
bitfield[index/8] |= mask;
|
||||
|
@ -382,32 +381,32 @@ bool BitfieldMan::setBitInternal(unsigned char* bitfield, int index, bool on) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool BitfieldMan::setUseBit(int index) {
|
||||
bool BitfieldMan::setUseBit(int32_t index) {
|
||||
return setBitInternal(useBitfield, index, true);
|
||||
}
|
||||
|
||||
bool BitfieldMan::unsetUseBit(int index) {
|
||||
bool BitfieldMan::unsetUseBit(int32_t index) {
|
||||
return setBitInternal(useBitfield, index, false);
|
||||
}
|
||||
|
||||
bool BitfieldMan::setBit(int index) {
|
||||
bool BitfieldMan::setBit(int32_t index) {
|
||||
return setBitInternal(bitfield, index, true);
|
||||
}
|
||||
|
||||
bool BitfieldMan::unsetBit(int index) {
|
||||
bool BitfieldMan::unsetBit(int32_t index) {
|
||||
return setBitInternal(bitfield, index, false);
|
||||
}
|
||||
|
||||
bool BitfieldMan::isAllBitSet() const {
|
||||
if(filterEnabled) {
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
for(int i = 0; i < bitfieldLength-1; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength-1; i++) {
|
||||
if(bitfield[i] != 0xff) {
|
||||
return false;
|
||||
}
|
||||
|
@ -420,21 +419,21 @@ bool BitfieldMan::isAllBitSet() const {
|
|||
}
|
||||
}
|
||||
|
||||
bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, int index) const {
|
||||
if(index < 0 || blocks <= index) { return false; }
|
||||
bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, int32_t index) const {
|
||||
if(index < 0 || (int32_t)blocks <= index) { return false; }
|
||||
unsigned char mask = 128 >> index%8;
|
||||
return (bitfield[index/8] & mask) != 0;
|
||||
}
|
||||
|
||||
bool BitfieldMan::isBitSet(int index) const {
|
||||
bool BitfieldMan::isBitSet(int32_t index) const {
|
||||
return isBitSetInternal(bitfield, index);
|
||||
}
|
||||
|
||||
bool BitfieldMan::isUseBitSet(int index) const {
|
||||
bool BitfieldMan::isUseBitSet(int32_t index) const {
|
||||
return isBitSetInternal(useBitfield, index);
|
||||
}
|
||||
|
||||
void BitfieldMan::setBitfield(const unsigned char* bitfield, int bitfieldLength) {
|
||||
void BitfieldMan::setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength) {
|
||||
if(this->bitfieldLength != bitfieldLength) {
|
||||
return;
|
||||
}
|
||||
|
@ -447,7 +446,7 @@ void BitfieldMan::clearAllBit() {
|
|||
}
|
||||
|
||||
void BitfieldMan::setAllBit() {
|
||||
for(int i = 0; i < blocks; i++) {
|
||||
for(uint32_t i = 0; i < blocks; i++) {
|
||||
setBit(i);
|
||||
}
|
||||
}
|
||||
|
@ -457,23 +456,23 @@ void BitfieldMan::clearAllUseBit() {
|
|||
}
|
||||
|
||||
void BitfieldMan::setAllUseBit() {
|
||||
for(int i = 0; i < blocks; i++) {
|
||||
for(uint32_t i = 0; i < blocks; i++) {
|
||||
setUseBit(i);
|
||||
}
|
||||
}
|
||||
|
||||
bool BitfieldMan::setFilterBit(int index) {
|
||||
bool BitfieldMan::setFilterBit(int32_t index) {
|
||||
return setBitInternal(filterBitfield, index, true);
|
||||
}
|
||||
|
||||
void BitfieldMan::addFilter(long long int offset, long long int length) {
|
||||
void BitfieldMan::addFilter(int64_t offset, uint64_t length) {
|
||||
if(!filterBitfield) {
|
||||
filterBitfield = new unsigned char[bitfieldLength];
|
||||
memset(filterBitfield, 0, bitfieldLength);
|
||||
}
|
||||
int startBlock = offset/blockLength;
|
||||
int endBlock = (offset+length-1)/blockLength;
|
||||
for(int i = startBlock; i <= endBlock && i < blocks; i++) {
|
||||
int32_t startBlock = offset/blockLength;
|
||||
int32_t endBlock = (offset+length-1)/blockLength;
|
||||
for(int i = startBlock; i <= endBlock && i < (int32_t)blocks; i++) {
|
||||
setFilterBit(i);
|
||||
}
|
||||
}
|
||||
|
@ -498,25 +497,25 @@ bool BitfieldMan::isFilterEnabled() const {
|
|||
return filterEnabled;
|
||||
}
|
||||
|
||||
long long int BitfieldMan::getFilteredTotalLength() const {
|
||||
uint64_t BitfieldMan::getFilteredTotalLength() const {
|
||||
if(!filterBitfield) {
|
||||
return 0;
|
||||
}
|
||||
int filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
|
||||
uint32_t filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
|
||||
if(filteredBlocks == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(isBitSetInternal(filterBitfield, blocks-1)) {
|
||||
return ((long long int)filteredBlocks-1)*blockLength+getLastBlockLength();
|
||||
return ((uint64_t)filteredBlocks-1)*blockLength+getLastBlockLength();
|
||||
} else {
|
||||
return ((long long int)filteredBlocks)*blockLength;
|
||||
return ((uint64_t)filteredBlocks)*blockLength;
|
||||
}
|
||||
}
|
||||
|
||||
long long int BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||
uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||
unsigned char* temp = new unsigned char[bitfieldLength];
|
||||
if(useFilter) {
|
||||
for(int i = 0; i < bitfieldLength; i++) {
|
||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||
temp[i] = bitfield[i];
|
||||
if(filterEnabled) {
|
||||
temp[i] &= filterBitfield[i];
|
||||
|
@ -525,25 +524,25 @@ long long int BitfieldMan::getCompletedLength(bool useFilter) const {
|
|||
} else {
|
||||
memcpy(temp, bitfield, bitfieldLength);
|
||||
}
|
||||
int completedBlocks = countSetBit(temp, bitfieldLength);
|
||||
long long int completedLength = 0;
|
||||
uint32_t completedBlocks = countSetBit(temp, bitfieldLength);
|
||||
uint64_t completedLength = 0;
|
||||
if(completedBlocks == 0) {
|
||||
completedLength = 0;
|
||||
} else {
|
||||
if(isBitSetInternal(temp, blocks-1)) {
|
||||
completedLength = ((long long int)completedBlocks-1)*blockLength+getLastBlockLength();
|
||||
completedLength = ((uint64_t)completedBlocks-1)*blockLength+getLastBlockLength();
|
||||
} else {
|
||||
completedLength = ((long long int)completedBlocks)*blockLength;
|
||||
completedLength = ((uint64_t)completedBlocks)*blockLength;
|
||||
}
|
||||
}
|
||||
delete [] temp;
|
||||
return completedLength;
|
||||
}
|
||||
|
||||
long long int BitfieldMan::getCompletedLength() const {
|
||||
uint64_t BitfieldMan::getCompletedLength() const {
|
||||
return getCompletedLength(false);
|
||||
}
|
||||
|
||||
long long int BitfieldMan::getFilteredCompletedLength() const {
|
||||
uint64_t BitfieldMan::getFilteredCompletedLength() const {
|
||||
return getCompletedLength(true);
|
||||
}
|
||||
|
|
|
@ -43,29 +43,29 @@ typedef deque<int> BlockIndexes;
|
|||
|
||||
class BitfieldMan {
|
||||
private:
|
||||
int blockLength;
|
||||
long long int totalLength;
|
||||
uint32_t blockLength;
|
||||
uint64_t totalLength;
|
||||
unsigned char* bitfield;
|
||||
unsigned char* useBitfield;
|
||||
unsigned char* filterBitfield;
|
||||
int bitfieldLength;
|
||||
int blocks;
|
||||
uint32_t bitfieldLength;
|
||||
uint32_t blocks;
|
||||
bool filterEnabled;
|
||||
RandomizerHandle randomizer;
|
||||
|
||||
int countSetBit(const unsigned char* bitfield, int len) const;
|
||||
int getNthBitIndex(const unsigned char bit, int nth) const;
|
||||
int getMissingIndexRandomly(const unsigned char* bitfield, int len) const;
|
||||
bool isBitSetInternal(const unsigned char* bitfield, int index) const;
|
||||
bool setBitInternal(unsigned char* bitfield, int index, bool on);
|
||||
bool setFilterBit(int index);
|
||||
uint32_t countSetBit(const unsigned char* bitfield, uint32_t len) const;
|
||||
int32_t getNthBitIndex(const unsigned char bit, uint32_t nth) const;
|
||||
int32_t getMissingIndexRandomly(const unsigned char* bitfield, uint32_t len) const;
|
||||
bool isBitSetInternal(const unsigned char* bitfield, int32_t index) const;
|
||||
bool setBitInternal(unsigned char* bitfield, int32_t index, bool on);
|
||||
bool setFilterBit(int32_t index);
|
||||
|
||||
int getStartIndex(int index) const;
|
||||
int getEndIndex(int index) const;
|
||||
int32_t getStartIndex(int32_t index) const;
|
||||
int32_t getEndIndex(int32_t index) const;
|
||||
|
||||
long long int getCompletedLength(bool useFilter) const;
|
||||
uint64_t getCompletedLength(bool useFilter) const;
|
||||
public:
|
||||
BitfieldMan(int blockLength, long long int totalLength);
|
||||
BitfieldMan(uint32_t blockLength, uint64_t totalLength);
|
||||
BitfieldMan(const BitfieldMan& bitfieldMan);
|
||||
~BitfieldMan();
|
||||
|
||||
|
@ -98,53 +98,56 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
int getBlockLength() const { return blockLength; }
|
||||
int getLastBlockLength() const {
|
||||
uint32_t getBlockLength() const { return blockLength; }
|
||||
|
||||
uint32_t getLastBlockLength() const {
|
||||
return totalLength-blockLength*(blocks-1);
|
||||
}
|
||||
int getBlockLength(int index) const {
|
||||
if(index == blocks-1) {
|
||||
|
||||
uint32_t getBlockLength(int32_t index) const {
|
||||
if(index == (int32_t)(blocks-1)) {
|
||||
return getLastBlockLength();
|
||||
} else if(0 <= index && index < blocks-1) {
|
||||
} else if(0 <= index && index < (int32_t)(blocks-1)) {
|
||||
return getBlockLength();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
long long int getTotalLength() const { return totalLength; }
|
||||
|
||||
uint64_t getTotalLength() const { return totalLength; }
|
||||
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
bool hasMissingPiece(const unsigned char* bitfield, int len) const;
|
||||
bool hasMissingPiece(const unsigned char* bitfield, uint32_t len) const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getMissingIndex(const unsigned char* bitfield, int len) const;
|
||||
int32_t getMissingIndex(const unsigned char* bitfield, uint32_t len) const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getMissingIndex() const;
|
||||
int32_t getMissingIndex() const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getFirstMissingUnusedIndex(const unsigned char* bitfield, int len) const;
|
||||
int32_t getFirstMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getFirstMissingUnusedIndex() const;
|
||||
int32_t getFirstMissingUnusedIndex() const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getMissingUnusedIndex(const unsigned char* bitfield, int len) const;
|
||||
int32_t getMissingUnusedIndex(const unsigned char* bitfield, uint32_t len) const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getMissingUnusedIndex() const;
|
||||
int32_t getMissingUnusedIndex() const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int getSparseMissingUnusedIndex() const;
|
||||
int32_t getSparseMissingUnusedIndex() const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
|
@ -152,19 +155,20 @@ public:
|
|||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, int len) const;
|
||||
BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, uint32_t len) const;
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int countMissingBlock() const;
|
||||
bool setUseBit(int index);
|
||||
bool unsetUseBit(int index);
|
||||
uint32_t countMissingBlock() const;
|
||||
|
||||
bool setBit(int index);
|
||||
bool unsetBit(int index);
|
||||
bool setUseBit(int32_t index);
|
||||
bool unsetUseBit(int32_t index);
|
||||
|
||||
bool isBitSet(int index) const;
|
||||
bool isUseBitSet(int index) const;
|
||||
bool setBit(int32_t index);
|
||||
bool unsetBit(int32_t index);
|
||||
|
||||
bool isBitSet(int32_t index) const;
|
||||
bool isUseBitSet(int32_t index) const;
|
||||
|
||||
/**
|
||||
* affected by filter
|
||||
|
@ -172,16 +176,16 @@ public:
|
|||
bool isAllBitSet() const;
|
||||
|
||||
const unsigned char* getBitfield() const { return bitfield; }
|
||||
int getBitfieldLength() const { return bitfieldLength; }
|
||||
uint32_t getBitfieldLength() const { return bitfieldLength; }
|
||||
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
int countBlock() const;
|
||||
uint32_t countBlock() const;
|
||||
|
||||
int getMaxIndex() const { return blocks-1; }
|
||||
int32_t getMaxIndex() const { return blocks-1; }
|
||||
|
||||
void setBitfield(const unsigned char* bitfield, int bitfieldLength);
|
||||
void setBitfield(const unsigned char* bitfield, uint32_t bitfieldLength);
|
||||
|
||||
void clearAllBit();
|
||||
void setAllBit();
|
||||
|
@ -189,7 +193,7 @@ public:
|
|||
void clearAllUseBit();
|
||||
void setAllUseBit();
|
||||
|
||||
void addFilter(long long int offset, long long int length);
|
||||
void addFilter(int64_t offset, uint64_t length);
|
||||
/**
|
||||
* Clears filter and disables filter
|
||||
*/
|
||||
|
@ -201,12 +205,14 @@ public:
|
|||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
long long int getFilteredTotalLength() const;
|
||||
long long int getCompletedLength() const;
|
||||
uint64_t getFilteredTotalLength() const;
|
||||
|
||||
uint64_t getCompletedLength() const;
|
||||
|
||||
/**
|
||||
* affected by filter
|
||||
*/
|
||||
long long int getFilteredCompletedLength() const;
|
||||
uint64_t getFilteredCompletedLength() const;
|
||||
|
||||
void setRandomizer(const RandomizerHandle& randomizer) {
|
||||
this->randomizer = randomizer;
|
||||
|
|
|
@ -39,11 +39,11 @@
|
|||
|
||||
BtAllowedFastMessageHandle BtAllowedFastMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "allowed fast", dataLength, 5);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "allowed fast", dataLength, 5);
|
||||
}
|
||||
uint32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "allowed fast", ID);
|
||||
}
|
||||
BtAllowedFastMessageHandle message = new BtAllowedFastMessage();
|
||||
|
@ -61,7 +61,7 @@ void BtAllowedFastMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtAllowedFastMessage::MESSAGE_LENGTH = 9;
|
||||
|
||||
const char* BtAllowedFastMessage::getMessage() {
|
||||
const unsigned char* BtAllowedFastMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
|
@ -69,7 +69,7 @@ const char* BtAllowedFastMessage::getMessage() {
|
|||
* piece index --- index, 4bytes
|
||||
* total: 9bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
}
|
||||
|
@ -85,5 +85,5 @@ void BtAllowedFastMessage::onSendComplete() {
|
|||
}
|
||||
|
||||
string BtAllowedFastMessage::toString() const {
|
||||
return "allowed fast index="+Util::uitos(index);
|
||||
return "allowed fast index="+Util::itos(index);
|
||||
}
|
||||
|
|
|
@ -43,12 +43,12 @@ typedef SharedHandle<BtAllowedFastMessage> BtAllowedFastMessageHandle;
|
|||
|
||||
class BtAllowedFastMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
char* msg;
|
||||
int32_t index;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtAllowedFastMessage(uint32_t index = 0)
|
||||
BtAllowedFastMessage(int32_t index = 0)
|
||||
:SimpleBtMessage(),
|
||||
index(index),
|
||||
msg(0) {}
|
||||
|
@ -61,18 +61,18 @@ public:
|
|||
ID = 17
|
||||
};
|
||||
|
||||
void setIndex(uint32_t index) {
|
||||
void setIndex(int32_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
uint32_t getIndex() const { return index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
|
||||
static BtAllowedFastMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ BtBitfieldMessage::create(const unsigned char* data, uint32_t dataLength)
|
|||
if(dataLength <= 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be greater than %d", "bitfield", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "bitfield", ID);
|
||||
|
@ -68,7 +68,7 @@ void BtBitfieldMessage::doReceivedAction() {
|
|||
peer->setBitfield(bitfield, bitfieldLength);
|
||||
}
|
||||
|
||||
const char* BtBitfieldMessage::getMessage() {
|
||||
const unsigned char* BtBitfieldMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1+bitfieldLength, 4bytes
|
||||
|
@ -77,7 +77,7 @@ const char* BtBitfieldMessage::getMessage() {
|
|||
* total: 5+len bytes
|
||||
*/
|
||||
msgLength = 5+bitfieldLength;
|
||||
msg = new char[msgLength];
|
||||
msg = new unsigned char[msgLength];
|
||||
PeerMessageUtil::createPeerMessageString(msg, msgLength,
|
||||
1+bitfieldLength, ID);
|
||||
memcpy(msg+5, bitfield, bitfieldLength);
|
||||
|
|
|
@ -45,8 +45,8 @@ class BtBitfieldMessage : public SimpleBtMessage {
|
|||
private:
|
||||
unsigned char* bitfield;
|
||||
uint32_t bitfieldLength;
|
||||
char* msg;
|
||||
int msgLength;
|
||||
unsigned char* msg;
|
||||
uint32_t msgLength;
|
||||
|
||||
void init() {
|
||||
bitfield = 0;
|
||||
|
@ -84,11 +84,11 @@ public:
|
|||
|
||||
static BtBitfieldMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@
|
|||
|
||||
BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "cancel", dataLength, 13);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "cancel", dataLength, 13);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "cancel", ID);
|
||||
}
|
||||
BtCancelMessageHandle message = new BtCancelMessage();
|
||||
|
@ -59,7 +59,7 @@ void BtCancelMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtCancelMessage::MESSAGE_LENGTH = 17;
|
||||
|
||||
const char* BtCancelMessage::getMessage() {
|
||||
const unsigned char* BtCancelMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
|
@ -69,7 +69,7 @@ const char* BtCancelMessage::getMessage() {
|
|||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
|
@ -83,6 +83,6 @@ uint32_t BtCancelMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
string BtCancelMessage::toString() const {
|
||||
return "cancel index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
return "cancel index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
}
|
||||
|
|
|
@ -43,14 +43,14 @@ typedef SharedHandle<BtCancelMessage> BtCancelMessageHandle;
|
|||
|
||||
class BtCancelMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtCancelMessage(uint32_t index = 0, uint32_t begin = 0, uint32_t length = 0)
|
||||
BtCancelMessage(int32_t index = 0, int32_t begin = 0, uint32_t length = 0)
|
||||
:SimpleBtMessage(),
|
||||
index(index),
|
||||
begin(begin),
|
||||
|
@ -65,13 +65,13 @@ public:
|
|||
ID = 8
|
||||
};
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
|
||||
void setIndex(uint32_t index) { this->index = index; }
|
||||
void setIndex(int32_t index) { this->index = index; }
|
||||
|
||||
uint32_t getBegin() const { return begin; }
|
||||
int32_t getBegin() const { return begin; }
|
||||
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
void setBegin(int32_t begin) { this->begin = begin; }
|
||||
|
||||
uint32_t getLength() const { return length; }
|
||||
|
||||
|
@ -79,11 +79,11 @@ public:
|
|||
|
||||
static BtCancelMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -39,28 +39,28 @@
|
|||
|
||||
class BtCancelSendingPieceEvent : public BtEvent {
|
||||
private:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
public:
|
||||
BtCancelSendingPieceEvent(uint32_t index, uint32_t begin, uint32_t length):
|
||||
BtCancelSendingPieceEvent(int32_t index, int32_t begin, uint32_t length):
|
||||
index(index), begin(begin), length(length) {}
|
||||
|
||||
virtual ~BtCancelSendingPieceEvent() {}
|
||||
|
||||
void setIndex(uint32_t index) {
|
||||
void setIndex(int32_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
|
||||
uint32_t getIndex() const {
|
||||
int32_t getIndex() const {
|
||||
return index;
|
||||
}
|
||||
|
||||
void setBegin(uint32_t begin) {
|
||||
void setBegin(int32_t begin) {
|
||||
this->begin = begin;
|
||||
}
|
||||
|
||||
uint32_t getBegin() const {
|
||||
int32_t getBegin() const {
|
||||
return begin;
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, uint32_t
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "choke", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "choke", ID);
|
||||
|
@ -60,14 +60,14 @@ bool BtChokeMessage::sendPredicate() const {
|
|||
|
||||
uint32_t BtChokeMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtChokeMessage::getMessage() {
|
||||
const unsigned char* BtChokeMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 0, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtChokeMessage> BtChokeMessageHandle;
|
|||
|
||||
class BtChokeMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -57,11 +57,11 @@ public:
|
|||
ID = 0
|
||||
};
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -73,9 +73,9 @@ BtHandshakeMessageHandle BtHandshakeMessage::create(const unsigned char* data, u
|
|||
return message;
|
||||
}
|
||||
|
||||
const char* BtHandshakeMessage::getMessage() {
|
||||
const unsigned char* BtHandshakeMessage::getMessage() {
|
||||
if(!msg) {
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
msg[0] = pstrlen;
|
||||
memcpy(msg+1, pstr, PSTR_LENGTH);
|
||||
memcpy(msg+20, reserved, RESERVED_LENGTH);
|
||||
|
|
|
@ -53,7 +53,7 @@ private:
|
|||
unsigned char* reserved;
|
||||
unsigned char* infoHash;
|
||||
unsigned char* peerId;
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
void init();
|
||||
public:
|
||||
BtHandshakeMessage();
|
||||
|
@ -73,11 +73,11 @@ public:
|
|||
delete [] peerId;
|
||||
}
|
||||
|
||||
virtual int32_t getId() const { return INT32_MAX; }
|
||||
virtual uint8_t getId() const { return UINT8_MAX; }
|
||||
|
||||
virtual void doReceivedAction() {};
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ BtHaveAllMessageHandle BtHaveAllMessage::create(const unsigned char* data, uint3
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "have all", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "have all", ID);
|
||||
|
@ -59,14 +59,14 @@ void BtHaveAllMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtHaveAllMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtHaveAllMessage::getMessage() {
|
||||
const unsigned char* BtHaveAllMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 14, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtHaveAllMessage> BtHaveAllMessageHandle;
|
|||
|
||||
class BtHaveAllMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -59,11 +59,11 @@ public:
|
|||
|
||||
static BtHaveAllMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@
|
|||
|
||||
BtHaveMessageHandle BtHaveMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "have", dataLength, 5);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "have", dataLength, 5);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "have", ID);
|
||||
}
|
||||
BtHaveMessageHandle message = new BtHaveMessage();
|
||||
|
@ -61,7 +61,7 @@ bool BtHaveMessage::sendPredicate() const {
|
|||
|
||||
uint32_t BtHaveMessage::MESSAGE_LENGTH = 9;
|
||||
|
||||
const char* BtHaveMessage::getMessage() {
|
||||
const unsigned char* BtHaveMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
|
@ -69,7 +69,7 @@ const char* BtHaveMessage::getMessage() {
|
|||
* piece index --- index, 4bytes
|
||||
* total: 9bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
}
|
||||
|
@ -81,5 +81,5 @@ uint32_t BtHaveMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
string BtHaveMessage::toString() const {
|
||||
return "have index="+Util::uitos(index);
|
||||
return "have index="+Util::itos(index);
|
||||
}
|
||||
|
|
|
@ -43,11 +43,11 @@ typedef SharedHandle<BtHaveMessage> BtHaveMessageHandle;
|
|||
|
||||
class BtHaveMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
char* msg;
|
||||
int32_t index;
|
||||
unsigned char* msg;
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtHaveMessage(uint32_t index = 0):index(index), msg(0) {}
|
||||
BtHaveMessage(int32_t index = 0):index(index), msg(0) {}
|
||||
|
||||
virtual ~BtHaveMessage() {
|
||||
delete [] msg;
|
||||
|
@ -57,19 +57,19 @@ public:
|
|||
ID = 4
|
||||
};
|
||||
|
||||
void setIndex(uint32_t index) {
|
||||
void setIndex(int32_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
|
||||
static BtHaveMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ BtHaveNoneMessageHandle BtHaveNoneMessage::create(const unsigned char* data, uin
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "have none", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "have none", ID);
|
||||
|
@ -58,14 +58,14 @@ void BtHaveNoneMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtHaveNoneMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtHaveNoneMessage::getMessage() {
|
||||
const unsigned char* BtHaveNoneMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 15, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtHaveNoneMessage> BtHaveNoneMessageHandle;
|
|||
|
||||
class BtHaveNoneMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -59,11 +59,11 @@ public:
|
|||
|
||||
static BtHaveNoneMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data,
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "interested", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "interested", ID);
|
||||
|
@ -59,14 +59,14 @@ bool BtInterestedMessage::sendPredicate() const {
|
|||
|
||||
uint32_t BtInterestedMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtInterestedMessage::getMessage() {
|
||||
const unsigned char* BtInterestedMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 2, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtInterestedMessage> BtInterestedMessageHandle;
|
|||
|
||||
class BtInterestedMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -59,11 +59,11 @@ public:
|
|||
|
||||
static BtInterestedMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -36,13 +36,13 @@
|
|||
|
||||
uint32_t BtKeepAliveMessage::MESSAGE_LENGTH = 4;
|
||||
|
||||
const char* BtKeepAliveMessage::getMessage() {
|
||||
const unsigned char* BtKeepAliveMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 0, 4bytes
|
||||
* total: 4bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
memset(msg, 0, MESSAGE_LENGTH);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtKeepAliveMessage> BtKeepAliveMessageHandle;
|
|||
|
||||
class BtKeepAliveMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -57,11 +57,11 @@ public:
|
|||
ID = 99
|
||||
};
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction() {}
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ public:
|
|||
|
||||
virtual bool isUploading() = 0;
|
||||
|
||||
virtual int32_t getId() = 0;
|
||||
virtual uint8_t getId() = 0;
|
||||
|
||||
virtual void doReceivedAction() = 0;
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ public:
|
|||
|
||||
virtual void sendMessages() = 0;
|
||||
|
||||
virtual void doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t blockLength) = 0;
|
||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length) = 0;
|
||||
|
||||
virtual void doCancelSendingPieceAction(const PieceHandle& piece) = 0;
|
||||
|
||||
|
@ -68,9 +68,9 @@ public:
|
|||
|
||||
virtual uint32_t countOutstandingRequest() = 0;
|
||||
|
||||
virtual bool isOutstandingRequest(uint32_t index, uint32_t blockIndex) = 0;
|
||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex) = 0;
|
||||
|
||||
virtual RequestSlot getOutstandingRequest(uint32_t index, uint32_t begin, uint32_t blockLength) = 0;
|
||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, uint32_t length) = 0;
|
||||
|
||||
virtual void removeOutstandingRequest(const RequestSlot& slot) = 0;
|
||||
|
||||
|
|
|
@ -54,15 +54,15 @@ public:
|
|||
const unsigned char* peerId) = 0;
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRequestMessage(const PieceHandle& piece, uint32_t blockIndex) = 0;
|
||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex) = 0;
|
||||
|
||||
virtual BtMessageHandle
|
||||
createCancelMessage(uint32_t index, uint32_t begin, uint32_t length) = 0;
|
||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
||||
|
||||
virtual BtMessageHandle
|
||||
createPieceMessage(uint32_t index, uint32_t begin, uint32_t length) = 0;
|
||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
||||
|
||||
virtual BtMessageHandle createHaveMessage(uint32_t index) = 0;
|
||||
virtual BtMessageHandle createHaveMessage(int32_t index) = 0;
|
||||
|
||||
virtual BtMessageHandle createChokeMessage() = 0;
|
||||
|
||||
|
@ -81,9 +81,9 @@ public:
|
|||
virtual BtMessageHandle createHaveNoneMessage() = 0;
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRejectMessage(uint32_t index, uint32_t begin, uint32_t length) = 0;
|
||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length) = 0;
|
||||
|
||||
virtual BtMessageHandle createAllowedFastMessage(uint32_t index) = 0;
|
||||
virtual BtMessageHandle createAllowedFastMessage(int32_t index) = 0;
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtMessageFactory> BtMessageFactoryHandle;
|
||||
|
|
|
@ -40,7 +40,7 @@ BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char*
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "not interested", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "not interested", ID);
|
||||
|
@ -59,14 +59,14 @@ bool BtNotInterestedMessage::sendPredicate() const {
|
|||
|
||||
uint32_t BtNotInterestedMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtNotInterestedMessage::getMessage() {
|
||||
const unsigned char* BtNotInterestedMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 3, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtNotInterestedMessage> BtNotInterestedMessageHandle;
|
|||
|
||||
class BtNotInterestedMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
|
@ -59,11 +59,11 @@ public:
|
|||
|
||||
static BtNotInterestedMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -43,17 +43,17 @@
|
|||
void BtPieceMessage::setBlock(const unsigned char* block, uint32_t blockLength) {
|
||||
delete [] this->block;
|
||||
this->blockLength = blockLength;
|
||||
this->block = new char[this->blockLength];
|
||||
this->block = new unsigned char[this->blockLength];
|
||||
memcpy(this->block, block, this->blockLength);
|
||||
}
|
||||
|
||||
BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength <= 9) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be greater than %d", "piece", dataLength, 9);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be greater than %d", "piece", dataLength, 9);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "piece", ID);
|
||||
}
|
||||
BtPieceMessageHandle message = new BtPieceMessage();
|
||||
|
@ -73,9 +73,9 @@ void BtPieceMessage::doReceivedAction() {
|
|||
peer->snubbing = false;
|
||||
peer->updateLatency(slot.getLatencyInMillis());
|
||||
PieceHandle piece = pieceStorage->getPiece(index);
|
||||
uint64_t offset =
|
||||
((uint64_t)index)*btContext->getPieceLength()+begin;
|
||||
logger->debug("CUID#%d - Piece received. index=%u, begin=%u, length=%u, offset=%llu, blockIndex=%u",
|
||||
int64_t offset =
|
||||
((int64_t)index)*btContext->getPieceLength()+begin;
|
||||
logger->debug("CUID#%d - Piece received. index=%d, begin=%d, length=%u, offset=%llu, blockIndex=%u",
|
||||
cuid, index, begin, blockLength, offset, slot.getBlockIndex());
|
||||
pieceStorage->getDiskAdaptor()->writeData(block,
|
||||
blockLength,
|
||||
|
@ -94,7 +94,7 @@ void BtPieceMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtPieceMessage::MESSAGE_HEADER_LENGTH = 13;
|
||||
|
||||
const char* BtPieceMessage::getMessageHeader() {
|
||||
const unsigned char* BtPieceMessage::getMessageHeader() {
|
||||
if(!msgHeader) {
|
||||
/**
|
||||
* len --- 9+blockLength, 4bytes
|
||||
|
@ -103,7 +103,7 @@ const char* BtPieceMessage::getMessageHeader() {
|
|||
* begin --- begin, 4bytes
|
||||
* total: 13bytes
|
||||
*/
|
||||
msgHeader = new char[MESSAGE_HEADER_LENGTH];
|
||||
msgHeader = new unsigned char[MESSAGE_HEADER_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msgHeader, MESSAGE_HEADER_LENGTH,
|
||||
9+blockLength, ID);
|
||||
PeerMessageUtil::setIntParam(&msgHeader[5], index);
|
||||
|
@ -141,8 +141,8 @@ void BtPieceMessage::send() {
|
|||
}
|
||||
if(headerSent) {
|
||||
sendingInProgress = false;
|
||||
uint64_t pieceDataOffset =
|
||||
((uint64_t)index)*btContext->getPieceLength()+begin+blockLength-leftDataLength;
|
||||
int64_t pieceDataOffset =
|
||||
((int64_t)index)*btContext->getPieceLength()+begin+blockLength-leftDataLength;
|
||||
uint32_t writtenLength =
|
||||
sendPieceData(pieceDataOffset, leftDataLength);
|
||||
peer->updateUploadLength(writtenLength);
|
||||
|
@ -153,13 +153,13 @@ void BtPieceMessage::send() {
|
|||
}
|
||||
}
|
||||
|
||||
uint32_t BtPieceMessage::sendPieceData(uint64_t offset, uint32_t length) const {
|
||||
uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
||||
uint32_t BUF_SIZE = 256;
|
||||
char buf[BUF_SIZE];
|
||||
unsigned char buf[BUF_SIZE];
|
||||
int32_t iteration = length/BUF_SIZE;
|
||||
uint32_t writtenLength = 0;
|
||||
for(int32_t i = 0; i < iteration; i++) {
|
||||
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < BUF_SIZE) {
|
||||
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < (int32_t)BUF_SIZE) {
|
||||
throw new DlAbortEx("Failed to read data from disk.");
|
||||
}
|
||||
uint32_t ws = PEER_CONNECTION(btContext, peer)->sendMessage(buf, BUF_SIZE);
|
||||
|
@ -181,13 +181,13 @@ uint32_t BtPieceMessage::sendPieceData(uint64_t offset, uint32_t length) const {
|
|||
}
|
||||
|
||||
string BtPieceMessage::toString() const {
|
||||
return "piece index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
return "piece index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
", length="+Util::uitos(blockLength);
|
||||
}
|
||||
|
||||
bool BtPieceMessage::checkPieceHash(const PieceHandle& piece) {
|
||||
uint64_t offset =
|
||||
((uint64_t)piece->getIndex())*btContext->getPieceLength();
|
||||
int64_t offset =
|
||||
((int64_t)piece->getIndex())*btContext->getPieceLength();
|
||||
return pieceStorage->getDiskAdaptor()->sha1Sum(offset, piece->getLength()) ==
|
||||
btContext->getPieceHash(piece->getIndex());
|
||||
}
|
||||
|
@ -207,10 +207,10 @@ void BtPieceMessage::onWrongPiece(const PieceHandle& piece) {
|
|||
|
||||
void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) {
|
||||
int32_t BUFSIZE = 4096;
|
||||
char buf[BUFSIZE];
|
||||
unsigned char buf[BUFSIZE];
|
||||
memset(buf, 0, BUFSIZE);
|
||||
uint64_t offset =
|
||||
((uint64_t)piece->getIndex())*btContext->getPieceLength();
|
||||
int64_t offset =
|
||||
((int64_t)piece->getIndex())*btContext->getPieceLength();
|
||||
for(int32_t i = 0; i < piece->getLength()/BUFSIZE; i++) {
|
||||
pieceStorage->getDiskAdaptor()->writeData(buf, BUFSIZE, offset);
|
||||
offset += BUFSIZE;
|
||||
|
@ -269,7 +269,7 @@ void BtPieceMessage::handleCancelSendingPieceEvent(const BtEventHandle& event) {
|
|||
begin == intEvent->getBegin() &&
|
||||
blockLength == intEvent->getLength()) {
|
||||
logger->debug("CUID#%d - Reject piece message in queue because cancel"
|
||||
" message received. index=%d, begin=%d, length=%d",
|
||||
" message received. index=%d, begin=%d, length=%u",
|
||||
cuid, index, begin, blockLength);
|
||||
if(peer->isFastExtensionEnabled()) {
|
||||
BtMessageHandle rej =
|
||||
|
|
|
@ -47,13 +47,13 @@ typedef SharedHandle<BtPieceMessage> BtPieceMessageHandle;
|
|||
|
||||
class BtPieceMessage : public AbstractBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t blockLength;
|
||||
char* block;
|
||||
unsigned char* block;
|
||||
uint32_t leftDataLength;
|
||||
bool headerSent;
|
||||
char* msgHeader;
|
||||
unsigned char* msgHeader;
|
||||
|
||||
static uint32_t MESSAGE_HEADER_LENGTH;
|
||||
|
||||
|
@ -65,7 +65,7 @@ private:
|
|||
|
||||
void erasePieceOnDisk(const PieceHandle& piece);
|
||||
|
||||
uint32_t sendPieceData(uint64_t offset, uint32_t length) const;
|
||||
uint32_t sendPieceData(int64_t offset, uint32_t length) const;
|
||||
|
||||
class BtChokingEventListener : public AbstractBtEventListener {
|
||||
private:
|
||||
|
@ -93,7 +93,7 @@ private:
|
|||
|
||||
typedef SharedHandle<BtCancelSendingPieceEventListener> BtCancelSendingPieceEventListenerHandle;
|
||||
public:
|
||||
BtPieceMessage(uint32_t index = 0, uint32_t begin = 0, uint32_t blockLength = 0)
|
||||
BtPieceMessage(int32_t index = 0, int32_t begin = 0, uint32_t blockLength = 0)
|
||||
:index(index),
|
||||
begin(begin),
|
||||
blockLength(blockLength),
|
||||
|
@ -116,15 +116,15 @@ public:
|
|||
ID = 7
|
||||
};
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
|
||||
void setIndex(uint32_t index) { this->index = index; }
|
||||
void setIndex(int32_t index) { this->index = index; }
|
||||
|
||||
uint32_t getBegin() const { return begin; }
|
||||
int32_t getBegin() const { return begin; }
|
||||
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
void setBegin(int32_t begin) { this->begin = begin; }
|
||||
|
||||
const char* getBlock() const { return block; }
|
||||
const unsigned char* getBlock() const { return block; }
|
||||
|
||||
void setBlock(const unsigned char* block, uint32_t blockLength);
|
||||
|
||||
|
@ -134,11 +134,11 @@ public:
|
|||
|
||||
static BtPieceMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
const char* getMessageHeader();
|
||||
const unsigned char* getMessageHeader();
|
||||
|
||||
uint32_t getMessageHeaderLength();
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ BtPortMessageHandle BtPortMessage::create(const unsigned char* data, uint32_t da
|
|||
if(dataLength != 3) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "port", dataLength, 3);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "piece", ID);
|
||||
|
|
|
@ -57,7 +57,7 @@ public:
|
|||
|
||||
void setPort(uint16_t port) { this->port = port; }
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
static BtPortMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@
|
|||
|
||||
BtRejectMessageHandle BtRejectMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "reject", dataLength, 13);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "reject", dataLength, 13);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "reject", ID);
|
||||
}
|
||||
BtRejectMessageHandle message = new BtRejectMessage();
|
||||
|
@ -72,7 +72,7 @@ void BtRejectMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtRejectMessage::MESSAGE_LENGTH = 17;
|
||||
|
||||
const char* BtRejectMessage::getMessage() {
|
||||
const unsigned char* BtRejectMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
|
@ -82,7 +82,7 @@ const char* BtRejectMessage::getMessage() {
|
|||
* length -- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
|
@ -96,6 +96,6 @@ uint32_t BtRejectMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
string BtRejectMessage::toString() const {
|
||||
return "reject index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
return "reject index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
}
|
||||
|
|
|
@ -43,13 +43,13 @@ typedef SharedHandle<BtRejectMessage> BtRejectMessageHandle;
|
|||
|
||||
class BtRejectMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtRejectMessage(uint32_t index = 0, uint32_t begin = 0, uint32_t length = 0)
|
||||
BtRejectMessage(int32_t index = 0, int32_t begin = 0, uint32_t length = 0)
|
||||
:index(index),
|
||||
begin(begin),
|
||||
length(length),
|
||||
|
@ -63,22 +63,22 @@ public:
|
|||
ID = 16
|
||||
};
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
void setIndex(uint32_t index) { this->index = index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
void setIndex(int32_t index) { this->index = index; }
|
||||
|
||||
uint32_t getBegin() const { return begin; }
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
int32_t getBegin() const { return begin; }
|
||||
void setBegin(int32_t begin) { this->begin = begin; }
|
||||
|
||||
uint32_t getLength() const { return length; }
|
||||
void setLength(uint32_t length) { this->length = length; }
|
||||
|
||||
static BtRejectMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -58,13 +58,13 @@ public:
|
|||
* addTargetPiece() and returns them.
|
||||
* The number of objects returned is capped by max.
|
||||
*/
|
||||
virtual BtMessages createRequestMessages(int max) = 0;
|
||||
virtual BtMessages createRequestMessages(uint32_t max) = 0;
|
||||
|
||||
/**
|
||||
* Use this method in end game mode.
|
||||
*
|
||||
*/
|
||||
virtual BtMessages createRequestMessagesOnEndGame(int max) = 0;
|
||||
virtual BtMessages createRequestMessagesOnEndGame(uint32_t max) = 0;
|
||||
};
|
||||
|
||||
typedef SharedHandle<BtRequestFactory> BtRequestFactoryHandle;
|
||||
|
|
|
@ -40,9 +40,9 @@
|
|||
|
||||
BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 13) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "request", dataLength, 13);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "request", dataLength, 13);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "request", ID);
|
||||
|
@ -76,7 +76,7 @@ void BtRequestMessage::doReceivedAction() {
|
|||
|
||||
uint32_t BtRequestMessage::MESSAGE_LENGTH = 17;
|
||||
|
||||
const char* BtRequestMessage::getMessage() {
|
||||
const unsigned char* BtRequestMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 13, 4bytes
|
||||
|
@ -86,7 +86,7 @@ const char* BtRequestMessage::getMessage() {
|
|||
* length --- length, 4bytes
|
||||
* total: 17bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 13, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
PeerMessageUtil::setIntParam(&msg[9], begin);
|
||||
|
@ -100,7 +100,7 @@ uint32_t BtRequestMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
string BtRequestMessage::toString() const {
|
||||
return "request index="+Util::uitos(index)+", begin="+Util::uitos(begin)+
|
||||
return "request index="+Util::itos(index)+", begin="+Util::itos(begin)+
|
||||
", length="+Util::uitos(length);
|
||||
}
|
||||
|
||||
|
|
|
@ -46,11 +46,11 @@ typedef SharedHandle<BtRequestMessage> BtRequestMessageHandle;
|
|||
|
||||
class BtRequestMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
uint32_t blockIndex;
|
||||
char* msg;
|
||||
int32_t blockIndex;
|
||||
unsigned char* msg;
|
||||
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
|
||||
|
@ -67,10 +67,10 @@ private:
|
|||
|
||||
typedef SharedHandle<BtAbortOutstandingRequestEventListener> BtAbortOutstandingRequestEventListenerHandle;
|
||||
public:
|
||||
BtRequestMessage(uint32_t index = 0,
|
||||
uint32_t begin = 0,
|
||||
BtRequestMessage(int32_t index = 0,
|
||||
int32_t begin = 0,
|
||||
uint32_t length = 0,
|
||||
uint32_t blockIndex = 0)
|
||||
int32_t blockIndex = 0)
|
||||
:index(index),
|
||||
begin(begin),
|
||||
length(length),
|
||||
|
@ -88,22 +88,25 @@ public:
|
|||
ID = 6
|
||||
};
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
void setIndex(uint32_t index) { this->index = index; }
|
||||
uint32_t getBegin() const { return begin; }
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
int32_t getIndex() const { return index; }
|
||||
void setIndex(int32_t index) { this->index = index; }
|
||||
|
||||
int32_t getBegin() const { return begin; }
|
||||
void setBegin(int32_t begin) { this->begin = begin; }
|
||||
|
||||
uint32_t getLength() const { return length; }
|
||||
void setLength(uint32_t length) { this->length = length; }
|
||||
uint32_t getBlockIndex() const { return blockIndex; }
|
||||
void setBlockIndex(uint32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||
|
||||
int32_t getBlockIndex() const { return blockIndex; }
|
||||
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||
|
||||
static BtRequestMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@
|
|||
|
||||
BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* data, uint32_t dataLength) {
|
||||
if(dataLength != 5) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "suggest piece", dataLength, 5);
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %u. It should be %d", "suggest piece", dataLength, 5);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
throw new DlAbortEx("invalid ID=%u for %s. It should be %d.",
|
||||
id, "suggest piece", ID);
|
||||
}
|
||||
BtSuggestPieceMessageHandle message = new BtSuggestPieceMessage();
|
||||
|
@ -53,7 +53,7 @@ BtSuggestPieceMessageHandle BtSuggestPieceMessage::create(const unsigned char* d
|
|||
|
||||
uint32_t BtSuggestPieceMessage::MESSAGE_LENGTH = 9;
|
||||
|
||||
const char* BtSuggestPieceMessage::getMessage() {
|
||||
const unsigned char* BtSuggestPieceMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 5, 4bytes
|
||||
|
@ -61,7 +61,7 @@ const char* BtSuggestPieceMessage::getMessage() {
|
|||
* piece index --- index, 4bytes
|
||||
* total: 9bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 5, ID);
|
||||
PeerMessageUtil::setIntParam(&msg[5], index);
|
||||
}
|
||||
|
@ -73,5 +73,5 @@ uint32_t BtSuggestPieceMessage::getMessageLength() {
|
|||
}
|
||||
|
||||
string BtSuggestPieceMessage::toString() const {
|
||||
return "suggest piece index="+Util::uitos(index);
|
||||
return "suggest piece index="+Util::itos(index);
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ typedef SharedHandle<BtSuggestPieceMessage> BtSuggestPieceMessageHandle;
|
|||
|
||||
class BtSuggestPieceMessage : public SimpleBtMessage {
|
||||
private:
|
||||
uint32_t index;
|
||||
char* msg;
|
||||
int32_t index;
|
||||
unsigned char* msg;
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtSuggestPieceMessage():index(0), msg(0) {}
|
||||
|
@ -57,21 +57,21 @@ public:
|
|||
ID = 13
|
||||
};
|
||||
|
||||
void setIndex(uint32_t index) {
|
||||
void setIndex(int32_t index) {
|
||||
this->index = index;
|
||||
}
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
int32_t getIndex() const { return index; }
|
||||
|
||||
static BtSuggestPieceMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction() {
|
||||
// TODO Current implementation ignores this message.
|
||||
}
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, uint3
|
|||
if(dataLength != 1) {
|
||||
throw new DlAbortEx("invalid payload size for %s, size = %d. It should be %d", "unchoke", dataLength, 1);
|
||||
}
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
if(id != ID) {
|
||||
throw new DlAbortEx("invalid ID=%d for %s. It should be %d.",
|
||||
id, "unchoke", ID);
|
||||
|
@ -59,14 +59,14 @@ bool BtUnchokeMessage::sendPredicate() const {
|
|||
|
||||
uint32_t BtUnchokeMessage::MESSAGE_LENGTH = 5;
|
||||
|
||||
const char* BtUnchokeMessage::getMessage() {
|
||||
const unsigned char* BtUnchokeMessage::getMessage() {
|
||||
if(!msg) {
|
||||
/**
|
||||
* len --- 1, 4bytes
|
||||
* id --- 1, 1byte
|
||||
* total: 5bytes
|
||||
*/
|
||||
msg = new char[MESSAGE_LENGTH];
|
||||
msg = new unsigned char[MESSAGE_LENGTH];
|
||||
PeerMessageUtil::createPeerMessageString(msg, MESSAGE_LENGTH, 1, ID);
|
||||
}
|
||||
return msg;
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef SharedHandle<BtUnchokeMessage> BtUnchokeMessageHandle;
|
|||
|
||||
class BtUnchokeMessage : public SimpleBtMessage {
|
||||
private:
|
||||
char* msg;
|
||||
unsigned char* msg;
|
||||
static uint32_t MESSAGE_LENGTH;
|
||||
public:
|
||||
BtUnchokeMessage():msg(0) {}
|
||||
|
@ -58,11 +58,11 @@ public:
|
|||
|
||||
static BtUnchokeMessageHandle create(const unsigned char* data, uint32_t dataLength);
|
||||
|
||||
virtual int32_t getId() const { return ID; }
|
||||
virtual uint8_t getId() const { return ID; }
|
||||
|
||||
virtual void doReceivedAction();
|
||||
|
||||
virtual const char* getMessage();
|
||||
virtual const unsigned char* getMessage();
|
||||
|
||||
virtual uint32_t getMessageLength();
|
||||
|
||||
|
|
|
@ -80,10 +80,10 @@ void DefaultBtMessageDispatcher::sendMessages() {
|
|||
}
|
||||
|
||||
// Cancel sending piece message to peer.
|
||||
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t blockLength)
|
||||
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length)
|
||||
{
|
||||
BtCancelSendingPieceEventHandle event =
|
||||
new BtCancelSendingPieceEvent(index, begin, blockLength);
|
||||
new BtCancelSendingPieceEvent(index, begin, length);
|
||||
|
||||
BtMessages tempQueue = messageQueue;
|
||||
for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); itr++) {
|
||||
|
@ -202,7 +202,7 @@ uint32_t DefaultBtMessageDispatcher::countOutstandingRequest()
|
|||
return requestSlots.size();
|
||||
}
|
||||
|
||||
bool DefaultBtMessageDispatcher::isOutstandingRequest(uint32_t index, uint32_t blockIndex) {
|
||||
bool DefaultBtMessageDispatcher::isOutstandingRequest(int32_t index, int32_t blockIndex) {
|
||||
for(RequestSlots::const_iterator itr = requestSlots.begin();
|
||||
itr != requestSlots.end(); itr++) {
|
||||
const RequestSlot& slot = *itr;
|
||||
|
|
|
@ -82,7 +82,7 @@ public:
|
|||
|
||||
virtual void sendMessages();
|
||||
|
||||
virtual void doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t blockLength);
|
||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length);
|
||||
|
||||
virtual void doCancelSendingPieceAction(const PieceHandle& piece);
|
||||
|
||||
|
@ -102,14 +102,14 @@ public:
|
|||
|
||||
virtual uint32_t countOutstandingRequest();
|
||||
|
||||
virtual bool isOutstandingRequest(uint32_t index, uint32_t blockIndex);
|
||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex);
|
||||
|
||||
virtual RequestSlot getOutstandingRequest(uint32_t index, uint32_t begin, uint32_t blockLength) {
|
||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, uint32_t length) {
|
||||
for(RequestSlots::iterator itr = requestSlots.begin();
|
||||
itr != requestSlots.end(); itr++) {
|
||||
if(itr->getIndex() == index &&
|
||||
itr->getBegin() == begin &&
|
||||
itr->getLength() == blockLength) {
|
||||
itr->getLength() == length) {
|
||||
return *itr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, uint32_t dat
|
|||
// keep-alive
|
||||
msg = new BtKeepAliveMessage();
|
||||
} else {
|
||||
int32_t id = PeerMessageUtil::getId(data);
|
||||
uint8_t id = PeerMessageUtil::getId(data);
|
||||
switch(id) {
|
||||
case BtChokeMessage::ID:
|
||||
msg = BtChokeMessage::create(data, dataLength);
|
||||
|
@ -162,7 +162,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, uint32_t dat
|
|||
break;
|
||||
}
|
||||
default:
|
||||
throw new DlAbortEx("Invalid message id. id = %d", id);
|
||||
throw new DlAbortEx("Invalid message id. id = %u", id);
|
||||
}
|
||||
}
|
||||
setCommonProperty(msg);
|
||||
|
@ -201,7 +201,7 @@ DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* infoHash,
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, uint32_t blockIndex)
|
||||
DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, int32_t blockIndex)
|
||||
{
|
||||
BtRequestMessageHandle msg =
|
||||
new BtRequestMessage(piece->getIndex(),
|
||||
|
@ -218,7 +218,7 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, uint32_t
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createCancelMessage(uint32_t index, uint32_t begin, uint32_t length)
|
||||
DefaultBtMessageFactory::createCancelMessage(int32_t index, int32_t begin, uint32_t length)
|
||||
{
|
||||
BtCancelMessageHandle msg = new BtCancelMessage(index, begin, length);
|
||||
BtMessageValidatorHandle validator =
|
||||
|
@ -231,7 +231,7 @@ DefaultBtMessageFactory::createCancelMessage(uint32_t index, uint32_t begin, uin
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createPieceMessage(uint32_t index, uint32_t begin, uint32_t length)
|
||||
DefaultBtMessageFactory::createPieceMessage(int32_t index, int32_t begin, uint32_t length)
|
||||
{
|
||||
BtPieceMessageHandle msg = new BtPieceMessage(index, begin, length);
|
||||
BtMessageValidatorHandle validator =
|
||||
|
@ -244,7 +244,7 @@ DefaultBtMessageFactory::createPieceMessage(uint32_t index, uint32_t begin, uint
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createHaveMessage(uint32_t index)
|
||||
DefaultBtMessageFactory::createHaveMessage(int32_t index)
|
||||
{
|
||||
BtHaveMessageHandle msg = new BtHaveMessage(index);
|
||||
msg->setBtMessageValidator(new BtHaveMessageValidator(msg.get(),
|
||||
|
@ -322,7 +322,7 @@ DefaultBtMessageFactory::createHaveNoneMessage()
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createRejectMessage(uint32_t index, uint32_t begin, uint32_t length)
|
||||
DefaultBtMessageFactory::createRejectMessage(int32_t index, int32_t begin, uint32_t length)
|
||||
{
|
||||
BtRejectMessageHandle msg = new BtRejectMessage(index, begin, length);
|
||||
BtMessageValidatorHandle validator =
|
||||
|
@ -335,7 +335,7 @@ DefaultBtMessageFactory::createRejectMessage(uint32_t index, uint32_t begin, uin
|
|||
}
|
||||
|
||||
BtMessageHandle
|
||||
DefaultBtMessageFactory::createAllowedFastMessage(uint32_t index)
|
||||
DefaultBtMessageFactory::createAllowedFastMessage(int32_t index)
|
||||
{
|
||||
BtAllowedFastMessageHandle msg = new BtAllowedFastMessage(index);
|
||||
BtMessageValidatorHandle validator =
|
||||
|
|
|
@ -73,15 +73,15 @@ public:
|
|||
const unsigned char* peerId);
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRequestMessage(const PieceHandle& piece, uint32_t blockIndex);
|
||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex);
|
||||
|
||||
virtual BtMessageHandle
|
||||
createCancelMessage(uint32_t index, uint32_t begin, uint32_t length);
|
||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length);
|
||||
|
||||
virtual BtMessageHandle
|
||||
createPieceMessage(uint32_t index, uint32_t begin, uint32_t length);
|
||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length);
|
||||
|
||||
virtual BtMessageHandle createHaveMessage(uint32_t index);
|
||||
virtual BtMessageHandle createHaveMessage(int32_t index);
|
||||
|
||||
virtual BtMessageHandle createChokeMessage();
|
||||
|
||||
|
@ -100,9 +100,9 @@ public:
|
|||
virtual BtMessageHandle createHaveNoneMessage();
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRejectMessage(uint32_t index, uint32_t begin, uint32_t length);
|
||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length);
|
||||
|
||||
virtual BtMessageHandle createAllowedFastMessage(uint32_t index);
|
||||
virtual BtMessageHandle createAllowedFastMessage(int32_t index);
|
||||
|
||||
void setPeer(const PeerHandle& peer) {
|
||||
this->peer = peer;
|
||||
|
|
|
@ -62,12 +62,12 @@ void DefaultBtRequestFactory::removeAllTargetPiece() {
|
|||
pieces.clear();
|
||||
}
|
||||
|
||||
BtMessages DefaultBtRequestFactory::createRequestMessages(int max) {
|
||||
BtMessages DefaultBtRequestFactory::createRequestMessages(uint32_t max) {
|
||||
BtMessages requests;
|
||||
for(Pieces::iterator itr = pieces.begin();
|
||||
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
||||
PieceHandle& piece = *itr;
|
||||
int blockIndex;
|
||||
int32_t blockIndex;
|
||||
while(requests.size() < (size_t)max &&
|
||||
(blockIndex = piece->getMissingUnusedBlockIndex()) != -1) {
|
||||
requests.push_back(BT_MESSAGE_FACTORY(btContext, peer)->
|
||||
|
@ -77,7 +77,7 @@ BtMessages DefaultBtRequestFactory::createRequestMessages(int max) {
|
|||
return requests;
|
||||
}
|
||||
|
||||
BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(int max) {
|
||||
BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(uint32_t max) {
|
||||
BtMessages requests;
|
||||
for(Pieces::iterator itr = pieces.begin();
|
||||
itr != pieces.end() && requests.size() < (size_t)max; itr++) {
|
||||
|
@ -87,7 +87,7 @@ BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(int max) {
|
|||
for(BlockIndexes::const_iterator bitr = missingBlockIndexes.begin();
|
||||
bitr != missingBlockIndexes.end() && requests.size() < (size_t)max;
|
||||
bitr++) {
|
||||
int blockIndex = *bitr;
|
||||
int32_t blockIndex = *bitr;
|
||||
if(!dispatcher->isOutstandingRequest(piece->getIndex(),
|
||||
blockIndex)) {
|
||||
requests.push_back(BT_MESSAGE_FACTORY(btContext, peer)->
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
|
||||
class DefaultBtRequestFactory : public BtRequestFactory {
|
||||
private:
|
||||
int cuid;
|
||||
int32_t cuid;
|
||||
BtContextHandle btContext;
|
||||
PieceStorageHandle pieceStorage;
|
||||
PeerHandle peer;
|
||||
|
@ -82,9 +82,9 @@ public:
|
|||
|
||||
virtual void removeCompletedPiece();
|
||||
|
||||
virtual BtMessages createRequestMessages(int max);
|
||||
virtual BtMessages createRequestMessages(uint32_t max);
|
||||
|
||||
virtual BtMessages createRequestMessagesOnEndGame(int max);
|
||||
virtual BtMessages createRequestMessagesOnEndGame(uint32_t max);
|
||||
|
||||
Pieces& getTargetPieces() {
|
||||
return pieces;
|
||||
|
|
|
@ -70,7 +70,7 @@ private:
|
|||
BitfieldMan* bitfieldMan;
|
||||
DiskAdaptor* diskAdaptor;
|
||||
Pieces usedPieces;
|
||||
int endGamePieceNum;
|
||||
uint32_t endGamePieceNum;
|
||||
Logger* logger;
|
||||
const Option* option;
|
||||
Haves haves;
|
||||
|
@ -125,11 +125,11 @@ public:
|
|||
|
||||
virtual const unsigned char* getBitfield();
|
||||
|
||||
void setEndGamePieceNum(int num) {
|
||||
void setEndGamePieceNum(uint32_t num) {
|
||||
endGamePieceNum = num;
|
||||
}
|
||||
|
||||
int getEndGamePieceNum() const {
|
||||
uint32_t getEndGamePieceNum() const {
|
||||
return endGamePieceNum;
|
||||
}
|
||||
|
||||
|
|
|
@ -60,15 +60,15 @@ void DiskAdaptor::initAndOpenFile() {
|
|||
diskWriter->initAndOpenFile(getFilePath());
|
||||
}
|
||||
|
||||
void DiskAdaptor::writeData(const char* data, int len, long long int position) {
|
||||
diskWriter->writeData(data, len, position);
|
||||
void DiskAdaptor::writeData(const unsigned char* data, uint32_t len, int64_t offset) {
|
||||
diskWriter->writeData(data, len, offset);
|
||||
}
|
||||
|
||||
int DiskAdaptor::readData(char* data, int len, long long int position) {
|
||||
return diskWriter->readData(data, len, position);
|
||||
int DiskAdaptor::readData(unsigned char* data, uint32_t len, int64_t offset) {
|
||||
return diskWriter->readData(data, len, offset);
|
||||
}
|
||||
|
||||
string DiskAdaptor::sha1Sum(long long int offset, long long int length) {
|
||||
string DiskAdaptor::sha1Sum(int64_t offset, uint64_t length) {
|
||||
return diskWriter->sha1Sum(offset, length);
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ FileEntryHandle DiskAdaptor::getFileEntryFromPath(const string& fileEntryPath) c
|
|||
return *itr;
|
||||
}
|
||||
}
|
||||
throw new DlAbortEx("no such file entry <%s>", fileEntryPath.c_str());
|
||||
throw new DlAbortEx("No such file entry <%s>", fileEntryPath.c_str());
|
||||
}
|
||||
|
||||
bool DiskAdaptor::addDownloadEntry(const string& fileEntryPath) {
|
||||
|
|
|
@ -57,9 +57,16 @@ public:
|
|||
virtual void closeFile();
|
||||
virtual void openExistingFile();
|
||||
virtual void initAndOpenFile();
|
||||
void writeData(const char* data, int len, long long int position);
|
||||
int readData(char* data, int len, long long int position);
|
||||
string sha1Sum(long long int offset, long long int length);
|
||||
void writeData(const unsigned char* data, uint32_t len, int64_t offset);
|
||||
void writeData(const char* data, uint32_t len, int64_t offset) {
|
||||
writeData((const unsigned char*)data, len, offset);
|
||||
}
|
||||
int readData(unsigned char* data, uint32_t len, int64_t offset);
|
||||
int readData(char* data, uint32_t len, int64_t offset) {
|
||||
return readData((unsigned char*)data, len, offset);
|
||||
}
|
||||
|
||||
string sha1Sum(int64_t offset, uint64_t length);
|
||||
|
||||
virtual void onDownloadComplete() = 0;
|
||||
|
||||
|
|
|
@ -79,8 +79,15 @@ public:
|
|||
* @param position the offset of this binary stream
|
||||
*/
|
||||
virtual void writeData(const char* data, int len, long long int position = 0) = 0;
|
||||
virtual void writeData(const unsigned char* data, int len, long long int position = 0)
|
||||
{
|
||||
writeData((const char*)data, len, position);
|
||||
}
|
||||
|
||||
virtual int readData(char* data, int len, long long int position) = 0;
|
||||
virtual int readData(unsigned char* data, int len, long long int position) {
|
||||
return readData((char*)data, len, position);
|
||||
}
|
||||
|
||||
virtual string sha1Sum(long long int offset, long long int length) = 0;
|
||||
|
||||
|
|
|
@ -63,14 +63,8 @@ public:
|
|||
|
||||
// Returns the number of bytes written
|
||||
uint32_t sendMessage(const unsigned char* data, uint32_t dataLength);
|
||||
uint32_t sendMessage(const char* msg, int length) {
|
||||
return sendMessage((const unsigned char*)msg, (uint32_t)length);
|
||||
}
|
||||
|
||||
bool receiveMessage(unsigned char* data, uint32_t& dataLength);
|
||||
bool receiveMessage(char* msg, int& length) {
|
||||
return receiveMessage((unsigned char*)msg, (uint32_t&)length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if a handshake message is fully received, otherwise returns
|
||||
|
@ -79,9 +73,6 @@ public:
|
|||
* is assigned to 'length'.
|
||||
*/
|
||||
bool receiveHandshake(unsigned char* data, uint32_t& dataLength);
|
||||
bool receiveHandshake(char* msg, int& length) {
|
||||
return receiveHandshake((unsigned char*)msg, (uint32_t&)length);
|
||||
}
|
||||
};
|
||||
|
||||
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
||||
|
|
|
@ -37,8 +37,8 @@
|
|||
#include "Util.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
int32_t PeerMessageUtil::getId(const unsigned char* msg) {
|
||||
return (int32_t)msg[0];
|
||||
uint8_t PeerMessageUtil::getId(const unsigned char* msg) {
|
||||
return msg[0];
|
||||
}
|
||||
|
||||
uint32_t PeerMessageUtil::getIntParam(const unsigned char* msg, int32_t offset) {
|
||||
|
@ -53,14 +53,14 @@ uint16_t PeerMessageUtil::getShortIntParam(const unsigned char* msg, int32_t off
|
|||
return ntohs(nParam);
|
||||
}
|
||||
|
||||
void PeerMessageUtil::checkIndex(uint32_t index, uint32_t pieces) {
|
||||
if(!(0 <= index && index < pieces)) {
|
||||
void PeerMessageUtil::checkIndex(int32_t index, uint32_t pieces) {
|
||||
if(!(0 <= index && index < (int32_t)pieces)) {
|
||||
throw new DlAbortEx("invalid index = %u", index);
|
||||
}
|
||||
}
|
||||
|
||||
void PeerMessageUtil::checkBegin(uint32_t begin, uint32_t pieceLength) {
|
||||
if(!(0 <= begin && begin < pieceLength)) {
|
||||
void PeerMessageUtil::checkBegin(int32_t begin, uint32_t pieceLength) {
|
||||
if(!(0 <= begin && begin < (int32_t)pieceLength)) {
|
||||
throw new DlAbortEx("invalid begin = %u", begin);
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ void PeerMessageUtil::checkLength(uint32_t length) {
|
|||
}
|
||||
}
|
||||
|
||||
void PeerMessageUtil::checkRange(uint32_t begin, uint32_t length, uint32_t pieceLength) {
|
||||
void PeerMessageUtil::checkRange(int32_t begin, uint32_t length, uint32_t pieceLength) {
|
||||
if(!(0 <= begin && 0 < length)) {
|
||||
throw new DlAbortEx("invalid range, begin = %u, length = %u",
|
||||
begin, length);
|
||||
|
@ -119,9 +119,9 @@ void PeerMessageUtil::setShortIntParam(unsigned char* dest, uint16_t param) {
|
|||
void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
|
||||
uint32_t msgLength,
|
||||
uint32_t payloadLength,
|
||||
int32_t messageId) {
|
||||
uint8_t messageId) {
|
||||
assert(msgLength >= 5);
|
||||
memset(msg, 0, msgLength);
|
||||
setIntParam(msg, payloadLength);
|
||||
msg[4] = (char)messageId;
|
||||
msg[4] = messageId;
|
||||
}
|
||||
|
|
|
@ -44,53 +44,27 @@ private:
|
|||
PeerMessageUtil() {}
|
||||
public:
|
||||
static uint32_t getIntParam(const unsigned char* msg, int32_t offset);
|
||||
static uint32_t getIntParam(const char* msg, int32_t offset) {
|
||||
return getIntParam((const unsigned char*)msg, offset);
|
||||
}
|
||||
|
||||
static uint16_t getShortIntParam(const unsigned char* msg, int32_t offset);
|
||||
static uint16_t getShortIntParam(const char* msg, int32_t offset) {
|
||||
return getShortIntParam((const unsigned char*)msg, offset);
|
||||
}
|
||||
|
||||
static void setIntParam(unsigned char* dest, uint32_t param);
|
||||
static void setIntParam(char* dest, uint32_t param) {
|
||||
setIntParam((unsigned char*)dest, param);
|
||||
}
|
||||
|
||||
static void setShortIntParam(unsigned char* dest, uint16_t param);
|
||||
static void setShortIntParam(char* dest, uint16_t param) {
|
||||
setShortIntParam((unsigned char*)dest, param);
|
||||
}
|
||||
|
||||
static int32_t getId(const unsigned char* msg);
|
||||
static int32_t getId(const char* msg) {
|
||||
return getId((const unsigned char*)msg);
|
||||
}
|
||||
static uint8_t getId(const unsigned char* msg);
|
||||
|
||||
static void checkIndex(uint32_t index, uint32_t pieces);
|
||||
static void checkBegin(uint32_t begin, uint32_t pieceLength);
|
||||
static void checkIndex(int32_t index, uint32_t pieces);
|
||||
static void checkBegin(int32_t begin, uint32_t pieceLength);
|
||||
static void checkLength(uint32_t length);
|
||||
static void checkRange(uint32_t begin, uint32_t length, uint32_t pieceLength);
|
||||
static void checkRange(int32_t begin, uint32_t length, uint32_t pieceLength);
|
||||
static void checkBitfield(const unsigned char* bitfield,
|
||||
uint32_t bitfieldLength,
|
||||
uint32_t pieces);
|
||||
static void checkBitfield(const char* bitfield,
|
||||
uint32_t bitfieldLength,
|
||||
uint32_t pieces) {
|
||||
checkBitfield((unsigned char*)bitfield, bitfieldLength, pieces);
|
||||
}
|
||||
|
||||
static void createPeerMessageString(unsigned char* msg,
|
||||
uint32_t msgLength,
|
||||
uint32_t payloadLength,
|
||||
int32_t messageId);
|
||||
static void createPeerMessageString(char* msg,
|
||||
uint32_t msgLength,
|
||||
uint32_t payloadLength,
|
||||
int32_t messageId) {
|
||||
createPeerMessageString((unsigned char*)msg, msgLength, payloadLength, messageId);
|
||||
}
|
||||
uint8_t messageId);
|
||||
};
|
||||
|
||||
#endif // _D_PEER_MESSAGE_UTIL_H_
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include "RequestSlot.h"
|
||||
#include "Util.h"
|
||||
|
||||
RequestSlot::RequestSlot(uint32_t index, uint32_t begin, uint32_t length, uint32_t blockIndex)
|
||||
RequestSlot::RequestSlot(int32_t index, int32_t begin, uint32_t length, int32_t blockIndex)
|
||||
:index(index), begin(begin), length(length), blockIndex(blockIndex) {}
|
||||
|
||||
RequestSlot::RequestSlot(const RequestSlot& requestSlot) {
|
||||
|
|
|
@ -41,13 +41,13 @@
|
|||
class RequestSlot {
|
||||
private:
|
||||
Time dispatchedTime;
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
uint32_t blockIndex;
|
||||
int32_t blockIndex;
|
||||
void copy(const RequestSlot& requestSlot);
|
||||
public:
|
||||
RequestSlot(uint32_t index, uint32_t begin, uint32_t length, uint32_t blockIndex);
|
||||
RequestSlot(int32_t index, int32_t begin, uint32_t length, int32_t blockIndex);
|
||||
RequestSlot(const RequestSlot& requestSlot);
|
||||
~RequestSlot() {}
|
||||
|
||||
|
@ -70,14 +70,17 @@ public:
|
|||
bool isTimeout(time_t timeoutSec) const;
|
||||
int getLatencyInMillis() const;
|
||||
|
||||
uint32_t getIndex() const { return index; }
|
||||
void setIndex(uint32_t index) { this->index = index; }
|
||||
uint32_t getBegin() const { return begin; }
|
||||
void setBegin(uint32_t begin) { this->begin = begin; }
|
||||
int32_t getIndex() const { return index; }
|
||||
void setIndex(int32_t index) { this->index = index; }
|
||||
|
||||
int32_t getBegin() const { return begin; }
|
||||
void setBegin(int32_t begin) { this->begin = begin; }
|
||||
|
||||
uint32_t getLength() const { return length; }
|
||||
void setLength(uint32_t length) { this->length = length; }
|
||||
uint32_t getBlockIndex() const { return blockIndex; }
|
||||
void setBlockIndex(uint32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||
|
||||
int32_t getBlockIndex() const { return blockIndex; }
|
||||
void setBlockIndex(int32_t blockIndex) { this->blockIndex = blockIndex; }
|
||||
|
||||
static RequestSlot nullSlot;
|
||||
|
||||
|
|
|
@ -363,7 +363,7 @@ bool SegmentMan::getSegment(Segment& segment, int cuid, int index) {
|
|||
if(!bitfield) {
|
||||
return onNullBitfield(segment, cuid);
|
||||
}
|
||||
if(index < 0 || bitfield->countBlock() <= index) {
|
||||
if(index < 0 || (int32_t)bitfield->countBlock() <= index) {
|
||||
return false;
|
||||
}
|
||||
if(bitfield->isBitSet(index) || bitfield->isUseBitSet(index)) {
|
||||
|
|
|
@ -45,7 +45,7 @@ void SimpleBtMessage::send() {
|
|||
return;
|
||||
}
|
||||
if(sendPredicate() || sendingInProgress) {
|
||||
const char* msg = getMessage();
|
||||
const unsigned char* msg = getMessage();
|
||||
int msgLength = getMessageLength();
|
||||
if(!sendingInProgress) {
|
||||
logger->info(MSG_SEND_PEER_MESSAGE,
|
||||
|
|
|
@ -47,7 +47,7 @@ public:
|
|||
|
||||
virtual void send();
|
||||
|
||||
virtual const char* getMessage() = 0;
|
||||
virtual const unsigned char* getMessage() = 0;
|
||||
|
||||
virtual uint32_t getMessageLength() = 0;
|
||||
|
||||
|
|
31
src/Util.cc
31
src/Util.cc
|
@ -50,13 +50,9 @@
|
|||
|
||||
|
||||
template<typename T>
|
||||
string int2str(T value, bool comma) {
|
||||
string uint2str(T value, bool comma) {
|
||||
string str;
|
||||
bool flag = false;
|
||||
if(value < 0) {
|
||||
flag = true;
|
||||
value = -value;
|
||||
} else if(value == 0) {
|
||||
if(value == 0) {
|
||||
str = "0";
|
||||
return str;
|
||||
}
|
||||
|
@ -70,14 +66,27 @@ string int2str(T value, bool comma) {
|
|||
str.insert(str.begin()+1, ',');
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
string int2str(T value, bool comma) {
|
||||
bool flag = false;
|
||||
if(value < 0) {
|
||||
flag = true;
|
||||
value = -value;
|
||||
}
|
||||
string str = uint2str<T>(value, comma);
|
||||
if(flag) {
|
||||
str.insert(str.begin(), '-');
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
string Util::uitos(uint16_t value, bool comma) {
|
||||
return int2str<uint16_t>(value, comma);
|
||||
return uint2str<uint16_t>(value, comma);
|
||||
}
|
||||
|
||||
string Util::itos(int16_t value, bool comma) {
|
||||
|
@ -85,7 +94,7 @@ string Util::itos(int16_t value, bool comma) {
|
|||
}
|
||||
|
||||
string Util::uitos(uint32_t value, bool comma) {
|
||||
return int2str<uint32_t>(value, comma);
|
||||
return uint2str<uint32_t>(value, comma);
|
||||
}
|
||||
|
||||
string Util::itos(int32_t value, bool comma) {
|
||||
|
@ -558,7 +567,7 @@ static int nbits[] = {
|
|||
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
|
||||
};
|
||||
|
||||
int Util::countBit(unsigned int n) {
|
||||
uint32_t Util::countBit(uint32_t n) {
|
||||
/*
|
||||
return
|
||||
nbits[n & 0xffu]+
|
||||
|
@ -566,8 +575,8 @@ int Util::countBit(unsigned int n) {
|
|||
nbits[(n >> 16) & 0xffu]+
|
||||
nbits[(n >> 24) & 0xffu];
|
||||
*/
|
||||
int count = 0;
|
||||
int size = sizeof(unsigned int);
|
||||
uint32_t count = 0;
|
||||
int size = sizeof(uint32_t);
|
||||
for(int i = 0; i < size; i++) {
|
||||
count += nbits[(n >> i*8) & 0xffu];
|
||||
}
|
||||
|
|
|
@ -122,7 +122,7 @@ public:
|
|||
int pieces, int fastSetSize);
|
||||
#endif // ENABLE_BITTORRENT
|
||||
|
||||
static int countBit(unsigned int);
|
||||
static uint32_t countBit(uint32_t n);
|
||||
|
||||
static string randomAlpha(int length);
|
||||
|
||||
|
|
|
@ -42,12 +42,12 @@ CPPUNIT_TEST_SUITE_REGISTRATION( BitfieldManTest );
|
|||
|
||||
void BitfieldManTest::testGetBlockSize() {
|
||||
BitfieldMan bt1(1024, 1024*10);
|
||||
CPPUNIT_ASSERT_EQUAL(1024, bt1.getBlockLength(9));
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, bt1.getBlockLength(9));
|
||||
|
||||
BitfieldMan bt2(1024, 1024*10+1);
|
||||
CPPUNIT_ASSERT_EQUAL(1024, bt2.getBlockLength(9));
|
||||
CPPUNIT_ASSERT_EQUAL(1, bt2.getBlockLength(10));
|
||||
CPPUNIT_ASSERT_EQUAL(0, bt2.getBlockLength(11));
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, bt2.getBlockLength(9));
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1, bt2.getBlockLength(10));
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)0, bt2.getBlockLength(11));
|
||||
}
|
||||
|
||||
void BitfieldManTest::testGetFirstMissingUnusedIndex() {
|
||||
|
@ -84,7 +84,7 @@ void BitfieldManTest::testIsAllBitSet() {
|
|||
}
|
||||
CPPUNIT_ASSERT(!bt1.isAllBitSet());
|
||||
|
||||
for(int i = 0; i < bt1.countBlock(); i++) {
|
||||
for(uint32_t i = 0; i < bt1.countBlock(); i++) {
|
||||
CPPUNIT_ASSERT(bt1.setBit(i));
|
||||
}
|
||||
CPPUNIT_ASSERT(bt1.isAllBitSet());
|
||||
|
@ -122,7 +122,7 @@ void BitfieldManTest::testFilter() {
|
|||
index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
|
||||
btman.setUseBit(index);
|
||||
CPPUNIT_ASSERT_EQUAL(-1, index);
|
||||
CPPUNIT_ASSERT_EQUAL((long long int)12, btman.getFilteredTotalLength());
|
||||
CPPUNIT_ASSERT_EQUAL((uint64_t)12, btman.getFilteredTotalLength());
|
||||
|
||||
// test offset=5, length=2
|
||||
btman.clearAllBit();
|
||||
|
@ -141,13 +141,13 @@ void BitfieldManTest::testFilter() {
|
|||
index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
|
||||
btman.setUseBit(index);
|
||||
CPPUNIT_ASSERT_EQUAL(-1, index);
|
||||
CPPUNIT_ASSERT_EQUAL((long long int)4, btman.getFilteredTotalLength());
|
||||
CPPUNIT_ASSERT_EQUAL((uint64_t)4, btman.getFilteredTotalLength());
|
||||
CPPUNIT_ASSERT(btman.isAllBitSet());
|
||||
|
||||
BitfieldMan btman2(2, 31);
|
||||
btman2.addFilter(0, 31);
|
||||
btman2.enableFilter();
|
||||
CPPUNIT_ASSERT_EQUAL((long long int)31, btman2.getFilteredTotalLength());
|
||||
CPPUNIT_ASSERT_EQUAL((uint64_t)31, btman2.getFilteredTotalLength());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -35,8 +35,8 @@ void BtAllowedFastMessageTest::testCreate() {
|
|||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 17);
|
||||
PeerMessageUtil::setIntParam(&msg[5], 12345);
|
||||
BtAllowedFastMessageHandle pm = BtAllowedFastMessage::create(&msg[4], 5);
|
||||
CPPUNIT_ASSERT_EQUAL(17, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)17, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -35,7 +35,7 @@ void BtBitfieldMessageTest::testCreate() {
|
|||
memset(bitfield, 0xff, sizeof(bitfield));
|
||||
memcpy(&msg[5], bitfield, sizeof(bitfield));
|
||||
BtBitfieldMessageHandle pm = BtBitfieldMessage::create(&msg[4], 3);
|
||||
CPPUNIT_ASSERT_EQUAL(5, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)5, pm->getId());
|
||||
CPPUNIT_ASSERT(memcmp(bitfield, pm->getBitfield(), sizeof(bitfield)) == 0);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)2, pm->getBitfieldLength());
|
||||
// case: payload size is wrong
|
||||
|
|
|
@ -37,15 +37,15 @@ public:
|
|||
|
||||
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
|
||||
public:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
public:
|
||||
MockBtMessageDispatcher2():index(0),
|
||||
begin(0),
|
||||
length(0) {}
|
||||
|
||||
virtual void doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length) {
|
||||
this->index = index;
|
||||
this->begin = begin;
|
||||
this->length = length;
|
||||
|
@ -65,9 +65,9 @@ void BtCancelMessageTest::testCreate() {
|
|||
PeerMessageUtil::setIntParam(&msg[9], 256);
|
||||
PeerMessageUtil::setIntParam(&msg[13], 1024);
|
||||
BtCancelMessageHandle pm = BtCancelMessage::create(&msg[4], 13);
|
||||
CPPUNIT_ASSERT_EQUAL(8, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)8, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength());
|
||||
|
||||
// case: payload size is wrong
|
||||
|
|
|
@ -65,7 +65,7 @@ void BtChokeMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 0);
|
||||
BtChokeMessageHandle pm = BtChokeMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(0, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)0, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -52,7 +52,7 @@ void BtHandshakeMessageTest::testCreate() {
|
|||
unsigned char msg[68];
|
||||
createHandshakeMessageData(msg);
|
||||
BtHandshakeMessageHandle message = BtHandshakeMessage::create(&msg[0], sizeof(msg));
|
||||
CPPUNIT_ASSERT_EQUAL(INT32_MAX, message->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)UINT8_MAX, message->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)19, message->getPstrlen());
|
||||
CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)BTPSTR.c_str(), BTPSTR.size()),
|
||||
Util::toHex(message->getPstr(), BtHandshakeMessage::PSTR_LENGTH));
|
||||
|
|
|
@ -29,7 +29,7 @@ void BtHaveAllMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 14);
|
||||
BtHaveAllMessageHandle pm = BtHaveAllMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(14, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)14, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -32,8 +32,8 @@ void BtHaveMessageTest::testCreate() {
|
|||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 4);
|
||||
PeerMessageUtil::setIntParam(&msg[5], 12345);
|
||||
BtHaveMessageHandle pm = BtHaveMessage::create(&msg[4], 5);
|
||||
CPPUNIT_ASSERT_EQUAL(4, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)4, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -31,7 +31,7 @@ void BtHaveNoneMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 15);
|
||||
BtHaveNoneMessageHandle pm = BtHaveNoneMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(15, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)15, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -33,7 +33,7 @@ void BtInterestedMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 2);
|
||||
BtInterestedMessageHandle pm = BtInterestedMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(2, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)2, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -33,7 +33,7 @@ void BtNotInterestedMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 3);
|
||||
BtNotInterestedMessageHandle pm = BtNotInterestedMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(3, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)3, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
@ -55,7 +55,7 @@ void BtNotInterestedMessageTest::testCreate() {
|
|||
|
||||
void BtNotInterestedMessageTest::testGetMessage() {
|
||||
BtNotInterestedMessage msg;
|
||||
char data[5];
|
||||
unsigned char data[5];
|
||||
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 1, 3);
|
||||
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 5) == 0);
|
||||
}
|
||||
|
|
|
@ -46,11 +46,11 @@ public:
|
|||
|
||||
class MockBtMessage2 : public MockBtMessage {
|
||||
public:
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
public:
|
||||
MockBtMessage2(uint32_t index, uint32_t begin, uint32_t length):index(index), begin(begin), length(length) {}
|
||||
MockBtMessage2(int32_t index, int32_t begin, uint32_t length):index(index), begin(begin), length(length) {}
|
||||
|
||||
};
|
||||
|
||||
|
@ -58,8 +58,8 @@ public:
|
|||
|
||||
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
||||
public:
|
||||
virtual BtMessageHandle createRejectMessage(uint32_t index,
|
||||
uint32_t begin,
|
||||
virtual BtMessageHandle createRejectMessage(int32_t index,
|
||||
int32_t begin,
|
||||
uint32_t length) {
|
||||
MockBtMessage2Handle msg = new MockBtMessage2(index, begin, length);
|
||||
return msg;
|
||||
|
@ -109,9 +109,9 @@ void BtPieceMessageTest::testCreate() {
|
|||
PeerMessageUtil::setIntParam(&msg[9], 256);
|
||||
memcpy(&msg[13], data, sizeof(data));
|
||||
BtPieceMessageHandle pm = BtPieceMessage::create(&msg[4], 11);
|
||||
CPPUNIT_ASSERT_EQUAL(7, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)7, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
||||
CPPUNIT_ASSERT(memcmp(data, pm->getBlock(), sizeof(data)) == 0);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)2, pm->getBlockLength());
|
||||
|
||||
|
@ -170,8 +170,8 @@ void BtPieceMessageTest::testChokingEvent_allowedFastEnabled() {
|
|||
CPPUNIT_ASSERT(msg->isInvalidate());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
|
||||
MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1, rej->index);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, rej->begin);
|
||||
CPPUNIT_ASSERT_EQUAL(1, rej->index);
|
||||
CPPUNIT_ASSERT_EQUAL(1024, rej->begin);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)16*1024, rej->length);
|
||||
}
|
||||
|
||||
|
@ -255,8 +255,8 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_allowedFastEnabled() {
|
|||
CPPUNIT_ASSERT(msg->isInvalidate());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size());
|
||||
MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get();
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1, rej->index);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, rej->begin);
|
||||
CPPUNIT_ASSERT_EQUAL(1, rej->index);
|
||||
CPPUNIT_ASSERT_EQUAL(1024, rej->begin);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)16*1024, rej->length);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ void BtPortMessageTest::testCreate() {
|
|||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 3, 9);
|
||||
PeerMessageUtil::setShortIntParam(&msg[5], 12345);
|
||||
BtPortMessageHandle pm = BtPortMessage::create(&msg[4], 3);
|
||||
CPPUNIT_ASSERT_EQUAL(9, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)9, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint16_t)12345, pm->getPort());
|
||||
|
||||
// case: payload size is wrong
|
||||
|
|
|
@ -37,7 +37,7 @@ public:
|
|||
this->slot = slot;
|
||||
}
|
||||
|
||||
virtual RequestSlot getOutstandingRequest(uint32_t index, uint32_t begin,
|
||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin,
|
||||
uint32_t length) {
|
||||
if(slot.getIndex() == index && slot.getBegin() == begin &&
|
||||
slot.getLength() == length) {
|
||||
|
@ -97,9 +97,9 @@ void BtRejectMessageTest::testCreate() {
|
|||
PeerMessageUtil::setIntParam(&msg[9], 256);
|
||||
PeerMessageUtil::setIntParam(&msg[13], 1024);
|
||||
BtRejectMessageHandle pm = BtRejectMessage::create(&msg[4], 13);
|
||||
CPPUNIT_ASSERT_EQUAL(16, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)16, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength());
|
||||
|
||||
// case: payload size is wrong
|
||||
|
|
|
@ -53,11 +53,11 @@ public:
|
|||
class MockBtMessage2 : public MockBtMessage {
|
||||
public:
|
||||
string type;
|
||||
uint32_t index;
|
||||
uint32_t begin;
|
||||
int32_t index;
|
||||
int32_t begin;
|
||||
uint32_t length;
|
||||
public:
|
||||
MockBtMessage2(string type, uint32_t index, uint32_t begin, uint32_t length):type(type), index(index), begin(begin), length(length) {}
|
||||
MockBtMessage2(string type, int32_t index, int32_t begin, uint32_t length):type(type), index(index), begin(begin), length(length) {}
|
||||
};
|
||||
|
||||
typedef SharedHandle<MockBtMessage2> MockBtMessage2Handle;
|
||||
|
@ -65,13 +65,13 @@ public:
|
|||
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
||||
public:
|
||||
virtual BtMessageHandle
|
||||
createPieceMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
MockBtMessage2Handle btMsg = new MockBtMessage2("piece", index, begin, length);
|
||||
return btMsg;
|
||||
}
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRejectMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
MockBtMessage2Handle btMsg = new MockBtMessage2("reject", index, begin, length);
|
||||
return btMsg;
|
||||
}
|
||||
|
@ -129,9 +129,9 @@ void BtRequestMessageTest::testCreate() {
|
|||
PeerMessageUtil::setIntParam(&msg[9], 256);
|
||||
PeerMessageUtil::setIntParam(&msg[13], 1024);
|
||||
BtRequestMessageHandle pm = BtRequestMessage::create(&msg[4], 13);
|
||||
CPPUNIT_ASSERT_EQUAL(6, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)6, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL(256, pm->getBegin());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1024, pm->getLength());
|
||||
|
||||
// case: payload size is wrong
|
||||
|
@ -172,8 +172,8 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() {
|
|||
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
||||
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
||||
CPPUNIT_ASSERT_EQUAL(string("piece"), pieceMsg->type);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL(1, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL(16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)32, pieceMsg->length);
|
||||
}
|
||||
|
||||
|
@ -185,8 +185,8 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExten
|
|||
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
||||
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
||||
CPPUNIT_ASSERT_EQUAL(string("reject"), pieceMsg->type);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)1, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL(1, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL(16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)32, pieceMsg->length);
|
||||
}
|
||||
|
||||
|
@ -206,8 +206,8 @@ void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionE
|
|||
CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size());
|
||||
MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get();
|
||||
CPPUNIT_ASSERT_EQUAL(string("reject"), pieceMsg->type);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)2, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL(2, pieceMsg->index);
|
||||
CPPUNIT_ASSERT_EQUAL(16, pieceMsg->begin);
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)32, pieceMsg->length);
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ void BtSuggestPieceMessageTest::testCreate() {
|
|||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 5, 13);
|
||||
PeerMessageUtil::setIntParam(&msg[5], 12345);
|
||||
BtSuggestPieceMessageHandle pm = BtSuggestPieceMessage::create(&msg[4], 5);
|
||||
CPPUNIT_ASSERT_EQUAL(13, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint32_t)12345, pm->getIndex());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)13, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL(12345, pm->getIndex());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -32,7 +32,7 @@ void BtUnchokeMessageTest::testCreate() {
|
|||
unsigned char msg[5];
|
||||
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 1);
|
||||
BtUnchokeMessageHandle pm = BtUnchokeMessage::create(&msg[4], 1);
|
||||
CPPUNIT_ASSERT_EQUAL(1, pm->getId());
|
||||
CPPUNIT_ASSERT_EQUAL((uint8_t)1, pm->getId());
|
||||
|
||||
// case: payload size is wrong
|
||||
try {
|
||||
|
|
|
@ -122,7 +122,7 @@ public:
|
|||
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
||||
public:
|
||||
virtual BtMessageHandle
|
||||
createCancelMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
MockBtMessage2Handle btMsg = new MockBtMessage2();
|
||||
btMsg->type = "cancel";
|
||||
return btMsg;
|
||||
|
|
|
@ -43,14 +43,14 @@ public:
|
|||
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
||||
public:
|
||||
virtual BtMessageHandle
|
||||
createRequestMessage(const PieceHandle& piece, uint32_t blockIndex) {
|
||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex) {
|
||||
return new MockBtRequestMessage(piece->getIndex(), blockIndex);
|
||||
}
|
||||
};
|
||||
|
||||
class MockBtMessageDispatcher2 : public MockBtMessageDispatcher {
|
||||
public:
|
||||
virtual bool isOutstandingRequest(uint32_t index, uint32_t blockIndex) {
|
||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex) {
|
||||
return index == 0 && blockIndex == 0;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -42,7 +42,7 @@ private:
|
|||
bool sendingInProgress;
|
||||
bool invalidate;
|
||||
bool uploading;
|
||||
int32_t id;
|
||||
uint8_t id;
|
||||
public:
|
||||
MockBtMessage() {}
|
||||
|
||||
|
@ -72,11 +72,11 @@ public:
|
|||
this->uploading = flag;
|
||||
}
|
||||
|
||||
virtual int32_t getId() {
|
||||
virtual uint8_t getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
void setId(int32_t id) {
|
||||
void setId(uint8_t id) {
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ public:
|
|||
|
||||
virtual void sendMessages() {}
|
||||
|
||||
virtual void doCancelSendingPieceAction(uint32_t index, uint32_t begin, uint32_t blockLength) {}
|
||||
virtual void doCancelSendingPieceAction(int32_t index, int32_t begin, uint32_t length) {}
|
||||
|
||||
virtual void doCancelSendingPieceAction(const PieceHandle& piece) {}
|
||||
|
||||
|
@ -43,11 +43,11 @@ public:
|
|||
return 0;
|
||||
}
|
||||
|
||||
virtual bool isOutstandingRequest(uint32_t index, uint32_t blockIndex) {
|
||||
virtual bool isOutstandingRequest(int32_t index, int32_t blockIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual RequestSlot getOutstandingRequest(uint32_t index, uint32_t begin, uint32_t blockLength) {
|
||||
virtual RequestSlot getOutstandingRequest(int32_t index, int32_t begin, uint32_t length) {
|
||||
return RequestSlot::nullSlot;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,21 +26,21 @@ public:
|
|||
}
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRequestMessage(const PieceHandle& piece, uint32_t blockIndex) {
|
||||
createRequestMessage(const PieceHandle& piece, int32_t blockIndex) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
|
||||
virtual BtMessageHandle
|
||||
createCancelMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createCancelMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
|
||||
virtual BtMessageHandle
|
||||
createPieceMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createPieceMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
|
||||
virtual BtMessageHandle createHaveMessage(uint32_t index) {
|
||||
virtual BtMessageHandle createHaveMessage(int32_t index) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
|
||||
|
@ -77,11 +77,11 @@ public:
|
|||
}
|
||||
|
||||
virtual BtMessageHandle
|
||||
createRejectMessage(uint32_t index, uint32_t begin, uint32_t length) {
|
||||
createRejectMessage(int32_t index, int32_t begin, uint32_t length) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
|
||||
virtual BtMessageHandle createAllowedFastMessage(uint32_t index) {
|
||||
virtual BtMessageHandle createAllowedFastMessage(int32_t index) {
|
||||
return BtMessageHandle(0);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue