[domistream] ENet host: EnetHostPeer, the accepting side (verify connect, session ids, reliable channels, pings, timeouts, disconnect)

One peer per client address. CONNECT answered with a reliable VERIFY CONNECT as stock ENet 1.3 does it (session ids picked from the client's offer, MTU, window and channels clamped, throttle echoed); connected once the client acknowledges the verify; reliable, unreliable and unsequenced delivery with fragment reassembly under the existing caps; duplicate unsequenced groups dropped; pings when the client is quiet; retransmission and timeout as the client; disconnect both ways. Shares the client's channel, fragment and in-flight types.
This commit is contained in:
2026-12-04 12:29:28 +01:00
parent 67ae663764
commit 03e593ab1c
3 changed files with 694 additions and 61 deletions
+56 -16
View File
@@ -13,8 +13,8 @@ use domistream::enet::protocol::{
Command, CommandBody, ConnectParams, Datagram, MAX_DATAGRAM_BYTES, NO_PEER_ID, encode_datagram, parse_datagram,
};
use domistream::enet::{
Delivery, EnetClient, EnetConfig, EnetEvent, EnetHostConfig, EnetHostEvent, EnetHostPeer, EnetHostState,
EnetState, MAX_PACKET_BYTES, MAX_SEND_QUEUE, parse_connect_request,
Delivery, EnetClient, EnetConfig, EnetEvent, EnetHostConfig, EnetHostEvent, EnetHostPeer, EnetHostState, EnetState,
MAX_PACKET_BYTES, MAX_SEND_QUEUE, parse_connect_request,
};
use domistream::{Error, Instant};
use proptest::prelude::*;
@@ -445,7 +445,10 @@ fn data_before_the_verify_ack_is_not_taken() {
h.handle_datagram(at(1), &to_host(0, Some(1), vec![data.clone()])).unwrap();
assert!(events(&mut h).is_empty());
assert!(
!transmitted(&mut h, at(1)).iter().flat_map(|d| &d.commands).any(|c| matches!(c.body, CommandBody::Acknowledge { .. })),
!transmitted(&mut h, at(1))
.iter()
.flat_map(|d| &d.commands)
.any(|c| matches!(c.body, CommandBody::Acknowledge { .. })),
"not acknowledged, so the client sends it again"
);
let ack = cmd(
@@ -474,6 +477,7 @@ fn lost_verify_is_retransmitted() {
#[test]
fn verify_never_acknowledged_times_out() {
let mut h = EnetHostPeer::accept(host_config(), at(0), &connect_datagram(connect_params(3), 0)).unwrap();
assert_eq!(transmitted(&mut h, at(0)).len(), 1, "the first verify");
let mut verifies = 0;
let mut guard = 0;
while h.state() != EnetHostState::Disconnected {
@@ -506,7 +510,8 @@ fn reliable_both_ways_in_order() {
}
w.run_for(1_000);
for ch in 0..2u8 {
let want: Vec<Vec<u8>> = (0..50u32).filter(|i| i % 2 == u32::from(ch)).map(|i| i.to_le_bytes().to_vec()).collect();
let want: Vec<Vec<u8>> =
(0..50u32).filter(|i| i % 2 == u32::from(ch)).map(|i| i.to_le_bytes().to_vec()).collect();
assert_eq!(w.host.received(ch), want, "host, channel {ch}");
let want: Vec<Vec<u8>> =
(0..50u32).filter(|i| i % 2 == u32::from(ch)).map(|i| (1000 + i).to_le_bytes().to_vec()).collect();
@@ -525,7 +530,8 @@ fn large_packets_fragment_both_ways() {
w.run_for(2_000);
assert_eq!(w.host.received(1), vec![up]);
assert_eq!(w.client.received(0), vec![down]);
let frags = w.host.sent.iter().flat_map(|d| &d.commands).filter(|c| matches!(c.body, CommandBody::SendFragment(_))).count();
let frags =
w.host.sent.iter().flat_map(|d| &d.commands).filter(|c| matches!(c.body, CommandBody::SendFragment(_))).count();
assert!(frags > 40, "the host fragmented its 64 KiB packet ({frags})");
}
@@ -555,8 +561,12 @@ fn unreliable_from_the_host() {
#[test]
fn duplicated_and_stale_datagrams_are_delivered_once() {
let mut h = scripted_connected();
let unseq = |group: u16, data: &[u8]| cmd(false, 0, 0, CommandBody::SendUnsequenced { unsequenced_group: group, data: data.to_vec() });
let unrel = |seq: u16, data: &[u8]| cmd(false, 0, 0, CommandBody::SendUnreliable { unreliable_sequence: seq, data: data.to_vec() });
let unseq = |group: u16, data: &[u8]| {
cmd(false, 0, 0, CommandBody::SendUnsequenced { unsequenced_group: group, data: data.to_vec() })
};
let unrel = |seq: u16, data: &[u8]| {
cmd(false, 0, 0, CommandBody::SendUnreliable { unreliable_sequence: seq, data: data.to_vec() })
};
let feed = |h: &mut EnetHostPeer, c: Command| h.handle_datagram(at(10), &to_host(0, Some(10), vec![c])).unwrap();
feed(&mut h, unseq(1, b"a"));
feed(&mut h, unseq(1, b"a"));
@@ -612,7 +622,10 @@ fn reliable_from_the_client_reordered_and_duplicated() {
}
// Far future and channels outside the grant: never delivered.
h.handle_datagram(at(13), &to_host(0, Some(13), vec![rel(40_000, b"evil")])).unwrap();
let _ = h.handle_datagram(at(13), &to_host(0, Some(13), vec![cmd(true, 9, 1, CommandBody::SendReliable(b"ch9".to_vec()))]));
let _ = h.handle_datagram(
at(13),
&to_host(0, Some(13), vec![cmd(true, 9, 1, CommandBody::SendReliable(b"ch9".to_vec()))]),
);
assert!(events(&mut h).is_empty());
assert_eq!(h.state(), EnetHostState::Connected);
}
@@ -633,7 +646,10 @@ fn acknowledged_commands_need_the_sent_time() {
h.handle_datagram(at(10), &d).unwrap();
assert_eq!(events(&mut h), vec![EnetHostEvent::Received { channel: 0, data: b"a".to_vec() }]);
assert!(
!transmitted(&mut h, at(10)).iter().flat_map(|d| &d.commands).any(|c| matches!(c.body, CommandBody::Acknowledge { .. }))
!transmitted(&mut h, at(10))
.iter()
.flat_map(|d| &d.commands)
.any(|c| matches!(c.body, CommandBody::Acknowledge { .. }))
);
}
@@ -744,15 +760,23 @@ fn the_host_pings_a_quiet_client() {
let start = w.host.sent.len();
w.run_for(30_000);
assert!(both_connected(&mut w));
let pings =
w.host.sent[start..].iter().flat_map(|d| &d.commands).filter(|c| c.body == CommandBody::Ping && c.acknowledge).count();
let pings = w.host.sent[start..]
.iter()
.flat_map(|d| &d.commands)
.filter(|c| c.body == CommandBody::Ping && c.acknowledge)
.count();
assert!(pings >= 30, "the host pings an idle client ({pings})");
// And when nothing comes back at all, its ping times out.
let mut h = scripted_connected();
let t = h.poll_timeout().expect("a ping deadline");
assert!(t <= at(2 + host_config().ping_interval.as_millis() as u64));
h.handle_timeout(t).unwrap();
assert!(transmitted(&mut h, t).iter().flat_map(|d| &d.commands).any(|c| c.body == CommandBody::Ping && c.channel_id == 0xFF));
assert!(
transmitted(&mut h, t)
.iter()
.flat_map(|d| &d.commands)
.any(|c| c.body == CommandBody::Ping && c.channel_id == 0xFF)
);
}
#[test]
@@ -760,7 +784,8 @@ fn vanished_client_times_out() {
let mut w = connected_wire(Impair::clean(5), Impair::clean(5), 10);
let cut_at = w.now_ms;
w.to_host.cut = true;
let limit = cut_at + host_config().ping_interval.as_millis() as u64 + host_config().timeout.as_millis() as u64 + 1_000;
let limit =
cut_at + host_config().ping_interval.as_millis() as u64 + host_config().timeout.as_millis() as u64 + 1_000;
assert!(w.run_until(limit, |w| w.host.events.contains(&EnetHostEvent::TimedOut)), "no timeout by {limit} ms");
let earliest = cut_at + host_config().timeout.as_millis() as u64 - host_config().ping_interval.as_millis() as u64;
assert!(w.now_ms >= earliest, "not before the timeout");
@@ -817,7 +842,11 @@ fn send_rules() {
assert_err!(h.send(at(0), 0, Delivery::Reliable, b"x"), Error::State { .. }, "before the verify is acknowledged");
let mut h = scripted_connected();
assert_err!(h.send(at(3), 2, Delivery::Reliable, b"x"), Error::UnexpectedValue { .. }, "channel 2 of 2");
assert_too_large!(h.send(at(3), 0, Delivery::Reliable, &vec![0; MAX_PACKET_BYTES + 1]), MAX_PACKET_BYTES, MAX_PACKET_BYTES + 1);
assert_too_large!(
h.send(at(3), 0, Delivery::Reliable, &vec![0; MAX_PACKET_BYTES + 1]),
MAX_PACKET_BYTES,
MAX_PACKET_BYTES + 1
);
assert!(matches!(h.send(at(3), 0, Delivery::Unsequenced, &[0; 1500]), Err(Error::TooLarge { .. })));
for i in 0..MAX_SEND_QUEUE {
h.send(at(3), 0, Delivery::Reliable, &[i as u8]).unwrap_or_else(|e| panic!("packet {i}: {e:?}"));
@@ -838,7 +867,11 @@ fn garbage_never_changes_state() {
assert!(h.handle_datagram(at(5), bad).is_err(), "{bad:?}");
assert_eq!(h.state(), EnetHostState::Connected);
}
assert_too_large!(h.handle_datagram(at(5), &vec![0; MAX_DATAGRAM_BYTES + 1]), MAX_DATAGRAM_BYTES, MAX_DATAGRAM_BYTES + 1);
assert_too_large!(
h.handle_datagram(at(5), &vec![0; MAX_DATAGRAM_BYTES + 1]),
MAX_DATAGRAM_BYTES,
MAX_DATAGRAM_BYTES + 1
);
// Another peer id: dropped without error.
let other = encode_datagram(&Datagram {
peer_id: HOST_PEER_ID + 1,
@@ -875,7 +908,14 @@ fn fragments_with_lying_lengths() {
MAX_PACKET_BYTES,
MAX_PACKET_BYTES + 1
);
for f in [frag(0, 0, 10, 0, 10), frag(2, 2, 20, 0, 10), frag(2, 0, 20, 15, 10), frag(2, 0, 5, 0, 10), frag(u32::MAX, 0, 100, 0, 10), frag(2, 0, u32::MAX, 0, 10)] {
for f in [
frag(0, 0, 10, 0, 10),
frag(2, 2, 20, 0, 10),
frag(2, 0, 20, 15, 10),
frag(2, 0, 5, 0, 10),
frag(u32::MAX, 0, 100, 0, 10),
frag(2, 0, u32::MAX, 0, 10),
] {
let mut h = scripted_connected();
let _ = h.handle_datagram(at(5), &to_host(0, Some(5), vec![f.clone()]));
assert!(events(&mut h).is_empty(), "{f:?}");