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;
|
||||
request.pieceIndex = pieceIndex;
|
||||
@ -55,7 +55,7 @@ int FileManager::read(int pieceIndex, int offset, int length)
|
||||
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;
|
||||
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);
|
||||
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);
|
||||
return (sha1s.size() == pieceIndex + 1)
|
||||
@ -282,7 +282,7 @@ bool FileManager::generateFiles()
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray FileManager::readBlock(int pieceIndex, int offset, int length)
|
||||
QByteArray FileManager::readBlock(qint32 pieceIndex, qint32 offset, qint32 length)
|
||||
{
|
||||
QByteArray block;
|
||||
qint64 startReadIndex = (quint64(pieceIndex) * pieceLength) + offset;
|
||||
@ -320,7 +320,7 @@ QByteArray FileManager::readBlock(int pieceIndex, int offset, int length)
|
||||
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 currentIndex = 0;
|
||||
@ -373,9 +373,9 @@ void FileManager::verifyFileContents()
|
||||
|
||||
int oldPercent = 0;
|
||||
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);
|
||||
|
||||
int percent = ((index + 1) * 100) / numPieces;
|
||||
@ -395,7 +395,7 @@ void FileManager::verifyFileContents()
|
||||
emit pieceVerified(index, verifySinglePiece(index));
|
||||
}
|
||||
|
||||
bool FileManager::verifySinglePiece(int pieceIndex)
|
||||
bool FileManager::verifySinglePiece(qint32 pieceIndex)
|
||||
{
|
||||
QByteArray block = readBlock(pieceIndex, 0, pieceLength);
|
||||
QByteArray sha1Sum = QCryptographicHash::hash(block, QCryptographicHash::Sha1);
|
||||
|
@ -29,13 +29,13 @@ public:
|
||||
inline void setMetaInfo(const MetaInfo &info) { metaInfo = info; }
|
||||
inline void setDestinationFolder(const QString &directory) { destinationPath = directory; }
|
||||
|
||||
int read(int pieceIndex, int offset, int length);
|
||||
void write(int pieceIndex, int offset, const QByteArray &data);
|
||||
void verifyPiece(int pieceIndex);
|
||||
qint32 read(qint32 pieceIndex, qint32 offset, qint32 length);
|
||||
void write(qint32 pieceIndex, qint32 offset, const QByteArray &data);
|
||||
void verifyPiece(qint32 pieceIndex);
|
||||
inline qint64 totalSize() const { return totalLength; }
|
||||
|
||||
inline int pieceCount() const { return numPieces; }
|
||||
int pieceLengthAt(int pieceIndex) const;
|
||||
inline qint32 pieceCount() const { return numPieces; }
|
||||
qint32 pieceLengthAt(qint32 pieceIndex) const;
|
||||
|
||||
QBitArray completedPieces() const;
|
||||
void setCompletedPieces(const QBitArray &pieces);
|
||||
@ -46,35 +46,35 @@ public slots:
|
||||
void startDataVerification();
|
||||
|
||||
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 verificationProgress(int percent);
|
||||
void verificationDone();
|
||||
void pieceVerified(int pieceIndex, bool verified);
|
||||
void pieceVerified(qint32 pieceIndex, bool verified);
|
||||
|
||||
protected:
|
||||
void run() override;
|
||||
|
||||
private slots:
|
||||
bool verifySinglePiece(int pieceIndex);
|
||||
bool verifySinglePiece(qint32 pieceIndex);
|
||||
void wakeUp();
|
||||
|
||||
private:
|
||||
bool generateFiles();
|
||||
QByteArray readBlock(int pieceIndex, int offset, int length);
|
||||
bool writeBlock(int pieceIndex, int offset, const QByteArray &data);
|
||||
QByteArray readBlock(qint32 pieceIndex, qint32 offset, qint32 length);
|
||||
bool writeBlock(qint32 pieceIndex, qint32 offset, const QByteArray &data);
|
||||
void verifyFileContents();
|
||||
|
||||
struct WriteRequest {
|
||||
int pieceIndex;
|
||||
int offset;
|
||||
qint32 pieceIndex;
|
||||
qint32 offset;
|
||||
QByteArray data;
|
||||
};
|
||||
struct ReadRequest {
|
||||
int pieceIndex;
|
||||
int offset;
|
||||
int length;
|
||||
int id;
|
||||
qint32 pieceIndex;
|
||||
qint32 offset;
|
||||
qint32 length;
|
||||
qint32 id;
|
||||
};
|
||||
|
||||
QString errString;
|
||||
|
@ -17,7 +17,7 @@ struct MetaInfoSingleFile
|
||||
qint64 length;
|
||||
QByteArray md5sum;
|
||||
QString name;
|
||||
int pieceLength;
|
||||
qint32 pieceLength;
|
||||
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
|
||||
// the handshake.
|
||||
void PeerWireClient::initialize(const QByteArray &infoHash, int pieceCount)
|
||||
void PeerWireClient::initialize(const QByteArray &infoHash, qint32 pieceCount)
|
||||
{
|
||||
this->infoHash = infoHash;
|
||||
peerPieces.resize(pieceCount);
|
||||
@ -142,7 +142,7 @@ void PeerWireClient::sendNotInterested()
|
||||
|
||||
// Sends a piece notification / a "have" message, informing the peer
|
||||
// that we have just downloaded a new piece.
|
||||
void PeerWireClient::sendPieceNotification(int piece)
|
||||
void PeerWireClient::sendPieceNotification(qint32 piece)
|
||||
{
|
||||
if (!sentHandShake)
|
||||
sendHandShake();
|
||||
@ -183,7 +183,7 @@ void PeerWireClient::sendPieceList(const QBitArray &bitField)
|
||||
}
|
||||
|
||||
// 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};
|
||||
qToBigEndian(13, &message[0]);
|
||||
@ -206,7 +206,7 @@ void PeerWireClient::requestBlock(int piece, int offset, int length)
|
||||
}
|
||||
|
||||
// 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};
|
||||
qToBigEndian(13, &message[0]);
|
||||
@ -222,7 +222,7 @@ void PeerWireClient::cancelRequest(int piece, int offset, int length)
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
@ -516,7 +516,7 @@ void PeerWireClient::processIncomingData()
|
||||
for (int i = 1; i < packet.size(); ++i) {
|
||||
for (int bit = 0; bit < 8; ++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()) {
|
||||
// Occasionally, broken clients claim to have
|
||||
// pieces whose index is outside the valid range.
|
||||
@ -534,12 +534,12 @@ void PeerWireClient::processIncomingData()
|
||||
quint32 index = qFromBigEndian<quint32>(&packet.data()[1]);
|
||||
quint32 begin = qFromBigEndian<quint32>(&packet.data()[5]);
|
||||
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
||||
emit blockRequested(int(index), int(begin), int(length));
|
||||
emit blockRequested(qint32(index), qint32(begin), qint32(length));
|
||||
break;
|
||||
}
|
||||
case PiecePacket: {
|
||||
int index = int(qFromBigEndian<quint32>(&packet.data()[1]));
|
||||
int begin = int(qFromBigEndian<quint32>(&packet.data()[5]));
|
||||
qint32 index = qint32(qFromBigEndian<quint32>(&packet.data()[1]));
|
||||
qint32 begin = qint32(qFromBigEndian<quint32>(&packet.data()[5]));
|
||||
|
||||
incoming.removeAll(TorrentBlock(index, begin, packet.size() - 9));
|
||||
|
||||
@ -560,9 +560,9 @@ void PeerWireClient::processIncomingData()
|
||||
quint32 length = qFromBigEndian<quint32>(&packet.data()[9]);
|
||||
for (int i = 0; i < pendingBlocks.size(); ++i) {
|
||||
const BlockInfo &blockInfo = pendingBlocks.at(i);
|
||||
if (blockInfo.pieceIndex == int(index)
|
||||
&& blockInfo.offset == int(begin)
|
||||
&& blockInfo.length == int(length)) {
|
||||
if (blockInfo.pieceIndex == qint32(index)
|
||||
&& blockInfo.offset == qint32(begin)
|
||||
&& blockInfo.length == qint32(length)) {
|
||||
pendingBlocks.removeAt(i);
|
||||
break;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ class TorrentPeer;
|
||||
|
||||
struct TorrentBlock
|
||||
{
|
||||
inline TorrentBlock(int p, int o, int l)
|
||||
inline TorrentBlock(qint32 p, qint32 o, qint32 l)
|
||||
: pieceIndex(p), offset(o), length(l)
|
||||
{
|
||||
}
|
||||
@ -27,9 +27,9 @@ struct TorrentBlock
|
||||
&& length == other.length;
|
||||
}
|
||||
|
||||
int pieceIndex;
|
||||
int offset;
|
||||
int length;
|
||||
qint32 pieceIndex;
|
||||
qint32 offset;
|
||||
qint32 length;
|
||||
};
|
||||
|
||||
class PeerWireClient : public QTcpSocket
|
||||
|
@ -70,9 +70,9 @@ public:
|
||||
int uploadScheduleTimer;
|
||||
|
||||
// Pieces
|
||||
QMap<int, PeerWireClient *> readIds;
|
||||
QMap<qint32, PeerWireClient *> readIds;
|
||||
QMultiMap<PeerWireClient *, TorrentPiece *> payloads;
|
||||
QMap<int, TorrentPiece *> pendingPieces;
|
||||
QMap<qint32, TorrentPiece *> pendingPieces;
|
||||
QBitArray completedPieces;
|
||||
QBitArray incompletePieces;
|
||||
int pieceCount;
|
||||
@ -269,8 +269,8 @@ void TorrentClient::setDumpedState(const QByteArray &dumpedState)
|
||||
stream >> d->completedPieces;
|
||||
|
||||
while (!stream.atEnd()) {
|
||||
int index;
|
||||
int length;
|
||||
qint32 index;
|
||||
qint32 length;
|
||||
QBitArray completed;
|
||||
stream >> index >> length >> completed;
|
||||
if (stream.status() != QDataStream::Ok) {
|
||||
@ -298,7 +298,7 @@ QByteArray TorrentClient::dumpedState() const
|
||||
|
||||
// Save the state of all partially downloaded pieces into a format
|
||||
// suitable for storing in settings.
|
||||
QMap<int, TorrentPiece *>::ConstIterator it = d->pendingPieces.constBegin();
|
||||
auto it = d->pendingPieces.constBegin();
|
||||
while (it != d->pendingPieces.constEnd()) {
|
||||
TorrentPiece *piece = it.value();
|
||||
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
|
||||
// still exists; otherwise do nothing. This slot is called by the
|
||||
@ -516,7 +517,7 @@ void TorrentClient::fullVerificationDone()
|
||||
d->completedPieces = d->fileManager.completedPieces();
|
||||
d->incompletePieces.resize(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))
|
||||
d->incompletePieces.setBit(i);
|
||||
}
|
||||
@ -525,7 +526,7 @@ void TorrentClient::fullVerificationDone()
|
||||
|
||||
// If the checksums show that what the dumped state thought was
|
||||
// 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()) {
|
||||
if (d->completedPieces.testBit(it.key()))
|
||||
it = d->pendingPieces.erase(it);
|
||||
@ -555,7 +556,7 @@ void TorrentClient::fullVerificationDone()
|
||||
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);
|
||||
|
||||
@ -583,7 +584,7 @@ void TorrentClient::pieceVerified(int pieceIndex, bool ok)
|
||||
if (!peer->interesting)
|
||||
continue;
|
||||
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)) {
|
||||
interesting = true;
|
||||
break;
|
||||
@ -901,8 +902,8 @@ void TorrentClient::peerPiecesAvailable(const QBitArray &pieces)
|
||||
// Check for interesting pieces, and tell the peer whether we are
|
||||
// interested or not.
|
||||
bool interested = false;
|
||||
int piecesSize = pieces.size();
|
||||
for (int pieceIndex = 0; pieceIndex < piecesSize; ++pieceIndex) {
|
||||
qint32 piecesSize = pieces.size();
|
||||
for (qint32 pieceIndex = 0; pieceIndex < piecesSize; ++pieceIndex) {
|
||||
if (!pieces.testBit(pieceIndex))
|
||||
continue;
|
||||
if (!d->completedPieces.testBit(pieceIndex)) {
|
||||
@ -914,7 +915,7 @@ void TorrentClient::peerPiecesAvailable(const QBitArray &pieces)
|
||||
}
|
||||
|
||||
QMultiMap<PeerWireClient *, TorrentPiece *>::Iterator it = d->payloads.find(client);
|
||||
int inProgress = 0;
|
||||
qint32 inProgress = 0;
|
||||
while (it != d->payloads.end() && it.key() == client) {
|
||||
if (it.value()->inProgress)
|
||||
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());
|
||||
|
||||
@ -949,7 +950,7 @@ void TorrentClient::peerRequestsBlock(int pieceIndex, int begin, int length)
|
||||
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());
|
||||
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.
|
||||
int blockBit = begin / BlockSize;
|
||||
qint32 blockBit = begin / BlockSize;
|
||||
TorrentPiece *piece = d->pendingPieces.value(pieceIndex);
|
||||
if (!piece || piece->completedBlocks.testBit(blockBit)) {
|
||||
// Discard blocks that we already have, and fill up the pipeline.
|
||||
@ -1026,7 +1027,7 @@ void TorrentClient::peerWireBytesReceived(qint64 size)
|
||||
emit dataSent(size);
|
||||
}
|
||||
|
||||
int TorrentClient::blocksLeftForPiece(const TorrentPiece *piece) const
|
||||
qint32 TorrentClient::blocksLeftForPiece(const TorrentPiece *piece) const
|
||||
{
|
||||
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
|
||||
// many blocks have been requested.
|
||||
QList<int> currentPieces;
|
||||
QList<qint32> currentPieces;
|
||||
bool somePiecesAreNotInProgress = false;
|
||||
TorrentPiece *lastPendingPiece = nullptr;
|
||||
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
|
||||
// clients that are currently uploading (more than 1.0KB/s).
|
||||
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()) {
|
||||
if (it.value()->inProgress)
|
||||
incompletePiecesAvailableToClient.clearBit(it.key());
|
||||
@ -1176,7 +1177,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
|
||||
incompletePiecesAvailableToClient &= client->availablePieces();
|
||||
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
// recovered, and if so, pick a random one of them.
|
||||
QList<TorrentPiece *> partialPieces;
|
||||
QMap<int, TorrentPiece *>::ConstIterator it = d->pendingPieces.constBegin();
|
||||
auto it = d->pendingPieces.constBegin();
|
||||
while (it != d->pendingPieces.constEnd()) {
|
||||
TorrentPiece *tmp = it.value();
|
||||
if (incompletePiecesAvailableToClient.testBit(it.key())) {
|
||||
|
Loading…
Reference in New Issue
Block a user