use core::fmt;
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::common::duration::DurationMs;
use crate::frame::{DEFAULT_MAX_PAYLOAD_BYTES, FrameLimits, MAX_SUPPORTED_PAYLOAD_BYTES};
pub const DEFAULT_MAX_ROUTES: u32 = 4_096;
pub const DEFAULT_MAX_INFLIGHT_FRAMES: u32 = 1_024;
pub const DEFAULT_MAX_SUBSCRIPTIONS: u32 = 256;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct NegotiatedLimits {
pub max_payload_bytes: u64,
pub max_routes: u32,
pub max_inflight_frames: u32,
pub max_subscriptions: u32,
pub heartbeat_interval: DurationMs,
pub keepalive_timeout: DurationMs,
pub require_crc: bool,
}
impl NegotiatedLimits {
#[must_use]
pub const fn new() -> Self {
Self {
max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES as u64,
max_routes: DEFAULT_MAX_ROUTES,
max_inflight_frames: DEFAULT_MAX_INFLIGHT_FRAMES,
max_subscriptions: DEFAULT_MAX_SUBSCRIPTIONS,
heartbeat_interval: DurationMs::HEARTBEAT,
keepalive_timeout: DurationMs::new(15_000),
require_crc: false,
}
}
#[must_use]
pub const fn network() -> Self {
Self {
require_crc: true,
..Self::new()
}
}
#[must_use]
pub const fn uds() -> Self {
Self::new()
}
#[must_use]
pub const fn with_max_payload_bytes(mut self, bytes: u64) -> Self {
let ceiling = MAX_SUPPORTED_PAYLOAD_BYTES as u64;
self.max_payload_bytes = if bytes > ceiling { ceiling } else { bytes };
self
}
#[must_use]
pub const fn with_max_routes(mut self, routes: u32) -> Self {
self.max_routes = routes;
self
}
#[must_use]
pub const fn with_max_inflight_frames(mut self, frames: u32) -> Self {
self.max_inflight_frames = frames;
self
}
#[must_use]
pub const fn with_max_subscriptions(mut self, subscriptions: u32) -> Self {
self.max_subscriptions = subscriptions;
self
}
#[must_use]
pub const fn with_heartbeat_interval(mut self, interval: DurationMs) -> Self {
self.heartbeat_interval = interval;
self
}
#[must_use]
pub const fn with_keepalive_timeout(mut self, timeout: DurationMs) -> Self {
self.keepalive_timeout = timeout;
self
}
#[must_use]
pub const fn with_require_crc(mut self, required: bool) -> Self {
self.require_crc = required;
self
}
#[must_use]
pub const fn negotiate(&self, other: &Self) -> Self {
let agreed = Self {
max_payload_bytes: min_u64(self.max_payload_bytes, other.max_payload_bytes),
max_routes: min_u32(self.max_routes, other.max_routes),
max_inflight_frames: min_u32(self.max_inflight_frames, other.max_inflight_frames),
max_subscriptions: min_u32(self.max_subscriptions, other.max_subscriptions),
heartbeat_interval: self.heartbeat_interval.min(other.heartbeat_interval),
keepalive_timeout: self.keepalive_timeout.min(other.keepalive_timeout),
require_crc: self.require_crc || other.require_crc,
};
agreed.repaired()
}
#[must_use]
pub const fn repaired(mut self) -> Self {
if self.heartbeat_interval.is_zero() {
self.keepalive_timeout = DurationMs::ZERO;
return self;
}
let floor = match self.heartbeat_interval.checked_mul(2) {
Some(floor) => floor,
None => DurationMs::MAX,
};
if self.keepalive_timeout.as_millis() < floor.as_millis() {
self.keepalive_timeout = floor;
}
self
}
#[must_use]
pub const fn permits(&self, other: &Self) -> bool {
self.max_payload_bytes >= other.max_payload_bytes
&& self.max_routes >= other.max_routes
&& self.max_inflight_frames >= other.max_inflight_frames
&& self.max_subscriptions >= other.max_subscriptions
}
#[must_use]
pub const fn to_frame_limits(&self) -> FrameLimits {
let bytes = if self.max_payload_bytes > MAX_SUPPORTED_PAYLOAD_BYTES as u64 {
MAX_SUPPORTED_PAYLOAD_BYTES
} else {
self.max_payload_bytes as usize
};
FrameLimits::new()
.with_max_payload_bytes(bytes)
.with_require_crc(self.require_crc)
}
#[must_use]
pub const fn from_frame_limits(limits: &FrameLimits) -> Self {
Self::new()
.with_max_payload_bytes(limits.max_payload_bytes() as u64)
.with_require_crc(limits.require_crc())
}
}
const fn min_u64(left: u64, right: u64) -> u64 {
if left < right { left } else { right }
}
const fn min_u32(left: u32, right: u32) -> u32 {
if left < right { left } else { right }
}
impl Default for NegotiatedLimits {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for NegotiatedLimits {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"≤{} B/frame, ≤{} routes, ≤{} in flight, ≤{} subscriptions, heartbeat {}, timeout {}, crc {}",
self.max_payload_bytes,
self.max_routes,
self.max_inflight_frames,
self.max_subscriptions,
self.heartbeat_interval,
self.keepalive_timeout,
if self.require_crc {
"required"
} else {
"optional"
}
)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::codec::round_trip;
#[test]
fn defaults_match_the_blueprint_tables() {
let limits = NegotiatedLimits::default();
assert_eq!(limits.max_payload_bytes, 64 * 1024 * 1024);
assert_eq!(limits.heartbeat_interval, DurationMs::HEARTBEAT);
assert!(!limits.require_crc);
assert!(NegotiatedLimits::network().require_crc);
assert_eq!(NegotiatedLimits::uds(), NegotiatedLimits::new());
}
#[test]
fn negotiation_takes_the_stricter_of_each_field() {
let left = NegotiatedLimits::new()
.with_max_payload_bytes(1 << 20)
.with_max_routes(10)
.with_max_inflight_frames(4)
.with_max_subscriptions(2)
.with_heartbeat_interval(DurationMs::from_secs(1))
.with_keepalive_timeout(DurationMs::from_secs(30));
let right = NegotiatedLimits::new()
.with_max_payload_bytes(1 << 24)
.with_max_routes(4)
.with_max_inflight_frames(64)
.with_max_subscriptions(8)
.with_heartbeat_interval(DurationMs::from_secs(5))
.with_keepalive_timeout(DurationMs::from_secs(20));
let agreed = left.negotiate(&right);
assert_eq!(agreed.max_payload_bytes, 1 << 20);
assert_eq!(agreed.max_routes, 4);
assert_eq!(agreed.max_inflight_frames, 4);
assert_eq!(agreed.max_subscriptions, 2);
assert_eq!(agreed.heartbeat_interval, DurationMs::from_secs(1));
assert_eq!(agreed.keepalive_timeout, DurationMs::from_secs(20));
}
#[test]
fn negotiation_is_commutative_and_idempotent() {
let left = NegotiatedLimits::network().with_max_routes(9);
let right = NegotiatedLimits::uds().with_max_payload_bytes(4096);
let agreed = left.negotiate(&right);
assert_eq!(agreed, right.negotiate(&left));
assert_eq!(agreed, agreed.negotiate(&agreed));
}
#[test]
fn the_stricter_integrity_policy_wins() {
let agreed = NegotiatedLimits::uds().negotiate(&NegotiatedLimits::network());
assert!(agreed.require_crc);
}
#[test]
fn a_timeout_below_two_heartbeats_is_repaired() {
let hostile = NegotiatedLimits::new()
.with_heartbeat_interval(DurationMs::from_secs(5))
.with_keepalive_timeout(DurationMs::new(1));
assert_eq!(hostile.repaired().keepalive_timeout.as_millis(), 10_000);
let quiet = NegotiatedLimits::new()
.with_heartbeat_interval(DurationMs::ZERO)
.with_keepalive_timeout(DurationMs::from_secs(3));
assert_eq!(quiet.repaired().keepalive_timeout, DurationMs::ZERO);
let absurd = NegotiatedLimits::new().with_heartbeat_interval(DurationMs::MAX);
assert_eq!(absurd.repaired().keepalive_timeout, DurationMs::MAX);
}
#[test]
fn negotiation_repairs_the_result_too() {
let left = NegotiatedLimits::new()
.with_heartbeat_interval(DurationMs::from_secs(5))
.with_keepalive_timeout(DurationMs::from_secs(30));
let right = NegotiatedLimits::new()
.with_heartbeat_interval(DurationMs::from_secs(5))
.with_keepalive_timeout(DurationMs::from_secs(6));
let agreed = left.negotiate(&right);
assert!(agreed.keepalive_timeout.as_millis() >= 10_000);
}
#[test]
fn the_payload_ceiling_cannot_exceed_the_header_field() {
let absurd = NegotiatedLimits::new().with_max_payload_bytes(u64::MAX);
assert_eq!(
absurd.max_payload_bytes, MAX_SUPPORTED_PAYLOAD_BYTES as u64,
"the len field is 32 bits wide"
);
assert_eq!(
absurd.to_frame_limits().max_payload_bytes(),
MAX_SUPPORTED_PAYLOAD_BYTES
);
}
#[test]
fn permits_is_a_one_way_comparison() {
let offered = NegotiatedLimits::new();
let granted = NegotiatedLimits::new().with_max_payload_bytes(1 << 20);
assert!(offered.permits(&granted));
assert!(!granted.permits(&offered));
assert!(offered.permits(&offered));
}
#[test]
fn frame_limits_convert_both_ways() {
let limits = NegotiatedLimits::new()
.with_max_payload_bytes(4096)
.with_require_crc(true);
let frame_limits = limits.to_frame_limits();
assert_eq!(frame_limits.max_payload_bytes(), 4096);
assert!(frame_limits.require_crc());
let back = NegotiatedLimits::from_frame_limits(&frame_limits);
assert_eq!(back.max_payload_bytes, 4096);
assert!(back.require_crc);
}
#[test]
fn the_wire_form_round_trips() {
let limits = NegotiatedLimits::network()
.with_max_routes(7)
.with_max_subscriptions(3);
assert_eq!(round_trip(&limits).unwrap(), limits);
let json = serde_json::to_string(&limits).unwrap();
assert_eq!(
serde_json::from_str::<NegotiatedLimits>(&json).unwrap(),
limits
);
}
#[test]
fn display_mentions_every_dimension() {
let text = NegotiatedLimits::network().to_string();
for needle in ["B/frame", "routes", "in flight", "subscriptions", "crc"] {
assert!(text.contains(needle), "{text} is missing {needle}");
}
}
}