memberlist_quic/
stream_layer.rs

1use std::{future::Future, io, net::SocketAddr};
2
3use agnostic_lite::RuntimeLite;
4use bytes::Bytes;
5
6/// QUIC stream layer based on [`quinn`](::quinn).
7#[cfg(feature = "quinn")]
8#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
9pub mod quinn;
10
11/// A trait for QUIC bidirectional streams.
12pub trait QuicStream:
13  memberlist_core::transport::Connection + Unpin + Send + Sync + 'static
14{
15  /// The send stream type.
16  type SendStream: memberlist_core::proto::ProtoWriter;
17
18  /// Read a packet from the the stream.
19  fn read_packet(&mut self) -> impl Future<Output = std::io::Result<Bytes>> + Send;
20}
21
22/// A trait for QUIC stream layers.
23pub trait StreamLayer: Sized + Send + Sync + 'static {
24  /// The runtime for this stream layer.
25  type Runtime: RuntimeLite;
26
27  /// The acceptor type.
28  type Acceptor: QuicAcceptor<Connection = Self::Connection>;
29
30  /// The connector type.
31  type Connector: QuicConnector<Connection = Self::Connection>;
32
33  /// The bidirectional stream type.
34  type Stream: QuicStream;
35
36  /// The connection type.
37  type Connection: QuicConnection<Stream = Self::Stream>;
38
39  /// The options type used to construct the stream layer.
40  type Options: Send + Sync + 'static;
41
42  /// Max unacknowledged data in bytes that may be send on a single stream.
43  fn max_stream_data(&self) -> usize;
44
45  /// Creates a new stream layer.
46  fn new(options: Self::Options) -> impl Future<Output = std::io::Result<Self>> + Send;
47
48  /// Binds to a local address. The `BiAcceptor` and `UniAcceptor` must bind to
49  /// the same address.
50  fn bind(
51    &self,
52    addr: SocketAddr,
53  ) -> impl Future<Output = std::io::Result<(SocketAddr, Self::Acceptor, Self::Connector)>> + Send;
54}
55
56/// A trait for QUIC acceptors. The stream layer will use this trait to
57/// accept a bi-directional connection from a remote peer.
58#[auto_impl::auto_impl(Box)]
59pub trait QuicAcceptor: Send + Sync + 'static {
60  /// The connection type.
61  type Connection: QuicConnection;
62
63  /// Accepts a connection from a remote peer.
64  fn accept(&mut self) -> impl Future<Output = io::Result<(Self::Connection, SocketAddr)>> + Send;
65
66  /// Close the acceptor.
67  fn close(&mut self) -> impl Future<Output = io::Result<()>> + Send;
68
69  /// Returns the local address.
70  fn local_addr(&self) -> SocketAddr;
71}
72
73/// A trait for QUIC connectors. The stream layer will use this trait to
74/// establish a connection to a remote peer.
75#[auto_impl::auto_impl(Box, Arc)]
76pub trait QuicConnector: Send + Sync + 'static {
77  /// The bidirectional stream type.
78  type Connection: QuicConnection;
79
80  /// Connects to a remote peer.
81  fn connect(&self, addr: SocketAddr) -> impl Future<Output = io::Result<Self::Connection>> + Send;
82
83  /// Closes the connector.
84  fn close(&self) -> impl Future<Output = io::Result<()>> + Send;
85
86  /// Wait for all connections on the endpoint to be cleanly shut down
87  ///
88  /// Waiting for this condition before exiting ensures that a good-faith effort is made to notify
89  /// peers of recent connection closes, whereas exiting immediately could force them to wait out
90  /// the idle timeout period.
91  ///
92  /// Does not proactively close existing connections or cause incoming connections to be
93  /// rejected. Consider calling `close()` if that is desired.
94  fn wait_idle(&self) -> impl Future<Output = io::Result<()>> + Send;
95
96  /// Returns the local address.
97  fn local_addr(&self) -> SocketAddr;
98}
99
100/// A connection between local and remote peer.
101#[auto_impl::auto_impl(Box, Arc)]
102pub trait QuicConnection: Send + Sync + 'static {
103  /// The bidirectional stream type.
104  type Stream: QuicStream;
105
106  /// Accepts a bidirectional stream from a remote peer.
107  fn accept_bi(&self) -> impl Future<Output = io::Result<(Self::Stream, SocketAddr)>> + Send;
108
109  /// Opens a bidirectional stream to a remote peer.
110  fn open_bi(&self) -> impl Future<Output = io::Result<(Self::Stream, SocketAddr)>> + Send;
111
112  /// Closes the connection.
113  fn close(&self) -> impl Future<Output = io::Result<()>> + Send;
114
115  /// Returns `true` if the connection is closed.
116  fn is_closed(&self) -> impl Future<Output = bool> + Send;
117
118  /// Returns the local address.
119  fn local_addr(&self) -> SocketAddr;
120}