pub struct EndgameState { /* private fields */ }Expand description
Tracks duplicate requests during endgame mode.
In endgame mode (when <=5 pieces remain incomplete), we request the same block from multiple peers simultaneously to speed up completion. When any peer responds with the block data, we send Cancel messages to the other peers that also received the request for that block.
This struct maintains the mapping from block identifiers to the list of peers that were sent duplicate requests, enabling efficient cancellation on arrival.
Implementations§
Source§impl EndgameState
impl EndgameState
Sourcepub fn enter_endgame(&mut self)
pub fn enter_endgame(&mut self)
Enter endgame mode - enables duplicate request tracking
Sourcepub fn exit_endgame(&mut self)
pub fn exit_endgame(&mut self)
Exit endgame mode and clear all tracked requests
Sourcepub fn track_request(
&mut self,
piece: u32,
offset: u32,
len: u32,
peer_id: usize,
)
pub fn track_request( &mut self, piece: u32, offset: u32, len: u32, peer_id: usize, )
Register that a request was sent to a peer during endgame
This tracks which peers have pending requests for each block so we can cancel redundant requests when the first response arrives.
Sourcepub fn get_cancel_targets(
&self,
piece: u32,
offset: u32,
len: u32,
) -> Vec<usize>
pub fn get_cancel_targets( &self, piece: u32, offset: u32, len: u32, ) -> Vec<usize>
When a block arrives, find other peers that have pending requests for the same block
Returns the list of peer indices that should receive Cancel messages. Does NOT remove the entry (call remove_request after sending cancels).
Sourcepub fn remove_request(&mut self, piece: u32, offset: u32, len: u32)
pub fn remove_request(&mut self, piece: u32, offset: u32, len: u32)
Remove a tracked request after cancel or completion
Called after Cancel messages have been sent and the block is fully processed.
Sourcepub fn is_endgame_active(&self) -> bool
pub fn is_endgame_active(&self) -> bool
Check if endgame mode is currently active
Sourcepub fn tracked_count(&self) -> usize
pub fn tracked_count(&self) -> usize
Get the number of actively tracked duplicate requests (for debugging/metrics)