use super::{AssetCache, HeldAsset, decoded};
use crate::stream_emit::{self, RawPoll, TimeoutRead, TimeoutWrite};
use indicatrix::renderer::env_map::EnvironmentMap;
use indicatrix_net::{
framing::FramingError,
messages::{
AssetError, ClientMessage, Done, ErrorMsg, NetError, Progress, RenderRequest, Stats,
StreamEvent, error_codes, hash_hex, read_asset_payload,
},
scene::HdrEnvironment,
};
use std::{
io::{Read, Write},
sync::{Arc, mpsc},
time::{Duration, Instant},
};
pub const ASSET_WAIT: Duration = Duration::from_secs(60);
const PAYLOAD_READ_TIMEOUT: Duration = Duration::from_secs(30);
const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
const POLL: Duration = Duration::from_millis(100);
pub enum Fetched<T = Arc<EnvironmentMap>> {
Ready(T),
Failed(ErrorMsg),
Superseded(Option<Box<RenderRequest>>),
Closed,
}
impl<T> Fetched<T> {
fn into_ready<U>(self) -> Result<T, Fetched<U>> {
match self {
Self::Ready(value) => Ok(value),
Self::Failed(error) => Err(Fetched::Failed(error)),
Self::Superseded(next) => Err(Fetched::Superseded(next)),
Self::Closed => Err(Fetched::Closed),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Pending<'a> {
pub request_id: u32,
pub cadence_ms: u32,
pub hdr: &'a HdrEnvironment,
}
pub fn ensure_environment<S: Read + Write + TimeoutRead + TimeoutWrite>(
stream: &mut S,
cache: &AssetCache,
pending: Pending<'_>,
) -> Result<Fetched, NetError> {
let hash = pending.hdr.content_hash;
if cache.has_held(&hash)
&& let Some(map) = decoded::lookup(&hash)
{
return Ok(Fetched::Ready(map));
}
let _ = stream.set_write_timeout(Some(WRITE_TIMEOUT));
let result = from_cache_or_client(stream, cache, pending);
let _ = stream.set_read_timeout(None);
let _ = stream.set_write_timeout(None);
result
}
fn from_cache_or_client<S: Read + Write + TimeoutRead>(
stream: &mut S,
cache: &AssetCache,
pending: Pending<'_>,
) -> Result<Fetched, NetError> {
let bytes = match cache.get(&pending.hdr.content_hash) {
Some(bytes) => bytes,
None => match request_from_client(stream, cache, pending)?.into_ready() {
Ok(bytes) => bytes,
Err(other) => return Ok(other),
},
};
decode_with_heartbeats(stream, pending, Arc::new(bytes))
}
pub fn ensure_held<S: Read + Write + TimeoutRead + TimeoutWrite>(
stream: &mut S,
cache: &Arc<AssetCache>,
pending: Pending<'_>,
decode: bool,
) -> Result<Fetched<Arc<HeldAsset>>, NetError> {
let _ = stream.set_write_timeout(Some(WRITE_TIMEOUT));
let result = hold(stream, cache, pending, decode);
let _ = stream.set_read_timeout(None);
let _ = stream.set_write_timeout(None);
result
}
fn hold<S: Read + Write + TimeoutRead>(
stream: &mut S,
cache: &Arc<AssetCache>,
pending: Pending<'_>,
decode: bool,
) -> Result<Fetched<Arc<HeldAsset>>, NetError> {
let hash = pending.hdr.content_hash;
let received: Arc<Vec<u8>> = match cache.get(&hash) {
Some(bytes) => Arc::new(bytes),
None => match request_from_client(stream, cache, pending)?.into_ready() {
Ok(bytes) => Arc::new(bytes),
Err(other) => return Ok(other),
},
};
let map = if decode {
match decode_held(stream, cache, pending, Some(&received))?.into_ready() {
Ok(map) => Some(map),
Err(other) => return Ok(other),
}
} else {
None
};
Ok(Fetched::Ready(Arc::new(HeldAsset::new(
*pending.hdr,
Arc::clone(cache),
Some(received),
map,
))))
}
fn decode_held<S: Write>(
stream: &mut S,
cache: &AssetCache,
pending: Pending<'_>,
received: Option<&Arc<Vec<u8>>>,
) -> Result<Fetched, NetError> {
let hash = pending.hdr.content_hash;
if let Some(map) = decoded::lookup(&hash) {
return Ok(Fetched::Ready(map));
}
let bytes = received.cloned().or_else(|| cache.get(&hash).map(Arc::new));
let Some(bytes) = bytes else {
return Ok(Fetched::Failed(asset_failed(
format!(
"HDR map {}: the coordinator's asset cache lost it; send the request again",
hash_hex(&hash)
),
pending.request_id,
)));
};
decode_with_heartbeats(stream, pending, bytes)
}
fn request_from_client<S: Read + Write + TimeoutRead>(
stream: &mut S,
cache: &AssetCache,
pending: Pending<'_>,
) -> Result<Fetched<Vec<u8>>, NetError> {
let hash = pending.hdr.content_hash;
tracing::info!(
"request {}: asking the client for HDR map {}",
pending.request_id,
hash_hex(&hash)
);
indicatrix_net::messages::write_stream_event(
stream,
&StreamEvent::NeedAsset { content_hash: hash },
None,
)?;
let started = Instant::now();
let mut last_heartbeat = Instant::now();
let mut timeouts = stream_emit::TimeoutCache::new();
loop {
let message = match stream_emit::poll_raw_client_message(stream, POLL, &mut timeouts)? {
RawPoll::Closed => return Ok(Fetched::Closed),
RawPoll::Pending => {
if started.elapsed() > ASSET_WAIT {
return Ok(Fetched::Failed(asset_failed(
format!(
"the client did not send HDR map {} within {} s",
hash_hex(&hash),
ASSET_WAIT.as_secs()
),
pending.request_id,
)));
}
if last_heartbeat.elapsed() >= stream_emit::HEARTBEAT_INTERVAL {
write_heartbeat(stream, pending.request_id)?;
last_heartbeat = Instant::now();
}
continue;
}
RawPoll::Message(message) => message,
};
match message {
ClientMessage::Asset(header) if header.content_hash == hash => {
let _ = stream.set_read_timeout(Some(PAYLOAD_READ_TIMEOUT));
let bytes = match read_asset_payload(stream, &header) {
Ok(bytes) => bytes,
Err(e @ (AssetError::HashMismatch | AssetError::LengthMismatch { .. })) => {
return Ok(Fetched::Failed(asset_failed(
format!("HDR map {}: {e}", hash_hex(&hash)),
pending.request_id,
)));
}
Err(AssetError::TooLarge { len }) => {
return Err(NetError::Framing(FramingError::FrameTooLarge {
len,
max: indicatrix_net::messages::MAX_ASSET_LEN,
}));
}
Err(AssetError::Framing(e)) => return Err(NetError::Framing(e)),
};
if let Err(e) = cache.put(&hash, &bytes) {
tracing::warn!("asset cache: not storing {}: {e}", hash_hex(&hash));
}
return Ok(Fetched::Ready(bytes));
}
ClientMessage::Asset(other) => {
discard_asset(stream, &other)?;
}
ClientMessage::Ping { nonce } => indicatrix_net::messages::write_stream_event(
stream,
&StreamEvent::Pong { nonce },
None,
)?,
ClientMessage::Cancel(cancel) if cancel.request_id == pending.request_id => {
write_cancelled_done(stream, pending)?;
return Ok(Fetched::Superseded(None));
}
ClientMessage::RenderRequest(next) => {
write_cancelled_done(stream, pending)?;
return Ok(Fetched::Superseded(Some(next)));
}
ClientMessage::Contribution(header) => {
indicatrix_net::messages::discard_contribution_payload(stream, &header)?;
}
other => tracing::debug!(
"request {}: ignoring {other:?} while waiting for its HDR map",
pending.request_id
),
}
}
}
pub fn discard_asset<S: Read>(
stream: &mut S,
header: &indicatrix_net::messages::AssetHeader,
) -> Result<(), NetError> {
tracing::debug!(
"discarding an unrequested asset {} ({} bytes)",
hash_hex(&header.content_hash),
header.len
);
match indicatrix_net::messages::discard_asset_payload(stream, header) {
Ok(()) | Err(AssetError::HashMismatch | AssetError::LengthMismatch { .. }) => Ok(()),
Err(AssetError::TooLarge { len }) => Err(NetError::Framing(FramingError::FrameTooLarge {
len,
max: indicatrix_net::messages::MAX_ASSET_LEN,
})),
Err(AssetError::Framing(e)) => Err(NetError::Framing(e)),
}
}
fn decode_with_heartbeats<S: Write>(
stream: &mut S,
pending: Pending<'_>,
bytes: Arc<Vec<u8>>,
) -> Result<Fetched, NetError> {
let hdr = *pending.hdr;
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(decoded::decode_and_register(&hdr, &bytes));
});
loop {
match rx.recv_timeout(stream_emit::HEARTBEAT_INTERVAL) {
Ok(Ok(map)) => return Ok(Fetched::Ready(map)),
Ok(Err(reason)) => {
return Ok(Fetched::Failed(asset_failed(
format!(
"HDR map {} does not decode: {reason}",
hash_hex(&hdr.content_hash)
),
pending.request_id,
)));
}
Err(mpsc::RecvTimeoutError::Timeout) => write_heartbeat(stream, pending.request_id)?,
Err(mpsc::RecvTimeoutError::Disconnected) => {
return Ok(Fetched::Failed(asset_failed(
"decoding the HDR map panicked".to_string(),
pending.request_id,
)));
}
}
}
}
fn write_heartbeat<S: Write>(stream: &mut S, request_id: u32) -> Result<(), NetError> {
indicatrix_net::messages::write_stream_event(
stream,
&StreamEvent::Progress(Progress {
request_id,
samples_done: 0,
}),
None,
)
}
fn write_cancelled_done<S: Write>(stream: &mut S, pending: Pending<'_>) -> Result<(), NetError> {
indicatrix_net::messages::write_stream_event(
stream,
&StreamEvent::Done(Done {
request_id: pending.request_id,
cancelled: true,
stats: Stats {
samples_done: 0,
requested_cadence_ms: pending.cadence_ms,
effective_cadence_ms: 0,
reclaimed_samples: 0,
},
}),
None,
)
}
fn asset_failed(message: String, request_id: u32) -> ErrorMsg {
tracing::info!("{message}");
ErrorMsg {
code: error_codes::ASSET_FAILED,
message,
request_id: Some(request_id),
}
}