pub struct InputQueue { /* private fields */ }Expand description
Deduplicates input redundancy and bounds per-client simulation backlog.
Checked admission preserves the legacy bool methods’ ordering and bounds.
All TimedInput::shot_tick values (including a tick without a fire bit)
remain accepted when the embedded frame is valid; authority rewind policy
belongs to the consumer, not this queue. No protocol bytes are changed.
Receive controls independently of rendering or local menus. Pop only for an actual authoritative tick; a local overlay must not suspend network/replay progression. Queue metrics describe removal, not executed simulation work.
This supplied-input recipe recovers an older fire press while using newest movement. Hand the returned command and shot time to the authority’s fixed tick, then derive feedback from its accepted game events.
use ferravex_net::{
BUTTON_FIRE, InputAdmissionError, InputFrame, InputQueue,
InputQueueMetrics, TimedInput,
};
let mut queue = InputQueue::default();
queue.push_timed_checked(TimedInput {
input: InputFrame {
sequence: 40, yaw: 1.0, pitch: 0.25, buttons: BUTTON_FIRE,
..InputFrame::default()
},
shot_tick: Some(120),
}).unwrap();
let movement = InputFrame { sequence: 41, move_x: 1.0, ..InputFrame::default() };
queue.push_checked(movement).unwrap();
assert_eq!(queue.push_checked(movement), Err(InputAdmissionError::DuplicateNewest));
// At the next actual authority tick:
let command = queue.pop_latest_timed().unwrap();
assert_eq!(command.input.sequence, 41);
assert_eq!(command.input.move_x, 1.0);
assert_eq!((command.input.yaw, command.input.pitch), (1.0, 0.25));
assert_eq!(command.input.buttons, BUTTON_FIRE);
assert_eq!(command.shot_tick, Some(120));
let metrics = queue.metrics(); // Read-only copy for host diagnostics.
assert_eq!(metrics.returned_inputs, 1);
assert_eq!(metrics.coalesced_inputs, 1);
assert_eq!(metrics.rejected_duplicate_newest, 1);
// A round reset discards pending controls but keeps replay protection.
assert!(queue.push(InputFrame { sequence: 42, ..movement }));
queue.clear_pending();
assert_eq!(queue.metrics().discarded_inputs, 1);
assert_eq!(queue.push_checked(movement), Err(InputAdmissionError::OutOfOrder));
let previous_window = queue.reset_metrics();
assert_eq!(previous_window.accepted_inputs, 3);
assert_eq!(queue.metrics(), InputQueueMetrics::default());
assert!(!queue.push(InputFrame { sequence: 42, ..movement }));
// Only after the host establishes a new authenticated session:
queue.reset_session(); // Clears pending and watermark; retains metrics/storage.
assert!(queue.push(InputFrame::default()));Implementations§
Source§impl InputQueue
impl InputQueue
Sourcepub fn push(&mut self, input: InputFrame) -> bool
pub fn push(&mut self, input: InputFrame) -> bool
Legacy admission; true exactly when Self::push_checked succeeds.
Sourcepub fn push_timed(&mut self, timed: TimedInput) -> bool
pub fn push_timed(&mut self, timed: TimedInput) -> bool
Legacy timed admission; counts once through Self::push_timed_checked.
Sourcepub fn push_checked(
&mut self,
input: InputFrame,
) -> Result<(), InputAdmissionError>
pub fn push_checked( &mut self, input: InputFrame, ) -> Result<(), InputAdmissionError>
Append untimed controls or report the first rejection reason.
Equivalent to Self::push_timed_checked with no shot tick. See the
InputQueue recipe for admission, coalescing and reset scopes.
Sourcepub fn push_timed_checked(
&mut self,
timed: TimedInput,
) -> Result<(), InputAdmissionError>
pub fn push_timed_checked( &mut self, timed: TimedInput, ) -> Result<(), InputAdmissionError>
Append timed controls, validating input, capacity, then sequence ordering.
Failure preserves all pending values and the watermark, and increments exactly one saturating rejection counter. Only the embedded frame is validated: shot_tick is opaque, as in the existing wire protocol. Success appends without eviction and advances the accepted watermark.
Sourcepub fn metrics(&self) -> InputQueueMetrics
pub fn metrics(&self) -> InputQueueMetrics
Read a copy of queue-local counters without changing any state.
Sourcepub fn reset_metrics(&mut self) -> InputQueueMetrics
pub fn reset_metrics(&mut self) -> InputQueueMetrics
Begin a new metrics window and return the previous counters.
Pending inputs, allocated storage and the accepted sequence watermark are retained. Later removals can count inputs admitted before this reset.
Sourcepub fn clear_pending(&mut self)
pub fn clear_pending(&mut self)
Discard queued controls while retaining the sequence watermark and metrics.
Increments discarded_inputs by the number removed; retains storage. Suitable for an authority round reset, never a local menu pause.
Sourcepub fn reset_session(&mut self)
pub fn reset_session(&mut self)
Discard pending controls and forget the accepted sequence watermark.
Retains allocated storage and metrics (including these discards). Call reset_metrics separately if a new reporting window is wanted. Use only when the host establishes a new session; using this for a round/menu would allow delayed controls from the same session to re-enter.
Sourcepub fn pop(&mut self) -> Option<InputFrame>
pub fn pop(&mut self) -> Option<InputFrame>
Return the oldest input without its shot time, counting one returned entry.
Sourcepub fn pop_latest(&mut self) -> Option<InputFrame>
pub fn pop_latest(&mut self) -> Option<InputFrame>
Coalesce a burst to its newest movement/look sample without losing a queued action press. The authority still simulates exactly one fixed step. This prevents redundant commands recovered after loss from adding permanent control latency. Previously consumed commands cannot re-enter the queue.
Sourcepub fn pop_latest_timed(&mut self) -> Option<TimedInput>
pub fn pop_latest_timed(&mut self) -> Option<TimedInput>
A recovered older quick-fire press retains its original aim and shot time.
Returns one entry and counts all older entries as coalesced. The newest fire sample wins if present; otherwise the oldest queued fire sample wins. Empty pops change no counters. Removal never resets the watermark.