Skip to main content

qbase/
lib.rs

1#![allow(clippy::all)]
2//! # The QUIC base library
3//!
4//! The `qbase` library defines the necessary basic structures in the QUIC protocol,
5//! including connection IDs, stream IDs, frames, packets, keys, parameters, error codes, etc.
6//!
7//! Additionally, based on these basic structures,
8//! it defines components for various mechanisms in QUIC,
9//! including flow control, handshake, tokens, stream ID management, connection ID management, etc.
10//!
11//! Finally, the `qbase` module also defines some utility functions
12//! for handling common data structures in the QUIC protocol.
13//!
14#![allow(clippy::all)]
15use std::{
16    ops::{Index, IndexMut},
17    pin::Pin,
18    sync::{Arc, Mutex},
19    task::{Context, Poll, Waker},
20};
21
22use futures::FutureExt;
23use thiserror::Error;
24
25/// Operations about QUIC connection IDs.
26pub mod cid;
27/// [QUIC errors](https://www.rfc-editor.org/rfc/rfc9000.html#name-error-codes).
28pub mod error;
29/// QUIC connection-level flow control.
30pub mod flow;
31/// QUIC frames and their codec.
32pub mod frame;
33/// Handshake signal for QUIC connections.
34pub mod handshake;
35/// QUIC connection metrics for tracking data volumes.
36pub mod metric;
37/// Endpoint address and Pathway.
38pub mod net;
39/// QUIC packets and their codec.
40pub mod packet;
41/// [QUIC transport parameters and their codec](https://www.rfc-editor.org/rfc/rfc9000.html#name-transport-parameter-encodin).
42pub mod param;
43/// QUIC client and server roles.
44pub mod role;
45/// Stream id types and controllers for different roles and different directions.
46pub mod sid;
47/// Max idle timer and defer idle timer.
48pub mod time;
49/// Issuing, storing and verifing tokens operations.
50pub mod token;
51/// Utilities for common data structures.
52pub mod util;
53/// [Variable-length integers](https://www.rfc-editor.org/rfc/rfc9000.html#name-variable-length-integer-enc).
54pub mod varint;
55
56/// The epoch of sending, usually been seen as the index of spaces.
57#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
58pub enum Epoch {
59    Initial = 0,
60    Handshake = 1,
61    Data = 2,
62}
63
64pub trait GetEpoch {
65    fn epoch(&self) -> Epoch;
66}
67
68impl Epoch {
69    pub const EPOCHS: [Epoch; 3] = [Epoch::Initial, Epoch::Handshake, Epoch::Data];
70    /// An iterator for the epoch of each spaces.
71    ///
72    /// Equals to `Epoch::EPOCHES.iter()`
73    pub fn iter() -> std::slice::Iter<'static, Epoch> {
74        Self::EPOCHS.iter()
75    }
76
77    /// The number of epoches.
78    pub const fn count() -> usize {
79        Self::EPOCHS.len()
80    }
81}
82
83impl<T> Index<Epoch> for [T]
84where
85    T: Sized,
86{
87    type Output = T;
88
89    fn index(&self, index: Epoch) -> &Self::Output {
90        self.index(index as usize)
91    }
92}
93
94impl<T> IndexMut<Epoch> for [T]
95where
96    T: Sized,
97{
98    fn index_mut(&mut self, index: Epoch) -> &mut Self::Output {
99        self.index_mut(index as usize)
100    }
101}
102
103#[derive(Debug, Default)]
104pub enum Receiving<F> {
105    #[default]
106    Pending,
107    Waiting(Waker),
108    Rcvd(F),
109    Read,
110    Reset,
111}
112
113impl<F> Receiving<F> {
114    fn recv_frame(&mut self, frame: F) {
115        match std::mem::take(self) {
116            Self::Pending => {
117                *self = Self::Rcvd(frame);
118            }
119            Self::Waiting(waker) => {
120                waker.wake();
121                *self = Self::Rcvd(frame);
122            }
123            _ => (),
124        }
125    }
126
127    fn reset(&mut self) {
128        if let Self::Waiting(waker) = std::mem::replace(self, Self::Reset) {
129            waker.wake();
130        }
131    }
132}
133
134#[derive(Debug, Error)]
135#[error("Reset")]
136pub struct ResetError;
137
138#[derive(Debug, Default, Clone)]
139pub struct ArcReceiving<F>(Arc<Mutex<Receiving<F>>>);
140
141impl<F> ArcReceiving<F> {
142    pub fn reset(&self) {
143        self.0.lock().unwrap().reset();
144    }
145}
146
147impl<F: Unpin> Future for ArcReceiving<F> {
148    type Output = Result<Option<F>, ResetError>;
149
150    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
151        self.0.lock().unwrap().poll_unpin(cx)
152    }
153}
154
155#[cfg(test)]
156mod tests {}