Debian Codegraph @

third_party/rust/neqo-transport/src/connection/saved.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::{mem, time::Instant};
8
9 use neqo_common::{qdebug, qinfo, Datagram};
10
11 use crate::crypto::Epoch;
12
13 /// The number of datagrams that are saved during the handshake when
14 /// keys to decrypt them are not yet available.
15 const MAX_SAVED_DATAGRAMS: usize = 4;
16
17 pub struct SavedDatagram {
18 /// The datagram.
19 pub d: Datagram,
20 /// The time that the datagram was received.
21 pub t: Instant,
22 }
23
24 #[derive(Default)]
25 pub struct SavedDatagrams {
26 handshake: Vec<SavedDatagram>,
27 application_data: Vec<SavedDatagram>,
28 available: Option<Epoch>,
29 }
30
31 impl SavedDatagrams {
32 fn store(&mut self, epoch: Epoch) -> &mut Vec<SavedDatagram> {
33 match epoch {
34 Epoch::Handshake => &mut self.handshake,
35 Epoch::ApplicationData => &mut self.application_data,
36 _ => panic!("unexpected space"),
37 }
38 }
39
40 pub fn save(&mut self, epoch: Epoch, d: Datagram, t: Instant) {
41 let store = self.store(epoch);
42
43 if store.len() < MAX_SAVED_DATAGRAMS {
44 qdebug!("saving datagram of {} bytes", d.len());
45 store.push(SavedDatagram { d, t });
46 } else {
47 qinfo!("not saving datagram of {} bytes", d.len());
48 }
49 }
50
51 pub fn make_available(&mut self, epoch: Epoch) {
52 debug_assert_ne!(epoch, Epoch::ZeroRtt);
53 debug_assert_ne!(epoch, Epoch::Initial);
54 if !self.store(epoch).is_empty() {
55 self.available = Some(epoch);
56 }
57 }
58
59 pub const fn available(&self) -> Option<Epoch> {
60 self.available
61 }
62
63 pub fn take_saved(&mut self) -> Vec<SavedDatagram> {
64 self.available
65 .take()
66 .map_or_else(Vec::new, |epoch| mem::take(self.store(epoch)))
67 }
68 }