#[cfg(test)]
mod tests;
use crate::base::protocol::constant::MAX_PAYLOAD;
use crate::base::protocol::features::INITIAL_DELAYED_ACK_BYTES;
pub const MIN_MAX_PAYLOAD: u32 = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConnectionConfig {
max_payload: u32,
initial_ack_bytes: u32,
max_rx_per_channel: usize,
max_channels: usize,
max_banner_properties: usize,
}
impl ConnectionConfig {
pub const DEFAULT_MAX_CHANNELS: usize = 32;
pub const MAX_CHANNELS: usize = 1 << 16;
pub const DEFAULT_MAX_BANNER_PROPERTIES: usize = 64;
pub const fn new() -> Self {
Self {
max_payload: MAX_PAYLOAD,
initial_ack_bytes: INITIAL_DELAYED_ACK_BYTES,
max_rx_per_channel: usize::MAX,
max_channels: Self::DEFAULT_MAX_CHANNELS,
max_banner_properties: Self::DEFAULT_MAX_BANNER_PROPERTIES,
}
}
pub const fn embedded() -> Self {
Self {
max_payload: 8 * 1024,
initial_ack_bytes: 32 * 1024,
max_rx_per_channel: 64 * 1024,
max_channels: Self::DEFAULT_MAX_CHANNELS,
max_banner_properties: Self::DEFAULT_MAX_BANNER_PROPERTIES,
}
}
pub const fn with_max_payload(mut self, bytes: u32) -> Self {
self.max_payload = if bytes > MAX_PAYLOAD {
MAX_PAYLOAD
} else if bytes < MIN_MAX_PAYLOAD {
MIN_MAX_PAYLOAD
} else {
bytes
};
self
}
pub const fn with_initial_ack_bytes(mut self, bytes: u32) -> Self {
self.initial_ack_bytes = if bytes == 0 {
1
} else if bytes > i32::MAX as u32 {
i32::MAX as u32
} else {
bytes
};
self
}
pub const fn with_max_rx_per_channel(mut self, bytes: usize) -> Self {
self.max_rx_per_channel = bytes;
self
}
pub const fn with_max_channels(mut self, n: usize) -> Self {
self.max_channels = if n > Self::MAX_CHANNELS {
Self::MAX_CHANNELS
} else if n == 0 {
1
} else {
n
};
self
}
pub const fn with_max_banner_properties(mut self, n: usize) -> Self {
self.max_banner_properties = n;
self
}
pub const fn max_payload(&self) -> u32 {
self.max_payload
}
pub const fn initial_ack_bytes(&self) -> u32 {
self.initial_ack_bytes
}
pub const fn max_rx_per_channel(&self) -> usize {
self.max_rx_per_channel
}
pub const fn max_channels(&self) -> usize {
self.max_channels
}
pub const fn max_banner_properties(&self) -> usize {
self.max_banner_properties
}
pub const fn rx_watermark(&self) -> usize {
let want = if self.initial_ack_bytes as usize > self.max_payload as usize {
self.initial_ack_bytes as usize
} else {
self.max_payload as usize
};
let ceiling = self.rx_ceiling();
if want > ceiling {
ceiling
} else {
want
}
}
pub(crate) const fn rx_cap(&self) -> usize {
if self.max_rx_per_channel < self.max_payload as usize {
self.max_payload as usize
} else {
self.max_rx_per_channel
}
}
const fn rx_ceiling(&self) -> usize {
self.rx_cap() - self.max_payload as usize + 1
}
pub(crate) const fn advertised_ack_bytes(&self) -> u32 {
let ceiling = self.rx_ceiling();
if self.initial_ack_bytes as usize > ceiling {
ceiling as u32
} else {
self.initial_ack_bytes
}
}
}
impl Default for ConnectionConfig {
fn default() -> Self {
Self::new()
}
}