Ack Cut 1d batch 2 (Rust): an Ack does not retire a Connect awaiting its Accept; the fresh attempt jumps by the receive window less one

While a Connect awaits its Accept, only an Accept the client honours retires it. An Ack that names the Connect (a server's reply to a repeat or a stale copy) no longer wedges the client with nothing to resend. The Connect payload lives on the session, so a fresh attempt can always be built. The fresh attempt uses the abandoned sequence plus the receive window less one (serial), so a late copy of the abandoned Connect stays inside the new one's stale window; only a session's first Connect draws at random.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 09:53:05 +02:00
commit 7b46b4c481
2 changed files with 161 additions and 23 deletions

View file

@ -120,17 +120,12 @@ impl Default for CultNetRudpSessionOptions {
}
}
/// A sequence drawn at random from [`floor`, 2^31). The Connect's sequence is
/// what tells a peer whether a Connect repeats one it already accepted or
/// starts a new session, so two sessions must not share one by default. At
/// least 2^31 sequences remain before the space is exhausted. A floor above
/// the range draws nothing new: the caller keeps its own sequence.
fn draw_sequence(floor: u32) -> u32 {
let floor = floor.max(1);
if floor >= 1_u32 << 31 {
return floor;
}
rand::random_range(floor..(1_u32 << 31))
/// A sequence drawn at random from [1, 2^31). The Connect's sequence is what
/// tells a peer whether a Connect repeats one it already accepted or starts a
/// new session, so two sessions must not share one by default. At least 2^31
/// sequences remain before the space is exhausted.
fn draw_sequence() -> u32 {
rand::random_range(1..(1_u32 << 31))
}
/// Whether `sequence` is at or before `mark` in serial order, within the
@ -227,6 +222,9 @@ pub struct CultNetRudpSession {
connect_sequence: Option<u32>,
/// A Connect this side sent is unanswered. Only then is an Accept honoured.
awaiting_accept: bool,
/// The payload of the Connect this side sent, kept so a fresh attempt can
/// always be built whatever became of the pending packet.
connect_payload: Vec<u8>,
/// When the unanswered Connect first went out; `due_resends` abandons it
/// for a fresh attempt once `RUDP_CONNECT_ATTEMPT_MS` has passed.
connect_started_at_ms: u64,
@ -268,13 +266,14 @@ impl CultNetRudpSession {
ended: false,
connect_sequence: None,
awaiting_accept: false,
connect_payload: Vec::new(),
connect_started_at_ms: 0,
connection_id: options.connection_id,
resend_delay_ms: options.resend_delay_ms,
max_pending_reliable_packets: options.max_pending_reliable_packets,
max_payload_bytes: None,
max_pending_fragment_sets: 64,
next_sequence: options.initial_sequence.unwrap_or_else(|| draw_sequence(1)),
next_sequence: options.initial_sequence.unwrap_or_else(draw_sequence),
next_sequenced_by_channel: BTreeMap::new(),
next_fragment_id: 1,
connected: false,
@ -470,6 +469,7 @@ impl CultNetRudpSession {
self.connect_sequence = Some(packet.sequence);
self.awaiting_accept = true;
self.connect_started_at_ms = now_ms;
self.connect_payload = packet.payload.clone();
self.track_reliable(packet.clone(), now_ms, None);
Ok(packet)
}
@ -670,7 +670,12 @@ impl CultNetRudpSession {
disconnect_reason: Vec::new(),
});
}
self.apply_acknowledgements(packet);
// While this side's Connect awaits its Accept, only the Accept it
// honours retires it: an Ack that names the Connect (a server's reply
// to a repeat or a stale copy) says the server did not start a session.
if honours_accept || !self.awaiting_accept {
self.apply_acknowledgements(packet);
}
self.purge_expired_reliable(now_ms);
let ready_to_send = self.promote_queued_reliable(now_ms);
self.last_received_at_ms = Some(now_ms);
@ -947,22 +952,26 @@ impl CultNetRudpSession {
}
/// A Connect unanswered for `RUDP_CONNECT_ATTEMPT_MS` is replaced, not
/// retransmitted further, by a Connect with a newly drawn sequence and the
/// same payload. Nothing was ever sent in an unanswered generation, so no
/// sequence issued so far is owed; the draw stays above them, so a frame
/// the peer still remembers from an earlier generation of this session
/// stays at or before the new Connect.
/// retransmitted further, by a Connect with the same payload and the
/// abandoned sequence plus the receive window less one. Nothing was ever
/// sent in an unanswered generation, so no sequence issued so far is owed.
/// The jump keeps a late copy of the abandoned Connect inside the new one's
/// stale window, so a server that took the new Connect answers the copy
/// with an Ack instead of restarting; and it leaves the stale window of
/// whatever generation the server holds, unless that generation sits
/// exactly at the jump, and then the next attempt leaves it. The first
/// Connect of a session is the only one drawn at random.
fn abandon_unanswered_connect(&mut self, now_ms: u64) -> Option<CultNetRudpPacket> {
if !self.awaiting_accept
|| now_ms.saturating_sub(self.connect_started_at_ms) < RUDP_CONNECT_ATTEMPT_MS
{
return None;
}
let payload = self
.connect_sequence
.and_then(|sequence| self.pending_reliable.get(&sequence))
.map(|pending| pending.packet.payload.clone())?;
self.next_sequence = draw_sequence(self.next_sequence);
let abandoned = self.connect_sequence?;
self.next_sequence = abandoned
.saturating_add(RUDP_RECEIVED_SEQUENCE_WINDOW as u32 - 1)
.min(u32::MAX - 1);
let payload = self.connect_payload.clone();
self.create_connect(now_ms, payload).ok()
}

View file

@ -870,3 +870,132 @@ fn an_accepted_peer_that_goes_silent_times_out() -> Result<()> {
assert!(server.check_timeout(100_000, 1_000));
Ok(())
}
// Ack Cut 1d, batch 2: the connect-attempt timeout.
/// A server that keeps a session answers a client's repeated or stale Connect
/// with an Ack that may name the client's pending Connect. That Ack says no
/// session started: only an Accept the client honours retires its Connect, so
/// the attempt still times out and is replaced.
#[test]
fn an_ack_naming_the_pending_connect_does_not_retire_it() -> Result<()> {
let mut server = session(500);
let mut old = session(1);
let old_accept = server.accept_connect(&old.create_connect(0, Vec::new())?, 0, Vec::new())?;
old.receive(&old_accept, 0)?;
server.receive(&old.create_ack_for_received(old_accept.sequence), 0)?;
let mut restarted = session(1);
let first = restarted.create_connect(0, b"join".to_vec())?;
let reply = server.accept_connect(&first, 1, Vec::new())?;
assert_eq!(reply.packet_type, CultNetRudpPacketType::Ack);
assert!(names_sequence(&reply, first.sequence), "the Ack must name the pending Connect for this test to bite");
restarted.receive(&reply, 1)?;
assert!(!restarted.connected());
assert_eq!(restarted.pending_reliable_sequences(), vec![first.sequence], "an Ack retired the Connect");
let fresh = restarted.due_resends(3_000);
assert_eq!(fresh.len(), 1, "the client waits for ever with nothing to resend");
assert_eq!(fresh[0].payload, b"join");
let accept = server.accept_connect(&fresh[0], 3_000, Vec::new())?;
assert_eq!(accept.packet_type, CultNetRudpPacketType::Accept);
restarted.receive(&accept, 3_000)?;
assert!(restarted.connected());
Ok(())
}
/// The fresh attempt is the abandoned sequence plus the receive window less
/// one, so a late copy of the abandoned Connect is stale for the server that
/// took the fresh one: answered with an Ack, no restart, no stranding.
#[test]
fn a_late_copy_of_an_abandoned_connect_is_stale_once_its_replacement_is_accepted() -> Result<()> {
let mut server = session(500);
let mut client = session(10);
let abandoned = client.create_connect(0, Vec::new())?;
let fresh = client.due_resends(3_000);
assert_eq!(fresh.len(), 1);
assert_eq!(fresh[0].sequence, abandoned.sequence + 4_095);
let accept = server.accept_connect(&fresh[0], 3_001, Vec::new())?;
client.receive(&accept, 3_002)?;
server.receive(&client.create_ack_for_received(accept.sequence), 3_002)?;
assert!(client.connected());
let a = send(&mut client, "a")?;
assert_eq!(names(&server.receive(&a, 3_003)?.delivered), ["a"]);
client.receive(&server.create_ack_for_received(a.sequence), 3_003)?;
assert!(server.connect_repeats(&abandoned), "the late copy would restart the server");
let reply = server.accept_connect(&abandoned, 3_004, Vec::new())?;
assert_eq!(reply.packet_type, CultNetRudpPacketType::Ack);
client.receive(&reply, 3_005)?;
assert!(client.connected());
let b = send(&mut client, "b")?;
assert_eq!(names(&server.receive(&b, 3_006)?.delivered), ["b"]);
Ok(())
}
/// A server whose generation sits exactly where the fresh attempt lands takes
/// it for a repeat; the attempt after that leaves the window and is admitted.
#[test]
fn a_server_sitting_at_the_jump_is_left_by_the_next_attempt() -> Result<()> {
let mut server = session(500);
let mut old = session(5_095);
let old_accept = server.accept_connect(&old.create_connect(0, Vec::new())?, 0, Vec::new())?;
old.receive(&old_accept, 0)?;
server.receive(&old.create_ack_for_received(old_accept.sequence), 0)?;
let mut restarted = session(1_000);
let first = restarted.create_connect(0, b"join".to_vec())?;
restarted.receive(&server.accept_connect(&first, 1, Vec::new())?, 1)?;
assert!(!restarted.connected());
let second = restarted.due_resends(3_000).remove(0);
assert_eq!(second.sequence, 5_095, "the jump lands on the server's generation");
let reply = server.accept_connect(&second, 3_001, Vec::new())?;
assert_eq!(reply.packet_type, CultNetRudpPacketType::Ack);
restarted.receive(&reply, 3_001)?;
assert!(!restarted.connected());
let third = restarted.due_resends(6_000).remove(0);
assert_eq!(third.sequence, 5_095 + 4_095);
let accept = server.accept_connect(&third, 6_001, Vec::new())?;
assert_eq!(accept.packet_type, CultNetRudpPacketType::Accept);
restarted.receive(&accept, 6_001)?;
assert!(restarted.connected());
Ok(())
}
fn receive_packet(socket: &UdpSocket) -> Result<CultNetRudpPacket> {
let mut buffer = vec![0_u8; 65_535];
let (received, _) = socket.recv_from(&mut buffer)?;
decode_rudp_packet(&buffer[..received])
}
/// A pinned client that restarts on the same address is a repeat by sequence,
/// so the hub answers with an Ack; the client's attempt times out, and the
/// fresh Connect (another sequence) starts a new generation the hub admits.
#[test]
fn the_hub_admits_a_pinned_client_that_restarts_on_the_same_address() -> Result<()> {
let mut hub = CultNetRudpServerHub::new(CultNetRudpServerHubOptions::new("hub", socket()?, CONNECTION_ID))?;
let hub_addr = hub.local_addr()?;
let peer = socket()?;
let mut first = session(1);
peer.send_to(&encode_rudp_packet(&first.create_connect(0, b"join".to_vec())?)?, hub_addr)?;
hub_event_matching(&mut hub, |event| matches!(event, CultNetRudpServerEvent::Connected { .. }))?;
let accept = receive_packet(&peer)?;
first.receive(&accept, 0)?;
peer.send_to(&encode_rudp_packet(&first.create_ack_for_received(accept.sequence))?, hub_addr)?;
hub.receive_event_once()?;
let mut restarted = session(1);
peer.send_to(&encode_rudp_packet(&restarted.create_connect(0, b"join".to_vec())?)?, hub_addr)?;
hub.receive_event_once()?;
restarted.receive(&receive_packet(&peer)?, 1)?;
assert!(!restarted.connected());
let fresh = restarted.due_resends(3_000).remove(0);
peer.send_to(&encode_rudp_packet(&fresh)?, hub_addr)?;
hub_event_matching(&mut hub, |event| matches!(event, CultNetRudpServerEvent::Connected { .. }))?;
restarted.receive(&receive_packet(&peer)?, 3_001)?;
assert!(restarted.connected(), "the restarted pinned client was never admitted");
Ok(())
}