use std::{future::Future, net::SocketAddr};
use bytes::Bytes;
use memberlist_core::transport::TimeoutableStream;
#[cfg(feature = "quinn")]
#[cfg_attr(docsrs, doc(cfg(feature = "quinn")))]
pub mod quinn;
#[cfg(feature = "s2n")]
#[cfg_attr(docsrs, doc(cfg(feature = "s2n")))]
pub mod s2n;
#[auto_impl::auto_impl(Box, Arc)]
pub trait QuicError: std::error::Error + Send + Sync + 'static {
fn is_remote_failure(&self) -> bool;
}
#[auto_impl::auto_impl(Box)]
pub trait QuicStream: TimeoutableStream + futures::AsyncRead + Send + Sync + 'static {
type Error: QuicError;
fn write_all(&mut self, src: Bytes) -> impl Future<Output = Result<usize, Self::Error>> + Send;
fn flush(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>> + Send;
fn read_exact(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn peek(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>> + Send;
fn peek_exact(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn finish(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn close(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
#[auto_impl::auto_impl(Box, Arc)]
pub trait StreamLayer: Sized + Send + Sync + 'static {
type Error: QuicError
+ From<<Self::Stream as QuicStream>::Error>
+ From<<Self::Connection as QuicConnection>::Error>
+ From<<Self::Acceptor as QuicAcceptor>::Error>
+ From<<Self::Connector as QuicConnector>::Error>;
type Acceptor: QuicAcceptor<Connection = Self::Connection>;
type Connector: QuicConnector<Connection = Self::Connection>;
type Stream: QuicStream;
type Connection: QuicConnection<Stream = Self::Stream>;
fn max_stream_data(&self) -> usize;
fn bind(
&self,
addr: SocketAddr,
) -> impl Future<Output = std::io::Result<(SocketAddr, Self::Acceptor, Self::Connector)>> + Send;
}
#[auto_impl::auto_impl(Box)]
pub trait QuicAcceptor: Send + Sync + 'static {
type Error: std::error::Error + Send + Sync + 'static;
type Connection: QuicConnection;
fn accept(
&mut self,
) -> impl Future<Output = Result<(Self::Connection, SocketAddr), Self::Error>> + Send;
fn close(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn local_addr(&self) -> SocketAddr;
}
#[auto_impl::auto_impl(Box, Arc)]
pub trait QuicConnector: Send + Sync + 'static {
type Error: std::error::Error + Send + Sync + 'static;
type Connection: QuicConnection;
fn connect(
&self,
addr: SocketAddr,
) -> impl Future<Output = Result<Self::Connection, Self::Error>> + Send;
fn connect_with_timeout(
&self,
addr: SocketAddr,
timeout: std::time::Duration,
) -> impl Future<Output = Result<Self::Connection, Self::Error>> + Send;
fn close(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn wait_idle(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn local_addr(&self) -> SocketAddr;
}
#[auto_impl::auto_impl(Box, Arc)]
pub trait QuicConnection: Send + Sync + 'static {
type Error: std::error::Error + Send + Sync + 'static;
type Stream: QuicStream;
fn accept_bi(
&self,
) -> impl Future<Output = Result<(Self::Stream, SocketAddr), Self::Error>> + Send;
fn open_bi(&self)
-> impl Future<Output = Result<(Self::Stream, SocketAddr), Self::Error>> + Send;
fn open_bi_with_deadline(
&self,
deadline: std::time::Instant,
) -> impl Future<Output = Result<(Self::Stream, SocketAddr), Self::Error>> + Send;
fn close(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn is_closed(&self) -> impl Future<Output = bool> + Send;
fn local_addr(&self) -> SocketAddr;
fn is_full(&self) -> bool;
}