Make Sleipnir input delivery loss tolerant

This commit is contained in:
Meta Vens 2026-07-17 10:36:10 +01:00
commit 9af2edb8ca
6 changed files with 409 additions and 66 deletions

View file

@ -0,0 +1,18 @@
use anyhow::{Context, Result};
use cultmesh_rs::CultMesh;
use odin_core::{EVE_PROVIDER_ADVERTISEMENT_SCHEMA, OdinDocuments};
use std::{env, path::PathBuf};
fn main() -> Result<()> {
let store = PathBuf::from(env::args().nth(1).context("usage: dump_provider_catalog <store>")?);
let node = CultMesh::create_node(&store, OdinDocuments, Default::default())
.with_context(|| format!("opening {}", store.display()))?;
for envelope in node.cache().snapshot() {
if envelope.schema_id.as_deref() != Some(EVE_PROVIDER_ADVERTISEMENT_SCHEMA) {
continue;
}
let value: serde_json::Value = rmp_serde::from_slice(&envelope.payload)?;
println!("key={} value={}", envelope.key, serde_json::to_string(&value)?);
}
Ok(())
}

View file

@ -0,0 +1,55 @@
#[cfg(windows)]
fn main() -> anyhow::Result<()> {
use anyhow::Context;
use std::{env, thread, time::Duration};
let count = env::args()
.nth(1)
.and_then(|value| value.parse::<u32>().ok())
.unwrap_or(100);
let pressed_ms = env::args()
.nth(2)
.and_then(|value| value.parse::<u64>().ok())
.unwrap_or(8);
let gap_ms = env::args()
.nth(3)
.and_then(|value| value.parse::<u64>().ok())
.unwrap_or(92);
let client = vigem_client::Client::connect().context("connecting to ViGEmBus")?;
let mut target =
vigem_client::Xbox360Wired::new(client, vigem_client::TargetId::XBOX360_WIRED);
target.plugin().context("plugging quick-tap fixture")?;
target.wait_ready().context("waiting for quick-tap fixture")?;
let neutral = vigem_client::XGamepad::default();
let pressed = vigem_client::XGamepad {
buttons: vigem_client::XButtons::from(vigem_client::XButtons::A),
..Default::default()
};
target.update(&neutral).context("neutralizing fixture")?;
println!("fixture_ready count={count} pressed_ms={pressed_ms} gap_ms={gap_ms}");
// Leave enough time for Muninn's dynamic XInput discovery and Sleipnir's
// subscription handshake before the first non-replaceable edge.
thread::sleep(Duration::from_secs(15));
for sequence in 1..=count {
target.update(&pressed).context("pressing fixture A")?;
thread::sleep(Duration::from_millis(pressed_ms));
target.update(&neutral).context("releasing fixture A")?;
println!("fixture_tap sequence={sequence}");
thread::sleep(Duration::from_millis(gap_ms));
}
thread::sleep(Duration::from_secs(3));
target.update(&neutral).context("final fixture neutral")?;
target.unplug().context("unplugging quick-tap fixture")?;
println!("fixture_complete count={count}");
Ok(())
}
#[cfg(not(windows))]
fn main() {
eprintln!("vigem_quick_tap_fixture is Windows-only");
std::process::exit(2);
}

View file

@ -0,0 +1,39 @@
use anyhow::{Context, Result, anyhow};
use cultmesh_rs::CultMesh;
use odin_core::{OdinDocuments, SleipnirInputMappingRecord};
use serde_json::json;
use std::{env, path::PathBuf};
fn required_arg(name: &str) -> Result<String> {
let prefix = format!("--{name}=");
env::args()
.find_map(|arg| arg.strip_prefix(&prefix).map(ToString::to_string))
.filter(|value| !value.trim().is_empty())
.ok_or_else(|| anyhow!("missing {prefix}<value>"))
}
fn main() -> Result<()> {
let store = PathBuf::from(required_arg("store")?);
let provider_id = required_arg("provider-id")?;
let device_filter = required_arg("device-filter")?;
let stream_id = required_arg("stream-id")?;
let updated_at = required_arg("updated-at")?;
let mut node = CultMesh::create_node(&store, OdinDocuments, Default::default())
.with_context(|| format!("opening {}", store.display()))?;
let record = SleipnirInputMappingRecord {
provider_id: provider_id.clone(),
enabled: true,
device_filter,
stream_id,
presentation: "xbox360".to_string(),
axis_map: json!({}),
button_map: json!({}),
pending_learn: json!({}),
updated_at,
source: "sleipnir-input-field-fixture".to_string(),
};
node.put(&provider_id, &record)?;
node.flush()?;
println!("wrote enabled Sleipnir input mapping for {provider_id}");
Ok(())
}

