diff --git a/docs/cultnet-rudp-ack-cut.md b/docs/cultnet-rudp-ack-cut.md index 47008c81..e44147ce 100644 --- a/docs/cultnet-rudp-ack-cut.md +++ b/docs/cultnet-rudp-ack-cut.md @@ -157,8 +157,8 @@ And places where the sender loses track, or is locked out, without the receiver' | G1 | A restarted client on the same endpoint and Connect payload gets an Ack, never an Accept, and stays unconnected | Rust hub, C# listener and C# server mode, TS and Python server mode (Rust server mode resets on every Connect instead) | Cut 1c | | G1b | C# and Python server-mode transports drop a Connect from any endpoint but the first, so a client that restarts on a new port is locked out for good | C#, Python | Cut 1c | | P2 | An expired reliable packet leaves a permanent hole. Ordered frames after it are held forever, and `flush_reliable` succeeds (outstanding 0) | Rust (the only runtime with reliable expiry) | Cut F (FORWARD-TSN, Q-A3 B); Cut 4 reports it honestly where a receiver never acknowledges the notice | -| P9 | A receipt for an expiring send reads `Acknowledged` once the packet expires unacknowledged | Rust | Cut 4 | -| P10 | `send_reliable` with a full window queues the frame, then returns an error ("receipts require a non-empty reliable packet set"), so a caller that retries sends it twice | Rust | Cut 4 | +| P9 | A receipt for an expiring send reads `Acknowledged` once the packet expires unacknowledged | Rust | Cut 4 (guard landed) | +| P10 | `send_reliable` with a full window queues the frame, then returns an error ("receipts require a non-empty reliable packet set"), so a caller that retries sends it twice | Rust | Cut 4 (receipt over queued sequences landed) | And two faults found by Soul passes during pass 3: @@ -208,8 +208,8 @@ Pass 2 (base `3bf1c0c`; mirror `CultLib-imag-ack2`): | P5 | 3-fragment reliable frame, 2 fragments acked; 70 lossy fragment sets; last fragment arrives | Rust, TS, Python: never delivered, sender pending `[]` | Same, Rust, TS, Python | Unchanged (Cut 2) | | P7 | An Ack with flag bit `0x10`, a nonzero sequence field, or a 5-byte payload | Decoded and applied in Rust, C#, TS, Python | Same, all four | n/a | | P8 | Reliable `g` lost while 4,200+ later sequences are sent and acked; then `g` arrives | `g` not delivered, and acknowledged. All four | Same, all four | Unchanged (Cut 3) | -| P9 | Rust socket: `send_reliable("media")` with expiry 1 ms, never read | `Pending`, then `Acknowledged` | Same | `Pending` | -| P10 | Rust socket: 40 `schema` sends, then `send_reliable` | Error "receipts require a non-empty reliable packet set", after the frame was queued | Same (the probe's `?` fails on that error) | Not addressed (Cut 4) | +| P9 | Rust socket: `send_reliable("media")` with expiry 1 ms, never read | `Pending`, then `Acknowledged` | Same | Refused: `send_reliable` takes only a non-expiring reliable channel. Addressed (Cut 4) | +| P10 | Rust socket: 40 `schema` sends, then `send_reliable` | Error "receipts require a non-empty reliable packet set", after the frame was queued | Same (the probe's `?` fails on that error) | A receipt covering the queued sequences, `Pending` until acknowledged; the frame is sent once. Addressed (Cut 4) | | G1 | Client A connects, sends one ordered frame (acknowledging the Accept), and exits. Client B, a fresh session with the same initial sequence, binds A's exact address and sends the same Connect payload five times | n/a | **Locked out.** Rust hub (payload `"svc"` and empty): 5 Acks, 0 Accepts, B unconnected, the hub still holds A's generation. C# listener: same, no `PeerDisconnected`. C# server mode: 5 Acks. Python server mode: 5 Acks. TS session: the answer is an Ack. Rust server mode (control): B gets an Accept and its frame is delivered, because it resets on every Connect | Not probed | | G1b | C# and Python server mode: a third client on a new port sends a Connect | n/a | No reply; the datagram is counted dropped | Not probed | | G2 | Client connects to server 1 (initial sequence 1) and receives `old1..3`. Server 1 is gone; the client connects again on the same session to server 2 (initial sequence 1), which sends `new1`, `new2` | n/a | **`new1`, `new2` never delivered, and acknowledged** (server 2's pending set empties). C#, Rust, TS, Python | Not probed | diff --git a/packages/cultnet-rs/src/rudp.rs b/packages/cultnet-rs/src/rudp.rs index 427a38c1..9ed3ff0d 100644 --- a/packages/cultnet-rs/src/rudp.rs +++ b/packages/cultnet-rs/src/rudp.rs @@ -398,7 +398,13 @@ impl CultNetRudpSession { if receipt .sequences .iter() - .any(|sequence| self.pending_reliable.contains_key(sequence)) + .any(|sequence| { + self.pending_reliable.contains_key(sequence) + || self + .queued_reliable + .iter() + .any(|(packet, _)| packet.sequence == *sequence) + }) { CultNetRudpReliableSendStatus::Pending } else { @@ -406,20 +412,15 @@ impl CultNetRudpSession { } } - fn non_expiring_reliable_receipt( - &self, - packets: &[CultNetRudpPacket], - ) -> Result { - if packets.is_empty() || packets.iter().any(|packet| !packet.reliable) { - return Err(anyhow!( - "RUDP acknowledgement receipts require a non-empty reliable packet set" - )); - } - Ok(CultNetRudpReliableSendReceipt { + /// A receipt for the reliable packets a send just created, those still + /// queued behind a full window included: their sequences were assigned + /// when they were made, and they read `Pending` until acknowledged. + fn reliable_receipt_from(&self, first_sequence: u32) -> CultNetRudpReliableSendReceipt { + CultNetRudpReliableSendReceipt { session_id: self.session_id, generation: self.generation, - sequences: packets.iter().map(|packet| packet.sequence).collect(), - }) + sequences: (first_sequence..self.next_sequence).collect(), + } } pub fn last_received_at_ms(&self) -> Option { @@ -2245,18 +2246,19 @@ impl CultNetRudpSocketTransportConnection { channel_reliable_expire_after_ms(&self.profile, channel_id), channel_delivery(&self.profile, channel_id), ); - // This lineage has no reliable-expiry concept, so every reliable channel is - // already non-expiring. Restore the expiry half of this guard if one is added. - if !options.reliable { + // An expiring packet can be dropped unsent or unacknowledged, and a + // receipt cannot tell that from delivery: refuse the channel. + if !options.reliable || options.reliable_expire_after_ms.is_some() { return Err(anyhow!( "RUDP acknowledgement receipts require a non-expiring reliable channel" )); } + let first_sequence = self.session.next_sequence; let packets = self.session .send_many(channel_id, payload, options, self.max_fragment_bytes)?; + let receipt = self.session.reliable_receipt_from(first_sequence); self.send_data_packets(&packets)?; - let receipt = self.session.non_expiring_reliable_receipt(&packets)?; self.stats.frames_sent += 1; Ok(receipt) } diff --git a/packages/cultnet-rs/tests/rudp_connect_generation.rs b/packages/cultnet-rs/tests/rudp_connect_generation.rs index 92f83322..ad07eca1 100644 --- a/packages/cultnet-rs/tests/rudp_connect_generation.rs +++ b/packages/cultnet-rs/tests/rudp_connect_generation.rs @@ -517,6 +517,13 @@ fn connected_client( server_socket: &UdpSocket, server_addr: SocketAddr, ) -> Result { + Ok(connected_client_and_session(server_socket, server_addr)?.0) +} + +fn connected_client_and_session( + server_socket: &UdpSocket, + server_addr: SocketAddr, +) -> Result<(CultNetRudpSocketTransportConnection, CultNetRudpSession)> { let mut client = CultNetRudpSocketTransportConnection::new(CultNetRudpSocketTransportOptions::client( "client", socket()?, @@ -533,11 +540,12 @@ fn connected_client( break (packet, from); } }; - let accept = session(500).accept_connect(&connect, 0, Vec::new())?; + let mut server_session = session(500); + let accept = server_session.accept_connect(&connect, 0, Vec::new())?; server_socket.send_to(&encode_rudp_packet(&accept)?, client_addr)?; client.receive_once()?; assert!(client.connected()); - Ok(client) + Ok((client, server_session)) } /// A receipt belongs to the transport that issued it: asked of another @@ -1021,3 +1029,54 @@ fn the_hub_admits_a_pinned_client_that_restarts_on_the_same_address() -> Result< assert!(restarted.connected(), "the restarted pinned client was never admitted"); Ok(()) } + +/// A send that lands behind a full flow window is queued, not refused: it gets +/// a receipt for the sequence it was assigned, and the receipt reads Pending +/// while the packet is still queued. A silent peer fills the window without a +/// clock. +#[test] +fn a_send_queued_behind_a_full_window_returns_a_pending_receipt() -> Result<()> { + let server = socket()?; + let server_addr = server.local_addr()?; + let (mut client, mut session) = connected_client_and_session(&server, server_addr)?; + let mut receipts = Vec::new(); + for _ in 0..CULTNET_RUDP_RELIABLE_SEND_WINDOW_PACKETS { + receipts.push(client.send_reliable("schema", b"fill".to_vec())?); + } + assert_eq!(client.outstanding_reliable_packet_count(), CULTNET_RUDP_RELIABLE_SEND_WINDOW_PACKETS); + let queued = client.send_reliable("schema", b"queued".to_vec())?; + assert_eq!(client.outstanding_reliable_packet_count(), CULTNET_RUDP_RELIABLE_SEND_WINDOW_PACKETS + 1); + assert_eq!(client.reliable_send_status(&queued), CultNetRudpReliableSendStatus::Pending); + // Drain: the server acknowledges every packet it sees; the queued one then + // goes out and is acknowledged too. + let mut buffer = vec![0_u8; 65_535]; + for _ in 0..200 { + if let Ok((received, from)) = server.recv_from(&mut buffer) { + let packet = decode_rudp_packet(&buffer[..received])?; + if packet.packet_type == CultNetRudpPacketType::Data { + session.receive(&packet, 0)?; + server.send_to(&encode_rudp_packet(&session.create_ack())?, from)?; + } + } + client.poll_resends()?; + while client.receive_once()?.is_some() {} + if client.reliable_send_status(&queued) == CultNetRudpReliableSendStatus::Acknowledged { + break; + } + } + assert_eq!(client.reliable_send_status(&queued), CultNetRudpReliableSendStatus::Acknowledged); + assert!(receipts.iter().all(|r| client.reliable_send_status(r) == CultNetRudpReliableSendStatus::Acknowledged)); + Ok(()) +} + +/// An expiring channel cannot give a receipt: a queued packet that expires +/// unsent would otherwise read Acknowledged. +#[test] +fn a_receipt_is_refused_on_an_expiring_channel() -> Result<()> { + let server = socket()?; + let server_addr = server.local_addr()?; + let mut client = connected_client(&server, server_addr)?; + assert!(client.send_reliable("media", b"m".to_vec()).is_err()); + assert_eq!(client.outstanding_reliable_packet_count(), 0, "a refused send queues nothing"); + Ok(()) +}