Skip to main content

netchan/
conn.rs

1// conn -- one connection's sequence numbers and acks, without I/O (docs/TRANSPORT.md "Folgenummern")
2//
3//   Conn::new(token, reliable, Settings, now_us)
4//   conn.write_data(payload, now, &mut out) -> seq     a DATA packet (empty payload: keep-alive)
5//   conn.on_data(seq, ack, bits, payload, now)         a DATA packet that came (token checked by
6//                                                      the caller)
7//   conn.take_event() -> Message / Delivered / Lost    conn.rate_ok(bytes, now), keepalive_due,
8//                                                      timed_out, stats, rtt_ms
9//   Conn::new(..).with_keys(Keys)                      sealed (UDP): write_data and
10//                                                      write_disconnect seal, open(wire, head,
11//                                                      seq) checks and decrypts what came
12//   conn.send_reliable(message, chunk_max)             the reliable channel (reliable.rs):
13//   conn.write_reliable(now, room, &mut out) -> seq    RELIABLE packets of the chunks due,
14//   conn.on_reliable(seq, ack, bits, payload, now)     what came (Event::Reliable: whole
15//                                                      messages in order); with_reliable(Limits)
16//
17// Every DATA packet has its own sequence number (u16, wrapping) and acknowledges the newest
18// number received plus the 32 before it as bits.  Unreliable (UDP): packets may come late, twice
19// or not at all; a number seen before (1024 back) is dropped, older ones too; whatever arrives
20// new is delivered at once, in the order it came.  A sent packet is Delivered when an ack names
21// it, Lost when the acks have moved more than 32 past it without naming it.  Reliable (WebSocket):
22// the transport keeps order and loses nothing, so an ack of n delivers everything up to n.
23// A peer that only receives (or sends less than one packet per 32 received) would let packets
24// pass the bits unacknowledged: after ACK_EVERY packets without one of its own, an unreliable
25// connection owes an empty DATA packet at once (ack_due).  RELIABLE packets are acknowledged
26// sooner, after two or within ACK_DELAY_US: their sender times its resends by the acks.
27// The round-trip time comes from the newest ack only (bits name older packets, whose wait would
28// count twice), smoothed as TCP does (RFC 6298).
29// Sealed (seal.rs): each packet sent takes the next packet number (u64; its low 16 bits are the
30// sequence number), the nonce of ChaCha20-Poly1305; open() takes the number nearest the largest
31// authenticated one and leaves the state alone when the tag does not check (Stats::forged).  The
32// receive window above then drops a replayed packet as a duplicate or stale: only after the tag.
33// RELIABLE packets take numbers and acks from the same sequence as DATA; their Delivered and
34// Lost come as events like those of keep-alives, and tell the reliable sender which chunks the
35// peer has.
36
37use alloc::collections::VecDeque;
38use alloc::vec;
39use alloc::vec::Vec;
40
41use crate::packet::{seq_newer, Packet, DATA_HEADER, DISCONNECT_HEADER, TAG};
42use crate::reliable::{self, Limits, Receiver, Sender};
43use crate::seal::{self, Keys};
44use crate::Error;
45
46/// how far back sequence numbers are remembered (sent and received)
47pub const WINDOW: usize = 1024;
48/// (unreliable) packets received without sending one, after which an ack is due (half the bits)
49pub const ACK_EVERY: u32 = 16;
50/// (unreliable) RELIABLE packets received without sending one, after which an ack is due: the
51/// peer waits for it to free its window and to send nothing twice (as TCP's delayed ack)
52pub const ACK_EVERY_RELIABLE: u32 = 2;
53/// (unreliable) the longest a RELIABLE packet waits for its ack (keepalive_due then holds)
54pub const ACK_DELAY_US: u64 = 10_000;
55
56/// the timing and limits of a connection
57#[derive(Clone, Copy, Debug)]
58pub struct Settings {
59    /// nothing received for this long ends the connection
60    pub timeout_ms: u32,
61    /// an empty DATA packet goes out when nothing was sent for this long (it carries the acks)
62    pub keepalive_ms: u32,
63    /// packets a second from the peer, on average (bursts of two seconds)
64    pub packets_per_s: u32,
65    /// bytes a second from the peer, on average (bursts of two seconds)
66    pub bytes_per_s: u32,
67}
68
69/// what a connection counted
70#[derive(Clone, Copy, Debug, Default, PartialEq)]
71pub struct Stats {
72    /// DATA packets sent (keep-alives included)
73    pub sent: u64,
74    /// DATA packets taken in (duplicates and stale ones not counted)
75    pub received: u64,
76    /// sent packets an ack named
77    pub delivered: u64,
78    /// sent packets the acks passed by
79    pub lost: u64,
80    /// packets that came a second time
81    pub duplicates: u64,
82    /// packets older than the window
83    pub stale: u64,
84    /// packets dropped for the rate limits or their size
85    pub dropped: u64,
86    /// sealed packets with this connection's token whose tag did not check (forged or damaged;
87    /// nothing else was done with them)
88    pub forged: u64,
89    /// bytes of DATA packets sent (headers included)
90    pub bytes_sent: u64,
91    /// bytes of DATA packets taken in (headers included)
92    pub bytes_received: u64,
93    /// chunks of the reliable channel sent again (UDP: lost, or no ack within the timeout)
94    pub resent: u64,
95    /// bytes of reliable messages the peer does not have yet
96    pub reliable_queued: u64,
97    /// the smoothed round-trip time (0 before the first sample)
98    pub rtt_ms: f32,
99    /// its variation
100    pub rtt_var_ms: f32,
101}
102
103/// what a connection reports
104#[derive(Clone, Debug, PartialEq, Eq)]
105#[non_exhaustive]
106pub enum Event {
107    /// a message of the peer with its sequence number
108    Message { seq: u16, data: Vec<u8> },
109    /// a message of the peer's reliable channel, whole and in order
110    Reliable(Vec<u8>),
111    /// the peer has a packet this side sent
112    Delivered(u16),
113    /// the peer's acks passed this packet by
114    Lost(u16),
115}
116
117/// a token bucket: `rate` a second, two seconds' worth saved up at most.  A take larger than the bucket goes
118/// when the bucket is full and leaves a debt the rate pays back (a reliable chunk of 64 KiB at 16 KiB/s stood
119/// for good: the bucket never held what it needed); the rate holds on average
120#[derive(Clone, Copy, Debug)]
121pub(crate) struct Bucket {
122    rate: i64,
123    // in millionths (µs × rate); below 0: a debt
124    level: i64,
125    last_us: u64,
126}
127
128impl Bucket {
129    pub(crate) fn new(rate: u32, now: u64) -> Bucket {
130        let rate = rate.max(1) as i64;
131        Bucket { rate, level: rate * 2_000_000, last_us: now }
132    }
133
134    pub(crate) fn take(&mut self, n: u64, now: u64) -> bool {
135        let cap = self.rate * 2_000_000;
136        let dt = now.saturating_sub(self.last_us).min(i64::MAX as u64) as i64;
137        self.last_us = now.max(self.last_us);
138        self.level = self.level.saturating_add(dt.saturating_mul(self.rate)).min(cap);
139        let need = (n.min(i64::MAX as u64) as i64).saturating_mul(1_000_000);
140        if self.level < need.min(cap) {
141            return false;
142        }
143        self.level = self.level.saturating_sub(need);
144        true
145    }
146
147    /// whether the bucket has filled up again (nothing taken lately)
148    pub(crate) fn full(&self, now: u64) -> bool {
149        let dt = now.saturating_sub(self.last_us).min(i64::MAX as u64) as i64;
150        self.level.saturating_add(dt.saturating_mul(self.rate)) >= self.rate * 2_000_000
151    }
152}
153
154const EMPTY: u8 = 0;
155const WAITING: u8 = 1;
156const DONE: u8 = 2;
157
158/// the state of one connection (see the top of this file)
159pub struct Conn {
160    token: u32,
161    reliable: bool,
162    set: Settings,
163    keys: Option<Keys>,
164    // the packet number the next sealed packet takes (its low bits: next_seq)
165    sent_pn: u64,
166    // the largest packet number authenticated
167    rx_pn: Option<u64>,
168    next_seq: u16,
169    sent_seq: Vec<u16>,
170    sent_time: Vec<u64>,
171    sent_state: Vec<u8>,
172    // the oldest sent number whose fate is open
173    oldest: u16,
174    recv_seq: Vec<u16>,
175    recv_have: Vec<bool>,
176    latest: u16,
177    any: bool,
178    last_send_us: u64,
179    last_recv_us: u64,
180    // packets received since this side last sent one
181    unacked_rx: u32,
182    // RELIABLE packets among them, and when the first came
183    unacked_rel: u32,
184    rel_since_us: u64,
185    srtt_us: f64,
186    rttvar_us: f64,
187    stats: Stats,
188    packets: Bucket,
189    bytes: Bucket,
190    events: VecDeque<Event>,
191    rel_tx: Sender,
192    rel_rx: Receiver,
193}
194
195impl Conn {
196    /// a connection with the token both sides put into DATA (0 over a WebSocket)
197    pub fn new(token: u32, reliable: bool, set: Settings, now_us: u64) -> Conn {
198        Conn {
199            token,
200            reliable,
201            set,
202            keys: None,
203            sent_pn: 0,
204            rx_pn: None,
205            next_seq: 0,
206            sent_seq: vec![0; WINDOW],
207            sent_time: vec![0; WINDOW],
208            sent_state: vec![EMPTY; WINDOW],
209            oldest: 0,
210            recv_seq: vec![0; WINDOW],
211            recv_have: vec![false; WINDOW],
212            latest: 0,
213            any: false,
214            last_send_us: now_us,
215            last_recv_us: now_us,
216            unacked_rx: 0,
217            unacked_rel: 0,
218            rel_since_us: 0,
219            srtt_us: 0.0,
220            rttvar_us: 0.0,
221            stats: Stats::default(),
222            packets: Bucket::new(set.packets_per_s, now_us),
223            bytes: Bucket::new(set.bytes_per_s, now_us),
224            events: VecDeque::new(),
225            rel_tx: Sender::new(Limits::default(), now_us),
226            rel_rx: Receiver::new(Limits::default().max_message),
227        }
228    }
229
230    /// the reliable channel with these limits (instead of Limits::default())
231    pub fn with_reliable(mut self, limits: Limits) -> Conn {
232        self.rel_tx = Sender::new(limits, self.last_send_us);
233        self.rel_rx = Receiver::new(limits.max_message);
234        self
235    }
236
237    /// the connection sealed with these keys (UDP after the handshake): every packet written is
238    /// encrypted and authenticated, every packet taken in must pass open()
239    pub fn with_keys(mut self, keys: Keys) -> Conn {
240        self.keys = Some(keys);
241        self
242    }
243
244    /// whether its packets are sealed
245    pub fn sealed(&self) -> bool {
246        self.keys.is_some()
247    }
248
249    /// the token of DATA and DISCONNECT packets
250    pub fn token(&self) -> u32 {
251        self.token
252    }
253
254    // the next packet number and its sequence number
255    fn take_pn(&mut self) -> (u64, u16) {
256        let pn = self.sent_pn;
257        self.sent_pn = pn.wrapping_add(1);
258        let seq = self.next_seq;
259        self.next_seq = seq.wrapping_add(1);
260        (pn, seq)
261    }
262
263    // seal out[start..]: the first `head` bytes authenticated, the rest encrypted, the tag after it
264    fn seal_tail(&self, pn: u64, out: &mut Vec<u8>, start: usize, head: usize) {
265        if let Some(k) = &self.keys {
266            let (aad, body) = out[start..].split_at_mut(head);
267            let tag = crypto::aead::seal_in_place(&k.send, &seal::nonce(pn), aad, body);
268            out.extend_from_slice(&tag);
269        }
270    }
271
272    /// check and decrypt a packet that came: `wire` the whole packet, `head` the bytes before its
273    /// payload (DATA_HEADER, DISCONNECT_HEADER), `seq` its sequence number -> the payload
274    /// (unsealed connections: the bytes after the header as they are).  None: too short or a
275    /// wrong tag (counted as forged); the connection's state is not touched then
276    pub fn open(&mut self, wire: &[u8], head: usize, seq: u16) -> Option<Vec<u8>> {
277        let Some(k) = &self.keys else { return wire.get(head..).map(|b| b.to_vec()) };
278        if wire.len() < head + TAG {
279            self.stats.forged += 1;
280            return None;
281        }
282        let pn = seal::full_pn(self.rx_pn, seq);
283        let (body, tag) = wire[head..].split_at(wire.len() - head - TAG);
284        let mut t = [0u8; TAG];
285        t.copy_from_slice(tag);
286        let mut plain = body.to_vec();
287        if !crypto::aead::open_in_place(&k.recv, &seal::nonce(pn), &wire[..head], &mut plain, &t) {
288            self.stats.forged += 1;
289            return None;
290        }
291        // authentic: a newer number moves the reference of the next reconstruction
292        self.rx_pn = Some(self.rx_pn.map_or(pn, |l| l.max(pn)));
293        Some(plain)
294    }
295
296    /// whether the transport is reliable (a WebSocket)
297    pub fn reliable(&self) -> bool {
298        self.reliable
299    }
300
301    /// the newest number received and the bits of the 32 before it (0xffff, 0 before any)
302    pub fn ack_fields(&self) -> (u16, u32) {
303        if !self.any {
304            return (0xffff, 0);
305        }
306        let mut bits = 0u32;
307        for i in 0..32u16 {
308            let s = self.latest.wrapping_sub(1 + i);
309            let k = s as usize % WINDOW;
310            if self.recv_have[k] && self.recv_seq[k] == s {
311                bits |= 1 << i;
312            }
313        }
314        (self.latest, bits)
315    }
316
317    /// append a DATA packet with `payload` (empty: a keep-alive), sealed if the connection is
318    /// -> its sequence number
319    pub fn write_data(&mut self, payload: &[u8], now: u64, out: &mut Vec<u8>) -> u16 {
320        self.write_packet(false, payload, now, out)
321    }
322
323    // the fate of a packet this side sent: the event, and the reliable chunks it carried
324    fn decide(&mut self, seq: u16, delivered: bool) {
325        if delivered {
326            self.stats.delivered += 1;
327            self.events.push_back(Event::Delivered(seq));
328            self.rel_tx.delivered(seq);
329        } else {
330            self.stats.lost += 1;
331            self.events.push_back(Event::Lost(seq));
332            self.rel_tx.lost(seq);
333        }
334    }
335
336    // a DATA or RELIABLE packet with the next number and the acks
337    fn write_packet(&mut self, reliable: bool, payload: &[u8], now: u64, out: &mut Vec<u8>) -> u16 {
338        let (pn, seq) = self.take_pn();
339        let k = seq as usize % WINDOW;
340        if self.sent_state[k] == WAITING {
341            // a whole window without an answer: decided now
342            let old = self.sent_seq[k];
343            self.sent_state[k] = DONE;
344            self.decide(old, self.reliable);
345        }
346        self.sent_seq[k] = seq;
347        self.sent_time[k] = now;
348        self.sent_state[k] = WAITING;
349        self.advance_oldest();
350        let (ack, bits) = self.ack_fields();
351        let before = out.len();
352        let token = self.token;
353        let p = if reliable { Packet::Reliable { token, seq, ack, bits, payload } } else { Packet::Data { token, seq, ack, bits, payload } };
354        p.write(out, false);
355        self.seal_tail(pn, out, before, DATA_HEADER);
356        self.last_send_us = now;
357        self.unacked_rx = 0;
358        self.unacked_rel = 0;
359        self.stats.sent += 1;
360        self.stats.bytes_sent += (out.len() - before) as u64;
361        seq
362    }
363
364    /// append a DISCONNECT with `reason` (BYE_*), sealed if the connection is; it takes a
365    /// sequence number of its own, so its seal never shares a nonce with a DATA packet
366    pub fn write_disconnect(&mut self, reason: u8, out: &mut Vec<u8>) {
367        let (pn, seq) = self.take_pn();
368        let before = out.len();
369        Packet::Disconnect { token: self.token, seq, reason, tag: &[] }.write(out, false);
370        self.seal_tail(pn, out, before, DISCONNECT_HEADER);
371    }
372
373    /// whether a packet of `bytes` from the peer stays within the rate limits (false: drop it,
374    /// or end a reliable connection)
375    pub fn rate_ok(&mut self, bytes: usize, now: u64) -> bool {
376        let ok = self.packets.take(1, now) && self.bytes.take(bytes as u64, now);
377        if !ok {
378            self.stats.dropped += 1;
379        }
380        ok
381    }
382
383    /// count a packet dropped by the caller (its size)
384    pub fn count_dropped(&mut self) {
385        self.stats.dropped += 1;
386    }
387
388    /// queue a message on the reliable channel, cut into chunks of at most `chunk_max` bytes
389    /// (the room of a packet less reliable::CHUNK_HEAD); Size: empty or over Limits::max_message,
390    /// Backlog: more than Limits::max_queued would wait
391    pub fn send_reliable(&mut self, msg: &[u8], chunk_max: usize) -> Result<(), Error> {
392        self.rel_tx.push(msg, chunk_max)
393    }
394
395    /// append a RELIABLE packet with the chunks that are due (new ones, and over UDP those whose
396    /// packet was lost or went unacknowledged past the timeout), at most `room` bytes of
397    /// payload -> its sequence number; None: nothing due now (or the rate of the channel spent)
398    pub fn write_reliable(&mut self, now: u64, room: usize, out: &mut Vec<u8>) -> Option<u16> {
399        let rto = reliable::rto_us(self.srtt_us, self.rttvar_us);
400        let mut payload = Vec::new();
401        let ids = self.rel_tx.fill(now, rto, !self.reliable, room, &mut payload);
402        if ids.is_empty() {
403            return None;
404        }
405        let seq = self.write_packet(true, &payload, now, out);
406        self.rel_tx.sent(seq, ids);
407        Some(seq)
408    }
409
410    /// whether the reliable channel has chunks the peer does not have yet
411    pub fn reliable_pending(&self) -> bool {
412        self.rel_tx.pending()
413    }
414
415    /// a DATA packet of the peer (its token already checked)
416    pub fn on_data(&mut self, seq: u16, ack: u16, bits: u32, payload: &[u8], now: u64) {
417        if self.take_in(seq, ack, bits, payload.len(), now) && !payload.is_empty() {
418            self.events.push_back(Event::Message { seq, data: payload.to_vec() });
419        }
420    }
421
422    /// a RELIABLE packet of the peer (its token checked, opened) -> false when its chunks break
423    /// the protocol (malformed, a message over max_message): the caller ends the connection
424    pub fn on_reliable(&mut self, seq: u16, ack: u16, bits: u32, payload: &[u8], now: u64) -> bool {
425        if !self.take_in(seq, ack, bits, payload.len(), now) {
426            return true;
427        }
428        if self.unacked_rel == 0 {
429            self.rel_since_us = now;
430        }
431        self.unacked_rel += 1;
432        if self.rel_rx.take(payload).is_err() {
433            return false;
434        }
435        while let Some(m) = self.rel_rx.message() {
436            self.events.push_back(Event::Reliable(m));
437        }
438        true
439    }
440
441    // a packet of the peer into the receive window and its acks -> whether it was new
442    fn take_in(&mut self, seq: u16, ack: u16, bits: u32, payload_len: usize, now: u64) -> bool {
443        let k = seq as usize % WINDOW;
444        if self.any {
445            if seq_newer(seq, self.latest) {
446                let gap = seq.wrapping_sub(self.latest) as usize;
447                if gap >= WINDOW {
448                    self.recv_have.iter_mut().for_each(|h| *h = false);
449                } else {
450                    for i in 1..gap {
451                        self.recv_have[self.latest.wrapping_add(i as u16) as usize % WINDOW] = false;
452                    }
453                }
454                self.latest = seq;
455            } else {
456                let age = self.latest.wrapping_sub(seq) as usize;
457                if age >= WINDOW {
458                    self.stats.stale += 1;
459                    return false;
460                }
461                if self.recv_have[k] && self.recv_seq[k] == seq {
462                    self.stats.duplicates += 1;
463                    return false;
464                }
465            }
466        } else {
467            self.any = true;
468            self.latest = seq;
469        }
470        self.recv_have[k] = true;
471        self.recv_seq[k] = seq;
472        self.last_recv_us = now;
473        self.unacked_rx = self.unacked_rx.saturating_add(1);
474        self.stats.received += 1;
475        self.stats.bytes_received += (DATA_HEADER + payload_len + if self.keys.is_some() { TAG } else { 0 }) as u64;
476        self.on_ack(ack, bits, now);
477        true
478    }
479
480    // the entry of a number this side sent and is still remembered
481    fn mine(&self, s: u16) -> Option<usize> {
482        let k = s as usize % WINDOW;
483        (self.sent_state[k] != EMPTY && self.sent_seq[k] == s && !seq_newer(s, self.next_seq.wrapping_sub(1))).then_some(k)
484    }
485
486    fn deliver(&mut self, k: usize) {
487        if self.sent_state[k] == WAITING {
488            self.sent_state[k] = DONE;
489            self.decide(self.sent_seq[k], true);
490        }
491    }
492
493    fn on_ack(&mut self, ack: u16, bits: u32, now: u64) {
494        // an ack of a number this side never sent (or forgot) says nothing
495        let Some(k) = self.mine(ack) else { return };
496        if self.sent_state[k] == WAITING {
497            let sample = now.saturating_sub(self.sent_time[k]) as f64;
498            if self.srtt_us == 0.0 {
499                self.srtt_us = sample.max(1.0);
500                self.rttvar_us = sample / 2.0;
501            } else {
502                let d = if self.srtt_us > sample { self.srtt_us - sample } else { sample - self.srtt_us };
503                self.rttvar_us = 0.75 * self.rttvar_us + 0.25 * d;
504                self.srtt_us = 0.875 * self.srtt_us + 0.125 * sample;
505            }
506            self.stats.rtt_ms = (self.srtt_us / 1000.0) as f32;
507            self.stats.rtt_var_ms = (self.rttvar_us / 1000.0) as f32;
508        }
509        self.deliver(k);
510        for i in 0..32u16 {
511            if bits & (1 << i) != 0 {
512                if let Some(k) = self.mine(ack.wrapping_sub(1 + i)) {
513                    self.deliver(k);
514                }
515            }
516        }
517        // the open ones behind the ack: reliable delivered, else lost once the bits passed them
518        let edge = if self.reliable { ack.wrapping_add(1) } else { ack.wrapping_sub(32) };
519        while self.oldest != self.next_seq && seq_newer(edge, self.oldest) {
520            let k = self.oldest as usize % WINDOW;
521            if self.sent_state[k] == WAITING && self.sent_seq[k] == self.oldest {
522                self.sent_state[k] = DONE;
523                self.decide(self.oldest, self.reliable);
524            }
525            self.oldest = self.oldest.wrapping_add(1);
526        }
527        self.advance_oldest();
528    }
529
530    // past the numbers already decided
531    fn advance_oldest(&mut self) {
532        while self.oldest != self.next_seq {
533            let k = self.oldest as usize % WINDOW;
534            if self.sent_state[k] == WAITING && self.sent_seq[k] == self.oldest {
535                break;
536            }
537            self.oldest = self.oldest.wrapping_add(1);
538        }
539    }
540
541    /// the next event (messages, deliveries, losses in the order they happened)
542    pub fn take_event(&mut self) -> Option<Event> {
543        self.events.pop_front()
544    }
545
546    /// (unreliable) whether an empty DATA packet is due at once: ACK_EVERY packets came without
547    /// one of this side's, so the older ones would pass the bits unacknowledged (or
548    /// ACK_EVERY_RELIABLE RELIABLE packets, whose sender waits for the acks)
549    pub fn ack_due(&self) -> bool {
550        !self.reliable && (self.unacked_rx >= ACK_EVERY || self.unacked_rel >= ACK_EVERY_RELIABLE)
551    }
552
553    /// whether a keep-alive is due (nothing sent for keepalive_ms; unreliable: or a RELIABLE
554    /// packet waited ACK_DELAY_US for its ack)
555    pub fn keepalive_due(&self, now: u64) -> bool {
556        now.saturating_sub(self.last_send_us) >= self.set.keepalive_ms as u64 * 1000
557            || (!self.reliable && self.unacked_rel > 0 && now.saturating_sub(self.rel_since_us) >= ACK_DELAY_US)
558    }
559
560    /// µs since the last packet of the peer that passed (authentic over UDP)
561    pub fn quiet_for(&self, now: u64) -> u64 {
562        now.saturating_sub(self.last_recv_us)
563    }
564
565    /// whether nothing came for timeout_ms
566    pub fn timed_out(&self, now: u64) -> bool {
567        now.saturating_sub(self.last_recv_us) >= self.set.timeout_ms as u64 * 1000
568    }
569
570    /// what the connection counted
571    pub fn stats(&self) -> Stats {
572        Stats { resent: self.rel_tx.resent, reliable_queued: self.rel_tx.queued() as u64, ..self.stats }
573    }
574
575    /// the smoothed round-trip time in ms (0 before the first ack)
576    pub fn rtt_ms(&self) -> f32 {
577        self.stats.rtt_ms
578    }
579
580    /// the sequence number the next packet gets
581    pub fn next_seq(&self) -> u16 {
582        self.next_seq
583    }
584}