use std::time::Duration;
pub const DEFAULT_MAX_SESSIONS: usize = 1024;
pub const DEFAULT_MAX_PACKET_LENGTH: usize = 65_507;
pub const DEFAULT_MIN_SEND_INTERVAL: Duration = Duration::from_millis(10);
pub const DEFAULT_BURST_ALLOWANCE: u32 = 5;
pub const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(60);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TimestampAllowance {
None,
Single,
#[default]
Dual,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerConfig {
hmac_key: Option<Vec<u8>>,
max_sessions: usize,
max_packet_length: usize,
min_send_interval: Duration,
burst_allowance: u32,
idle_timeout: Duration,
max_test_duration: Option<Duration>,
timestamp_allowance: TimestampAllowance,
dscp_allowed: bool,
}
impl ServerConfig {
#[must_use]
pub fn with_hmac_key(mut self, key: impl Into<Vec<u8>>) -> Self {
self.hmac_key = Some(key.into());
self
}
#[must_use]
pub fn with_max_sessions(mut self, max_sessions: usize) -> Self {
self.max_sessions = max_sessions;
self
}
#[must_use]
pub fn with_max_packet_length(mut self, max_packet_length: usize) -> Self {
self.max_packet_length = max_packet_length;
self
}
#[must_use]
pub fn with_min_send_interval(mut self, min_send_interval: Duration) -> Self {
self.min_send_interval = min_send_interval;
self
}
#[must_use]
pub fn with_burst_allowance(mut self, burst_allowance: u32) -> Self {
self.burst_allowance = burst_allowance;
self
}
#[must_use]
pub fn with_idle_timeout(mut self, idle_timeout: Duration) -> Self {
self.idle_timeout = idle_timeout;
self
}
#[must_use]
pub fn with_max_test_duration(mut self, max_test_duration: Duration) -> Self {
self.max_test_duration = (!max_test_duration.is_zero()).then_some(max_test_duration);
self
}
#[must_use]
pub fn without_max_test_duration(mut self) -> Self {
self.max_test_duration = None;
self
}
#[must_use]
pub fn with_timestamp_allowance(mut self, timestamp_allowance: TimestampAllowance) -> Self {
self.timestamp_allowance = timestamp_allowance;
self
}
#[must_use]
pub fn with_dscp_allowed(mut self, dscp_allowed: bool) -> Self {
self.dscp_allowed = dscp_allowed;
self
}
#[must_use]
pub fn hmac_key(&self) -> Option<&[u8]> {
self.hmac_key.as_deref()
}
#[must_use]
pub fn max_sessions(&self) -> usize {
self.max_sessions
}
#[must_use]
pub fn max_packet_length(&self) -> usize {
self.max_packet_length
}
#[must_use]
pub fn min_send_interval(&self) -> Duration {
self.min_send_interval
}
#[must_use]
pub fn burst_allowance(&self) -> u32 {
self.burst_allowance
}
#[must_use]
pub fn idle_timeout(&self) -> Duration {
self.idle_timeout
}
#[must_use]
pub fn max_test_duration(&self) -> Option<Duration> {
self.max_test_duration
}
#[must_use]
pub fn timestamp_allowance(&self) -> TimestampAllowance {
self.timestamp_allowance
}
#[must_use]
pub fn dscp_allowed(&self) -> bool {
self.dscp_allowed
}
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
hmac_key: None,
max_sessions: DEFAULT_MAX_SESSIONS,
max_packet_length: DEFAULT_MAX_PACKET_LENGTH,
min_send_interval: DEFAULT_MIN_SEND_INTERVAL,
burst_allowance: DEFAULT_BURST_ALLOWANCE,
idle_timeout: DEFAULT_IDLE_TIMEOUT,
max_test_duration: None,
timestamp_allowance: TimestampAllowance::Dual,
dscp_allowed: true,
}
}
}