[domistream] Implementation: HTTP, pairing, RTSP/SDP, ENet, control, input, RTP, FEC, video and audio receivers
Per PROTOCOL.md and the Wolf docs; interop with a real host is unverified. Strict, capped parsers throughout, shared helpers in util.rs. Where PROTOCOL.md says UNKNOWN the Wolf-documented behaviour is used. The audio receiver waits for the jitter budget before rebuilding a missing packet, so a late packet beats a rebuilt one.
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
//! [`HostMessage`]s.
|
||||
|
||||
use core::time::Duration;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::enet::EnetConfig;
|
||||
use crate::enet::{Delivery, EnetClient, EnetConfig, EnetEvent};
|
||||
use crate::{Instant, RandomSource, Result};
|
||||
|
||||
pub mod frame;
|
||||
@@ -54,71 +55,113 @@ pub enum ControlEvent {
|
||||
/// One control stream.
|
||||
#[derive(Debug)]
|
||||
pub struct ControlStream {
|
||||
next_sequence: u32,
|
||||
enet: EnetClient,
|
||||
cipher: ControlCipher,
|
||||
/// Next sequence number to seal with; `u32::MAX + 1` once the space is used up.
|
||||
next_sequence: u64,
|
||||
/// Highest host sequence number accepted so far.
|
||||
last_host_sequence: Option<u32>,
|
||||
events: VecDeque<ControlEvent>,
|
||||
}
|
||||
|
||||
impl ControlStream {
|
||||
/// Starts connecting.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`crate::enet::EnetClient::connect`]. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// As [`crate::enet::EnetClient::connect`].
|
||||
pub fn connect(config: ControlConfig, now: Instant, rng: &mut dyn RandomSource) -> Result<Self> {
|
||||
let _ = (config, now, rng);
|
||||
Err(crate::Error::Unimplemented)
|
||||
Ok(Self {
|
||||
enet: EnetClient::connect(config.enet, now, rng)?,
|
||||
cipher: ControlCipher::new(config.rikey),
|
||||
next_sequence: 0,
|
||||
last_host_sequence: None,
|
||||
events: VecDeque::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Moves ENet events into control events, opening every host frame.
|
||||
fn drain(&mut self) {
|
||||
while let Some(e) = self.enet.poll_event() {
|
||||
let event = match e {
|
||||
EnetEvent::Connected => ControlEvent::Connected,
|
||||
EnetEvent::Disconnected { .. } => ControlEvent::Disconnected,
|
||||
EnetEvent::TimedOut => ControlEvent::TimedOut,
|
||||
EnetEvent::Received { data, .. } => self.open(&data),
|
||||
};
|
||||
self.events.push_back(event);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens one host frame. A frame that fails, or whose number is not above the last accepted one, is dropped
|
||||
/// without touching the replay state; a frame that opens moves it forward even if its message is unknown.
|
||||
fn open(&mut self, frame: &[u8]) -> ControlEvent {
|
||||
let Ok((sequence, plain)) = self.cipher.open(Direction::HostToClient, frame) else {
|
||||
return ControlEvent::Dropped;
|
||||
};
|
||||
if self.last_host_sequence.is_some_and(|last| sequence <= last) {
|
||||
return ControlEvent::Dropped;
|
||||
}
|
||||
self.last_host_sequence = Some(sequence);
|
||||
parse_host_message(&plain).map_or(ControlEvent::Dropped, ControlEvent::Message)
|
||||
}
|
||||
|
||||
/// Feeds one datagram from the control port.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`crate::enet::EnetClient::handle_datagram`]. Bad frames inside a good datagram are not errors; they
|
||||
/// become [`ControlEvent::Dropped`]. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// become [`ControlEvent::Dropped`].
|
||||
pub fn handle_datagram(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
|
||||
let _ = (now, datagram);
|
||||
Err(crate::Error::Unimplemented)
|
||||
let r = self.enet.handle_datagram(now, datagram);
|
||||
self.drain();
|
||||
r
|
||||
}
|
||||
|
||||
/// Seals and queues a message (reliable, channel 0 unless measurement says otherwise).
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`message::encode_client_message`] and [`crate::enet::EnetClient::send`]; [`crate::Error::Closed`] once
|
||||
/// the sequence space is used up (2^32 messages). Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// the sequence space is used up (2^32 messages).
|
||||
pub fn send(&mut self, now: Instant, message: &ClientMessage) -> Result<()> {
|
||||
let _ = (now, message, self.next_sequence);
|
||||
Err(crate::Error::Unimplemented)
|
||||
let plain = message::encode_client_message(message)?;
|
||||
let sequence = u32::try_from(self.next_sequence).map_err(|_| crate::Error::Closed)?;
|
||||
let frame = self.cipher.seal(sequence, Direction::ClientToHost, &plain)?;
|
||||
self.enet.send(now, 0, Delivery::Reliable, &frame)?;
|
||||
self.next_sequence += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The next datagram to send.
|
||||
pub fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
|
||||
let _ = now;
|
||||
None
|
||||
self.enet.poll_transmit(now)
|
||||
}
|
||||
|
||||
/// The next event.
|
||||
pub fn poll_event(&mut self) -> Option<ControlEvent> {
|
||||
None
|
||||
self.events.pop_front()
|
||||
}
|
||||
|
||||
/// When [`Self::handle_timeout`] is due.
|
||||
#[must_use]
|
||||
pub fn poll_timeout(&self) -> Option<Instant> {
|
||||
None
|
||||
self.enet.poll_timeout()
|
||||
}
|
||||
|
||||
/// Advances timers (ENet, periodic ping).
|
||||
/// Advances timers (ENet). `PERIODIC_PING` is not sent while its body is UNKNOWN (U10); ENet's own pings keep
|
||||
/// the connection alive.
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// As [`crate::enet::EnetClient::handle_timeout`].
|
||||
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
|
||||
let _ = now;
|
||||
Err(crate::Error::Unimplemented)
|
||||
let r = self.enet.handle_timeout(now);
|
||||
self.drain();
|
||||
r
|
||||
}
|
||||
|
||||
/// Closes the stream gracefully.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Closed`] when already closed. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::Closed`] when already closed.
|
||||
pub fn close(&mut self, now: Instant) -> Result<()> {
|
||||
let _ = now;
|
||||
Err(crate::Error::Unimplemented)
|
||||
self.enet.disconnect(now, 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user