fix endless loop bug

This commit is contained in:
MaysWind 2016-07-04 22:57:05 +08:00
parent c9e6601607
commit 701d945371

View file

@ -132,6 +132,10 @@
var getPieceStatus = function (bitField, pieceCount) { var getPieceStatus = function (bitField, pieceCount) {
var pieces = []; var pieces = [];
for (var i = 0; i < pieceCount; i++) {
pieces.push(false);
}
if (!bitField) { if (!bitField) {
return pieces; return pieces;
} }
@ -145,8 +149,7 @@
var bit = (1 << (4 - j)); var bit = (1 << (4 - j));
var isCompleted = (bitSet & bit) == bit; var isCompleted = (bitSet & bit) == bit;
pieces.push(isCompleted); pieces[pieceIndex++] = isCompleted;
pieceIndex++;
if (pieceIndex >= pieceCount) { if (pieceIndex >= pieceCount) {
return pieces; return pieces;
@ -483,7 +486,7 @@
return getCombinedPieces(bitField, pieceCount); return getCombinedPieces(bitField, pieceCount);
}, },
estimateHealthPercentFromPeers: function (task, peers) { estimateHealthPercentFromPeers: function (task, peers) {
if (peers.length < 1) { if (!task || task.numPieces < 1 || peers.length < 1) {
return task.completePercent; return task.completePercent;
} }
@ -519,20 +522,22 @@
var totalCompletedPieceCount = 0; var totalCompletedPieceCount = 0;
while (true) { if (totalPieces.length > 0) {
var completed = true; while (true) {
var completed = true;
for (var i = 0; i < totalPieces.length; i++) { for (var i = 0; i < totalPieces.length; i++) {
if (totalPieces[i] > 0) { if (totalPieces[i] > 0) {
totalCompletedPieceCount++; totalCompletedPieceCount++;
totalPieces[i]--; totalPieces[i]--;
} else { } else {
completed = false; completed = false;
}
} }
}
if (!completed) { if (!completed) {
break; break;
}
} }
} }