[domistream] Control host: tests first (ControlStream against ControlHost, replay, stop deadline), API stubs
Input arrives typed and byte-identical to what the client encoded; host rumble, trigger rumble, LED, motion and termination reach the client; replays, forgeries and malformed messages are dropped; the stop deadline ends a session without PERIODIC_PING. Alloc caps for the host parsers. They fail against the stubs.
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
//! The host side of the control stream (`docs/PROTOCOL.md` 3.2, 3.3), over one [`EnetHostPeer`].
|
||||
|
||||
use core::time::Duration;
|
||||
|
||||
use super::message::{HdrMetadata, HostMessage};
|
||||
use crate::enet::{EnetHostConfig, EnetHostPeer};
|
||||
use crate::input::InputEvent;
|
||||
use crate::{Error, Instant, Result};
|
||||
|
||||
/// Stop a session after this long without a `PERIODIC_PING` (PROTOCOL.md 3.3, Moonshine's default).
|
||||
pub const DEFAULT_STOP_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
/// The extended termination code for a graceful end (PROTOCOL.md 3.3).
|
||||
pub const TERMINATION_GRACEFUL: u32 = 0x8003_0023;
|
||||
|
||||
/// Settings of the host side of one control stream. `Debug` never prints the key.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct ControlHostConfig {
|
||||
/// The ENet host-side peer.
|
||||
pub enet: EnetHostConfig,
|
||||
/// The session's `rikey`.
|
||||
pub rikey: [u8; 16],
|
||||
/// End the session after this long without a `PERIODIC_PING`; `None` never ends it for that.
|
||||
pub stop_timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for ControlHostConfig {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("ControlHostConfig")
|
||||
.field("enet", &self.enet)
|
||||
.field("rikey", &"<redacted>")
|
||||
.field("stop_timeout", &self.stop_timeout)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A message from the client, as the host reads it.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ClientControlMessage {
|
||||
/// `START_B`: start video and audio.
|
||||
StartB,
|
||||
/// `PERIODIC_PING`: the client is still there.
|
||||
PeriodicPing,
|
||||
/// `INPUT_DATA` with a known layout.
|
||||
Input(InputEvent),
|
||||
/// `INPUT_DATA` that [`crate::input::parse_input`] refused (unknown or unsupported layout).
|
||||
InputRejected {
|
||||
/// Body length.
|
||||
body_len: usize,
|
||||
/// Why.
|
||||
error: Error,
|
||||
},
|
||||
/// `IDR_FRAME`: send a key frame.
|
||||
IdrRequest,
|
||||
/// `INVALIDATE_REF_FRAMES`: stop referencing frames `first..=last`.
|
||||
InvalidateRefFrames {
|
||||
/// First lost frame.
|
||||
first: u32,
|
||||
/// Last lost frame.
|
||||
last: u32,
|
||||
},
|
||||
/// `LOSS_STATS`, body kept as it came (layout UNKNOWN U10).
|
||||
LossStats(Vec<u8>),
|
||||
/// `FRAME_STATS`, body kept as it came (layout UNKNOWN U10).
|
||||
FrameStats(Vec<u8>),
|
||||
/// Any other type (including `START_A`), skipped.
|
||||
Unknown {
|
||||
/// Type code.
|
||||
message_type: u16,
|
||||
/// Body length.
|
||||
body_len: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// Interprets a decrypted message from the client.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`super::message::parse_message`].
|
||||
pub fn parse_client_message(message: &[u8]) -> Result<ClientControlMessage> {
|
||||
let _ = message;
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Serializes a message to the client (type, length, body), ready for [`super::ControlCipher::seal`].
|
||||
///
|
||||
/// # Errors
|
||||
/// [`Error::Unsupported`] for a message whose layout is not known well enough to send.
|
||||
pub fn encode_host_message(message: &HostMessage) -> Result<Vec<u8>> {
|
||||
let _ = message;
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// The stop deadline of PROTOCOL.md 3.3: each `PERIODIC_PING` pushes it out by the timeout.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StopDeadline {
|
||||
timeout: Duration,
|
||||
deadline: Instant,
|
||||
}
|
||||
|
||||
impl StopDeadline {
|
||||
/// A deadline `timeout` after `now`.
|
||||
#[must_use]
|
||||
pub fn new(now: Instant, timeout: Duration) -> Self {
|
||||
Self { timeout, deadline: now.saturating_add(timeout) }
|
||||
}
|
||||
|
||||
/// A ping arrived at `now`.
|
||||
pub fn on_ping(&mut self, now: Instant) {
|
||||
let _ = now;
|
||||
}
|
||||
|
||||
/// When the session ends without another ping.
|
||||
#[must_use]
|
||||
pub const fn deadline(&self) -> Instant {
|
||||
self.deadline
|
||||
}
|
||||
|
||||
/// Whether the deadline has passed at `now`.
|
||||
#[must_use]
|
||||
pub fn expired(&self, now: Instant) -> bool {
|
||||
let _ = now;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// What happened on the host side of the control stream.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum ControlHostEvent {
|
||||
/// The client's ENet connection is up.
|
||||
Connected,
|
||||
/// A message from the client.
|
||||
Message(ClientControlMessage),
|
||||
/// A client frame failed its tag, came out of order or held a malformed message, and was skipped.
|
||||
Dropped,
|
||||
/// No `PERIODIC_PING` within the stop timeout: the session should end.
|
||||
PingTimedOut,
|
||||
/// The client closed the stream.
|
||||
Disconnected,
|
||||
/// The client stopped answering (ENet).
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
/// The host side of one control stream.
|
||||
#[derive(Debug)]
|
||||
pub struct ControlHost {
|
||||
enet: EnetHostPeer,
|
||||
}
|
||||
|
||||
impl ControlHost {
|
||||
/// Accepts a client's ENet CONNECT datagram.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`EnetHostPeer::accept`].
|
||||
pub fn accept(config: ControlHostConfig, now: Instant, datagram: &[u8]) -> Result<Self> {
|
||||
let _ = (config, now, datagram);
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Feeds one datagram from the client.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`EnetHostPeer::handle_datagram`].
|
||||
pub fn handle_datagram(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
|
||||
let _ = (now, datagram);
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Seals and queues a message to the client.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`encode_host_message`] and [`EnetHostPeer::send`].
|
||||
pub fn send(&mut self, now: Instant, message: &HostMessage) -> Result<()> {
|
||||
let _ = (now, message);
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Rumbles a controller.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`Self::send`].
|
||||
pub fn send_rumble(&mut self, now: Instant, controller: u16, low_frequency: u16, high_frequency: u16) -> Result<()> {
|
||||
self.send(now, &HostMessage::Rumble { controller, low_frequency, high_frequency })
|
||||
}
|
||||
|
||||
/// Rumbles a controller's triggers.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`Self::send`].
|
||||
pub fn send_rumble_triggers(&mut self, now: Instant, controller: u16, left: u16, right: u16) -> Result<()> {
|
||||
self.send(now, &HostMessage::RumbleTriggers { controller, left, right })
|
||||
}
|
||||
|
||||
/// Ends the session with an extended termination code ([`TERMINATION_GRACEFUL`] for a normal end).
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`Self::send`].
|
||||
pub fn send_termination(&mut self, now: Instant, error_code: u32) -> Result<()> {
|
||||
self.send(now, &HostMessage::Termination { error_code: Some(error_code) })
|
||||
}
|
||||
|
||||
/// HDR mode: not implemented.
|
||||
///
|
||||
/// # Errors
|
||||
/// Always [`Error::Unsupported`].
|
||||
pub fn send_hdr_mode(&mut self, now: Instant, enabled: bool, metadata: Option<HdrMetadata>) -> Result<()> {
|
||||
self.send(now, &HostMessage::HdrMode { enabled, metadata })
|
||||
}
|
||||
|
||||
/// The next datagram to send.
|
||||
pub fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
|
||||
self.enet.poll_transmit(now)
|
||||
}
|
||||
|
||||
/// The next event.
|
||||
pub fn poll_event(&mut self) -> Option<ControlHostEvent> {
|
||||
None
|
||||
}
|
||||
|
||||
/// When [`Self::handle_timeout`] is due.
|
||||
#[must_use]
|
||||
pub fn poll_timeout(&self) -> Option<Instant> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Advances timers.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`EnetHostPeer::handle_timeout`].
|
||||
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
|
||||
let _ = now;
|
||||
Err(Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Closes the stream gracefully (ENet disconnect).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`Error::Closed`] when already closed.
|
||||
pub fn close(&mut self, now: Instant) -> Result<()> {
|
||||
self.enet.disconnect(now, 0)
|
||||
}
|
||||
|
||||
/// The ENet peer underneath (state, connect id, round-trip time).
|
||||
#[must_use]
|
||||
pub const fn enet(&self) -> &EnetHostPeer {
|
||||
&self.enet
|
||||
}
|
||||
|
||||
/// When the session ends without another `PERIODIC_PING`, while that clock runs.
|
||||
#[must_use]
|
||||
pub const fn stop_deadline(&self) -> Option<Instant> {
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user