[][src]Trait libp2p_core::protocols_handler::ProtocolsHandler

pub trait ProtocolsHandler {
    type InEvent;
    type OutEvent;
    type Error: Error;
    type Substream: AsyncRead + AsyncWrite;
    type InboundProtocol: InboundUpgrade<Self::Substream>;
    type OutboundProtocol: OutboundUpgrade<Self::Substream>;
    type OutboundOpenInfo;
    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol>;
fn inject_fully_negotiated_inbound(
        &mut self,
        protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
    );
fn inject_fully_negotiated_outbound(
        &mut self,
        protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
        info: Self::OutboundOpenInfo
    );
fn inject_event(&mut self, event: Self::InEvent);
fn inject_dial_upgrade_error(
        &mut self,
        info: Self::OutboundOpenInfo,
        error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>
    );
fn connection_keep_alive(&self) -> KeepAlive;
fn poll(
        &mut self
    ) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>; fn map_in_event<TNewIn, TMap>(
        self,
        map: TMap
    ) -> MapInEvent<Self, TNewIn, TMap>
    where
        Self: Sized,
        TMap: Fn(&TNewIn) -> Option<&Self::InEvent>
, { ... }
fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap>
    where
        Self: Sized,
        TMap: FnMut(Self::OutEvent) -> TNewOut
, { ... }
fn select<TProto2>(
        self,
        other: TProto2
    ) -> ProtocolsHandlerSelect<Self, TProto2>
    where
        Self: Sized
, { ... }
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
    where
        Self: Sized
, { ... }
fn into_node_handler(self) -> NodeHandlerWrapper<Self>
    where
        Self: Sized
, { ... } }

A handler for a set of protocols used on a connection with a remote.

This trait should be implemented for a type that maintains the state for the execution of a specific protocol with a remote.

Handling a protocol

Communication with a remote over a set of protocols is initiated in one of two ways:

  1. Dialing by initiating a new outbound substream. In order to do so, ProtocolsHandler::poll() must return an [OutboundSubstreamRequest], providing an instance of [ProtocolsHandler::OutboundUpgrade] that is used to negotiate the protocol(s). Upon success, ProtocolsHandler::inject_fully_negotiated_outbound is called with the final output of the upgrade.

  2. Listening by accepting a new inbound substream. When a new inbound substream is created on a connection, ProtocolsHandler::listen_protocol is called to obtain an instance of [ProtocolsHandler::InboundUpgrade] that is used to negotiate the protocol(s). Upon success, ProtocolsHandler::inject_fully_negotiated_inbound is called with the final output of the upgrade.

Connection Keep-Alive

A ProtocolsHandler can influence the lifetime of the underlying connection through ProtocolsHandler::connection_keep_alive. That is, the protocol implemented by the handler can include conditions for terminating the connection. The lifetime of successfully negotiated substreams is fully controlled by the handler.

Implementors of this trait should keep in mind that the connection can be closed at any time. When a connection is closed gracefully, the substreams used by the handler may still continue reading data until the remote closes its side of the connection.

Associated Types

type InEvent

Custom event that can be received from the outside.

type OutEvent

Custom event that can be produced by the handler and that will be returned to the outside.

type Error: Error

The type of errors returned by ProtocolsHandler::poll.

type Substream: AsyncRead + AsyncWrite

The type of substreams on which the protocol(s) are negotiated.

type InboundProtocol: InboundUpgrade<Self::Substream>

The inbound upgrade for the protocol(s) used by the handler.

type OutboundProtocol: OutboundUpgrade<Self::Substream>

The outbound upgrade for the protocol(s) used by the handler.

type OutboundOpenInfo

The type of additional information passed to an OutboundSubstreamRequest.

Loading content...

Required methods

fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol>

The InboundUpgrade to apply on inbound substreams to negotiate the desired protocols.

Note: The returned InboundUpgrade should always accept all the generally supported protocols, even if in a specific context a particular one is not supported, (eg. when only allowing one substream at a time for a protocol). This allows a remote to put the list of supported protocols in a cache.

fn inject_fully_negotiated_inbound(
    &mut self,
    protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
)

Injects the output of a successful upgrade on a new inbound substream.

fn inject_fully_negotiated_outbound(
    &mut self,
    protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
    info: Self::OutboundOpenInfo
)

Injects the output of a successful upgrade on a new outbound substream.

The second argument is the information that was previously passed to ProtocolsHandlerEvent::OutboundSubstreamRequest.

fn inject_event(&mut self, event: Self::InEvent)

Injects an event coming from the outside in the handler.

fn inject_dial_upgrade_error(
    &mut self,
    info: Self::OutboundOpenInfo,
    error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>
)

Indicates to the handler that upgrading a substream to the given protocol has failed.

fn connection_keep_alive(&self) -> KeepAlive

Returns until when the connection should be kept alive.

This method is called by the Swarm after each invocation of ProtocolsHandler::poll to determine if the connection and the associated ProtocolsHandlers should be kept alive as far as this handler is concerned and if so, for how long.

Returning KeepAlive::No indicates that the connection should be closed and this handler destroyed immediately.

Returning KeepAlive::Until indicates that the connection may be closed and this handler destroyed after the specified Instant.

Returning KeepAlive::Yes indicates that the connection should be kept alive until the next call to this method.

