2018-11-26 14:01:08 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
//! Once a connection to a remote peer is established, a `ProtocolsHandler` negotiates
|
|
|
|
//! and handles one or more specific protocols on the connection.
|
2019-01-07 11:39:08 +01:00
|
|
|
//!
|
2019-04-16 15:57:29 +02:00
|
|
|
//! Protocols are negotiated and used on individual substreams of the connection.
|
|
|
|
//! Thus a `ProtocolsHandler` defines the inbound and outbound upgrades to apply
|
|
|
|
//! when creating a new inbound or outbound substream, respectively, and is notified
|
|
|
|
//! by a `Swarm` when these upgrades have been successfully applied, including the
|
|
|
|
//! final output of the upgrade. A `ProtocolsHandler` can then continue communicating
|
|
|
|
//! with the peer over the substream using the negotiated protocol(s).
|
2019-01-07 11:39:08 +01:00
|
|
|
//!
|
2019-04-16 15:57:29 +02:00
|
|
|
//! Two `ProtocolsHandler`s can be composed with [`ProtocolsHandler::select()`]
|
|
|
|
//! in order to build a new handler supporting the combined set of protocols,
|
|
|
|
//! with methods being dispatched to the appropriate handler according to the
|
|
|
|
//! used protocol(s) determined by the associated types of the handlers.
|
2019-01-07 11:39:08 +01:00
|
|
|
//!
|
2019-04-16 15:57:29 +02:00
|
|
|
//! > **Note**: A `ProtocolsHandler` handles one or more protocols in the context of a single
|
2019-01-07 11:39:08 +01:00
|
|
|
//! > connection with a remote. In order to handle a protocol that requires knowledge of
|
|
|
|
//! > the network as a whole, see the `NetworkBehaviour` trait.
|
|
|
|
|
2019-01-14 14:22:25 +01:00
|
|
|
use crate::PeerId;
|
2018-11-26 14:01:08 +01:00
|
|
|
use crate::upgrade::{
|
|
|
|
InboundUpgrade,
|
|
|
|
OutboundUpgrade,
|
2018-12-18 11:23:13 +01:00
|
|
|
UpgradeError,
|
2018-11-26 14:01:08 +01:00
|
|
|
};
|
|
|
|
use futures::prelude::*;
|
2019-02-14 12:35:24 +02:00
|
|
|
use std::{cmp::Ordering, error, fmt, time::Duration, time::Instant};
|
2018-11-26 14:01:08 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
|
|
pub use self::dummy::DummyProtocolsHandler;
|
|
|
|
pub use self::map_in::MapInEvent;
|
|
|
|
pub use self::map_out::MapOutEvent;
|
2019-03-11 17:19:50 +01:00
|
|
|
pub use self::node_handler::{NodeHandlerWrapper, NodeHandlerWrapperBuilder, NodeHandlerWrapperError};
|
2019-01-22 14:45:03 +01:00
|
|
|
pub use self::one_shot::OneShotHandler;
|
2019-01-14 14:22:25 +01:00
|
|
|
pub use self::select::{IntoProtocolsHandlerSelect, ProtocolsHandlerSelect};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
|
|
|
mod dummy;
|
|
|
|
mod map_in;
|
|
|
|
mod map_out;
|
|
|
|
mod node_handler;
|
2019-01-22 14:45:03 +01:00
|
|
|
mod one_shot;
|
2018-11-26 14:01:08 +01:00
|
|
|
mod select;
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// A handler for a set of protocols used on a connection with a remote.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// This trait should be implemented for a type that maintains the state for
|
|
|
|
/// the execution of a specific protocol with a remote.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
|
|
|
/// # Handling a protocol
|
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Communication with a remote over a set of protocols is initiated in one of two ways:
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// # Connection Keep-Alive
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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.
|
2018-11-26 14:01:08 +01:00
|
|
|
pub trait ProtocolsHandler {
|
|
|
|
/// Custom event that can be received from the outside.
|
|
|
|
type InEvent;
|
|
|
|
/// Custom event that can be produced by the handler and that will be returned to the outside.
|
|
|
|
type OutEvent;
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The type of errors returned by [`ProtocolsHandler::poll`].
|
2018-12-28 15:11:35 +01:00
|
|
|
type Error: error::Error;
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The type of substreams on which the protocol(s) are negotiated.
|
2018-11-26 14:01:08 +01:00
|
|
|
type Substream: AsyncRead + AsyncWrite;
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The inbound upgrade for the protocol(s) used by the handler.
|
2018-11-26 14:01:08 +01:00
|
|
|
type InboundProtocol: InboundUpgrade<Self::Substream>;
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The outbound upgrade for the protocol(s) used by the handler.
|
2018-11-26 14:01:08 +01:00
|
|
|
type OutboundProtocol: OutboundUpgrade<Self::Substream>;
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The type of additional information passed to an `OutboundSubstreamRequest`.
|
2018-11-26 14:01:08 +01:00
|
|
|
type OutboundOpenInfo;
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The [`InboundUpgrade`] to apply on inbound substreams to negotiate the
|
|
|
|
/// desired protocols.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// > **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 listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol>;
|
2018-11-26 14:01:08 +01:00
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Injects the output of a successful upgrade on a new inbound substream.
|
2018-11-26 14:01:08 +01:00
|
|
|
fn inject_fully_negotiated_inbound(
|
|
|
|
&mut self,
|
|
|
|
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
|
|
|
|
);
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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`].
|
2018-11-26 14:01:08 +01:00
|
|
|
fn inject_fully_negotiated_outbound(
|
|
|
|
&mut self,
|
|
|
|
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
|
|
|
info: Self::OutboundOpenInfo
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Injects an event coming from the outside in the handler.
|
|
|
|
fn inject_event(&mut self, event: Self::InEvent);
|
|
|
|
|
|
|
|
/// Indicates to the handler that upgrading a substream to the given protocol has failed.
|
2019-04-16 15:57:29 +02:00
|
|
|
fn inject_dial_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
info: Self::OutboundOpenInfo,
|
|
|
|
error: ProtocolsHandlerUpgrErr<
|
|
|
|
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error
|
|
|
|
>
|
|
|
|
);
|
2018-11-26 14:01:08 +01:00
|
|
|
|
2019-01-30 16:37:34 +01:00
|
|
|
/// Returns until when the connection should be kept alive.
|
2019-01-04 12:02:39 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// This method is called by the `Swarm` after each invocation of
|
|
|
|
/// [`ProtocolsHandler::poll`] to determine if the connection and the associated
|
|
|
|
/// `ProtocolsHandler`s should be kept alive and if so, for how long.
|
|
|
|
///
|
|
|
|
/// Returning [`KeepAlive::Now`] 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`.
|
2019-01-04 12:02:39 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Returning [`KeepAlive::Forever`] indicates that the connection should
|
|
|
|
/// be kept alive until the next call to this method.
|
2019-01-04 12:02:39 +01:00
|
|
|
///
|
2019-04-16 15:57:29 +02:00
|
|
|
/// > **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.
|
2019-01-04 12:02:39 +01:00
|
|
|
///
|
2019-01-30 16:37:34 +01:00
|
|
|
fn connection_keep_alive(&self) -> KeepAlive;
|
2019-01-04 12:02:39 +01:00
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Should behave like `Stream::poll()`.
|
2018-11-26 14:01:08 +01:00
|
|
|
///
|
2019-03-11 17:19:50 +01:00
|
|
|
/// Returning an error will close the connection to the remote.
|
2019-04-16 15:57:29 +02:00
|
|
|
fn poll(&mut self) -> Poll<
|
|
|
|
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
|
|
|
Self::Error
|
|
|
|
>;
|
2018-11-26 14:01:08 +01:00
|
|
|
|
|
|
|
/// Adds a closure that turns the input event into something else.
|
|
|
|
#[inline]
|
|
|
|
fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
TMap: Fn(&TNewIn) -> Option<&Self::InEvent>,
|
|
|
|
{
|
|
|
|
MapInEvent::new(self, map)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a closure that turns the output event into something else.
|
|
|
|
#[inline]
|
|
|
|
fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
TMap: FnMut(Self::OutEvent) -> TNewOut,
|
|
|
|
{
|
|
|
|
MapOutEvent::new(self, map)
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// 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.
|
2018-11-26 14:01:08 +01:00
|
|
|
#[inline]
|
|
|
|
fn select<TProto2>(self, other: TProto2) -> ProtocolsHandlerSelect<Self, TProto2>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
ProtocolsHandlerSelect::new(self, other)
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Creates a builder that allows creating a `NodeHandler` that handles this protocol
|
2018-11-26 14:01:08 +01:00
|
|
|
/// exclusively.
|
2019-04-16 15:57:29 +02:00
|
|
|
///
|
|
|
|
/// > **Note**: This method should not be redefined in a custom `ProtocolsHandler`.
|
2018-11-26 14:01:08 +01:00
|
|
|
#[inline]
|
|
|
|
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2019-01-14 14:22:25 +01:00
|
|
|
IntoProtocolsHandler::into_node_handler_builder(self)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds an implementation of `NodeHandler` that handles this protocol exclusively.
|
|
|
|
///
|
|
|
|
/// > **Note**: This is a shortcut for `self.into_node_handler_builder().build()`.
|
|
|
|
#[inline]
|
2019-01-14 14:22:25 +01:00
|
|
|
#[deprecated(note = "Use into_node_handler_builder instead")]
|
2018-11-26 14:01:08 +01:00
|
|
|
fn into_node_handler(self) -> NodeHandlerWrapper<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2019-01-14 14:22:25 +01:00
|
|
|
#![allow(deprecated)]
|
2018-11-26 14:01:08 +01:00
|
|
|
self.into_node_handler_builder().build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Configuration of inbound or outbound substream protocol(s)
|
|
|
|
/// for a [`ProtocolsHandler`].
|
|
|
|
///
|
|
|
|
/// The inbound substream protocol(s) are defined by [`ProtocolsHandler::listen_protocol`]
|
|
|
|
/// and the outbound substream protocol(s) by [`ProtocolsHandlerEvent::OutboundSubstreamRequest`].
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct SubstreamProtocol<TUpgrade> {
|
|
|
|
upgrade: TUpgrade,
|
|
|
|
timeout: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TUpgrade> SubstreamProtocol<TUpgrade> {
|
|
|
|
/// Create a new `ListenProtocol` from the given upgrade.
|
|
|
|
///
|
|
|
|
/// The default timeout for applying the given upgrade on a substream is
|
|
|
|
/// 10 seconds.
|
|
|
|
pub fn new(upgrade: TUpgrade) -> SubstreamProtocol<TUpgrade> {
|
|
|
|
SubstreamProtocol {
|
|
|
|
upgrade,
|
|
|
|
timeout: Duration::from_secs(10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps a function over the protocol upgrade.
|
|
|
|
pub fn map_upgrade<U, F>(self, f: F) -> SubstreamProtocol<U>
|
|
|
|
where
|
|
|
|
F: FnOnce(TUpgrade) -> U,
|
|
|
|
{
|
|
|
|
SubstreamProtocol {
|
|
|
|
upgrade: f(self.upgrade),
|
|
|
|
timeout: self.timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets a new timeout for the protocol upgrade.
|
|
|
|
pub fn with_timeout(mut self, timeout: Duration) -> Self {
|
|
|
|
self.timeout = timeout;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the contained protocol upgrade.
|
|
|
|
pub fn upgrade(&self) -> &TUpgrade {
|
|
|
|
&self.upgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the timeout for the protocol upgrade.
|
|
|
|
pub fn timeout(&self) -> &Duration {
|
|
|
|
&self.timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the substream protocol configuration into the contained upgrade.
|
|
|
|
pub fn into_upgrade(self) -> TUpgrade {
|
|
|
|
self.upgrade
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TUpgrade> From<TUpgrade> for SubstreamProtocol<TUpgrade> {
|
|
|
|
fn from(upgrade: TUpgrade) -> SubstreamProtocol<TUpgrade> {
|
|
|
|
SubstreamProtocol::new(upgrade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:01:08 +01:00
|
|
|
/// Event produced by a handler.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom> {
|
|
|
|
/// Request a new outbound substream to be opened with the remote.
|
|
|
|
OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The protocol(s) to apply on the substream.
|
|
|
|
protocol: SubstreamProtocol<TConnectionUpgrade>,
|
2018-11-26 14:01:08 +01:00
|
|
|
/// User-defined information, passed back when the substream is open.
|
|
|
|
info: TOutboundOpenInfo,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Other event.
|
|
|
|
Custom(TCustom),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Event produced by a handler.
|
|
|
|
impl<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
|
|
|
|
ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
|
|
|
|
{
|
2019-04-16 15:57:29 +02:00
|
|
|
/// If this is an `OutboundSubstreamRequest`, maps the `info` member from a
|
|
|
|
/// `TOutboundOpenInfo` to something else.
|
2018-11-26 14:01:08 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn map_outbound_open_info<F, I>(
|
|
|
|
self,
|
|
|
|
map: F,
|
|
|
|
) -> ProtocolsHandlerEvent<TConnectionUpgrade, I, TCustom>
|
|
|
|
where
|
|
|
|
F: FnOnce(TOutboundOpenInfo) -> I,
|
|
|
|
{
|
|
|
|
match self {
|
2019-04-16 15:57:29 +02:00
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
2018-11-26 14:01:08 +01:00
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
protocol,
|
2018-11-26 14:01:08 +01:00
|
|
|
info: map(info),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// If this is an `OutboundSubstreamRequest`, maps the protocol (`TConnectionUpgrade`)
|
|
|
|
/// to something else.
|
2018-11-26 14:01:08 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn map_protocol<F, I>(
|
|
|
|
self,
|
|
|
|
map: F,
|
|
|
|
) -> ProtocolsHandlerEvent<I, TOutboundOpenInfo, TCustom>
|
|
|
|
where
|
|
|
|
F: FnOnce(TConnectionUpgrade) -> I,
|
|
|
|
{
|
|
|
|
match self {
|
2019-04-16 15:57:29 +02:00
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
2018-11-26 14:01:08 +01:00
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
protocol: protocol.map_upgrade(map),
|
2018-11-26 14:01:08 +01:00
|
|
|
info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If this is a `Custom` event, maps the content to something else.
|
|
|
|
#[inline]
|
|
|
|
pub fn map_custom<F, I>(
|
|
|
|
self,
|
|
|
|
map: F,
|
|
|
|
) -> ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, I>
|
|
|
|
where
|
|
|
|
F: FnOnce(TCustom) -> I,
|
|
|
|
{
|
|
|
|
match self {
|
2019-04-16 15:57:29 +02:00
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info } => {
|
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol, info }
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(map(val)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:23:13 +01:00
|
|
|
|
|
|
|
/// Error that can happen on an outbound substream opening attempt.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ProtocolsHandlerUpgrErr<TUpgrErr> {
|
|
|
|
/// The opening attempt timed out before the negotiation was fully completed.
|
|
|
|
Timeout,
|
|
|
|
/// There was an error in the timer used.
|
|
|
|
Timer,
|
|
|
|
/// Error while upgrading the substream to the protocol we want.
|
|
|
|
Upgrade(UpgradeError<TUpgrErr>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TUpgrErr> fmt::Display for ProtocolsHandlerUpgrErr<TUpgrErr>
|
|
|
|
where
|
|
|
|
TUpgrErr: fmt::Display,
|
|
|
|
{
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-18 11:23:13 +01:00
|
|
|
match self {
|
|
|
|
ProtocolsHandlerUpgrErr::Timeout => {
|
|
|
|
write!(f, "Timeout error while opening a substream")
|
|
|
|
},
|
|
|
|
ProtocolsHandlerUpgrErr::Timer => {
|
|
|
|
write!(f, "Timer error while opening a substream")
|
|
|
|
},
|
|
|
|
ProtocolsHandlerUpgrErr::Upgrade(err) => write!(f, "{}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TUpgrErr> error::Error for ProtocolsHandlerUpgrErr<TUpgrErr>
|
|
|
|
where
|
|
|
|
TUpgrErr: error::Error + 'static
|
|
|
|
{
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
ProtocolsHandlerUpgrErr::Timeout => None,
|
|
|
|
ProtocolsHandlerUpgrErr::Timer => None,
|
|
|
|
ProtocolsHandlerUpgrErr::Upgrade(err) => Some(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 14:22:25 +01:00
|
|
|
|
|
|
|
/// Prototype for a `ProtocolsHandler`.
|
|
|
|
pub trait IntoProtocolsHandler {
|
|
|
|
/// The protocols handler.
|
|
|
|
type Handler: ProtocolsHandler;
|
|
|
|
|
|
|
|
/// Builds the protocols handler.
|
|
|
|
///
|
|
|
|
/// The `PeerId` is the id of the node the handler is going to handle.
|
|
|
|
fn into_handler(self, remote_peer_id: &PeerId) -> Self::Handler;
|
|
|
|
|
|
|
|
/// Builds an implementation of `IntoProtocolsHandler` that handles both this protocol and the
|
|
|
|
/// other one together.
|
|
|
|
#[inline]
|
|
|
|
fn select<TProto2>(self, other: TProto2) -> IntoProtocolsHandlerSelect<Self, TProto2>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
IntoProtocolsHandlerSelect::new(self, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a builder that will allow creating a `NodeHandler` that handles this protocol
|
|
|
|
/// exclusively.
|
|
|
|
#[inline]
|
|
|
|
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2019-04-16 15:57:29 +02:00
|
|
|
NodeHandlerWrapperBuilder::new(self)
|
2019-01-14 14:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoProtocolsHandler for T
|
|
|
|
where T: ProtocolsHandler
|
|
|
|
{
|
|
|
|
type Handler = Self;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn into_handler(self, _: &PeerId) -> Self {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 16:37:34 +01:00
|
|
|
|
|
|
|
/// How long the connection should be kept alive.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum KeepAlive {
|
|
|
|
/// If nothing new happens, the connection should be closed at the given `Instant`.
|
|
|
|
Until(Instant),
|
|
|
|
/// Keep the connection alive.
|
|
|
|
Forever,
|
|
|
|
/// Close the connection as soon as possible.
|
|
|
|
Now,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeepAlive {
|
|
|
|
/// Returns true for `Forever`, false otherwise.
|
|
|
|
pub fn is_forever(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
KeepAlive::Forever => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 12:35:24 +02:00
|
|
|
|
|
|
|
impl PartialOrd for KeepAlive {
|
|
|
|
fn partial_cmp(&self, other: &KeepAlive) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for KeepAlive {
|
|
|
|
fn cmp(&self, other: &KeepAlive) -> Ordering {
|
|
|
|
use self::KeepAlive::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
|
|
|
(Now, Now) | (Forever, Forever) => Ordering::Equal,
|
|
|
|
(Now, _) | (_, Forever) => Ordering::Less,
|
|
|
|
(_, Now) | (Forever, _) => Ordering::Greater,
|
|
|
|
(Until(expiration), Until(other_expiration)) => expiration.cmp(other_expiration),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|