2018-11-15 17:41:11 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
2018-05-02 11:50:48 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
//! Contains everything related to upgrading a connection or a substream to use a protocol.
|
|
|
|
//!
|
|
|
|
//! After a connection with a remote has been successfully established or a substream successfully
|
|
|
|
//! opened, the next step is to *upgrade* this connection or substream to use a protocol.
|
|
|
|
//!
|
|
|
|
//! This is where the `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` traits come into play.
|
|
|
|
//! The `InboundUpgrade` and `OutboundUpgrade` traits are implemented on types that represent a
|
|
|
|
//! collection of one or more possible protocols for respectively an ingoing or outgoing
|
|
|
|
//! connection or substream.
|
|
|
|
//!
|
|
|
|
//! > **Note**: Multiple versions of the same protocol are treated as different protocols.
|
|
|
|
//! > For example, `/foo/1.0.0` and `/foo/1.1.0` are totally unrelated as far as
|
|
|
|
//! > upgrading is concerned.
|
|
|
|
//!
|
|
|
|
//! # Upgrade process
|
|
|
|
//!
|
|
|
|
//! An upgrade is performed in two steps:
|
|
|
|
//!
|
|
|
|
//! - A protocol negotiation step. The `UpgradeInfo::protocol_names` method is called to determine
|
|
|
|
//! which protocols are supported by the trait implementation. The `multistream-select` protocol
|
|
|
|
//! is used in order to agree on which protocol to use amongst the ones supported.
|
|
|
|
//!
|
|
|
|
//! - A handshake. After a successful negotiation, the `InboundUpgrade::upgrade_inbound` or
|
|
|
|
//! `OutboundUpgrade::upgrade_outbound` method is called. This method will return a `Future` that
|
|
|
|
//! performs a handshake. This handshake is considered mandatory, however in practice it is
|
|
|
|
//! possible for the trait implementation to return a dummy `Future` that doesn't perform any
|
|
|
|
//! action and immediately succeeds.
|
|
|
|
//!
|
|
|
|
//! After an upgrade is successful, an object of type `InboundUpgrade::Output` or
|
|
|
|
//! `OutboundUpgrade::Output` is returned. The actual object depends on the implementation and
|
|
|
|
//! there is no constraint on the traits that it should implement, however it is expected that it
|
|
|
|
//! can be used by the user to control the behaviour of the protocol.
|
|
|
|
//!
|
|
|
|
//! > **Note**: You can use the `apply_inbound` or `apply_outbound` methods to try upgrade a
|
|
|
|
//! connection or substream. However if you use the recommended `Swarm` or
|
|
|
|
//! `ProtocolsHandler` APIs, the upgrade is automatically handled for you and you don't
|
|
|
|
//! need to use these methods.
|
|
|
|
//!
|
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
mod apply;
|
|
|
|
mod denied;
|
|
|
|
mod error;
|
|
|
|
mod map;
|
|
|
|
mod or;
|
|
|
|
mod toggleable;
|
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::future::Future;
|
|
|
|
|
|
|
|
pub use self::{
|
2018-11-19 15:19:07 +01:00
|
|
|
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
|
2018-11-15 17:41:11 +01:00
|
|
|
denied::DeniedUpgrade,
|
|
|
|
error::UpgradeError,
|
2018-11-19 16:08:00 +01:00
|
|
|
map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr},
|
2018-11-15 17:41:11 +01:00
|
|
|
or::OrUpgrade,
|
|
|
|
toggleable::{toggleable, Toggleable}
|
|
|
|
};
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Common trait for upgrades that can be applied on inbound substreams, outbound substreams,
|
|
|
|
/// or both.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Opaque type representing a negotiable protocol.
|
2018-11-15 17:41:11 +01:00
|
|
|
type UpgradeId;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Iterator returned by `protocol_names`.
|
2018-11-15 17:41:11 +01:00
|
|
|
type NamesIter: Iterator<Item = (Bytes, Self::UpgradeId)>;
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns the list of protocols that are supported. Used during the negotiation process.
|
|
|
|
///
|
|
|
|
/// Each item returned by the iterator is a pair of a protocol name and an opaque identifier.
|
2018-11-15 17:41:11 +01:00
|
|
|
fn protocol_names(&self) -> Self::NamesIter;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible upgrade on an inbound connection or substream.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait InboundUpgrade<C>: UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Output after the upgrade has been successfully negotiated and the handshake performed.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Output;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible error during the handshake.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Future that performs the handshake with the remote.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Future: Future<Item = Self::Output, Error = Self::Error>;
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// After we have determined that the remote supports one of the protocols we support, this
|
|
|
|
/// method is called to start the handshake.
|
|
|
|
///
|
|
|
|
/// The `id` is the identifier of the protocol, as produced by `protocol_names()`.
|
2018-11-15 17:41:11 +01:00
|
|
|
fn upgrade_inbound(self, socket: C, id: Self::UpgradeId) -> Self::Future;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Extension trait for `InboundUpgrade`. Automatically implemented on all types that implement
|
|
|
|
/// `InboundUpgrade`.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait InboundUpgradeExt<C>: InboundUpgrade<C> {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
2018-11-19 10:58:45 +01:00
|
|
|
fn map_inbound<F, T>(self, f: F) -> MapInboundUpgrade<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Output) -> T
|
|
|
|
{
|
2018-11-19 10:58:45 +01:00
|
|
|
MapInboundUpgrade::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
2018-11-19 16:08:00 +01:00
|
|
|
fn map_inbound_err<F, T>(self, f: F) -> MapInboundUpgradeErr<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Error) -> T
|
|
|
|
{
|
2018-11-19 16:08:00 +01:00
|
|
|
MapInboundUpgradeErr::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that combines `Self` and another upgrade to support both at the same
|
|
|
|
/// time.
|
2018-11-15 17:41:11 +01:00
|
|
|
fn or_inbound<U>(self, upgrade: U) -> OrUpgrade<Self, U>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
U: InboundUpgrade<C, Output = Self::Output, Error = Self::Error>
|
|
|
|
{
|
|
|
|
OrUpgrade::new(self, upgrade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, U: InboundUpgrade<C>> InboundUpgradeExt<C> for U {}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible upgrade on an outbound connection or substream.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait OutboundUpgrade<C>: UpgradeInfo {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Output after the upgrade has been successfully negotiated and the handshake performed.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Output;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Possible error during the handshake.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Error;
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Future that performs the handshake with the remote.
|
2018-11-15 17:41:11 +01:00
|
|
|
type Future: Future<Item = Self::Output, Error = Self::Error>;
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// After we have determined that the remote supports one of the protocols we support, this
|
|
|
|
/// method is called to start the handshake.
|
|
|
|
///
|
|
|
|
/// The `id` is the identifier of the protocol, as produced by `protocol_names()`.
|
2018-11-15 17:41:11 +01:00
|
|
|
fn upgrade_outbound(self, socket: C, id: Self::UpgradeId) -> Self::Future;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Extention trait for `OutboundUpgrade`. Automatically implemented on all types that implement
|
|
|
|
/// `OutboundUpgrade`.
|
2018-11-15 17:41:11 +01:00
|
|
|
pub trait OutboundUpgradeExt<C>: OutboundUpgrade<C> {
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Output`.
|
2018-11-19 10:58:45 +01:00
|
|
|
fn map_outbound<F, T>(self, f: F) -> MapOutboundUpgrade<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Output) -> T
|
|
|
|
{
|
2018-11-19 10:58:45 +01:00
|
|
|
MapOutboundUpgrade::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that wraps around `Self` and applies a closure to the `Error`.
|
2018-11-19 16:08:00 +01:00
|
|
|
fn map_outbound_err<F, T>(self, f: F) -> MapOutboundUpgradeErr<Self, F>
|
2018-11-15 17:41:11 +01:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnOnce(Self::Error) -> T
|
|
|
|
{
|
2018-11-19 16:08:00 +01:00
|
|
|
MapOutboundUpgradeErr::new(self, f)
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:11:44 +01:00
|
|
|
/// Returns a new object that combines `Self` and another upgrade to support both at the same
|
|
|
|
/// time.
|
2018-11-15 17:41:11 +01:00
|
|
|
fn or_outbound<U>(self, upgrade: U) -> OrUpgrade<Self, U>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
U: OutboundUpgrade<C, Output = Self::Output, Error = Self::Error>
|
|
|
|
{
|
|
|
|
OrUpgrade::new(self, upgrade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C, U: OutboundUpgrade<C>> OutboundUpgradeExt<C> for U {}
|
|
|
|
|