pub struct Peer {
pub last_heard: Instant,
pub last_sent: Instant,
pub remote_address: SocketAddr,
/* private fields */
}Expand description
Represents a remote peer in the network. Tracks network quality, processes packets, and manages connection state.
Fields§
§last_heard: InstantLast time we received a packet from this client
last_sent: InstantLast time we sent a packet to this client
remote_address: SocketAddrThe address of the remote endpoint
Implementations§
Source§impl Peer
impl Peer
Sourcepub fn process_command_packet(
&mut self,
data: &[u8],
time: Instant,
) -> Result<IncomingPackets>
pub fn process_command_packet( &mut self, data: &[u8], time: Instant, ) -> Result<IncomingPackets>
Decodes and processes an incoming command packet.
This is the command-based alternative to process_incoming.
Returns all user packets that resulted from processing the commands.
Validates CRC32 checksum if enabled, then decompresses if needed.
Sourcepub fn process_command(
&mut self,
command: &ProtocolCommand,
time: Instant,
) -> Result<IncomingPackets>
pub fn process_command( &mut self, command: &ProtocolCommand, time: Instant, ) -> Result<IncomingPackets>
Processes an incoming protocol command and returns resulting actions.
This is the command-based alternative to process_incoming.
Automatically enqueues response commands (ACK, Pong) when appropriate.
Source§impl Peer
impl Peer
Sourcepub fn encode_queued_commands(&mut self) -> Result<Vec<u8>>
pub fn encode_queued_commands(&mut self) -> Result<Vec<u8>>
Encodes all queued commands into a CommandPacket and returns the bytes. Drains the command queue in the process. Applies compression if enabled, then appends CRC32 checksum if enabled.
Sourcepub fn encode_queued_commands_bounded(
&mut self,
max_size: usize,
) -> Result<Option<Vec<u8>>>
pub fn encode_queued_commands_bounded( &mut self, max_size: usize, ) -> Result<Option<Vec<u8>>>
Encodes up to max_size bytes worth of queued commands into a single datagram.
- Returns
Ok(None)if there are no queued commands. - Returns
Ok(Some(bytes))wherebytes.len() <= max_sizewhen data was produced.
This prevents producing UDP payloads larger than the configured receive buffer (and typical MTUs), avoiding OS-level EMSGSIZE errors and IP fragmentation.
Source§impl Peer
impl Peer
Source§impl Peer
impl Peer
Sourcepub fn new(addr: SocketAddr, config: &Config, time: Instant) -> Peer
pub fn new(addr: SocketAddr, config: &Config, time: Instant) -> Peer
Creates and returns a new peer for the provided socket address.
Sourcepub fn record_send(&mut self) -> bool
pub fn record_send(&mut self) -> bool
Records that this connection has sent a packet. Returns whether the connection has become acknowledged because of this send.
Sourcepub fn record_recv(&mut self) -> bool
pub fn record_recv(&mut self) -> bool
Records that this connection has received a packet. Returns whether the connection has become acknowledged because of this receive.
Sourcepub fn is_established(&self) -> bool
pub fn is_established(&self) -> bool
Returns if the connection has been established
Sourcepub fn disconnect(&mut self)
pub fn disconnect(&mut self)
Initiates graceful disconnect
Sourcepub fn initiate_connect(&mut self)
pub fn initiate_connect(&mut self)
Initiates a connection handshake by sending CONNECT command (step 1 of 3). Should be called when transitioning from Idle to Connecting.
Sourcepub fn packets_in_flight(&self) -> u16
pub fn packets_in_flight(&self) -> u16
Returns the current number of not yet acknowledged packets
Sourcepub fn last_heard(&self, time: Instant) -> Duration
pub fn last_heard(&self, time: Instant) -> Duration
Returns a Duration representing the interval since we last heard from the client
Sourcepub fn last_sent(&self, time: Instant) -> Duration
pub fn last_sent(&self, time: Instant) -> Duration
Returns a Duration representing the interval since we last sent to the client
Sourcepub fn update_throttle(&mut self, time: Instant) -> bool
pub fn update_throttle(&mut self, time: Instant) -> bool
Updates the congestion throttle based on current network conditions.
Sourcepub fn gather_dropped_packets(&mut self) -> Vec<SentPacket>
pub fn gather_dropped_packets(&mut self) -> Vec<SentPacket>
Gathers dropped packets from the acknowledgment handler.
Sourcepub fn statistics(&self) -> &PeerStatistics
pub fn statistics(&self) -> &PeerStatistics
Returns a reference to the peer’s statistics.
Sourcepub fn statistics_mut(&mut self) -> &mut PeerStatistics
pub fn statistics_mut(&mut self) -> &mut PeerStatistics
Returns a mutable reference to the peer’s statistics.
Sourcepub fn current_fragment_size(&self) -> u16
pub fn current_fragment_size(&self) -> u16
Returns current per-peer fragment size in bytes.
Sourcepub fn handle_pmtu(&mut self, time: Instant)
pub fn handle_pmtu(&mut self, time: Instant)
Handles PMTU probing state machine (enqueue probes, process timeouts).
Sourcepub fn window_size(&self) -> u32
pub fn window_size(&self) -> u32
Returns the current window size (in packets).
Sourcepub fn reliable_data_in_transit(&self) -> u32
pub fn reliable_data_in_transit(&self) -> u32
Returns the amount of reliable data currently in transit (in bytes).
Sourcepub fn set_window_size(&mut self, window_size: u32)
pub fn set_window_size(&mut self, window_size: u32)
Sets the window size (for negotiation during handshake).
Sourcepub fn record_reliable_data_sent(&mut self, data_size: u32)
pub fn record_reliable_data_sent(&mut self, data_size: u32)
Records reliable data being sent (adds to in-transit counter).
Sourcepub fn record_reliable_data_acked(&mut self, data_size: u32)
pub fn record_reliable_data_acked(&mut self, data_size: u32)
Records reliable data being acknowledged (removes from in-transit counter).
Sourcepub fn can_send_reliable(&self) -> bool
pub fn can_send_reliable(&self) -> bool
Checks if we can send more data based on window-based flow control. Returns true if we have room in the window.
Sourcepub fn adjust_window_size(&mut self)
pub fn adjust_window_size(&mut self)
Dynamically adjusts the window size based on network conditions. Called periodically to adapt to changing network conditions.
Sourcepub fn update_bandwidth_window(&mut self, time: Instant) -> bool
pub fn update_bandwidth_window(&mut self, time: Instant) -> bool
Updates bandwidth tracking window, resetting counters if window expired. Returns true if the window was reset.
Sourcepub fn record_bytes_sent(&mut self, bytes: u32)
pub fn record_bytes_sent(&mut self, bytes: u32)
Records bytes sent for bandwidth tracking.
Sourcepub fn record_bytes_received(&mut self, bytes: u32)
pub fn record_bytes_received(&mut self, bytes: u32)
Records bytes received for bandwidth tracking.
Sourcepub fn can_send_within_bandwidth(&self) -> bool
pub fn can_send_within_bandwidth(&self) -> bool
Checks if we can send based on outgoing bandwidth limit. Returns true if we’re under the limit or if throttling is disabled (limit == 0).
Sourcepub fn bandwidth_utilization(&self) -> f32
pub fn bandwidth_utilization(&self) -> f32
Returns current bandwidth utilization (0.0 to 1.0+). Returns 0.0 if bandwidth limiting is disabled.
Sourcepub fn can_receive_within_bandwidth(&self) -> bool
pub fn can_receive_within_bandwidth(&self) -> bool
Checks if we can receive based on incoming bandwidth limit. Returns true if we’re under the limit or if throttling is disabled (limit == 0).
Sourcepub fn incoming_bandwidth_utilization(&self) -> f32
pub fn incoming_bandwidth_utilization(&self) -> f32
Returns current incoming bandwidth utilization (0.0 to 1.0+). Returns 0.0 if bandwidth limiting is disabled.
Sourcepub fn next_unsequenced_group(&mut self) -> u16
pub fn next_unsequenced_group(&mut self) -> u16
Gets the next outgoing unsequenced group ID and increments the counter.
Sourcepub fn is_unsequenced_duplicate(&self, group: u16) -> bool
pub fn is_unsequenced_duplicate(&self, group: u16) -> bool
Checks if an incoming unsequenced group is a duplicate or within the window. Returns true if this is a duplicate (already received), false if new.
Sourcepub fn mark_unsequenced_received(&mut self, group: u16)
pub fn mark_unsequenced_received(&mut self, group: u16)
Marks an incoming unsequenced group as received in the window. Advances the window base if necessary.
Sourcepub fn cleanup_stale_fragments(&mut self, time: Instant)
pub fn cleanup_stale_fragments(&mut self, time: Instant)
Cleans up stale fragment buffers that haven’t been completed within the timeout period. This prevents memory leaks from incomplete fragments (e.g., due to packet loss or malicious behavior).
Call this periodically (e.g., once per second) to prevent accumulation of stale buffers. Default timeout is 5 seconds after the first fragment is received.
Sourcepub fn enqueue_command(&mut self, command: ProtocolCommand) -> bool
pub fn enqueue_command(&mut self, command: ProtocolCommand) -> bool
Enqueues a protocol command for batching. Returns true if the queue should be flushed (reached max size). Returns false and drops the command if it would exceed max_waiting_data limit.
Sourcepub fn enqueue_ack_command(&mut self, sent_time: Option<u32>)
pub fn enqueue_ack_command(&mut self, sent_time: Option<u32>)
Generates and enqueues an Acknowledge command based on current state. This should be called after receiving reliable packets to send ACKs back.
Sourcepub fn enqueue_ping_command(&mut self, timestamp: u32)
pub fn enqueue_ping_command(&mut self, timestamp: u32)
Generates and enqueues a Ping command with the current timestamp.
Sourcepub fn enqueue_pong_command(&mut self, timestamp: u32)
pub fn enqueue_pong_command(&mut self, timestamp: u32)
Generates and enqueues a Pong command in response to a Ping.
Sourcepub fn drain_commands(&mut self) -> impl Iterator<Item = ProtocolCommand> + '_
pub fn drain_commands(&mut self) -> impl Iterator<Item = ProtocolCommand> + '_
Drains all pending commands from the queue. Resets the total_waiting_data counter since commands are being sent.
Sourcepub fn queued_commands_count(&self) -> usize
pub fn queued_commands_count(&self) -> usize
Returns the number of queued commands.
Sourcepub fn has_queued_commands(&self) -> bool
pub fn has_queued_commands(&self) -> bool
Returns true if the command queue is empty.