View file

@ -12,11 +12,12 @@ use odin_core::{
IdunnDaemonHealthRecord, MUNINN_HID_CONTROLLER_STATE_SCHEMA, MuninnHidControllerStateRecord,
OdinDocuments, OdinEndpointQuery, SleipnirInputMappingRecord, discover_provider_endpoints,
};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::env;
use std::fs;
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
use std::path::PathBuf;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};
@ -27,6 +28,11 @@ const IDUNN_HEALTH_RUDP_CONNECTION_ID: u32 = 0x1d0d_0001;
const CULTNET_RUDP_PROTOCOL_ID: &str = "cultnet.transport.rudp.v0";
const INPUT_STALE_NEUTRAL_AFTER: Duration = Duration::from_millis(1_500);
const INPUT_RECEIVE_DRAIN_BUDGET: Duration = Duration::from_millis(2);
const INPUT_RECEIVE_MAX_STATE_RECORDS: usize = 4;
// Hold each semantic edge across one 60 Hz consumer frame plus scheduler
// phase margin. ViGEm can accept faster updates, but an XInput game sampling
// once per frame cannot observe an 8 ms press/release pair reliably.
const INPUT_EDGE_MINIMUM_VISIBILITY: Duration = Duration::from_millis(24);
const INPUT_STREAM_RECONNECT_AFTER: Duration = Duration::from_secs(3);
const DEFAULT_STICK_DEADZONE: f32 = 0.12;
const HID_RUDP_MAX_FRAGMENT_BYTES: u32 = 1_200;
@ -433,8 +439,11 @@ fn main() -> Result<()> {
publish_sleipnir_runtime_surface(&mut node, &options, &initial_state, true)?;
let mut last_idunn_health_attempt_at = Instant::now() - Duration::from_secs(60);
publish_idunn_health_if_configured(&options, &initial_state, &mut last_idunn_health_attempt_at);
let runtime_publisher = start_runtime_publisher(options.clone())?;
let mut semantic_cursor = HidSemanticCursor::default();
let mut ready_edges = VecDeque::<HidButtonEdge>::new();
let mut last_edge_actuation_at: Option<Instant> = None;
let mut ignored_stream_frames = 0u64;
let mut last_surface_publish_at = Instant::now() - Duration::from_secs(60);
let mut last_odin_catalog_pull_at = Instant::now() - Duration::from_secs(60);
@ -442,6 +451,7 @@ fn main() -> Result<()> {
let mut recent_input_latency: Option<SleipnirInputLatencySnapshot> = None;
let mut output_is_neutral = true;
let mut last_output_frame_at: Option<Instant> = None;
let mut last_slow_input_trace_at = Instant::now() - Duration::from_secs(60);
loop {
let input_stream_needs_discovery = rudp_stream
.as_ref()
@ -451,6 +461,23 @@ fn main() -> Result<()> {
{
pull_odin_catalog_snapshot(discovery_node.as_mut().unwrap_or(&mut node), &options);
last_odin_catalog_pull_at = Instant::now();
if desired_mapping.enabled {
let rediscovered_endpoint = effective_muninn_hid_endpoint(
discovery_node.as_ref().unwrap_or(&node),
current_device_filter.as_deref(),
desired_mapping.stream_id.as_deref(),
options.trace,
);
if rediscovered_endpoint.is_some() {
drop(rudp_stream.take());
rudp_stream = create_rudp_stream(
options.rudp_bind,
rediscovered_endpoint,
options.trace,
)?;
last_rudp_discovery_attempt_at = Instant::now();
}
}
}
let next_desired_mapping =
read_desired_mapping_from_sources(&mut node, discovery_node.as_mut(), &options);
@ -480,6 +507,8 @@ fn main() -> Result<()> {
rudp_stream =
create_rudp_stream(options.rudp_bind, rediscovered_endpoint, options.trace)?;
semantic_cursor = HidSemanticCursor::default();
ready_edges.clear();
last_edge_actuation_at = None;
}
let mut last_applied_record: Option<MuninnHidControllerStateRecord> = None;
if let Some(stream) = rudp_stream.as_mut() {
@ -489,6 +518,8 @@ fn main() -> Result<()> {
output_is_neutral = true;
last_output_frame_at = Some(Instant::now());
semantic_cursor = HidSemanticCursor::default();
ready_edges.clear();
last_edge_actuation_at = None;
if options.trace {
eprintln!(
"Sleipnir neutralized virtual pad before recreating stale Muninn HID RUDP connection for filter={:?}",
@ -521,7 +552,7 @@ fn main() -> Result<()> {
for timed_record in records {
let epoch = timed_record.epoch;
let state_sequence = timed_record.state_sequence;
let record = timed_record.record;
let mut record = timed_record.record;
let timing = timed_record.timing;
if record_matches_filter(&record, current_device_filter.as_deref()) {
stream.last_frame_at = Some(Instant::now());
@ -533,16 +564,42 @@ fn main() -> Result<()> {
desired_mapping = updated_mapping;
}
}
let previous_epoch = semantic_cursor.epoch;
if semantic_cursor.accept_state(epoch, state_sequence) || options.once {
if previous_epoch != epoch {
ready_edges.clear();
last_edge_actuation_at = None;
}
if previous_epoch == epoch
&& let Some(previous) = recent_applied_record.as_ref()
{
// Ordered edges own button visibility after the epoch baseline.
// A newer replaceable snapshot may update axes immediately, but it
// must not collapse an admitted press/release pair before actuation.
record.buttons = previous.buttons.clone();
}
let state = map_record_to_virtual_pad(&record, &desired_mapping);
let axis_classified_at = Instant::now();
let output_frame_at = Instant::now();
if options.trace {
let receive_to_apply_us = timing
.frame_received_at
.elapsed()
.as_micros();
let trace_slow_frame = receive_to_apply_us >= 5_000
&& last_slow_input_trace_at.elapsed() >= Duration::from_secs(1);
if options.trace
&& (record.sequence <= 5
|| record.sequence % 120 == 0
|| trace_slow_frame)
{
if trace_slow_frame {
last_slow_input_trace_at = Instant::now();
}
eprintln!(
"Sleipnir applying fast HID frame device={} seq={} receive_to_apply_us={} lx={} ly={} lt={} rt={} buttons=[{}]",
record.device_id,
record.sequence,
timing.frame_received_at.elapsed().as_micros(),
receive_to_apply_us,
state.left_x,
state.left_y,
state.left_trigger,
@ -597,36 +654,49 @@ fn main() -> Result<()> {
}
let edges = std::mem::take(&mut stream.received_edges);
for edge in edges {
let edge_epoch = edge.epoch;
let edge_sequence = edge.edge_sequence;
let edge_device = edge.device_id.clone();
for applied in semantic_cursor.push_edge(edge) {
let Some(record) = last_applied_record
.as_mut()
.or(recent_applied_record.as_mut())
else {
continue;
};
if applied.pressed {
if !record.buttons.contains(&applied.button) {
record.buttons.push(applied.button.clone());
}
} else {
record.buttons.retain(|button| button != &applied.button);
ready_edges.push_back(applied);
}
}
if !ready_edges.is_empty()
&& last_edge_actuation_at.is_none_or(|last| {
last.elapsed() >= INPUT_EDGE_MINIMUM_VISIBILITY
})
&& let Some(applied) = ready_edges.pop_front()
&& let Some(record) = last_applied_record
.as_mut()
.or(recent_applied_record.as_mut())
{
if applied.pressed {
if !record.buttons.contains(&applied.button) {
record.buttons.push(applied.button.clone());
}
let state = map_record_to_virtual_pad(record, &desired_mapping);
backend.update(&state)?;
output_is_neutral = state == VirtualPadState::default();
last_output_frame_at = Some(Instant::now());
} else {
record.buttons.retain(|button| button != &applied.button);
}
if edge_epoch == semantic_cursor.epoch
&& edge_sequence <= semantic_cursor.edge_sequence
{
let ack = serde_json::json!({"epoch": edge_epoch, "device_id": edge_device, "edge_sequence": semantic_cursor.edge_sequence});
stream
.transport
.send("hid.edge.ack", serde_json::to_vec(&ack)?)?;
let state = map_record_to_virtual_pad(record, &desired_mapping);
backend.update(&state)?;
output_is_neutral = state == VirtualPadState::default();
last_output_frame_at = Some(Instant::now());
last_edge_actuation_at = Some(Instant::now());
if options.trace {
eprintln!(
"Sleipnir applied HID edge device={} edge_sequence={} button={} pressed={} queued={}",
applied.device_id,
applied.edge_sequence,
applied.button,
applied.pressed,
ready_edges.len()
);
}
let ack = serde_json::json!({
"epoch": applied.epoch,
"device_id": applied.device_id,
"edge_sequence": applied.edge_sequence
});
stream
.transport
.send("hid.edge.ack", serde_json::to_vec(&ack)?)?;
}
if desired_mapping.enabled
&& !output_is_neutral
@ -673,12 +743,7 @@ fn main() -> Result<()> {
recent_input_latency.as_ref(),
)
};
publish_sleipnir_runtime_surface(&mut node, &options, &runtime_state, true)?;
publish_idunn_health_if_configured(
&options,
&runtime_state,
&mut last_idunn_health_attempt_at,
);
let _ = runtime_publisher.try_send((runtime_state, true));
} else if options.trace && desired_mapping.enabled {
eprintln!("Sleipnir still has no Odin-discovered Muninn HID endpoint");
}
@ -708,17 +773,7 @@ fn main() -> Result<()> {
recent_input_latency.as_ref(),
)
};
publish_sleipnir_runtime_surface(
&mut node,
&options,
&runtime_state,
has_new_applied_record,
)?;
publish_idunn_health_if_configured(
&options,
&runtime_state,
&mut last_idunn_health_attempt_at,
);
let _ = runtime_publisher.try_send((runtime_state, has_new_applied_record));
last_surface_publish_at = Instant::now();
}
if options.once {
@ -729,6 +784,49 @@ fn main() -> Result<()> {
Ok(())
}
fn start_runtime_publisher(
options: Options,
) -> Result<mpsc::SyncSender<(SleipnirRuntimeState, bool)>> {
let (tx, rx) = mpsc::sync_channel::<(SleipnirRuntimeState, bool)>(1);
thread::Builder::new()
.name("sleipnir-runtime-publisher".to_string())
.spawn(move || {
let mut node = match CultMesh::create_node(
&options.store_path,
OdinDocuments,
CultMeshNodeOptions {
runtime_id: "sleipnir-runtime-publisher".to_string(),
pull_on_start: false,
},
) {
Ok(node) => node,
Err(error) => {
eprintln!("Sleipnir runtime publisher could not open CultMesh state: {error:#}");
return;
}
};
let mut last_idunn_health_attempt_at = Instant::now() - Duration::from_secs(60);
while let Ok((mut state, mut publish_remote)) = rx.recv() {
while let Ok((newer_state, newer_publish_remote)) = rx.try_recv() {
state = newer_state;
publish_remote |= newer_publish_remote;
}
if let Err(error) =
publish_sleipnir_runtime_surface(&mut node, &options, &state, publish_remote)
{
eprintln!("Sleipnir runtime publisher warning: {error:#}");
}
publish_idunn_health_if_configured(
&options,
&state,
&mut last_idunn_health_attempt_at,
);
}
})
.context("starting Sleipnir runtime publisher")?;
Ok(tx)
}
impl SleipnirRuntimeState {
fn from_runtime(
options: &Options,
@ -2234,8 +2332,8 @@ fn create_rudp_stream(
let socket = UdpSocket::bind(bind_address)
.with_context(|| format!("binding Sleipnir RUDP HID client at {bind_address}"))?;
socket
.set_read_timeout(Some(Duration::from_millis(1)))
.context("setting Sleipnir RUDP HID client timeout")?;
.set_nonblocking(true)
.context("setting Sleipnir RUDP HID client nonblocking")?;
if trace {
eprintln!(
"Sleipnir connecting to Muninn HID RUDP at {target} connection_id={SLEIPNIR_HID_RUDP_CONNECTION_ID:08x}"
@ -2262,7 +2360,10 @@ fn create_rudp_stream(
target: Some(target),
last_connect_attempt_at: Some(Instant::now()),
connected_logged: false,
last_frame_at: Some(Instant::now()),
// Absence of a selected hot-plug device is valid idle state. Only
// a stream that has delivered a selected frame can later become
// stale and justify connection recreation.
last_frame_at: None,
last_stale_log_at: None,
last_subscription: None,
last_subscription_at: None,
@ -2275,8 +2376,8 @@ fn create_rudp_stream(
let socket = UdpSocket::bind(bind)
.with_context(|| format!("binding Sleipnir RUDP HID stream at {bind}"))?;
socket
.set_read_timeout(Some(Duration::from_millis(1)))
.with_context(|| format!("setting Sleipnir RUDP HID stream {bind} timeout"))?;
.set_nonblocking(true)
.with_context(|| format!("setting Sleipnir RUDP HID stream {bind} nonblocking"))?;
if trace {
eprintln!(
"Sleipnir listening for fast HID RUDP on {bind} connection_id={SLEIPNIR_HID_RUDP_CONNECTION_ID:08x}"
@ -2404,14 +2505,25 @@ fn discover_muninn_hid_endpoint(
if let Some(endpoint) = discover_available_hid_endpoint(node, device_filter, stream_id) {
return Some(endpoint);
}
// Provider advertisements are map-shaped CultNet documents in the live
// Node runtime. Resolve the host-scoped endpoint from those raw envelopes
// even when the requested hot-plugged device is not yet listed.
let host_hint = stream_host_hint(stream_id);
if let Some(host) = host_hint.as_deref()
&& let Some(endpoint) = discover_provider_hid_endpoint_by_host(node, host)
{
return Some(endpoint);
}
let endpoints = discover_provider_endpoints(
node,
OdinEndpointQuery {
schema: Some(MUNINN_HID_CONTROLLER_STATE_SCHEMA),
transport_contains: Some("rudp"),
host_hint: host_hint.as_deref(),
device_filter,
// The provider route is host-scoped. Device selection belongs to
// the subscription sent after connecting, so hot-plugged devices
// must not need to pre-exist in the advertisement.
device_filter: None,
},
);
if trace && endpoints.is_empty() {
@ -2426,6 +2538,48 @@ fn discover_muninn_hid_endpoint(
.map(|endpoint| endpoint.address)
}
fn discover_provider_hid_endpoint_by_host(
node: &cultmesh_rs::CultMeshNode,
host: &str,
) -> Option<String> {
let matches_host = |value: &serde_json::Value| {
string_field(value, &["providerId", "provider_id"])
.is_some_and(|provider| provider.eq_ignore_ascii_case(&format!("muninn.telemetry.{host}")))
|| string_field(value, &["verseId", "verse_id"])
.is_some_and(|verse| verse.eq_ignore_ascii_case(&format!("{host}.local")))
};
for provider in node
.cache()
.get_all::<EveProviderAdvertisementRecord>()
.ok()
.into_iter()
.flatten()
{
if matches_host(&provider.value)
&& let Some(endpoint) =
matching_provider_advertised_hid_endpoint(&provider.value, None, None)
{
return Some(endpoint);
}
}
for envelope in node.cache().snapshot() {
if envelope.schema_id.as_deref() != Some(EVE_PROVIDER_ADVERTISEMENT_SCHEMA)
&& envelope.r#type != "gamecult.eve.provider_advertisement"
{
continue;
}
let Ok(value) = rmp_serde::from_slice::<serde_json::Value>(&envelope.payload) else {
continue;
};
if matches_host(&value)
&& let Some(endpoint) = matching_provider_advertised_hid_endpoint(&value, None, None)
{
return Some(endpoint);
}
}
None
}
fn discover_available_hid_endpoint(
node: &cultmesh_rs::CultMeshNode,
device_filter: Option<&str>,
@ -2510,20 +2664,15 @@ fn receive_rudp_records(stream: &mut ActiveRudpStream, trace: bool) -> Result<Ve
let mut empty_polls = 0usize;
let drain_started_at = Instant::now();
for _ in 0..64 {
if !records.is_empty() && drain_started_at.elapsed() >= INPUT_RECEIVE_DRAIN_BUDGET {
if records.len() >= INPUT_RECEIVE_MAX_STATE_RECORDS
|| (!records.is_empty() && drain_started_at.elapsed() >= INPUT_RECEIVE_DRAIN_BUDGET)
{
break;
}
match stream.transport.receive_once() {
Ok(Some(frame)) if frame.channel_id == "latest" || frame.channel_id == "hid" => {
empty_polls = 0;
let frame_received_at = Instant::now();
if trace {
eprintln!(
"Sleipnir received fast HID frame channel={} bytes={}",
frame.channel_id,
frame.payload.len()
);
}
let decoded = decode_latest_state_frame(&frame.payload);
let record = match decoded.as_ref() {
Some(frame) => frame.record.clone(),
@ -2572,7 +2721,9 @@ fn receive_rudp_records(stream: &mut ActiveRudpStream, trace: bool) -> Result<Ve
}
}
}
if let Err(error) = stream.transport.poll_resends() {
if stream.transport.connected()
&& let Err(error) = stream.transport.poll_resends()
{
if trace {
eprintln!("Sleipnir RUDP resend warning: {error:#}");
}
@ -3180,7 +3331,7 @@ mod tests {
}
#[test]
fn runtime_state_prefers_recent_frames_over_handshake_churn() {
fn runtime_state_does_not_claim_connected_before_first_frame_or_handshake() {
let (node, store_path) = test_node("runtime-fresh-frame");
let options = Options::parse(
["--store", store_path.to_str().unwrap(), "--dry-run"]
@ -3203,7 +3354,7 @@ mod tests {
None,
);
assert_eq!(state.stream_state, "connected");
assert_eq!(state.stream_state, "connecting");
let _ = fs::remove_file(store_path);
}
@ -3335,6 +3486,17 @@ mod tests {
.as_deref(),
Some("198.51.100.66:17888")
);
assert_eq!(
discover_muninn_hid_endpoint(
&node,
Some("hotplugged-xinput-1"),
Some("starfire:hotplugged-xinput-1:hid-controller-state"),
false,
)
.as_deref(),
Some("198.51.100.66:17888"),
"host-level provider routes must accept later device subscriptions"
);
let _ = fs::remove_file(store_path);
}

View file

@ -0,0 +1,2 @@
loss_basis_points = 500
max_scheduled = 4096

View file

@ -135,6 +135,7 @@ pub struct CultNetRudpSession {
next_fragment_id: u16,
connected: bool,
last_received_at_ms: Option<u64>,
last_received_sequence: Option<u32>,
highest_received_sequence: Option<u32>,
received_sequences: BTreeSet<u32>,
pending_reliable: BTreeMap<u32, PendingReliablePacket>,
@ -156,6 +157,7 @@ impl CultNetRudpSession {
next_fragment_id: 1,
connected: false,
last_received_at_ms: None,
last_received_sequence: None,
highest_received_sequence: None,
received_sequences: BTreeSet::new(),
pending_reliable: BTreeMap::new(),
@ -198,6 +200,7 @@ impl CultNetRudpSession {
self.next_fragment_id = 1;
self.connected = false;
self.last_received_at_ms = None;
self.last_received_sequence = None;
self.highest_received_sequence = None;
self.received_sequences.clear();
self.pending_reliable.clear();
@ -452,7 +455,11 @@ impl CultNetRudpSession {
}
pub fn create_ack(&mut self) -> CultNetRudpPacket {
let (ack, ack_mask) = self.ack_state();
// ACK the packet that actually caused this response. A retransmitted
// reliable packet can arrive more than 32 global sequence numbers
// behind replaceable realtime traffic; a highest-sequence-only ACK
// can never retire it once it falls outside that mask.
let (ack, ack_mask) = self.direct_ack_state();
CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Ack,
connection_id: self.connection_id,
@ -623,6 +630,7 @@ impl CultNetRudpSession {
}
fn remember_received(&mut self, sequence: u32) {
self.last_received_sequence = Some(sequence);
self.received_sequences.insert(sequence);
if self
.highest_received_sequence
@ -643,6 +651,23 @@ impl CultNetRudpSession {
(ack, ack_mask)
}
fn direct_ack_state(&self) -> (u32, u32) {
let last = self.last_received_sequence.unwrap_or(0);
let highest = self.highest_received_sequence.unwrap_or(last);
let ack = if highest.saturating_sub(last) > 32 {
last
} else {
highest
};
let mut ack_mask = 0_u32;
for bit in 0..32 {
if ack > bit && self.received_sequences.contains(&(ack - bit - 1)) {
ack_mask |= 1_u32 << bit;
}
}
(ack, ack_mask)
}
fn reassemble(
&mut self,
packet: &CultNetRudpPacket,
@ -1126,7 +1151,10 @@ impl CultNetRudpSocketTransportConnection {
});
self.stats.frames_received += 1;
}
if packet.packet_type == CultNetRudpPacketType::Accept || delivered_any {
// Reliable duplicates still need an ACK. Suppressing the ACK because
// deduplication produced no application frame creates an immortal
// resend loop after the original ACK is lost.
if packet.packet_type == CultNetRudpPacketType::Accept || delivered_any || packet.reliable {
let ack = self.session.create_ack();
self.send_packet(&ack)?;
}
@ -1480,6 +1508,12 @@ fn channel_send_options(channel_id: &str, now_ms: u64) -> CultNetRudpSendOptions
sequenced: false,
now_ms,
},
"hid.edge" | "hid.edge.ack" | "hid.subscribe" => CultNetRudpSendOptions {
reliable: true,
ordered: false,
sequenced: false,
now_ms,
},
"latest" => CultNetRudpSendOptions {
reliable: false,
ordered: false,
@ -1525,6 +1559,21 @@ fn packet_type_from_code(code: u8) -> Result<CultNetRudpPacketType> {
mod socket_transport_tests {
use super::*;
#[test]
fn hid_control_channels_are_reliable_while_latest_state_is_replaceable() {
for channel in ["hid.edge", "hid.edge.ack", "hid.subscribe"] {
let options = channel_send_options(channel, 7);
assert!(options.reliable, "{channel} must survive packet loss");
assert!(
!options.ordered,
"{channel} uses application sequence and must not head-of-line block behind latest state"
);
}
let latest = channel_send_options("latest", 7);
assert!(!latest.reliable);
assert!(latest.sequenced);
}
fn data_packet(sequence: u32) -> CultNetRudpPacket {
CultNetRudpPacket {
packet_type: CultNetRudpPacketType::Data,
@ -1632,4 +1681,22 @@ mod socket_transport_tests {
assert!(session.pending_reliable_sequences().is_empty());
Ok(())
}
#[test]
fn ack_directly_names_late_reliable_packet_outside_cumulative_window() -> Result<()> {
let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 7,
..CultNetRudpSessionOptions::default()
});
session.connected = true;
session.receive(&data_packet(100), 1)?;
let mut late = data_packet(10);
late.reliable = true;
session.receive(&late, 2)?;
let ack = session.create_ack();
assert_eq!(ack.ack, 10);
Ok(())
}
}