Torrent example: update usage of integer types
Some of the 'int's are purposefully 32-bit because that's what the protocol is, but others aren't. So, be more explicit. Task-number: QTBUG-110622 Pick-to: 6.5 Change-Id: I338abca1f13b0c95f49a6f52933712f43f147590 Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
parent
e8a711ef3b
commit
1a55c8d887
@ -36,7 +36,7 @@ FileManager::~FileManager()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int FileManager::read(int pieceIndex, int offset, int length)
|
qint32 FileManager::read(qint32 pieceIndex, qint32 offset, qint32 length)
|
||||||
{
|
{
|
||||||
ReadRequest request;
|
ReadRequest request;
|
||||||
request.pieceIndex = pieceIndex;
|
request.pieceIndex = pieceIndex;
|
||||||
@ -55,7 +55,7 @@ int FileManager::read(int pieceIndex, int offset, int length)
|
|||||||
return request.id;
|
return request.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileManager::write(int pieceIndex, int offset, const QByteArray &data)
|
void FileManager::write(qint32 pieceIndex, qint32 offset, const QByteArray &data)
|
||||||
{
|
{
|
||||||
WriteRequest request;
|
WriteRequest request;
|
||||||
request.pieceIndex = pieceIndex;
|
request.pieceIndex = pieceIndex;
|
||||||
@ -71,7 +71,7 @@ void FileManager::write(int pieceIndex, int offset, const QByteArray &data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileManager::verifyPiece(int pieceIndex)
|
void FileManager::verifyPiece(qint32 pieceIndex)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&mutex);
|
QMutexLocker locker(&mutex);
|
||||||
pendingVerificationRequests << pieceIndex;
|
pendingVerificationRequests << pieceIndex;
|
||||||
@ -83,7 +83,7 @@ void FileManager::verifyPiece(int pieceIndex)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int FileManager::pieceLengthAt(int pieceIndex) const
|
qint32 FileManager::pieceLengthAt(qint32 pieceIndex) const
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&mutex);
|
QMutexLocker locker(&mutex);
|
||||||
return (sha1s.size() == pieceIndex + 1)
|
return (sha1s.size() == pieceIndex + 1)
|
||||||
@ -282,7 +282,7 @@ bool FileManager::generateFiles()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray FileManager::readBlock(int pieceIndex, int offset, int length)
|
QByteArray FileManager::readBlock(qint32 pieceIndex, qint32 offset, qint32 length)
|
||||||
{
|
{
|
||||||
QByteArray block;
|
QByteArray block;
|
||||||
qint64 startReadIndex = (quint64(pieceIndex) * pieceLength) + offset;
|
qint64 startReadIndex = (quint64(pieceIndex) * pieceLength) + offset;
|
||||||
@ -320,7 +320,7 @@ QByteArray FileManager::readBlock(int pieceIndex, int offset, int length)
|
|||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileManager::writeBlock(int pieceIndex, int offset, const QByteArray &data)
|
bool FileManager::writeBlock(qint32 pieceIndex, qint32 offset, const QByteArray &data)
|
||||||
{
|
{
|
||||||
qint64 startWriteIndex = (qint64(pieceIndex) * pieceLength) + offset;
|
qint64 startWriteIndex = (qint64(pieceIndex) * pieceLength) + offset;
|
||||||
qint64 currentIndex = 0;
|
qint64 currentIndex = 0;
|
||||||
@ -373,9 +373,9 @@ void FileManager::verifyFileContents()
|
|||||||
|
|
||||||
int oldPercent = 0;
|
int oldPercent = 0;
|
||||||
if (!newFile) {
|
if (!newFile) {
|
||||||
int numPieces = sha1s.size();
|
qint32 numPieces = sha1s.size();
|
||||||
|
|
||||||
for (int index = 0; index < numPieces; ++index) {
|
for (qint32 index = 0; index < numPieces; ++index) {
|
||||||
verifySinglePiece(index);
|
verifySinglePiece(index);
|
||||||
|
|
||||||
int percent = ((index + 1) * 100) / numPieces;
|
int percent = ((index + 1) * 100) / numPieces;
|
||||||
@ -395,7 +395,7 @@ void FileManager::verifyFileContents()
|
|||||||
emit pieceVerified(index, verifySinglePiece(index));
|
emit pieceVerified(index, verifySinglePiece(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileManager::verifySinglePiece(int pieceIndex)
|
bool FileManager::verifySinglePiece(qint32 pieceIndex)
|
||||||
{
|
{
|
||||||
QByteArray block = readBlock(pieceIndex, 0, pieceLength);
|
QByteArray block = readBlock(pieceIndex, 0, pieceLength);
|
||||||
QByteArray sha1Sum = QCryptographicHash::hash(block, QCryptographicHash::Sha1);
|
QByteArray sha1Sum = QCryptographicHash::hash(block, QCryptographicHash::Sha1);
|
||||||
|
@ -29,13 +29,13 @@ public:
|
|||||||
inline void setMetaInfo(const MetaInfo &info) { metaInfo = info; }
|
inline void setMetaInfo(const MetaInfo &info) { metaInfo = info; }
|
||||||
inline void setDestinationFolder(const QString &directory) { destinationPath = directory; }
|
inline void setDestinationFolder(const QString &directory) { destinationPath = directory; }
|
||||||
|
|
||||||
int read(int pieceIndex, int offset, int length);
|
qint32 read(qint32 pieceIndex, qint32 offset, qint32 length);
|
||||||
void write(int pieceIndex, int offset, const QByteArray &data);
|
void write(qint32 pieceIndex, qint32 offset, const QByteArray &data);
|
||||||
void verifyPiece(int pieceIndex);
|
void verifyPiece(qint32 pieceIndex);
|
||||||
inline qint64 totalSize() const { return totalLength; }
|
inline qint64 totalSize() const { return totalLength; }
|
||||||
|
|
||||||
inline int pieceCount() const { return numPieces; }
|
inline qint32 pieceCount() const { return numPieces; }
|
||||||
int pieceLengthAt(int pieceIndex) const;
|
qint32 pieceLengthAt(qint32 pieceIndex) const;
|
||||||
|
|
||||||
QBitArray completedPieces() const;
|
QBitArray completedPieces() const;
|
||||||
void setCompletedPieces(const QBitArray &pieces);
|
void setCompletedPieces(const QBitArray &pieces);
|
||||||
@ -46,35 +46,35 @@ public slots:
|
|||||||
void startDataVerification();
|
void startDataVerification();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void dataRead(int id, int pieceIndex, int offset, const QByteArray &data);
|
void dataRead(qint32 id, qint32 pieceIndex, qint32 offset, const QByteArray &data);
|
||||||
void error();
|
void error();
|
||||||
void verificationProgress(int percent);
|
void verificationProgress(int percent);
|
||||||
void verificationDone();
|
void verificationDone();
|
||||||
void pieceVerified(int pieceIndex, bool verified);
|
void pieceVerified(qint32 pieceIndex, bool verified);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void run() override;
|
void run() override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
bool verifySinglePiece(int pieceIndex);
|
bool verifySinglePiece(qint32 pieceIndex);
|
||||||
void wakeUp();
|
void wakeUp();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool generateFiles();
|
bool generateFiles();
|
||||||
QByteArray readBlock(int pieceIndex, int offset, int length);
|
QByteArray readBlock(qint32 pieceIndex, qint32 offset, qint32 length);
|
||||||
bool writeBlock(int pieceIndex, int offset, const QByteArray &data);
|
bool writeBlock(qint32 pieceIndex, qint32 offset, const QByteArray &data);
|
||||||
void verifyFileContents();
|
void verifyFileContents();
|
||||||
|
|
||||||
struct WriteRequest {
|
struct WriteRequest {
|
||||||
int pieceIndex;
|
qint32 pieceIndex;
|
||||||
int offset;
|
qint32 offset;
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
};
|
};
|
||||||
struct ReadRequest {
|
struct ReadRequest {
|
||||||
int pieceIndex;
|
qint32 pieceIndex;
|
||||||
int offset;
|
qint32 offset;
|
||||||
int length;
|
qint32 length;
|
||||||
int id;
|
qint32 id;
|
||||||
};
|
};
|
||||||
|
|
||||||
QString errString;
|
QString errString;
|
||||||
|
@ -17,7 +17,7 @@ struct MetaInfoSingleFile
|
|||||||
qint64 length;
|
qint64 length;
|
||||||
QByteArray md5sum;
|
QByteArray md5sum;
|
||||||
QString name;
|
QString name;
|
||||||
int pieceLength;
|
qint32 pieceLength;
|
||||||
QList<QByteArray> sha1Sums;
|
QList<QByteArray> sha1Sums;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ PeerWireClient::PeerWireClient(const QByteArray &peerId, QObject *parent)
|
|||||||
|
|
||||||
// Registers the peer ID and SHA1 sum of the torrent, and initiates
|
// Registers the peer ID and SHA1 sum of the torrent, and initiates
|
||||||
// the handshake.
|
// the handshake.
|
||||||
void PeerWireClient::initialize(const QByteArray &infoHash, int pieceCount)
|
void PeerWireClient::initialize(const QByteArray &infoHash, qint32 pieceCount)
|
||||||
{
|
{
|
||||||
this->infoHash = infoHash;
|
this->infoHash = infoHash;
|
||||||
peerPieces.resize(pieceCount);
|
peerPieces.resize(pieceCount);
|
||||||
@ -142,7 +142,7 @@ void PeerWireClient::sendNotInterested()
|
|||||||
|
|
||||||
// Sends a piece notification / a "have" message, informing the peer
|
// Sends a piece notification / a "have" message, informing the peer
|
||||||
// that we have just downloaded a new piece.
|
// that we have just downloaded a new piece.
|
||||||
void PeerWireClient::sendPieceNotification(int piece)
|
void PeerWireClient::sendPieceNotification(qint32 piece)
|
||||||
{
|
{
|
||||||
if (!sentHandShake)
|
if (!sentHandShake)
|
||||||
sendHandShake();
|
sendHandShake();
|
||||||
@ -183,7 +183,7 @@ void PeerWireClient::sendPieceList(const QBitArray &bitField)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sends a request for a block.
|
// Sends a request for a block.
|
||||||
void PeerWireClient::requestBlock(int piece, int offset, int length)
|
void PeerWireClient::requestBlock(qint32 piece, qint32 offset, qint32 length)
|
||||||
{
|
{
|
||||||
char message[] = {0, 0, 0, 1, 6};
|
char message[] = {0, 0, 0, 1, 6};
|
||||||
qToBigEndian(13, &message[0]);
|
qToBigEndian(13, &message[0]);
|
||||||
@ -206,7 +206,7 @@ void PeerWireClient::requestBlock(int piece, int offset, int length)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cancels a request for a block.
|
// Cancels a request for a block.
|
||||||
void PeerWireClient::cancelRequest(int piece, int offset, int length)
|
void PeerWireClient::cancelRequest(qint32 piece, qint32 offset, qint32 length)
|
||||||
{
|
{
|
||||||
char message[] = {0, 0, 0, 1, 8};
|
char message[] = {0, 0, 0, 1, 8};
|
||||||
qToBigEndian(13, &message[0]);
|
qToBigEndian(13, &message[0]);
|
||||||
@ -222,7 +222,7 @@ void PeerWireClient::cancelRequest(int piece, int offset, int length)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sends a block to the peer.
|
// Sends a block to the peer.
|
||||||
void PeerWireClient::sendBlock(int piece, int offset, const QByteArray &data)
|
void PeerWireClient::sendBlock(qint32 piece, qint32 offset, const QByteArray &data)
|
||||||
{
|
{
|
||||||
QByteArray block;
|
QByteArray block;
|
||||||
|
|
||||||
@ -516,7 +516,7 @@ void PeerWireClient::processIncomingData()
|
|||||||
for (int i = 1; i < packet.size(); ++i) {
|
for (int i = 1; i < packet.size(); ++i) {
|
||||||
for (int bit = 0; bit < 8; ++bit) {
|
for (int bit = 0; bit < 8; ++bit) {
|
||||||
if (packet.at(i) & (1 << (7 - bit))) {
|
if (packet.at(i) & (1 << (7 - bit))) {
|
||||||
int bitIndex = int(((i - 1) * 8) + bit);
|
qint32 bitIndex = qint32(((i - 1) * 8) + bit);
|
||||||
if (bitIndex >= 0 && bitIndex < peerPieces.size()) {
|
if (bitIndex >= 0 && bitIndex < peerPieces.size()) {
|
||||||
// Occasionally, broken clients claim to have
|
// Occasionally, broken clients claim to have
|
||||||
// pieces whose index is outside the valid range.
|
// pieces whose index is outside the valid range.
|
||||||
@ -534,12 +534,12 @@ void PeerWireClient::processIncomingData()
|
|||||||
quint32 index = qFromBigEndian<quint32>(&packet.data()[1]);
|
quint32 index = qFromBigEndian<quint32>(&packet.data()[1]);
|
||||||
quint32 begin = qFromBigEndian<quint32>(&packet.data()[5]);
|
quint32 begin = qFromBigEndian<quint32>(&packet.data()[5]);
|
||||||
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
||||||
emit blockRequested(int(index), int(begin), int(length));
|
emit blockRequested(qint32(index), qint32(begin), qint32(length));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PiecePacket: {
|
case PiecePacket: {
|
||||||
int index = int(qFromBigEndian<quint32>(&packet.data()[1]));
|
qint32 index = qint32(qFromBigEndian<quint32>(&packet.data()[1]));
|
||||||
int begin = int(qFromBigEndian<quint32>(&packet.data()[5]));
|
qint32 begin = qint32(qFromBigEndian<quint32>(&packet.data()[5]));
|
||||||
|
|
||||||
incoming.removeAll(TorrentBlock(index, begin, packet.size() - 9));
|
incoming.removeAll(TorrentBlock(index, begin, packet.size() - 9));
|
||||||
|
|
||||||
@ -560,9 +560,9 @@ void PeerWireClient::processIncomingData()
|
|||||||
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
||||||
for (int i = 0; i < pendingBlocks.size(); ++i) {
|
for (int i = 0; i < pendingBlocks.size(); ++i) {
|
||||||
const BlockInfo &blockInfo = pendingBlocks.at(i);
|
const BlockInfo &blockInfo = pendingBlocks.at(i);
|
||||||
if (blockInfo.pieceIndex == int(index)
|
if (blockInfo.pieceIndex == qint32(index)
|
||||||
&& blockInfo.offset == int(begin)
|
&& blockInfo.offset == qint32(begin)
|
||||||
&& blockInfo.length == int(length)) {
|
&& blockInfo.length == qint32(length)) {
|
||||||
pendingBlocks.removeAt(i);
|
pendingBlocks.removeAt(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ class TorrentPeer;
|
|||||||
|
|
||||||
struct TorrentBlock
|
struct TorrentBlock
|
||||||
{
|
{
|
||||||
inline TorrentBlock(int p, int o, int l)
|
inline TorrentBlock(qint32 p, qint32 o, qint32 l)
|
||||||
: pieceIndex(p), offset(o), length(l)
|
: pieceIndex(p), offset(o), length(l)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -27,9 +27,9 @@ struct TorrentBlock
|
|||||||
&& length == other.length;
|
&& length == other.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pieceIndex;
|
qint32 pieceIndex;
|
||||||
int offset;
|
qint32 offset;
|
||||||
int length;
|
qint32 length;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PeerWireClient : public QTcpSocket
|
class PeerWireClient : public QTcpSocket
|
||||||
|
@ -70,9 +70,9 @@ public:
|
|||||||
int uploadScheduleTimer;
|
int uploadScheduleTimer;
|
||||||
|
|
||||||
// Pieces
|
// Pieces
|
||||||
QMap<int, PeerWireClient *> readIds;
|
QMap<qint32, PeerWireClient *> readIds;
|
||||||
QMultiMap<PeerWireClient *, TorrentPiece *> payloads;
|
QMultiMap<PeerWireClient *, TorrentPiece *> payloads;
|
||||||
QMap<int, TorrentPiece *> pendingPieces;
|
QMap<qint32, TorrentPiece *> pendingPieces;
|
||||||
QBitArray completedPieces;
|
QBitArray completedPieces;
|
||||||
QBitArray incompletePieces;
|
QBitArray incompletePieces;
|
||||||
int pieceCount;
|
int pieceCount;
|
||||||
@ -269,8 +269,8 @@ void TorrentClient::setDumpedState(const QByteArray &dumpedState)
|
|||||||
stream >> d->completedPieces;
|
stream >> d->completedPieces;
|
||||||
|
|
||||||
while (!stream.atEnd()) {
|
while (!stream.atEnd()) {
|
||||||
int index;
|
qint32 index;
|
||||||
int length;
|
qint32 length;
|
||||||
QBitArray completed;
|
QBitArray completed;
|
||||||
stream >> index >> length >> completed;
|
stream >> index >> length >> completed;
|
||||||
if (stream.status() != QDataStream::Ok) {
|
if (stream.status() != QDataStream::Ok) {
|
||||||
@ -298,7 +298,7 @@ QByteArray TorrentClient::dumpedState() const
|
|||||||
|
|
||||||
// Save the state of all partially downloaded pieces into a format
|
// Save the state of all partially downloaded pieces into a format
|
||||||
// suitable for storing in settings.
|
// suitable for storing in settings.
|
||||||
QMap<int, TorrentPiece *>::ConstIterator it = d->pendingPieces.constBegin();
|
auto it = d->pendingPieces.constBegin();
|
||||||
while (it != d->pendingPieces.constEnd()) {
|
while (it != d->pendingPieces.constEnd()) {
|
||||||
TorrentPiece *piece = it.value();
|
TorrentPiece *piece = it.value();
|
||||||
if (blocksLeftForPiece(piece) > 0 && blocksLeftForPiece(piece) < piece->completedBlocks.size()) {
|
if (blocksLeftForPiece(piece) > 0 && blocksLeftForPiece(piece) < piece->completedBlocks.size()) {
|
||||||
@ -497,7 +497,8 @@ void TorrentClient::timerEvent(QTimerEvent *event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorrentClient::sendToPeer(int readId, int pieceIndex, int begin, const QByteArray &data)
|
void TorrentClient::sendToPeer(qint32 readId, qint32 pieceIndex, qint32 begin,
|
||||||
|
const QByteArray &data)
|
||||||
{
|
{
|
||||||
// Send the requested block to the peer if the client connection
|
// Send the requested block to the peer if the client connection
|
||||||
// still exists; otherwise do nothing. This slot is called by the
|
// still exists; otherwise do nothing. This slot is called by the
|
||||||
@ -516,7 +517,7 @@ void TorrentClient::fullVerificationDone()
|
|||||||
d->completedPieces = d->fileManager.completedPieces();
|
d->completedPieces = d->fileManager.completedPieces();
|
||||||
d->incompletePieces.resize(d->completedPieces.size());
|
d->incompletePieces.resize(d->completedPieces.size());
|
||||||
d->pieceCount = d->completedPieces.size();
|
d->pieceCount = d->completedPieces.size();
|
||||||
for (int i = 0; i < d->fileManager.pieceCount(); ++i) {
|
for (qint32 i = 0; i < d->fileManager.pieceCount(); ++i) {
|
||||||
if (!d->completedPieces.testBit(i))
|
if (!d->completedPieces.testBit(i))
|
||||||
d->incompletePieces.setBit(i);
|
d->incompletePieces.setBit(i);
|
||||||
}
|
}
|
||||||
@ -525,7 +526,7 @@ void TorrentClient::fullVerificationDone()
|
|||||||
|
|
||||||
// If the checksums show that what the dumped state thought was
|
// If the checksums show that what the dumped state thought was
|
||||||
// partial was in fact complete, then we trust the checksums.
|
// partial was in fact complete, then we trust the checksums.
|
||||||
QMap<int, TorrentPiece *>::Iterator it = d->pendingPieces.begin();
|
auto it = d->pendingPieces.begin();
|
||||||
while (it != d->pendingPieces.end()) {
|
while (it != d->pendingPieces.end()) {
|
||||||
if (d->completedPieces.testBit(it.key()))
|
if (d->completedPieces.testBit(it.key()))
|
||||||
it = d->pendingPieces.erase(it);
|
it = d->pendingPieces.erase(it);
|
||||||
@ -555,7 +556,7 @@ void TorrentClient::fullVerificationDone()
|
|||||||
d->trackerClient.start(d->metaInfo);
|
d->trackerClient.start(d->metaInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorrentClient::pieceVerified(int pieceIndex, bool ok)
|
void TorrentClient::pieceVerified(qint32 pieceIndex, bool ok)
|
||||||
{
|
{
|
||||||
TorrentPiece *piece = d->pendingPieces.value(pieceIndex);
|
TorrentPiece *piece = d->pendingPieces.value(pieceIndex);
|
||||||
|
|
||||||
@ -583,7 +584,7 @@ void TorrentClient::pieceVerified(int pieceIndex, bool ok)
|
|||||||
if (!peer->interesting)
|
if (!peer->interesting)
|
||||||
continue;
|
continue;
|
||||||
bool interesting = false;
|
bool interesting = false;
|
||||||
for (int i = 0; i < d->pieceCount; ++i) {
|
for (qint32 i = 0; i < d->pieceCount; ++i) {
|
||||||
if (peer->pieces.testBit(i) && d->incompletePieces.testBit(i)) {
|
if (peer->pieces.testBit(i) && d->incompletePieces.testBit(i)) {
|
||||||
interesting = true;
|
interesting = true;
|
||||||
break;
|
break;
|
||||||
@ -901,8 +902,8 @@ void TorrentClient::peerPiecesAvailable(const QBitArray &pieces)
|
|||||||
// Check for interesting pieces, and tell the peer whether we are
|
// Check for interesting pieces, and tell the peer whether we are
|
||||||
// interested or not.
|
// interested or not.
|
||||||
bool interested = false;
|
bool interested = false;
|
||||||
int piecesSize = pieces.size();
|
qint32 piecesSize = pieces.size();
|
||||||
for (int pieceIndex = 0; pieceIndex < piecesSize; ++pieceIndex) {
|
for (qint32 pieceIndex = 0; pieceIndex < piecesSize; ++pieceIndex) {
|
||||||
if (!pieces.testBit(pieceIndex))
|
if (!pieces.testBit(pieceIndex))
|
||||||
continue;
|
continue;
|
||||||
if (!d->completedPieces.testBit(pieceIndex)) {
|
if (!d->completedPieces.testBit(pieceIndex)) {
|
||||||
@ -914,7 +915,7 @@ void TorrentClient::peerPiecesAvailable(const QBitArray &pieces)
|
|||||||
}
|
}
|
||||||
|
|
||||||
QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client);
|
QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client);
|
||||||
int inProgress = 0;
|
qint32 inProgress = 0;
|
||||||
while (it != d->payloads.end() && it.key() == client) {
|
while (it != d->payloads.end() && it.key() == client) {
|
||||||
if (it.value()->inProgress)
|
if (it.value()->inProgress)
|
||||||
inProgress += it.value()->requestedBlocks.count(true);
|
inProgress += it.value()->requestedBlocks.count(true);
|
||||||
@ -932,7 +933,7 @@ void TorrentClient::peerPiecesAvailable(const QBitArray &pieces)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorrentClient::peerRequestsBlock(int pieceIndex, int begin, int length)
|
void TorrentClient::peerRequestsBlock(qint32 pieceIndex, qint32 begin, qint32 length)
|
||||||
{
|
{
|
||||||
PeerWireClient *client = qobject_cast<PeerWireClient *>(sender());
|
PeerWireClient *client = qobject_cast<PeerWireClient *>(sender());
|
||||||
|
|
||||||
@ -949,7 +950,7 @@ void TorrentClient::peerRequestsBlock(int pieceIndex, int begin, int length)
|
|||||||
qobject_cast<PeerWireClient *>(sender()));
|
qobject_cast<PeerWireClient *>(sender()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TorrentClient::blockReceived(int pieceIndex, int begin, const QByteArray &data)
|
void TorrentClient::blockReceived(qint32 pieceIndex, qint32 begin, const QByteArray &data)
|
||||||
{
|
{
|
||||||
PeerWireClient *client = qobject_cast<PeerWireClient *>(sender());
|
PeerWireClient *client = qobject_cast<PeerWireClient *>(sender());
|
||||||
if (data.size() == 0) {
|
if (data.size() == 0) {
|
||||||
@ -958,7 +959,7 @@ void TorrentClient::blockReceived(int pieceIndex, int begin, const QByteArray &d
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ignore it if we already have this block.
|
// Ignore it if we already have this block.
|
||||||
int blockBit = begin / BlockSize;
|
qint32 blockBit = begin / BlockSize;
|
||||||
TorrentPiece *piece = d->pendingPieces.value(pieceIndex);
|
TorrentPiece *piece = d->pendingPieces.value(pieceIndex);
|
||||||
if (!piece || piece->completedBlocks.testBit(blockBit)) {
|
if (!piece || piece->completedBlocks.testBit(blockBit)) {
|
||||||
// Discard blocks that we already have, and fill up the pipeline.
|
// Discard blocks that we already have, and fill up the pipeline.
|
||||||
@ -1026,7 +1027,7 @@ void TorrentClient::peerWireBytesReceived(qint64 size)
|
|||||||
emit dataSent(size);
|
emit dataSent(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int TorrentClient::blocksLeftForPiece(const TorrentPiece *piece) const
|
qint32 TorrentClient::blocksLeftForPiece(const TorrentPiece *piece) const
|
||||||
{
|
{
|
||||||
return piece->completedBlocks.count(false);
|
return piece->completedBlocks.count(false);
|
||||||
}
|
}
|
||||||
@ -1113,7 +1114,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
|
|||||||
|
|
||||||
// Make a list of all the client's pending pieces, and count how
|
// Make a list of all the client's pending pieces, and count how
|
||||||
// many blocks have been requested.
|
// many blocks have been requested.
|
||||||
QList<int> currentPieces;
|
QList<qint32> currentPieces;
|
||||||
bool somePiecesAreNotInProgress = false;
|
bool somePiecesAreNotInProgress = false;
|
||||||
TorrentPiece *lastPendingPiece = nullptr;
|
TorrentPiece *lastPendingPiece = nullptr;
|
||||||
QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client);
|
QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client);
|
||||||
@ -1164,7 +1165,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
|
|||||||
// the same piece. In endgame mode, this only applies to
|
// the same piece. In endgame mode, this only applies to
|
||||||
// clients that are currently uploading (more than 1.0KB/s).
|
// clients that are currently uploading (more than 1.0KB/s).
|
||||||
if ((d->state == Endgame && client->uploadSpeed() < 1024) || d->state != WarmingUp) {
|
if ((d->state == Endgame && client->uploadSpeed() < 1024) || d->state != WarmingUp) {
|
||||||
QMap<int, TorrentPiece *>::ConstIterator it = d->pendingPieces.constBegin();
|
auto it = d->pendingPieces.constBegin();
|
||||||
while (it != d->pendingPieces.constEnd()) {
|
while (it != d->pendingPieces.constEnd()) {
|
||||||
if (it.value()->inProgress)
|
if (it.value()->inProgress)
|
||||||
incompletePiecesAvailableToClient.clearBit(it.key());
|
incompletePiecesAvailableToClient.clearBit(it.key());
|
||||||
@ -1176,7 +1177,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
|
|||||||
incompletePiecesAvailableToClient &= client->availablePieces();
|
incompletePiecesAvailableToClient &= client->availablePieces();
|
||||||
|
|
||||||
// Remove all pieces that this client has already requested.
|
// Remove all pieces that this client has already requested.
|
||||||
for (int i : std::as_const(currentPieces))
|
for (qint32 i : std::as_const(currentPieces))
|
||||||
incompletePiecesAvailableToClient.clearBit(i);
|
incompletePiecesAvailableToClient.clearBit(i);
|
||||||
|
|
||||||
// Only continue if more pieces can be scheduled. If no pieces
|
// Only continue if more pieces can be scheduled. If no pieces
|
||||||
@ -1188,7 +1189,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
|
|||||||
// Check if any of the partially completed pieces can be
|
// Check if any of the partially completed pieces can be
|
||||||
// recovered, and if so, pick a random one of them.
|
// recovered, and if so, pick a random one of them.
|
||||||
QList<TorrentPiece *> partialPieces;
|
QList<TorrentPiece *> partialPieces;
|
||||||
QMap<int, TorrentPiece *>::ConstIterator it = d->pendingPieces.constBegin();
|
auto it = d->pendingPieces.constBegin();
|
||||||
while (it != d->pendingPieces.constEnd()) {
|
while (it != d->pendingPieces.constEnd()) {
|
||||||
TorrentPiece *tmp = it.value();
|
TorrentPiece *tmp = it.value();
|
||||||
if (incompletePiecesAvailableToClient.testBit(it.key())) {
|
if (incompletePiecesAvailableToClient.testBit(it.key())) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user