[domistream] ENet API: sans-I/O 1.3 datagram codec and client peer
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
//! A sans-I/O ENet 1.3 client peer, compatible with GameStream hosts (`docs/PROTOCOL.md` 3.1,
|
||||
//! `docs/research/open-point-2.md`).
|
||||
//!
|
||||
//! One peer, one connection. The caller owns the UDP socket:
|
||||
//!
|
||||
//! ```text
|
||||
//! loop {
|
||||
//! while let Some(d) = peer.poll_transmit(now) { socket.send(&d) }
|
||||
//! wait until a datagram arrives or peer.poll_timeout()
|
||||
//! peer.handle_datagram(now, &d) or peer.handle_timeout(now)
|
||||
//! while let Some(e) = peer.poll_event() { … }
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use core::time::Duration;
|
||||
|
||||
use crate::{Instant, RandomSource, Result};
|
||||
|
||||
pub mod protocol;
|
||||
|
||||
/// Maximum size of one reassembled packet, in bytes (control messages are small; this caps fragment memory).
|
||||
pub const MAX_PACKET_BYTES: usize = 64 * 1024;
|
||||
/// Maximum number of packets queued for sending before [`EnetClient::send`] refuses more.
|
||||
pub const MAX_SEND_QUEUE: usize = 1024;
|
||||
/// Maximum number of reliable commands waiting for an acknowledgement.
|
||||
pub const MAX_IN_FLIGHT: usize = 256;
|
||||
|
||||
/// How a packet is delivered.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Delivery {
|
||||
/// Retransmitted until acknowledged, in order.
|
||||
Reliable,
|
||||
/// In order, may be lost, late packets dropped.
|
||||
Unreliable,
|
||||
/// May be lost, any order.
|
||||
Unsequenced,
|
||||
}
|
||||
|
||||
/// Settings of the connection the client offers. All values are UNKNOWN U9 for Sunshine; tests set them
|
||||
/// explicitly.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct EnetConfig {
|
||||
/// Channels to open.
|
||||
pub channel_count: u8,
|
||||
/// MTU to offer (576..=4096).
|
||||
pub mtu: u16,
|
||||
/// The connect command's user data word.
|
||||
pub connect_data: u32,
|
||||
/// Send a ping when nothing was sent for this long.
|
||||
pub ping_interval: Duration,
|
||||
/// Give up when nothing was acknowledged for this long.
|
||||
pub timeout: Duration,
|
||||
}
|
||||
|
||||
/// Connection state.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum EnetState {
|
||||
/// Connect sent, waiting for verify connect.
|
||||
Connecting,
|
||||
/// Connected.
|
||||
Connected,
|
||||
/// Disconnect sent, waiting for its acknowledgement.
|
||||
Disconnecting,
|
||||
/// Closed (by either side or by timeout).
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
/// What happened.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum EnetEvent {
|
||||
/// The host verified the connection.
|
||||
Connected,
|
||||
/// A packet arrived (reassembled, in order for reliable and unreliable delivery).
|
||||
Received {
|
||||
/// Channel.
|
||||
channel: u8,
|
||||
/// The packet.
|
||||
data: Vec<u8>,
|
||||
},
|
||||
/// The host closed the connection.
|
||||
Disconnected {
|
||||
/// The host's disconnect data word.
|
||||
data: u32,
|
||||
},
|
||||
/// The host stopped answering.
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
/// The ENet client peer.
|
||||
#[derive(Debug)]
|
||||
pub struct EnetClient {
|
||||
state: EnetState,
|
||||
}
|
||||
|
||||
impl EnetClient {
|
||||
/// Starts connecting: queues the connect datagram. The connect id comes from `rng`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Malformed`] for a config outside ENet's limits. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
pub fn connect(config: EnetConfig, now: Instant, rng: &mut dyn RandomSource) -> Result<Self> {
|
||||
let _ = (config, now, rng);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Feeds one received datagram. Datagrams for another peer id or session are dropped without error.
|
||||
///
|
||||
/// # Errors
|
||||
/// Parser errors as [`protocol::parse_datagram`]; [`crate::Error::TooLarge`] when a reassembly would exceed
|
||||
/// [`MAX_PACKET_BYTES`]; [`crate::Error::Closed`] after `Disconnected`. A malformed datagram never changes the
|
||||
/// connection state. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn handle_datagram(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
|
||||
let _ = (now, datagram);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Queues a packet. Packets larger than the MTU are fragmented.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::State`] unless connected, [`crate::Error::UnexpectedValue`] for a channel outside the
|
||||
/// negotiated count, [`crate::Error::TooLarge`] over [`MAX_PACKET_BYTES`] or a full queue. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
pub fn send(&mut self, now: Instant, channel: u8, delivery: Delivery, data: &[u8]) -> Result<()> {
|
||||
let _ = (now, channel, delivery, data);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Starts a graceful disconnect.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Closed`] when already disconnected. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn disconnect(&mut self, now: Instant, data: u32) -> Result<()> {
|
||||
let _ = (now, data);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// The next datagram to send, at most [`protocol::MAX_DATAGRAM_BYTES`] (acknowledgements, retransmissions,
|
||||
/// queued packets, pings).
|
||||
pub fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
|
||||
let _ = now;
|
||||
None
|
||||
}
|
||||
|
||||
/// The next event.
|
||||
pub fn poll_event(&mut self) -> Option<EnetEvent> {
|
||||
None
|
||||
}
|
||||
|
||||
/// When [`Self::handle_timeout`] must be called next (retransmission, ping or timeout), if ever.
|
||||
#[must_use]
|
||||
pub fn poll_timeout(&self) -> Option<Instant> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Advances timers: retransmits, pings, or gives up with [`EnetEvent::TimedOut`].
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
|
||||
let _ = now;
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Connection state.
|
||||
#[must_use]
|
||||
pub const fn state(&self) -> EnetState {
|
||||
self.state
|
||||
}
|
||||
|
||||
/// Smoothed round-trip time, once measured.
|
||||
#[must_use]
|
||||
pub fn round_trip_time(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user