Note: The connection is always closed and the handler destroyed when ProtocolsHandler::poll returns an error. Furthermore, the connection may be closed for reasons outside of the control of the handler.

fn poll(
    &mut self
) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>

Should behave like Stream::poll().

Returning an error will close the connection to the remote.

Loading content...

Provided methods

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 

Adds a closure that turns the input event into something else.

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 

Adds a closure that turns the output event into something else.

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized

Creates a new ProtocolsHandler that selects either this handler or other by delegating methods calls appropriately.

Note: The largest KeepAlive returned by the two handlers takes precedence, i.e. is returned from ProtocolsHandler::connection_keep_alive by the returned handler.

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized

Creates a builder that allows creating a NodeHandler that handles this protocol exclusively.

Note: This method should not be redefined in a custom ProtocolsHandler.

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized

Deprecated:

Use into_node_handler_builder instead

Builds an implementation of NodeHandler that handles this protocol exclusively.

Note: This is a shortcut for self.into_node_handler_builder().build().

Loading content...

Implementors

impl<TInner> ProtocolsHandler for ToggleProtoHandler<TInner> where
    TInner: ProtocolsHandler
[src]

type InEvent = TInner::InEvent

type OutEvent = TInner::OutEvent

type Error = TInner::Error

type Substream = TInner::Substream

type InboundProtocol = EitherUpgrade<TInner::InboundProtocol, DeniedUpgrade>

type OutboundProtocol = TInner::OutboundProtocol

type OutboundOpenInfo = TInner::OutboundOpenInfo

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler, TNewIn, TMap> where
    TProtoHandler: ProtocolsHandler,
    TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>, 
[src]

type InEvent = TNewIn

type OutEvent = TProtoHandler::OutEvent

type Error = TProtoHandler::Error

type Substream = TProtoHandler::Substream

type InboundProtocol = TProtoHandler::InboundProtocol

type OutboundProtocol = TProtoHandler::OutboundProtocol

type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

impl<TProtoHandler, TMap, TNewOut> ProtocolsHandler for MapOutEvent<TProtoHandler, TMap> where
    TProtoHandler: ProtocolsHandler,
    TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut, 
[src]

type InEvent = TProtoHandler::InEvent

type OutEvent = TNewOut

type Error = TProtoHandler::Error

type Substream = TProtoHandler::Substream

type InboundProtocol = TProtoHandler::InboundProtocol

type OutboundProtocol = TProtoHandler::OutboundProtocol

type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

impl<TSubstream> ProtocolsHandler for DummyProtocolsHandler<TSubstream> where
    TSubstream: AsyncRead + AsyncWrite
[src]

type InEvent = Void

type OutEvent = Void

type Error = Void

type Substream = TSubstream

type InboundProtocol = DeniedUpgrade

type OutboundProtocol = DeniedUpgrade

type OutboundOpenInfo = Void

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

impl<TSubstream, TInProto, TOutProto, TOutEvent> ProtocolsHandler for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent> where
    TSubstream: AsyncRead + AsyncWrite,
    TInProto: InboundUpgrade<TSubstream>,
    TOutProto: OutboundUpgrade<TSubstream>,
    TInProto::Output: Into<TOutEvent>,
    TOutProto::Output: Into<TOutEvent>,
    TOutProto::Error: Error + 'static,
    SubstreamProtocol<TInProto>: Clone
[src]

type InEvent = TOutProto

type OutEvent = TOutEvent

type Error = ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>

type Substream = TSubstream

type InboundProtocol = TInProto

type OutboundProtocol = TOutProto

type OutboundOpenInfo = ()

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

impl<TSubstream, TProto1, TProto2> ProtocolsHandler for ProtocolsHandlerSelect<TProto1, TProto2> where
    TProto1: ProtocolsHandler<Substream = TSubstream>,
    TProto2: ProtocolsHandler<Substream = TSubstream>,
    TSubstream: AsyncRead + AsyncWrite,
    TProto1::InboundProtocol: InboundUpgrade<TSubstream>,
    TProto2::InboundProtocol: InboundUpgrade<TSubstream>,
    TProto1::OutboundProtocol: OutboundUpgrade<TSubstream>,
    TProto2::OutboundProtocol: OutboundUpgrade<TSubstream>, 
[src]

type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>

type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>

type Error = EitherError<TProto1::Error, TProto2::Error>

type Substream = TSubstream

type InboundProtocol = SelectUpgrade<<TProto1 as ProtocolsHandler>::InboundProtocol, <TProto2 as ProtocolsHandler>::InboundProtocol>

type OutboundProtocol = EitherUpgrade<TProto1::OutboundProtocol, TProto2::OutboundProtocol>

type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>

fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap> where
    Self: Sized,
    TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, 
[src]

fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap> where
    Self: Sized,
    TMap: FnMut(Self::OutEvent) -> TNewOut, 
[src]

fn select<TProto2>(
    self,
    other: TProto2
) -> ProtocolsHandlerSelect<Self, TProto2> where
    Self: Sized
[src]

fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self> where
    Self: Sized
[src]

fn into_node_handler(self) -> NodeHandlerWrapper<Self> where
    Self: Sized
[src]

Deprecated:

Use into_node_handler_builder instead

Loading content...