use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PathStats {
pub rtt: Duration,
pub min_rtt: Duration,
pub cwnd: u64,
pub congestion_events: u64,
pub lost_packets: u64,
pub lost_bytes: u64,
pub sent_packets: u64,
pub current_mtu: u16,
pub max_datagram_size: Option<usize>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct UdpCounts {
pub datagrams: u64,
pub bytes: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TransportStats {
pub path: PathStats,
pub tx: UdpCounts,
pub rx: UdpCounts,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RemoteStats {
pub rtt: Duration,
pub min_rtt: Duration,
pub cwnd: u64,
pub congestion_events: u64,
pub lost_packets: u64,
pub lost_bytes: u64,
pub sent_packets: u64,
pub current_mtu: u16,
pub tx: UdpCounts,
pub rx: UdpCounts,
pub age: Duration,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConnectionStats {
pub url: Arc<str>,
pub age: Duration,
pub redials: u64,
pub transport: Option<TransportStats>,
pub remote: Option<RemoteStats>,
}
impl TransportStats {
pub(crate) fn of(conn: &quinn::Connection) -> TransportStats {
let stats = conn.stats();
let path = stats.path;
let udp = |s: quinn::UdpStats| UdpCounts {
datagrams: s.datagrams,
bytes: s.bytes,
};
TransportStats {
path: PathStats {
rtt: path.rtt,
min_rtt: path.min_rtt,
cwnd: path.cwnd,
congestion_events: path.congestion_events,
lost_packets: path.lost_packets,
lost_bytes: path.lost_bytes,
sent_packets: path.sent_packets,
current_mtu: path.current_mtu,
max_datagram_size: conn.max_datagram_size(),
},
tx: udp(stats.udp_tx),
rx: udp(stats.udp_rx),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::FlowStats;
trait NoAddress {}
impl NoAddress for u16 {}
impl NoAddress for u64 {}
impl NoAddress for usize {}
impl NoAddress for Duration {}
impl NoAddress for Arc<str> {}
impl NoAddress for PathStats {}
impl NoAddress for UdpCounts {}
impl NoAddress for TransportStats {}
impl NoAddress for RemoteStats {}
impl<T: NoAddress> NoAddress for Option<T> {}
fn allowed<T: NoAddress>(_: &T) {}
#[test]
fn statistics_types_carry_no_address() {
fn path(s: &PathStats) {
let PathStats {
rtt,
min_rtt,
cwnd,
congestion_events,
lost_packets,
lost_bytes,
sent_packets,
current_mtu,
max_datagram_size,
} = s;
allowed(rtt);
allowed(min_rtt);
allowed(cwnd);
allowed(congestion_events);
allowed(lost_packets);
allowed(lost_bytes);
allowed(sent_packets);
allowed(current_mtu);
allowed(max_datagram_size);
}
fn udp(s: &UdpCounts) {
let UdpCounts { datagrams, bytes } = s;
allowed(datagrams);
allowed(bytes);
}
fn transport(s: &TransportStats) {
let TransportStats { path, tx, rx } = s;
allowed(path);
allowed(tx);
allowed(rx);
}
fn remote(s: &RemoteStats) {
let RemoteStats {
rtt,
min_rtt,
cwnd,
congestion_events,
lost_packets,
lost_bytes,
sent_packets,
current_mtu,
tx,
rx,
age,
} = s;
allowed(rtt);
allowed(min_rtt);
allowed(cwnd);
allowed(congestion_events);
allowed(lost_packets);
allowed(lost_bytes);
allowed(sent_packets);
allowed(current_mtu);
allowed(tx);
allowed(rx);
allowed(age);
}
fn connection(s: &ConnectionStats) {
let ConnectionStats {
url,
age,
redials,
transport,
remote,
} = s;
allowed(url);
allowed(age);
allowed(redials);
allowed(transport);
allowed(remote);
}
fn flow(s: &FlowStats) {
let FlowStats {
sent,
too_large,
discarded,
not_live,
received,
overflow,
} = s;
allowed(sent);
allowed(too_large);
allowed(discarded);
allowed(not_live);
allowed(received);
allowed(overflow);
}
let path_stats = PathStats {
rtt: Duration::from_millis(20),
min_rtt: Duration::from_millis(18),
cwnd: 12_000,
congestion_events: 0,
lost_packets: 0,
lost_bytes: 0,
sent_packets: 10,
current_mtu: 1200,
max_datagram_size: None,
};
let transport_stats = TransportStats {
path: path_stats,
tx: UdpCounts::default(),
rx: UdpCounts::default(),
};
path(&path_stats);
udp(&transport_stats.tx);
transport(&transport_stats);
let remote_stats = RemoteStats {
rtt: Duration::from_millis(20),
min_rtt: Duration::from_millis(18),
cwnd: 12_000,
congestion_events: 0,
lost_packets: 0,
lost_bytes: 0,
sent_packets: 10,
current_mtu: 1200,
tx: UdpCounts::default(),
rx: UdpCounts::default(),
age: Duration::ZERO,
};
remote(&remote_stats);
connection(&ConnectionStats {
url: Arc::from("weida://example.org:4433/voice"),
age: Duration::ZERO,
redials: 0,
transport: Some(transport_stats),
remote: Some(remote_stats),
});
flow(&FlowStats::default());
}
}