Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TorrentDownloadController #1208

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,21 @@ class TorrentDownloadController(
private val footerSize: Long = headerSize,
private val possibleFooterSize: Long = headerSize,
) : SynchronizedObject() {
private val totalSize: Long = pieces.sumOf { it.size }
private val totalPieceSize: Long = pieces.sumOf { it.size }
private val pieceOffsetStart = with(pieces) { pieces.first().dataStartOffset }

private val footerPieces = pieces.dropWhile { it.dataLastOffset < totalSize - footerSize }
private val possibleFooterRange = pieces.dropWhile { it.dataLastOffset < totalSize - possibleFooterSize }
private val footerPieces = pieces.dropWhile { it.dataLastOffset < pieceOffsetStart + totalPieceSize - footerSize }
private val possibleFooterRange = pieces
.dropWhile { it.dataLastOffset < pieceOffsetStart + totalPieceSize - possibleFooterSize }
.let {
if (it.isEmpty()) IntRange.EMPTY
else it.first().pieceIndex..it.last().pieceIndex
}

private val lastIndex = pieces.pieceIndexOfFirst { it.dataLastOffset >= totalSize - footerSize } - 1
private val lastIndex = pieces
.pieceIndexOfFirst { it.dataLastOffset >= pieceOffsetStart + totalPieceSize - footerSize } - 1

private var currentWindowStart = 0
private var currentWindowStart = pieces.initialPieceIndex

// inclusive
private var currentWindowEnd = (currentWindowStart + windowSize - 1).coerceAtMost(lastIndex)
Expand All @@ -88,7 +91,7 @@ class TorrentDownloadController(
}

fun onTorrentResumed() = synchronized(this) {
onSeek(0)
onSeek(pieces.initialPieceIndex)
}

fun onSeek(pieceIndex: Int) = synchronized(this) {
Expand Down
Loading