use std::time::Duration;
use super::{
msg::MAX_MSG_LENGTH,
sizer::{BufferSizer, DynamicBuffer},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PortsExhausted {
Fail,
Wait(Option<Duration>),
}
#[derive(Debug, Clone)]
pub struct Cfg {
pub connection_timeout: Option<Duration>,
pub flush_interval: Option<Duration>,
pub io_buffer_size: usize,
pub max_ports: u32,
pub ports_exhausted: PortsExhausted,
pub max_data_size: usize,
pub max_received_ports: usize,
pub chunk_size: u32,
pub port_receive_buffer: u32,
pub port_receive_throttle: u32,
pub shared_receive_buffer: Box<dyn BufferSizer>,
pub shared_send_queue: usize,
pub transport_send_queue: usize,
pub transport_receive_queue: usize,
pub connect_queue: u16,
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Default for Cfg {
fn default() -> Self {
Self {
connection_timeout: Some(Duration::from_secs(60)),
flush_interval: None,
io_buffer_size: 65_536,
max_ports: 4096,
ports_exhausted: PortsExhausted::Wait(Some(Duration::from_secs(60))),
max_data_size: 524_288,
max_received_ports: 128,
chunk_size: 32_768,
port_receive_buffer: 131_072,
port_receive_throttle: 1_048_576,
shared_receive_buffer: DynamicBuffer::new(65_536, 134_217_728),
shared_send_queue: 32,
transport_send_queue: 32,
transport_receive_queue: 64,
connect_queue: 128,
_non_exhaustive: (),
}
}
}
impl Cfg {
pub(crate) fn check(&self) {
if self.max_ports > 2u32.pow(30) {
panic!("maximum ports must not exceed 2^30");
}
if self.chunk_size < 4 {
panic!("chunk size must be at least 4");
}
if self.port_receive_buffer < 4 {
panic!("port receive buffer must be at least 4 bytes");
}
if self.shared_send_queue == 0 {
panic!("shared send queue length must not be zero");
}
if self.transport_send_queue == 0 {
panic!("transport send queue length must not be zero");
}
if self.transport_receive_queue == 0 {
panic!("transport receive queue length must not be zero");
}
if self.connect_queue == 0 {
panic!("connect queue length must not be zero");
}
}
pub fn max_frame_length(&self) -> u32 {
(MAX_MSG_LENGTH as u32).checked_add(self.chunk_size).expect("maximum frame size exceeds u32::MAX")
}
}