Trait libp2p::Transport[][src]

pub trait Transport {
    type Output;
    type Error: Error;
    type Listener: Stream;
    type ListenerUpgrade: Future;
    type Dial: Future;
    fn listen_on(
        addr: Multiaddr
    ) -> Result<Self::Listener, TransportError<Self::Error>>;
fn dial(
        addr: Multiaddr
    ) -> Result<Self::Dial, TransportError<Self::Error>>;
fn address_translation(
        listen: &Multiaddr,
        observed: &Multiaddr
    ) -> Option<Multiaddr>; fn boxed(self) -> Boxed<Self::Output>
        Self: Transport + Clone + Send + Sync + 'static,
        Self::Dial: Send,
        Self::Dial: 'static,
        Self::Listener: Send,
        Self::Listener: 'static,
        Self::ListenerUpgrade: Send,
        Self::ListenerUpgrade: 'static,
        Self::Error: Send,
        Self::Error: Sync
, { ... }
fn map<F, O>(self, f: F) -> Map<Self, F>
        F: FnOnce(Self::Output, ConnectedPoint) -> O + Clone
, { ... }
fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
        F: FnOnce(Self::Error) -> E + Clone
, { ... }
fn or_transport<U>(self, other: U) -> OrTransport<Self, U>
        U: Transport,
        <U as Transport>::Error: 'static
, { ... }
fn and_then<C, F, O>(self, f: C) -> AndThen<Self, C>
        C: FnOnce(Self::Output, ConnectedPoint) -> F + Clone,
        F: TryFuture<Ok = O>,
        <F as TryFuture>::Error: Error,
        <F as TryFuture>::Error: 'static
, { ... }
fn upgrade(self, version: Version) -> Builder<Self>
        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: The methods of this trait use self and not &self or &mut self. In other words, listening or dialing consumes the transport object. This has been designed so that you would implement this trait on &Foo or &mut Foo instead of directly on Foo.

Associated Types

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.

An error that occurred during connection setup.

A stream of Outputs for inbound connections.

An item 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.

If this stream produces an error, it is considered fatal and the listener is killed. It is possible to report non-fatal errors by producing a ListenerEvent::Error.

A pending Output for an inbound connection, obtained from the Listener 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.

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

Required methods

Listens on the given Multiaddr, producing a stream of pending, inbound connections and addresses this transport is listening on (cf. ListenerEvent).

Returning an error from the stream is considered fatal. The listener can also report non-fatal errors by producing a ListenerEvent::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.

Performs a transport-specific mapping of an address observed by a remote onto a local listen address to yield an address for the local node that may be reachable for other peers.

Provided methods

Boxes the transport, including custom transport errors.

Applies a function on the connections created by the transport.

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

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.

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.

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


When port reuse is disabled and hence ephemeral local ports are used for outgoing connections, the returned address is the observed address with the port replaced by the port of the listen address.

If port reuse is enabled, Some(observed) is returned, as there is a chance that the observed address and port are reachable for other peers if there is a NAT in the way that does endpoint- independent filtering. Furthermore, even if that is not the case and TCP hole punching techniques must be used for NAT traversal, the observed address is still the one that a remote should connect to for the purpose of the hole punching procedure, as it represents the mapped IP and port of the NAT device in front of the local node.

None is returned if one of the given addresses is not a TCP/IP address.