[domistream] ENet host: tests first (client against host over an in-memory lossy wire), API stubs

EnetHostPeer is the accepting side, one peer per client address. The tests drive our EnetClient against it with simulated time, loss, duplication and reordering, plus a scripted client for the stock ENet 1.3 facts (verify connect fields, session ids, what is acknowledged when). They fail against the stubs.
This commit is contained in:
2026-12-04 12:24:14 +01:00
parent eae3b9e754
commit e77c5fcd83
5 changed files with 1352 additions and 0 deletions
+200
View File
@@ -0,0 +1,200 @@
//! The accepting side of the ENet 1.3 layer (`docs/PROTOCOL.md` 3.1): one host-side peer per client.
//!
//! The caller owns the UDP socket and **demultiplexes by source address**: a datagram from an address it does not
//! know yet goes to [`EnetHostPeer::accept`] (which takes only a CONNECT); every later datagram from that address
//! goes to [`EnetHostPeer::handle_datagram`] of the peer it created. A CONNECT with a new connect id from a known
//! address is a new connection (the client restarted): [`parse_connect_request`] lets the caller see that and
//! replace the old peer.
//!
//! ```text
//! on datagram d from addr:
//! peer = peers[addr] or EnetHostPeer::accept(config, now, &d)? // refused: drop d
//! peer.handle_datagram(now, &d) // (accept already took d)
//! loop {
//! while let Some(d) = peer.poll_transmit(now) { socket.send_to(&d, addr) }
//! while let Some(e) = peer.poll_event() { … }
//! wake at peer.poll_timeout() → peer.handle_timeout(now)
//! }
//! ```
use core::time::Duration;
use super::Delivery;
use crate::{Error, Instant, Result};
/// Settings of the host side.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct EnetHostConfig {
/// Our peer id, which the client puts in the header of every datagram it sends us (0..=0xFFE).
pub peer_id: u16,
/// Most channels we grant (the client's request is capped to this; at least 1).
pub channel_limit: u8,
/// Our MTU (576..=4096); the connection uses the smaller of this and the client's.
pub mtu: u16,
/// Send a ping when nothing was sent for this long.
pub ping_interval: Duration,
/// Give up when a reliable command stays unacknowledged this long.
pub timeout: Duration,
}
/// State of the host-side peer.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EnetHostState {
/// Verify connect sent, waiting for the client to acknowledge it.
AcknowledgingConnect,
/// Connected.
Connected,
/// Our disconnect sent, waiting for its acknowledgement.
Disconnecting,
/// Closed (by either side or by timeout).
Disconnected,
}
/// What happened.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EnetHostEvent {
/// The client acknowledged our verify connect.
Connected {
/// The connect command's user data word.
data: u32,
},
/// A packet arrived (reassembled, in order for reliable and unreliable delivery).
Received {
/// Channel.
channel: u8,
/// The packet.
data: Vec<u8>,
},
/// The client closed the connection.
Disconnected {
/// The client's disconnect data word.
data: u32,
},
/// The client stopped answering.
TimedOut,
}
/// A client's CONNECT, as [`parse_connect_request`] finds it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ConnectRequest {
/// The connect parameters.
pub params: super::protocol::ConnectParams,
/// The connect command's user data word.
pub data: u32,
}
/// The CONNECT in a datagram addressed to "no peer yet", if it holds one.
///
/// # Errors
/// Parser errors as [`super::protocol::parse_datagram`]; [`Error::UnexpectedValue`] when the datagram is addressed
/// to a peer or holds no CONNECT, or the channel count is outside 1..=255.
pub fn parse_connect_request(datagram: &[u8]) -> Result<ConnectRequest> {
let _ = datagram;
Err(Error::Unimplemented)
}
/// The host-side ENet peer of one client.
#[derive(Debug)]
pub struct EnetHostPeer {
config: EnetHostConfig,
}
impl EnetHostPeer {
/// Accepts a client's CONNECT datagram: queues the verify connect.
///
/// # Errors
/// [`Error::Malformed`] for a config outside ENet's limits; anything [`parse_connect_request`] refuses.
pub fn accept(config: EnetHostConfig, now: Instant, datagram: &[u8]) -> Result<Self> {
let _ = (config, now, datagram);
Err(Error::Unimplemented)
}
/// Feeds one datagram from the client.
///
/// # Errors
/// Parser errors; [`Error::Closed`] once disconnected.
pub fn handle_datagram(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
let _ = (now, datagram);
Err(Error::Unimplemented)
}
/// Queues a packet.
///
/// # Errors
/// [`Error::State`] unless connected.
pub fn send(&mut self, now: Instant, channel: u8, delivery: Delivery, data: &[u8]) -> Result<()> {
let _ = (now, channel, delivery, data);
Err(Error::Unimplemented)
}
/// Starts a graceful disconnect.
///
/// # Errors
/// [`Error::Closed`] when already disconnected or disconnecting.
pub fn disconnect(&mut self, now: Instant, data: u32) -> Result<()> {
let _ = (now, data);
Err(Error::Unimplemented)
}
/// The next datagram to send.
pub fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
let _ = now;
None
}
/// The next event.
pub fn poll_event(&mut self) -> Option<EnetHostEvent> {
None
}
/// When [`Self::handle_timeout`] is due.
#[must_use]
pub fn poll_timeout(&self) -> Option<Instant> {
None
}
/// Advances timers.
///
/// # Errors
/// None today.
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
let _ = now;
Err(Error::Unimplemented)
}
/// Connection state.
#[must_use]
pub const fn state(&self) -> EnetHostState {
EnetHostState::Disconnected
}
/// The connect id of this connection.
#[must_use]
pub const fn connect_id(&self) -> u32 {
0
}
/// Channels granted.
#[must_use]
pub const fn channel_count(&self) -> u8 {
0
}
/// The MTU in use.
#[must_use]
pub const fn mtu(&self) -> usize {
0
}
/// Smoothed round-trip time, once measured.
#[must_use]
pub const fn round_trip_time(&self) -> Option<Duration> {
None
}
/// The configuration.
#[must_use]
pub const fn config(&self) -> &EnetHostConfig {
&self.config
}
}