libp2p

Trait Transport

pub trait Transport {
    type Output;
    type Error: Error;
    type ListenerUpgrade: Future<Output = Result<Self::Output, Self::Error>>;
    type Dial: Future<Output = Result<Self::Output, Self::Error>>;

    // Required methods
    fn listen_on(
        &mut self,
        id: ListenerId,
        addr: Multiaddr,
    ) -> Result<(), TransportError<Self::Error>>;
    fn remove_listener(&mut self, id: ListenerId) -> bool;
    fn dial(
        &mut self,
        addr: Multiaddr,
        opts: DialOpts,
    ) -> Result<Self::Dial, TransportError<Self::Error>>;
    fn poll(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>>;

    // Provided methods
    fn boxed(self) -> Boxed<Self::Output>
       where Self: Sized + Send + Unpin + 'static,
             Self::Dial: Send + 'static,
             Self::ListenerUpgrade: Send + 'static,
             Self::Error: Send + Sync { ... }
    fn map<F, O>(self, f: F) -> Map<Self, F>
       where Self: Sized,
             F: FnOnce(Self::Output, ConnectedPoint) -> O { ... }
    fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
       where Self: Sized,
             F: FnOnce(Self::Error) -> E { ... }
    fn or_transport<U>(self, other: U) -> OrTransport<Self, U>
       where Self: Sized,
             U: Transport,
             <U as Transport>::Error: 'static { ... }
    fn and_then<C, F, O>(self, f: C) -> AndThen<Self, C>
       where Self: Sized,
             C: FnOnce(Self::Output, ConnectedPoint) -> F,
             F: TryFuture<Ok = O>,
             <F as TryFuture>::Error: Error + 'static { ... }
    fn upgrade(self, version: Version) -> Builder<Self>
       where Self: Sized,
             Self::Error: 'static { ... }
}
Expand description

A transport provides connection-oriented communication between two peers through ordered streams of data (i.e. connections).

Connections are established either by listening or dialing on a Transport. A peer that obtains a connection by listening is often referred to as the listener and the peer that initiated the connection through dialing as the dialer, in contrast to the traditional roles of server and client.

Most transports also provide a form of reliable delivery on the established connections but the precise semantics of these guarantees depend on the specific transport.

This trait is implemented for concrete connection-oriented transport protocols like TCP or Unix Domain Sockets, but also on wrappers that add additional functionality to the dialing or listening process (e.g. name resolution via the DNS).

Additional protocols can be layered on top of the connections established by a Transport through an upgrade mechanism that is initiated via upgrade.

Note for implementors: Futures returned by Transport::dial should only do work once polled for the first time. E.g. in the case of TCP, connecting to the remote should not happen immediately on Transport::dial but only once the returned Future is polled. The caller of Transport::dial may call the method multiple times with a set of addresses, racing a subset of the returned dials to success concurrently.

Required Associated Types§

type Output

The result of a connection setup process, including protocol upgrades.

Typically the output contains at least a handle to a data stream (i.e. a connection or a substream multiplexer on top of a connection) that provides APIs for sending and receiving data through the connection.

type Error: Error

An error that occurred during connection setup.

type ListenerUpgrade: Future<Output = Result<Self::Output, Self::Error>>

A pending Output for an inbound connection, obtained from the Transport stream.

After a connection has been accepted by the transport, it may need to go through asynchronous post-processing (i.e. protocol upgrade negotiations). Such post-processing should not block the Listener from producing the next connection, hence further connection setup proceeds asynchronously. Once a ListenerUpgrade future resolves it yields the Output of the connection setup process.

type Dial: Future<Output = Result<Self::Output, Self::Error>>

A pending Output for an outbound connection, obtained from dialing.

Required Methods§

fn listen_on( &mut self, id: ListenerId, addr: Multiaddr, ) -> Result<(), TransportError<Self::Error>>

Listens on the given Multiaddr for inbound connections with a provided ListenerId.

fn remove_listener(&mut self, id: ListenerId) -> bool

Remove a listener.

Return true if there was a listener with this Id, false otherwise.

fn dial( &mut self, addr: Multiaddr, opts: DialOpts, ) -> Result<Self::Dial, TransportError<Self::Error>>

Dials the given Multiaddr, returning a future for a pending outbound connection.

If TransportError::MultiaddrNotSupported is returned, it may be desirable to try an alternative Transport, if available.

fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>>

Poll for TransportEvents.

A TransportEvent::Incoming should be produced whenever a connection is received at the lowest level of the transport stack. The item must be a ListenerUpgrade future that resolves to an Output value once all protocol upgrades have been applied.

Transports are expected to produce TransportEvent::Incoming events only for listen addresses which have previously been announced via a TransportEvent::NewAddress event and which have not been invalidated by an TransportEvent::AddressExpired event yet.

Provided Methods§

fn boxed(self) -> Boxed<Self::Output>
where Self: Sized + Send + Unpin + 'static, Self::Dial: Send + 'static, Self::ListenerUpgrade: Send + 'static, Self::Error: Send + Sync,

Boxes the transport, including custom transport errors.

fn map<F, O>(self, f: F) -> Map<Self, F>
where Self: Sized, F: FnOnce(Self::Output, ConnectedPoint) -> O,

Applies a function on the connections created by the transport.

fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where Self: Sized, F: FnOnce(Self::Error) -> E,

Applies a function on the errors generated by the futures of the transport.

fn or_transport<U>(self, other: U) -> OrTransport<Self, U>
where Self: Sized, U: Transport, <U as Transport>::Error: 'static,

Adds a fallback transport that is used when encountering errors while establishing inbound or outbound connections.

The returned transport will act like self, except that if listen_on or dial return an error then other will be tried.

fn and_then<C, F, O>(self, f: C) -> AndThen<Self, C>
where Self: Sized, C: FnOnce(Self::Output, ConnectedPoint) -> F, F: TryFuture<Ok = O>, <F as TryFuture>::Error: Error + 'static,

Applies a function producing an asynchronous result to every connection created by this transport.

This function can be used for ad-hoc protocol upgrades or for processing or adapting the output for following configurations.

For the high-level transport upgrade procedure, see Transport::upgrade.

fn upgrade(self, version: Version) -> Builder<Self>
where Self: Sized, Self::Error: 'static,

Begins a series of protocol upgrades via an upgrade::Builder.

Implementations on Foreign Types§

§

impl<A, B> Transport for Either<A, B>
where B: Transport, A: Transport,

§

type Output = Either<<A as Transport>::Output, <B as Transport>::Output>

§

type Error = Either<<A as Transport>::Error, <B as Transport>::Error>

§

type ListenerUpgrade = EitherFuture<<A as Transport>::ListenerUpgrade, <B as Transport>::ListenerUpgrade>

§

type Dial = EitherFuture<<A as Transport>::Dial, <B as Transport>::Dial>

§

fn poll( self: Pin<&mut Either<A, B>>, cx: &mut Context<'_>, ) -> Poll<TransportEvent<<Either<A, B> as Transport>::ListenerUpgrade, <Either<A, B> as Transport>::Error>>

§

fn remove_listener(&mut self, id: ListenerId) -> bool

§

fn listen_on( &mut self, id: ListenerId, addr: Multiaddr, ) -> Result<(), TransportError<<Either<A, B> as Transport>::Error>>

§

fn dial( &mut self, addr: Multiaddr, opts: DialOpts, ) -> Result<<Either<A, B> as Transport>::Dial, TransportError<<Either<A, B> as Transport>::Error>>

§

impl<P> Transport for GenTransport<P>
where P: Provider,

§

type Output = (PeerId, Connection)

§

type Error = Error

§

type ListenerUpgrade = Connecting

§

type Dial = Pin<Box<dyn Future<Output = Result<<GenTransport<P> as Transport>::Output, <GenTransport<P> as Transport>::Error>> + Send>>

§

fn listen_on( &mut self, listener_id: ListenerId, addr: Multiaddr, ) -> Result<(), TransportError<<GenTransport<P> as Transport>::Error>>

§

fn remove_listener(&mut self, id: ListenerId) -> bool

§

fn dial( &mut self, addr: Multiaddr, dial_opts: DialOpts, ) -> Result<<GenTransport<P> as Transport>::Dial, TransportError<<GenTransport<P> as Transport>::Error>>

§

fn poll( self: Pin<&mut GenTransport<P>>, cx: &mut Context<'_>, ) -> Poll<TransportEvent<<GenTransport<P> as Transport>::ListenerUpgrade, <GenTransport<P> as Transport>::Error>>

Implementors§

§

impl Transport for MemoryTransport

§

impl Transport for libp2p::relay::client::Transport

§

impl Transport for libp2p::websocket_websys::Transport

§

impl Transport for libp2p::webtransport_websys::Transport

§

impl<A, B> Transport for OrTransport<A, B>
where B: Transport, A: Transport,

§

impl<InnerTrans> Transport for TransportTimeout<InnerTrans>
where InnerTrans: Transport, <InnerTrans as Transport>::Error: 'static,

§

type Output = <InnerTrans as Transport>::Output

§

type Error = TransportTimeoutError<<InnerTrans as Transport>::Error>

§

type ListenerUpgrade = Timeout<<InnerTrans as Transport>::ListenerUpgrade>

§

type Dial = Timeout<<InnerTrans as Transport>::Dial>

§

impl<O> Transport for Boxed<O>

§

type Output = O

§

type Error = Error

§

type ListenerUpgrade = Pin<Box<dyn Future<Output = Result<O, Error>> + Send>>

§

type Dial = Pin<Box<dyn Future<Output = Result<O, Error>> + Send>>

§

impl<T> Transport for libp2p::core::transport::global_only::Transport<T>
where T: Transport + Unpin,

§

impl<T> Transport for OptionalTransport<T>
where T: Transport,

§

impl<T> Transport for Multiplexed<T>
where T: Transport,

§

impl<T> Transport for libp2p::tcp::Transport<T>
where T: Provider + Send + 'static, <T as Provider>::Listener: Unpin, <T as Provider>::Stream: Unpin,

§

type Output = <T as Provider>::Stream

§

type Error = Error

§

type Dial = Pin<Box<dyn Future<Output = Result<<Transport<T> as Transport>::Output, <Transport<T> as Transport>::Error>> + Send>>

§

type ListenerUpgrade = Ready<Result<<Transport<T> as Transport>::Output, <Transport<T> as Transport>::Error>>

Source§

impl<T> Transport for libp2p::websocket::framed::WsConfig<T>
where T: Transport + Send + Unpin + 'static, <T as Transport>::Error: Send + 'static, <T as Transport>::Dial: Send + 'static, <T as Transport>::ListenerUpgrade: Send + 'static, <T as Transport>::Output: AsyncRead + AsyncWrite + Unpin + Send + 'static,

Source§

impl<T> Transport for libp2p::websocket::WsConfig<T>
where T: Transport + Send + Unpin + 'static, <T as Transport>::Error: Send + 'static, <T as Transport>::Dial: Send + 'static, <T as Transport>::ListenerUpgrade: Send + 'static, <T as Transport>::Output: AsyncRead + AsyncWrite + Unpin + Send + 'static,

Source§

type Output = RwStreamSink<BytesConnection<<T as Transport>::Output>>

Source§

type Error = Error<<T as Transport>::Error>

Source§

type ListenerUpgrade = MapFuture<Pin<Box<dyn Future<Output = Result<Connection<<T as Transport>::Output>, Error<<T as Transport>::Error>>> + Send>>, fn(_: Connection<<T as Transport>::Output>, _: ConnectedPoint) -> RwStreamSink<BytesConnection<<T as Transport>::Output>>>

Source§

type Dial = MapFuture<Pin<Box<dyn Future<Output = Result<Connection<<T as Transport>::Output>, Error<<T as Transport>::Error>>> + Send>>, fn(_: Connection<<T as Transport>::Output>, _: ConnectedPoint) -> RwStreamSink<BytesConnection<<T as Transport>::Output>>>

§

impl<T, C, D, U, E> Transport for Upgrade<T, U>
where T: Transport<Output = (PeerId, C)>, <T as Transport>::Error: 'static, C: AsyncRead + AsyncWrite + Unpin, U: InboundConnectionUpgrade<Negotiated<C>, Output = D, Error = E> + OutboundConnectionUpgrade<Negotiated<C>, Output = D, Error = E> + Clone, E: Error + 'static,

§

impl<T, C, F, O> Transport for AndThen<T, C>
where T: Transport, C: FnOnce(<T as Transport>::Output, ConnectedPoint) -> F + Clone, F: TryFuture<Ok = O>, <F as TryFuture>::Error: Error,

§

type Output = O

§

type Error = Either<<T as Transport>::Error, <F as TryFuture>::Error>

§

type ListenerUpgrade = AndThenFuture<<T as Transport>::ListenerUpgrade, C, F>

§

type Dial = AndThenFuture<<T as Transport>::Dial, C, F>

§

impl<T, F, D> Transport for Map<T, F>
where T: Transport, F: FnOnce(<T as Transport>::Output, ConnectedPoint) -> D + Clone,

§

impl<T, F, TErr> Transport for MapErr<T, F>
where T: Transport, F: FnOnce(<T as Transport>::Error) -> TErr + Clone, TErr: Error,

§

type Output = <T as Transport>::Output

§

type Error = TErr

§

type ListenerUpgrade = MapErrListenerUpgrade<T, F>

§

type Dial = MapErrDial<T, F>

Source§

impl<T, M> Transport for libp2p::metrics::BandwidthTransport<T>
where T: Transport<Output = (PeerId, M)>, M: StreamMuxer + Send + 'static, <M as StreamMuxer>::Substream: Send + 'static, <M as StreamMuxer>::Error: Send + Sync + 'static,

Source§

type Output = (PeerId, Muxer<M>)

Source§

type Error = <T as Transport>::Error

Source§

type ListenerUpgrade = MapOk<<T as Transport>::ListenerUpgrade, Box<dyn FnOnce((PeerId, M)) -> (PeerId, Muxer<M>) + Send>>

Source§

type Dial = MapOk<<T as Transport>::Dial, Box<dyn FnOnce((PeerId, M)) -> (PeerId, Muxer<M>) + Send>>

§

impl<T, R> Transport for libp2p::dns::Transport<T, R>
where T: Transport + Send + Unpin + 'static, <T as Transport>::Error: Send, <T as Transport>::Dial: Send, R: Clone + Send + Sync + Resolver + 'static,

§

type Output = <T as Transport>::Output

§

type Error = Error<<T as Transport>::Error>

§

type ListenerUpgrade = MapErr<<T as Transport>::ListenerUpgrade, fn(_: <T as Transport>::Error) -> <Transport<T, R> as Transport>::Error>

§

type Dial = Either<MapErr<<T as Transport>::Dial, fn(_: <T as Transport>::Error) -> <Transport<T, R> as Transport>::Error>, Pin<Box<dyn Future<Output = Result<<Transport<T, R> as Transport>::Output, <Transport<T, R> as Transport>::Error>> + Send>>>

§

impl<TOut> Transport for DummyTransport<TOut>

§

type Output = TOut

§

type Error = Error

§

type ListenerUpgrade = Pending<Result<<DummyTransport<TOut> as Transport>::Output, Error>>

§

type Dial = Pending<Result<<DummyTransport<TOut> as Transport>::Output, Error>>