s2n_quic_core/endpoint/
mod.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    io::{rx, tx},
6    path::{self, mtu},
7    time::{Clock, Timestamp},
8};
9use core::{
10    fmt,
11    future::Future,
12    task::{Context, Poll},
13};
14
15pub mod limits;
16pub use limits::Limiter;
17
18/// Enumerates endpoint types
19#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
20pub enum Type {
21    /// The endpoint is a client
22    Client,
23    /// The endpoint is a server
24    Server,
25}
26
27impl Type {
28    /// Returns true if the given endpoint is a QUIC client
29    pub fn is_client(self) -> bool {
30        self == Self::Client
31    }
32
33    /// Returns true if the given endpoint is a QUIC server
34    pub fn is_server(self) -> bool {
35        self == Self::Server
36    }
37
38    /// Returns the [`Type`] of the peer.
39    /// - If called on `Client` this will return `Server`
40    /// - If called on `Server` this will return `Client`
41    #[must_use]
42    pub fn peer_type(self) -> Self {
43        match self {
44            Self::Client => Self::Server,
45            Self::Server => Self::Client,
46        }
47    }
48}
49
50/// Enumerates endpoint locations
51#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub enum Location {
53    /// The local endpoint
54    Local,
55    /// The remote endpoint
56    Remote,
57}
58
59impl fmt::Display for Location {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        match self {
62            Self::Local => write!(f, "the local endpoint"),
63            Self::Remote => write!(f, "the remote endpoint"),
64        }
65    }
66}
67
68impl Location {
69    /// Returns true if the given endpoint is local
70    pub fn is_local(self) -> bool {
71        self == Self::Local
72    }
73
74    /// Returns true if the given endpoint is remote
75    pub fn is_remote(self) -> bool {
76        self == Self::Remote
77    }
78
79    /// Returns the [`Location`] of the peer.
80    /// - If called on `Local` this will return `Remote`
81    /// - If called on `Remote` this will return `Local`
82    #[must_use]
83    pub fn peer_type(self) -> Self {
84        match self {
85            Self::Local => Self::Remote,
86            Self::Remote => Self::Local,
87        }
88    }
89}
90
91/// The main interface for a QUIC endpoint
92pub trait Endpoint: 'static + Send + Sized {
93    type PathHandle: path::Handle;
94    type Subscriber: crate::event::Subscriber;
95
96    const ENDPOINT_TYPE: Type;
97
98    /// Receives and processes datagrams for the Rx queue
99    fn receive<Rx, C>(&mut self, rx: &mut Rx, clock: &C)
100    where
101        Rx: rx::Queue<Handle = Self::PathHandle>,
102        C: Clock;
103
104    /// Transmits outgoing datagrams into the Tx queue
105    fn transmit<Tx, C>(&mut self, tx: &mut Tx, clock: &C)
106    where
107        Tx: tx::Queue<Handle = Self::PathHandle>,
108        C: Clock;
109
110    /// Returns a future which polls for application-space wakeups
111    ///
112    /// When successful, the number of wakeups is returned.
113    fn wakeups<'a, C: Clock>(&'a mut self, clock: &'a C) -> Wakeups<'a, Self, C> {
114        Wakeups {
115            endpoint: self,
116            clock,
117        }
118    }
119
120    /// Polls for any application-space wakeups
121    ///
122    /// When successful, the number of wakeups is returned.
123    fn poll_wakeups<C: Clock>(
124        &mut self,
125        cx: &mut Context<'_>,
126        clock: &C,
127    ) -> Poll<Result<usize, CloseError>>;
128
129    /// Returns the latest Timestamp at which `transmit` should be called
130    fn timeout(&self) -> Option<Timestamp>;
131
132    /// Sets configuration for the maximum transmission unit (MTU) that can be sent on a path
133    fn set_mtu_config(&mut self, mtu_config: mtu::Config);
134
135    /// Returns the endpoint's event subscriber
136    fn subscriber(&mut self) -> &mut Self::Subscriber;
137}
138
139/// A future which polls an endpoint for application-space wakeups
140pub struct Wakeups<'a, E: Endpoint, C: Clock> {
141    endpoint: &'a mut E,
142    clock: &'a C,
143}
144
145impl<E: Endpoint, C: Clock> Future for Wakeups<'_, E, C> {
146    type Output = Result<usize, CloseError>;
147
148    fn poll(mut self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
149        let clock = self.clock;
150        self.endpoint.poll_wakeups(cx, clock)
151    }
152}
153
154/// Indicates the endpoint is no longer processing connections.
155#[derive(Clone, Copy, Debug)]
156pub struct CloseError;