http_type/stream/trait.rs
1use super::*;
2
3/// A bidirectional byte pipe that a request/response cycle can run over.
4///
5/// The transport is the only part of the server that knows what is carrying
6/// the bytes. Everything above this trait is protocol agnostic: a plain TCP
7/// socket, a TLS session and a per-request duplex stream are the same thing to
8/// the parsing pipeline once they are type erased behind `Transport`.
9///
10/// Implementations report their own socket metadata, so a transport without a
11/// socket identity (a duplex pair, an in-memory pipe) answers with an error
12/// instead of pretending to have an address.
13pub trait Transport: AsyncRead + AsyncWrite + Debug + Unpin + Send + Sync {
14 /// Reports the address of the remote end of the transport.
15 ///
16 /// # Returns
17 ///
18 /// - `Result<SocketAddr, io::Error>` - The remote address, or an error when the
19 /// transport has no socket identity.
20 fn peer_addr(&self) -> Result<SocketAddr, io::Error>;
21
22 /// Reports the address of the local end of the transport.
23 ///
24 /// # Returns
25 ///
26 /// - `Result<SocketAddr, io::Error>` - The local address, or an error when the
27 /// transport has no socket identity.
28 fn local_addr(&self) -> Result<SocketAddr, io::Error>;
29}