use std::net::SocketAddr;
use std::time::Duration;
use super::constants::IDNFLG_STATUS_REALTIME;
use super::parser::{ChunkType, ParsedChunk, ReceivedPoint, SampleFormat};
#[derive(Clone, Copy, Debug)]
pub struct ReceivedChunk<'a> {
pub source_addr: SocketAddr,
pub sequence: u16,
pub content_id: u16,
pub channel_id: u8,
pub chunk_type: ChunkType,
pub config_or_last_fragment: bool,
pub has_config: bool,
pub is_last_fragment: bool,
pub timestamp_us_u32: u32,
pub duration_us: u32,
pub format: SampleFormat,
pub points: &'a [ReceivedPoint],
}
impl<'a> ReceivedChunk<'a> {
pub(crate) fn new(source_addr: SocketAddr, chunk: &'a ParsedChunk) -> Self {
Self {
source_addr,
sequence: chunk.sequence,
content_id: chunk.content_id,
channel_id: chunk.channel_id,
chunk_type: chunk.chunk_type,
config_or_last_fragment: chunk.config_or_last_fragment,
has_config: chunk.has_config,
is_last_fragment: chunk.is_last_fragment,
timestamp_us_u32: chunk.timestamp_us_u32,
duration_us: chunk.duration_us,
format: chunk.format,
points: &chunk.points,
}
}
}
pub trait ServerBehavior: Send + 'static {
fn on_packet_received(&mut self, _raw_data: &[u8]) {}
fn on_frame_received(&mut self, _raw_data: &[u8]) {}
fn on_chunk_received(&mut self, chunk: ReceivedChunk<'_>) {
self.on_points_received(chunk.points);
}
fn on_points_received(&mut self, _points: &[ReceivedPoint]) {}
fn should_respond(&self, command: u8) -> bool;
fn get_status_byte(&self) -> u8;
fn get_ack_result_code(&self) -> u8;
fn get_simulated_latency(&self) -> Duration {
Duration::ZERO
}
fn on_client_connected(&mut self, _addr: SocketAddr) {}
fn on_client_disconnected(&mut self) {}
fn should_force_disconnect(&mut self) -> bool {
false
}
fn is_occupied(&self) -> bool {
false
}
fn is_excluded(&self) -> bool {
false
}
}
#[derive(Default)]
pub struct SimpleBehavior;
impl ServerBehavior for SimpleBehavior {
fn should_respond(&self, _command: u8) -> bool {
true
}
fn get_status_byte(&self) -> u8 {
IDNFLG_STATUS_REALTIME
}
fn get_ack_result_code(&self) -> u8 {
0x00
}
}