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,
}
impl ConnectionConfig {
pub const fn new() -> Self {
Self {
max_payload: MAX_PAYLOAD,
initial_ack_bytes: INITIAL_DELAYED_ACK_BYTES,
max_rx_per_channel: usize::MAX,
}
}
pub const fn embedded() -> Self {
Self {
max_payload: 8 * 1024,
initial_ack_bytes: 32 * 1024,
max_rx_per_channel: 64 * 1024,
}
}
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 = 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 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 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_cap();
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
}
}
pub(crate) const fn advertised_ack_bytes(&self) -> u32 {
let cap = self.rx_cap();
if self.initial_ack_bytes as usize > cap {
cap as u32
} else {
self.initial_ack_bytes
}
}
}
impl Default for ConnectionConfig {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_matches_adb_desktop_behaviour() {
let c = ConnectionConfig::new();
assert_eq!(c.max_payload(), MAX_PAYLOAD);
assert_eq!(c.initial_ack_bytes(), INITIAL_DELAYED_ACK_BYTES);
assert_eq!(c.max_rx_per_channel(), usize::MAX);
assert_eq!(ConnectionConfig::default(), c);
}
#[test]
fn embedded_preset_is_bounded() {
let c = ConnectionConfig::embedded();
assert_eq!(c.max_payload(), 8 * 1024);
assert_eq!(c.initial_ack_bytes(), 32 * 1024);
assert_eq!(c.max_rx_per_channel(), 64 * 1024);
}
#[test]
fn advertised_credit_never_exceeds_the_enforced_cap() {
let c = ConnectionConfig::new()
.with_initial_ack_bytes(32 * 1024 * 1024)
.with_max_rx_per_channel(1024 * 1024);
assert_eq!(c.advertised_ack_bytes(), 1024 * 1024);
let unbounded = ConnectionConfig::new().with_initial_ack_bytes(32 * 1024 * 1024);
assert_eq!(unbounded.advertised_ack_bytes(), 32 * 1024 * 1024);
}
#[test]
fn max_payload_is_clamped_to_the_protocol_maximum() {
let c = ConnectionConfig::new().with_max_payload(MAX_PAYLOAD * 4);
assert_eq!(c.max_payload(), MAX_PAYLOAD);
}
#[test]
fn max_payload_is_clamped_to_the_minimum() {
let c = ConnectionConfig::new().with_max_payload(0);
assert_eq!(c.max_payload(), MIN_MAX_PAYLOAD);
}
#[test]
fn max_payload_in_range_is_kept_verbatim() {
let c = ConnectionConfig::new().with_max_payload(8 * 1024);
assert_eq!(c.max_payload(), 8 * 1024);
}
#[test]
fn rx_cap_never_falls_below_one_payload() {
let c = ConnectionConfig::new()
.with_max_payload(8 * 1024)
.with_max_rx_per_channel(1024);
assert_eq!(c.rx_cap(), 8 * 1024);
}
#[test]
fn rx_cap_uses_the_configured_value_when_larger() {
let c = ConnectionConfig::new()
.with_max_payload(8 * 1024)
.with_max_rx_per_channel(64 * 1024);
assert_eq!(c.rx_cap(), 64 * 1024);
}
#[test]
fn builder_is_usable_in_const_context() {
const C: ConnectionConfig = ConnectionConfig::embedded().with_max_payload(4 * 1024);
assert_eq!(C.max_payload(), 4 * 1024);
assert_eq!(C.initial_ack_bytes(), 32 * 1024);
}
}