[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:
@@ -1,5 +1,9 @@
|
||||
//! `GET /serverinfo` (`docs/PROTOCOL.md` 1.2).
|
||||
|
||||
use super::xml::Element;
|
||||
use crate::Result;
|
||||
use crate::util::{cap, hex_decode, malformed, parse_dec, parse_u16, parse_u32};
|
||||
|
||||
/// Maximum size of a serverinfo body, in bytes.
|
||||
pub const MAX_SERVERINFO_BYTES: usize = 64 * 1024;
|
||||
/// Maximum length of any string field in serverinfo, in bytes.
|
||||
@@ -40,19 +44,84 @@ impl ServerInfo {
|
||||
/// Whether the host reports a running app (`state` ends in `_BUSY` or `currentgame != 0`).
|
||||
///
|
||||
/// # Errors
|
||||
/// Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn is_busy(&self) -> crate::Result<bool> {
|
||||
Err(crate::Error::Unimplemented)
|
||||
/// None today; the `Result` leaves room for stricter state checks.
|
||||
pub fn is_busy(&self) -> Result<bool> {
|
||||
Ok(self.state.ends_with("_BUSY") || self.current_game != 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The text of field `name` if present, after the field cap; `None` when missing.
|
||||
fn field<'a>(root: &'a Element, name: &str) -> Result<Option<&'a str>> {
|
||||
match root.child(name) {
|
||||
Some(e) => {
|
||||
cap("serverinfo field", MAX_SERVERINFO_FIELD_BYTES, e.text.len())?;
|
||||
Ok(Some(e.text.as_str()))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Present and non-empty.
|
||||
fn non_empty<'a>(root: &'a Element, name: &str) -> Result<Option<&'a str>> {
|
||||
Ok(field(root, name)?.filter(|t| !t.is_empty()))
|
||||
}
|
||||
|
||||
fn parse_mac(s: &str) -> Result<[u8; 6]> {
|
||||
let mut out = [0u8; 6];
|
||||
let mut parts = s.split(':');
|
||||
for slot in &mut out {
|
||||
let part = parts.next().filter(|p| p.len() == 2).ok_or(malformed("serverinfo mac"))?;
|
||||
let b = hex_decode("serverinfo mac", part)?;
|
||||
*slot = b.first().copied().ok_or(malformed("serverinfo mac"))?;
|
||||
}
|
||||
if parts.next().is_some() {
|
||||
return Err(malformed("serverinfo mac"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Parses a serverinfo body.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_SERVERINFO_BYTES`], [`crate::Error::HostStatus`] for a non-200 answer,
|
||||
/// [`crate::Error::Malformed`] for a missing required element (`hostname`, `uniqueid`, `HttpsPort`) or a bad
|
||||
/// number. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn parse_server_info(body: &[u8]) -> crate::Result<ServerInfo> {
|
||||
let _ = body;
|
||||
Err(crate::Error::Unimplemented)
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_SERVERINFO_BYTES`] or [`MAX_SERVERINFO_FIELD_BYTES`],
|
||||
/// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for a missing required element
|
||||
/// (`hostname`, `uniqueid`, `HttpsPort`) or a bad number.
|
||||
pub fn parse_server_info(body: &[u8]) -> Result<ServerInfo> {
|
||||
let doc = super::document(body, MAX_SERVERINFO_BYTES, "serverinfo")?;
|
||||
let r = &doc.root;
|
||||
// Cap every field first, so an oversized field is TooLarge whatever else is wrong.
|
||||
for c in &r.children {
|
||||
cap("serverinfo field", MAX_SERVERINFO_FIELD_BYTES, c.text.len())?;
|
||||
}
|
||||
let hostname = non_empty(r, "hostname")?.ok_or(malformed("serverinfo: no hostname"))?.to_owned();
|
||||
let unique_id = non_empty(r, "uniqueid")?.ok_or(malformed("serverinfo: no uniqueid"))?.to_owned();
|
||||
let https_port = parse_u16("serverinfo HttpsPort", field(r, "HttpsPort")?.ok_or(malformed("serverinfo: no HttpsPort"))?)?;
|
||||
let http_port = non_empty(r, "ExternalPort")?.map(|p| parse_u16("serverinfo ExternalPort", p)).transpose()?;
|
||||
let mac = non_empty(r, "mac")?.map(parse_mac).transpose()?;
|
||||
let max_luma_pixels_hevc =
|
||||
non_empty(r, "MaxLumaPixelsHEVC")?.map(|v| parse_dec("serverinfo MaxLumaPixelsHEVC", v)).transpose()?;
|
||||
let codec_mode_support =
|
||||
non_empty(r, "ServerCodecModeSupport")?.map(|v| parse_u32("serverinfo ServerCodecModeSupport", v)).transpose()?;
|
||||
let paired = match non_empty(r, "PairStatus")? {
|
||||
None | Some("0") => false,
|
||||
Some("1") => true,
|
||||
Some(_) => return Err(malformed("serverinfo PairStatus")),
|
||||
};
|
||||
let current_game =
|
||||
non_empty(r, "currentgame")?.map(|v| parse_u32("serverinfo currentgame", v)).transpose()?.unwrap_or(0);
|
||||
Ok(ServerInfo {
|
||||
hostname,
|
||||
app_version: field(r, "appversion")?.unwrap_or_default().to_owned(),
|
||||
gfe_version: non_empty(r, "GfeVersion")?.map(str::to_owned),
|
||||
unique_id,
|
||||
https_port,
|
||||
http_port,
|
||||
mac,
|
||||
local_ip: non_empty(r, "LocalIP")?.map(str::to_owned),
|
||||
max_luma_pixels_hevc,
|
||||
codec_mode_support,
|
||||
paired,
|
||||
current_game,
|
||||
state: field(r, "state")?.unwrap_or_default().to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user