Separate RUDP reliable and lossy sequence domains
This commit is contained in:
parent
e692e2f5eb
commit
d97107df35
2 changed files with 176 additions and 16 deletions
65
vendor/cultnet-rs/src/rudp.rs
vendored
65
vendor/cultnet-rs/src/rudp.rs
vendored
|
|
@ -134,11 +134,13 @@ pub struct CultNetRudpSession {
|
|||
max_pending_fragment_sets: usize,
|
||||
initial_sequence: u32,
|
||||
next_sequence: u32,
|
||||
next_sequenced_by_channel: BTreeMap<String, u32>,
|
||||
next_fragment_id: u16,
|
||||
connected: bool,
|
||||
last_received_at_ms: Option<u64>,
|
||||
highest_received_sequence: Option<u32>,
|
||||
received_sequences: BTreeSet<u32>,
|
||||
latest_sequenced_by_channel: BTreeMap<String, u32>,
|
||||
pending_reliable: BTreeMap<u32, PendingReliablePacket>,
|
||||
queued_reliable: VecDeque<CultNetRudpPacket>,
|
||||
ordered_next_sequence_by_channel: BTreeMap<String, u32>,
|
||||
|
|
@ -156,11 +158,13 @@ impl CultNetRudpSession {
|
|||
max_pending_fragment_sets: 64,
|
||||
initial_sequence: options.initial_sequence,
|
||||
next_sequence: options.initial_sequence,
|
||||
next_sequenced_by_channel: BTreeMap::new(),
|
||||
next_fragment_id: 1,
|
||||
connected: false,
|
||||
last_received_at_ms: None,
|
||||
highest_received_sequence: None,
|
||||
received_sequences: BTreeSet::new(),
|
||||
latest_sequenced_by_channel: BTreeMap::new(),
|
||||
pending_reliable: BTreeMap::new(),
|
||||
queued_reliable: VecDeque::new(),
|
||||
ordered_next_sequence_by_channel: BTreeMap::new(),
|
||||
|
|
@ -225,11 +229,13 @@ impl CultNetRudpSession {
|
|||
|
||||
pub fn reset_peer_state(&mut self) {
|
||||
self.next_sequence = self.initial_sequence;
|
||||
self.next_sequenced_by_channel.clear();
|
||||
self.next_fragment_id = 1;
|
||||
self.connected = false;
|
||||
self.last_received_at_ms = None;
|
||||
self.highest_received_sequence = None;
|
||||
self.received_sequences.clear();
|
||||
self.latest_sequenced_by_channel.clear();
|
||||
self.pending_reliable.clear();
|
||||
self.queued_reliable.clear();
|
||||
self.ordered_next_sequence_by_channel.clear();
|
||||
|
|
@ -312,6 +318,9 @@ impl CultNetRudpSession {
|
|||
"Cannot send RUDP data before the session is connected"
|
||||
));
|
||||
}
|
||||
if options.ordered && !options.reliable {
|
||||
return Err(anyhow!("RUDP ordered delivery requires reliability"));
|
||||
}
|
||||
self.require_payload_size(payload.len())?;
|
||||
|
||||
if let Some(max_fragment_bytes) = max_fragment_bytes {
|
||||
|
|
@ -394,7 +403,6 @@ impl CultNetRudpSession {
|
|||
}
|
||||
|
||||
if packet.packet_type == CultNetRudpPacketType::Ping {
|
||||
self.remember_received(packet.sequence);
|
||||
return Ok(CultNetRudpReceiveResult {
|
||||
delivered: Vec::new(),
|
||||
ready_to_send,
|
||||
|
|
@ -416,9 +424,6 @@ impl CultNetRudpSession {
|
|||
if packet.packet_type == CultNetRudpPacketType::Ack
|
||||
|| packet.packet_type == CultNetRudpPacketType::Pong
|
||||
{
|
||||
if packet.packet_type == CultNetRudpPacketType::Pong {
|
||||
self.remember_received(packet.sequence);
|
||||
}
|
||||
return Ok(CultNetRudpReceiveResult {
|
||||
delivered: Vec::new(),
|
||||
ready_to_send,
|
||||
|
|
@ -435,7 +440,6 @@ impl CultNetRudpSession {
|
|||
}
|
||||
|
||||
if packet.packet_type == CultNetRudpPacketType::Disconnect {
|
||||
self.remember_received(packet.sequence);
|
||||
self.connected = false;
|
||||
return Ok(CultNetRudpReceiveResult {
|
||||
delivered: Vec::new(),
|
||||
|
|
@ -460,12 +464,15 @@ impl CultNetRudpSession {
|
|||
});
|
||||
}
|
||||
|
||||
let duplicate = self.received_sequences.contains(&packet.sequence)
|
||||
|| self.highest_received_sequence.is_some_and(|highest| {
|
||||
packet.sequence < highest
|
||||
&& highest - packet.sequence >= RUDP_RECEIVED_SEQUENCE_WINDOW as u32
|
||||
});
|
||||
self.remember_received(packet.sequence);
|
||||
let duplicate = packet.reliable
|
||||
&& (self.received_sequences.contains(&packet.sequence)
|
||||
|| self.highest_received_sequence.is_some_and(|highest| {
|
||||
packet.sequence < highest
|
||||
&& highest - packet.sequence >= RUDP_RECEIVED_SEQUENCE_WINDOW as u32
|
||||
}));
|
||||
if packet.reliable {
|
||||
self.remember_received(packet.sequence);
|
||||
}
|
||||
if duplicate {
|
||||
return Ok(CultNetRudpReceiveResult {
|
||||
delivered: Vec::new(),
|
||||
|
|
@ -491,6 +498,19 @@ impl CultNetRudpSession {
|
|||
};
|
||||
let delivered = if ordered {
|
||||
self.deliver_ordered(frame, next_sequence, expected_sequence_if_uninitialized)
|
||||
} else if packet.sequenced {
|
||||
let latest = self
|
||||
.latest_sequenced_by_channel
|
||||
.get(&frame.channel_id)
|
||||
.copied();
|
||||
let newest_sequence = next_sequence.saturating_sub(1);
|
||||
if latest.is_some_and(|latest| newest_sequence <= latest) {
|
||||
Vec::new()
|
||||
} else {
|
||||
self.latest_sequenced_by_channel
|
||||
.insert(frame.channel_id.clone(), newest_sequence);
|
||||
vec![frame]
|
||||
}
|
||||
} else {
|
||||
vec![frame]
|
||||
};
|
||||
|
|
@ -635,11 +655,24 @@ impl CultNetRudpSession {
|
|||
fragment_index: u16,
|
||||
fragment_count: u16,
|
||||
) -> CultNetRudpPacket {
|
||||
let sequence = self.next_sequence;
|
||||
self.next_sequence = self
|
||||
.next_sequence
|
||||
.checked_add(1)
|
||||
.expect("sequence overflow");
|
||||
let sequence = if reliable {
|
||||
let sequence = self.next_sequence;
|
||||
self.next_sequence = self
|
||||
.next_sequence
|
||||
.checked_add(1)
|
||||
.expect("reliable sequence overflow");
|
||||
sequence
|
||||
} else if sequenced {
|
||||
let next = self
|
||||
.next_sequenced_by_channel
|
||||
.entry(channel_id.to_string())
|
||||
.or_insert(1);
|
||||
let sequence = *next;
|
||||
*next = next.checked_add(1).expect("sequenced channel overflow");
|
||||
sequence
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let (ack, ack_mask) = self.ack_state();
|
||||
CultNetRudpPacket {
|
||||
packet_type,
|
||||
|
|
|
|||
127
vendor/cultnet-rs/tests/cultnet.rs
vendored
127
vendor/cultnet-rs/tests/cultnet.rs
vendored
|
|
@ -771,6 +771,133 @@ fn rudp_session_pings_and_detects_receive_timeout() -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rudp_lossy_packets_cannot_create_reliable_ordered_gaps() -> Result<()> {
|
||||
let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
connection_id: 198,
|
||||
initial_sequence: 1,
|
||||
..CultNetRudpSessionOptions::default()
|
||||
});
|
||||
let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
connection_id: 198,
|
||||
initial_sequence: 100,
|
||||
..CultNetRudpSessionOptions::default()
|
||||
});
|
||||
let connect = client.create_connect(0, Vec::new())?;
|
||||
let accept = server.accept_connect(&connect, 1, Vec::new())?;
|
||||
client.receive(&accept, 2)?;
|
||||
|
||||
let ping = client.create_ping(b"pulse".to_vec());
|
||||
let pong = server
|
||||
.receive(&ping, 3)?
|
||||
.reply
|
||||
.expect("ping should produce pong");
|
||||
let realtime = server.send(
|
||||
"realtime",
|
||||
b"discarded realtime".to_vec(),
|
||||
CultNetRudpSendOptions::default(),
|
||||
)?;
|
||||
let latest = server.send(
|
||||
"latest",
|
||||
b"discarded latest state".to_vec(),
|
||||
CultNetRudpSendOptions {
|
||||
sequenced: true,
|
||||
..CultNetRudpSendOptions::default()
|
||||
},
|
||||
)?;
|
||||
let schema = server.send(
|
||||
"schema",
|
||||
b"committed response".to_vec(),
|
||||
CultNetRudpSendOptions {
|
||||
reliable: true,
|
||||
ordered: true,
|
||||
..CultNetRudpSendOptions::default()
|
||||
},
|
||||
)?;
|
||||
|
||||
assert_eq!(ping.sequence, 0);
|
||||
assert_eq!(pong.sequence, 0);
|
||||
assert_eq!(realtime.sequence, 0);
|
||||
assert_eq!(latest.sequence, 1);
|
||||
assert_eq!(schema.sequence, accept.sequence + 1);
|
||||
assert_eq!(
|
||||
client.receive(&schema, 4)?.delivered[0].payload,
|
||||
b"committed response"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rudp_unreliable_sequenced_delivery_is_scoped_to_its_channel() -> Result<()> {
|
||||
let mut sender = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
connection_id: 197,
|
||||
initial_sequence: 50,
|
||||
..CultNetRudpSessionOptions::default()
|
||||
});
|
||||
let mut receiver = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
connection_id: 197,
|
||||
initial_sequence: 100,
|
||||
..CultNetRudpSessionOptions::default()
|
||||
});
|
||||
let connect = sender.create_connect(0, Vec::new())?;
|
||||
let accept = receiver.accept_connect(&connect, 1, Vec::new())?;
|
||||
sender.receive(&accept, 2)?;
|
||||
let options = CultNetRudpSendOptions {
|
||||
sequenced: true,
|
||||
..CultNetRudpSendOptions::default()
|
||||
};
|
||||
let older = sender.send("latest", b"older".to_vec(), options.clone())?;
|
||||
let newer = sender.send("latest", b"newer".to_vec(), options)?;
|
||||
|
||||
assert_eq!(older.sequence, 1);
|
||||
assert_eq!(newer.sequence, 2);
|
||||
assert_eq!(receiver.receive(&newer, 3)?.delivered[0].payload, b"newer");
|
||||
assert!(receiver.receive(&older, 4)?.delivered.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rudp_rejects_unreliable_ordered_delivery() -> Result<()> {
|
||||
let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
connection_id: 196,
|
||||
..CultNetRudpSessionOptions::default()
|
||||
});
|
||||
session.receive(
|
||||
&CultNetRudpPacket {
|
||||
packet_type: CultNetRudpPacketType::Accept,
|
||||
connection_id: 196,
|
||||
sequence: 50,
|
||||
ack: 0,
|
||||
ack_mask: 0,
|
||||
channel_id: "control".to_string(),
|
||||
reliable: false,
|
||||
ordered: false,
|
||||
sequenced: false,
|
||||
fragment_id: 0,
|
||||
fragment_index: 0,
|
||||
fragment_count: 0,
|
||||
payload: Vec::new(),
|
||||
},
|
||||
0,
|
||||
)?;
|
||||
let error = session
|
||||
.send(
|
||||
"schema",
|
||||
b"cannot order what will not retransmit".to_vec(),
|
||||
CultNetRudpSendOptions {
|
||||
ordered: true,
|
||||
..CultNetRudpSendOptions::default()
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("ordered delivery requires reliability")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rudp_session_bounds_pending_reliable_packets_before_enqueue() -> Result<()> {
|
||||
let mut session = CultNetRudpSession::new(CultNetRudpSessionOptions {
|
||||
|
|
|
|||
Loading…
Reference in a new issue