diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index 1bd0879..70970c3 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -194,10 +194,6 @@ impl ChunkTracker { .filter_map(|id| self.lengths.validate_piece_index(id)) } - pub(crate) fn is_piece_queued(&self, id: ValidPieceIndex) -> bool { - self.queue_pieces[id.get() as usize] - } - pub(crate) fn is_piece_have(&self, id: ValidPieceIndex) -> bool { self.have[id.get() as usize] } diff --git a/crates/librqbit/src/torrent_state/live/mod.rs b/crates/librqbit/src/torrent_state/live/mod.rs index cc0b51e..42af1e7 100644 --- a/crates/librqbit/src/torrent_state/live/mod.rs +++ b/crates/librqbit/src/torrent_state/live/mod.rs @@ -710,7 +710,9 @@ impl TorrentStateLive { self.streams.wake_streams_on_piece_completed(id); if self.is_finished() { - info!("torrent finished downloading"); + if self.lock_read("chunks").get_chunks()?.get_selected_pieces()[id.get_usize()] { + info!("torrent finished downloading"); + } self.finished_notify.notify_waiters(); if !self.has_active_streams_unfinished_files() { diff --git a/crates/librqbit/src/torrent_state/streaming.rs b/crates/librqbit/src/torrent_state/streaming.rs index 839fab0..692d0a4 100644 --- a/crates/librqbit/src/torrent_state/streaming.rs +++ b/crates/librqbit/src/torrent_state/streaming.rs @@ -304,16 +304,17 @@ impl ManagedTorrent { }) } - fn maybe_reconnect_needed_peers_for_file(&self, file_id: usize) { + fn maybe_reconnect_needed_peers_for_file(&self, file_id: usize) -> bool { // If we have the full file, don't bother. if let Ok(true) = self.with_opened_file(file_id, |f| f.approx_is_finished()) { - return; + return false; } self.with_state(|state| { if let crate::ManagedTorrentState::Live(l) = &state { l.reconnect_all_not_needed_peers(); } - }) + }); + true } pub fn stream(self: Arc, file_id: usize) -> anyhow::Result { @@ -331,7 +332,9 @@ impl ManagedTorrent { file_torrent_abs_offset: fd_offset, torrent: self, }; - s.torrent.maybe_reconnect_needed_peers_for_file(file_id); + if s.torrent.maybe_reconnect_needed_peers_for_file(file_id) { + s.torrent.with_opened_file(file_id, |f| f.reopen(false))??; + } streams.streams.insert( s.stream_id, StreamState { diff --git a/crates/librqbit_core/src/lengths.rs b/crates/librqbit_core/src/lengths.rs index 96e2d73..80c1204 100644 --- a/crates/librqbit_core/src/lengths.rs +++ b/crates/librqbit_core/src/lengths.rs @@ -67,6 +67,9 @@ impl ValidPieceIndex { pub const fn get(&self) -> u32 { self.0 } + pub const fn get_usize(&self) -> usize { + self.0 as usize + } } impl Lengths {