From 1d47cf48700f8c9632066c261b6dc25f807ca1b5 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Thu, 20 Mar 2025 02:48:52 +0100 Subject: [PATCH] Updates client streaming to use new piece validator interface --- codex/rest/api.nim | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/codex/rest/api.nim b/codex/rest/api.nim index 76e2cc32..cb1403d0 100644 --- a/codex/rest/api.nim +++ b/codex/rest/api.nim @@ -196,23 +196,18 @@ proc retrieveInfoHash( let stream = await node.streamTorrent(torrentManifest, codexManifest, torrentPieceValidator) - let pieceIter = torrentPieceValidator.getNewPieceIterator() + while not stream.atEof: + trace "Waiting for piece..." + let pieceIndex = await torrentPieceValidator.waitForNextPiece() - var pieceIndex = 0 - - while not pieceIter.finished and not stream.atEof: - trace "Waiting for piece", pieceIndex - if not (await torrentPieceValidator.waitForNextPiece()): - warn "No more torrent pieces expected. TorrentPieceValidator out of sync" + if -1 == pieceIndex: + warn "No more torrent pieces expected. TorrentPieceValidator might be out of sync!" break trace "Got piece", pieceIndex - inc pieceIndex let blocksPerPieceIter = torrentPieceValidator.getNewBlocksPerPieceIterator() - while not stream.atEof: - if blocksPerPieceIter.finished: - break + while not blocksPerPieceIter.finished and not stream.atEof: var buff = newSeqUninitialized[byte](BitTorrentBlockSize.int) # wait for the next the piece to prefetch let len = await stream.readOnce(addr buff[0], buff.len)