Merge pull request #76 from ikatson/fix-66

Fix for #66
This commit is contained in:
Igor Katson 2024-01-21 11:06:49 +00:00 committed by GitHub
commit 5ff88dcad2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 24 deletions

View file

@ -132,6 +132,10 @@ fn dummy_file() -> anyhow::Result<std::fs::File> {
.with_context(|| format!("error opening {}", DEVNULL)) .with_context(|| format!("error opening {}", DEVNULL))
} }
fn make_piece_bitfield(lengths: &Lengths) -> BF {
BF::from_vec(vec![0; lengths.piece_bitfield_bytes()])
}
pub(crate) struct TorrentStateLocked { pub(crate) struct TorrentStateLocked {
// What chunks we have and need. // What chunks we have and need.
// If this is None, the torrent was paused, and this live state is useless, and needs to be dropped. // If this is None, the torrent was paused, and this live state is useless, and needs to be dropped.
@ -429,10 +433,7 @@ impl TorrentStateLive {
addr: checked_peer.addr, addr: checked_peer.addr,
on_bitfield_notify: Default::default(), on_bitfield_notify: Default::default(),
unchoke_notify: Default::default(), unchoke_notify: Default::default(),
locked: RwLock::new(PeerHandlerLocked { locked: RwLock::new(PeerHandlerLocked { i_am_choked: true }),
i_am_choked: true,
previously_requested_pieces: BF::new(),
}),
requests_sem: Semaphore::new(0), requests_sem: Semaphore::new(0),
state: self.clone(), state: self.clone(),
tx, tx,
@ -493,10 +494,7 @@ impl TorrentStateLive {
addr, addr,
on_bitfield_notify: Default::default(), on_bitfield_notify: Default::default(),
unchoke_notify: Default::default(), unchoke_notify: Default::default(),
locked: RwLock::new(PeerHandlerLocked { locked: RwLock::new(PeerHandlerLocked { i_am_choked: true }),
i_am_choked: true,
previously_requested_pieces: BF::new(),
}),
requests_sem: Semaphore::new(0), requests_sem: Semaphore::new(0),
state: state.clone(), state: state.clone(),
tx, tx,
@ -780,10 +778,6 @@ impl TorrentStateLive {
struct PeerHandlerLocked { struct PeerHandlerLocked {
pub i_am_choked: bool, pub i_am_choked: bool,
// This is used to only request a piece from a peer once when stealing from others.
// So that you don't steal then re-steal the same piece in a loop.
pub previously_requested_pieces: BF,
} }
// All peer state that would never be used by other actors should pe put here. // All peer state that would never be used by other actors should pe put here.
@ -1117,8 +1111,7 @@ impl PeerHandler {
// If bitfield wasn't allocated yet, let's do it. Some clients start empty so they never // If bitfield wasn't allocated yet, let's do it. Some clients start empty so they never
// send bitfields. // send bitfields.
if live.bitfield.is_empty() { if live.bitfield.is_empty() {
live.bitfield = live.bitfield = make_piece_bitfield(&self.state.lengths);
BF::from_vec(vec![0; self.state.lengths.piece_bitfield_bytes()]);
} }
match live.bitfield.get_mut(have as usize) { match live.bitfield.get_mut(have as usize) {
Some(mut v) => *v = true, Some(mut v) => *v = true,
@ -1128,8 +1121,8 @@ impl PeerHandler {
} }
}; };
trace!("updated bitfield with have={}", have); trace!("updated bitfield with have={}", have);
self.on_bitfield_notify.notify_waiters();
}); });
self.on_bitfield_notify.notify_waiters();
} }
fn on_bitfield(&self, bitfield: ByteString) -> anyhow::Result<()> { fn on_bitfield(&self, bitfield: ByteString) -> anyhow::Result<()> {
@ -1140,7 +1133,6 @@ impl PeerHandler {
self.state.lengths.piece_bitfield_bytes(), self.state.lengths.piece_bitfield_bytes(),
); );
} }
self.locked.write().previously_requested_pieces = BF::from_vec(vec![0; bitfield.len()]);
self.state self.state
.peers .peers
.update_bitfield_from_vec(self.addr, bitfield.0); .update_bitfield_from_vec(self.addr, bitfield.0);
@ -1229,11 +1221,6 @@ impl PeerHandler {
} }
}; };
self.locked
.write()
.previously_requested_pieces
.set(next.get() as usize, true);
for chunk in self.state.lengths.iter_chunk_infos(next) { for chunk in self.state.lengths.iter_chunk_infos(next) {
let request = Request { let request = Request {
index: next.get(), index: next.get(),

View file

@ -1867,7 +1867,7 @@ dependencies = [
[[package]] [[package]]
name = "librqbit" name = "librqbit"
version = "5.4.0" version = "5.4.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
@ -1936,7 +1936,7 @@ version = "2.2.1"
[[package]] [[package]]
name = "librqbit-core" name = "librqbit-core"
version = "3.4.0" version = "3.5.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"directories", "directories",
@ -3022,7 +3022,7 @@ dependencies = [
[[package]] [[package]]
name = "rqbit-desktop" name = "rqbit-desktop"
version = "5.4.0" version = "5.4.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.21.5", "base64 0.21.5",