2019-01-29 19:52:22 +01:00
|
|
|
// Copyright 2019 Parity Technologies (UK) Ltd.
|
2018-11-26 14:01:08 +01: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.
|
|
|
|
|
|
|
|
use crate::{
|
2019-01-14 14:22:25 +01:00
|
|
|
PeerId,
|
2018-12-18 11:23:13 +01:00
|
|
|
either::EitherError,
|
2018-11-26 14:01:08 +01:00
|
|
|
either::EitherOutput,
|
2019-01-14 14:22:25 +01:00
|
|
|
protocols_handler::{
|
2019-01-30 16:37:34 +01:00
|
|
|
KeepAlive,
|
2019-04-16 15:57:29 +02:00
|
|
|
SubstreamProtocol,
|
2019-01-14 14:22:25 +01:00
|
|
|
IntoProtocolsHandler,
|
|
|
|
ProtocolsHandler,
|
|
|
|
ProtocolsHandlerEvent,
|
|
|
|
ProtocolsHandlerUpgrErr,
|
|
|
|
},
|
2019-05-10 11:05:22 +02:00
|
|
|
nodes::raw_swarm::ConnectedPoint,
|
2018-11-26 14:01:08 +01:00
|
|
|
upgrade::{
|
|
|
|
InboundUpgrade,
|
|
|
|
OutboundUpgrade,
|
|
|
|
EitherUpgrade,
|
2018-12-18 11:23:13 +01:00
|
|
|
SelectUpgrade,
|
|
|
|
UpgradeError,
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
use futures::prelude::*;
|
2019-01-30 16:37:34 +01:00
|
|
|
use std::cmp;
|
2018-11-26 14:01:08 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
2019-01-14 14:22:25 +01:00
|
|
|
/// Implementation of `IntoProtocolsHandler` that combines two protocols into one.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct IntoProtocolsHandlerSelect<TProto1, TProto2> {
|
2019-01-29 19:52:22 +01:00
|
|
|
/// The first protocol.
|
2019-01-14 14:22:25 +01:00
|
|
|
proto1: TProto1,
|
2019-01-29 19:52:22 +01:00
|
|
|
/// The second protocol.
|
2019-01-14 14:22:25 +01:00
|
|
|
proto2: TProto2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TProto1, TProto2> IntoProtocolsHandlerSelect<TProto1, TProto2> {
|
|
|
|
/// Builds a `IntoProtocolsHandlerSelect`.
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn new(proto1: TProto1, proto2: TProto2) -> Self {
|
|
|
|
IntoProtocolsHandlerSelect {
|
|
|
|
proto1,
|
|
|
|
proto2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TProto1, TProto2, TSubstream> IntoProtocolsHandler for IntoProtocolsHandlerSelect<TProto1, TProto2>
|
|
|
|
where
|
|
|
|
TProto1: IntoProtocolsHandler,
|
|
|
|
TProto2: IntoProtocolsHandler,
|
|
|
|
TProto1::Handler: ProtocolsHandler<Substream = TSubstream>,
|
|
|
|
TProto2::Handler: ProtocolsHandler<Substream = TSubstream>,
|
|
|
|
TSubstream: AsyncRead + AsyncWrite,
|
|
|
|
<TProto1::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<TSubstream>,
|
|
|
|
<TProto2::Handler as ProtocolsHandler>::InboundProtocol: InboundUpgrade<TSubstream>,
|
|
|
|
<TProto1::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<TSubstream>,
|
|
|
|
<TProto2::Handler as ProtocolsHandler>::OutboundProtocol: OutboundUpgrade<TSubstream>
|
|
|
|
{
|
|
|
|
type Handler = ProtocolsHandlerSelect<TProto1::Handler, TProto2::Handler>;
|
|
|
|
|
2019-05-10 11:05:22 +02:00
|
|
|
fn into_handler(self, remote_peer_id: &PeerId, connected_point: &ConnectedPoint) -> Self::Handler {
|
2019-01-14 14:22:25 +01:00
|
|
|
ProtocolsHandlerSelect {
|
2019-05-10 11:05:22 +02:00
|
|
|
proto1: self.proto1.into_handler(remote_peer_id, connected_point),
|
|
|
|
proto2: self.proto2.into_handler(remote_peer_id, connected_point),
|
2019-01-14 14:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 20:23:28 +02:00
|
|
|
|
|
|
|
fn inbound_protocol(&self) -> <Self::Handler as ProtocolsHandler>::InboundProtocol {
|
|
|
|
SelectUpgrade::new(self.proto1.inbound_protocol(), self.proto2.inbound_protocol())
|
|
|
|
}
|
2019-01-14 14:22:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 14:01:08 +01:00
|
|
|
/// Implementation of `ProtocolsHandler` that combines two protocols into one.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ProtocolsHandlerSelect<TProto1, TProto2> {
|
2019-01-29 19:52:22 +01:00
|
|
|
/// The first protocol.
|
2019-03-11 17:19:50 +01:00
|
|
|
proto1: TProto1,
|
2019-01-29 19:52:22 +01:00
|
|
|
/// The second protocol.
|
2019-03-11 17:19:50 +01:00
|
|
|
proto2: TProto2,
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<TProto1, TProto2> ProtocolsHandlerSelect<TProto1, TProto2> {
|
|
|
|
/// Builds a `ProtocolsHandlerSelect`.
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn new(proto1: TProto1, proto2: TProto2) -> Self {
|
|
|
|
ProtocolsHandlerSelect {
|
2019-03-11 17:19:50 +01:00
|
|
|
proto1,
|
|
|
|
proto2,
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 16:10:34 +01:00
|
|
|
impl<TSubstream, TProto1, TProto2>
|
2018-11-26 14:01:08 +01:00
|
|
|
ProtocolsHandler for ProtocolsHandlerSelect<TProto1, TProto2>
|
|
|
|
where
|
|
|
|
TProto1: ProtocolsHandler<Substream = TSubstream>,
|
|
|
|
TProto2: ProtocolsHandler<Substream = TSubstream>,
|
|
|
|
TSubstream: AsyncRead + AsyncWrite,
|
2018-11-27 16:10:34 +01:00
|
|
|
TProto1::InboundProtocol: InboundUpgrade<TSubstream>,
|
|
|
|
TProto2::InboundProtocol: InboundUpgrade<TSubstream>,
|
|
|
|
TProto1::OutboundProtocol: OutboundUpgrade<TSubstream>,
|
|
|
|
TProto2::OutboundProtocol: OutboundUpgrade<TSubstream>
|
2018-11-26 14:01:08 +01:00
|
|
|
{
|
|
|
|
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
|
|
|
|
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
|
2018-12-28 15:11:35 +01:00
|
|
|
type Error = EitherError<TProto1::Error, TProto2::Error>;
|
2018-11-26 14:01:08 +01:00
|
|
|
type Substream = TSubstream;
|
2019-03-11 17:19:50 +01:00
|
|
|
type InboundProtocol = SelectUpgrade<<TProto1 as ProtocolsHandler>::InboundProtocol, <TProto2 as ProtocolsHandler>::InboundProtocol>;
|
2018-11-26 14:01:08 +01:00
|
|
|
type OutboundProtocol = EitherUpgrade<TProto1::OutboundProtocol, TProto2::OutboundProtocol>;
|
|
|
|
type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>;
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-16 15:57:29 +02:00
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
2018-11-26 14:01:08 +01:00
|
|
|
let proto1 = self.proto1.listen_protocol();
|
|
|
|
let proto2 = self.proto2.listen_protocol();
|
2019-04-16 15:57:29 +02:00
|
|
|
let timeout = std::cmp::max(proto1.timeout(), proto2.timeout()).clone();
|
|
|
|
SubstreamProtocol::new(SelectUpgrade::new(proto1.into_upgrade(), proto2.into_upgrade()))
|
|
|
|
.with_timeout(timeout)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_outbound(&mut self, protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output, endpoint: Self::OutboundOpenInfo) {
|
|
|
|
match (protocol, endpoint) {
|
|
|
|
(EitherOutput::First(protocol), EitherOutput::First(info)) =>
|
|
|
|
self.proto1.inject_fully_negotiated_outbound(protocol, info),
|
|
|
|
(EitherOutput::Second(protocol), EitherOutput::Second(info)) =>
|
|
|
|
self.proto2.inject_fully_negotiated_outbound(protocol, info),
|
|
|
|
(EitherOutput::First(_), EitherOutput::Second(_)) =>
|
|
|
|
panic!("wrong API usage: the protocol doesn't match the upgrade info"),
|
|
|
|
(EitherOutput::Second(_), EitherOutput::First(_)) =>
|
|
|
|
panic!("wrong API usage: the protocol doesn't match the upgrade info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_inbound(&mut self, protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output) {
|
|
|
|
match protocol {
|
|
|
|
EitherOutput::First(protocol) =>
|
|
|
|
self.proto1.inject_fully_negotiated_inbound(protocol),
|
|
|
|
EitherOutput::Second(protocol) =>
|
|
|
|
self.proto2.inject_fully_negotiated_inbound(protocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inject_event(&mut self, event: Self::InEvent) {
|
|
|
|
match event {
|
|
|
|
EitherOutput::First(event) => self.proto1.inject_event(event),
|
|
|
|
EitherOutput::Second(event) => self.proto2.inject_event(event),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-12-18 11:23:13 +01:00
|
|
|
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
|
|
|
match (info, error) {
|
|
|
|
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timer) => {
|
|
|
|
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
|
|
|
|
},
|
|
|
|
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Timeout) => {
|
|
|
|
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
|
|
|
|
},
|
|
|
|
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
|
|
|
|
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
|
|
|
|
},
|
|
|
|
(EitherOutput::First(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(err)))) => {
|
|
|
|
self.proto1.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
|
|
|
|
},
|
|
|
|
(EitherOutput::First(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(_)))) => {
|
|
|
|
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
|
|
|
|
},
|
|
|
|
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timeout) => {
|
|
|
|
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout)
|
|
|
|
},
|
|
|
|
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Timer) => {
|
|
|
|
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer)
|
|
|
|
},
|
|
|
|
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err))) => {
|
|
|
|
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(err)))
|
|
|
|
},
|
|
|
|
(EitherOutput::Second(info), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(err)))) => {
|
|
|
|
self.proto2.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)))
|
|
|
|
},
|
|
|
|
(EitherOutput::Second(_), ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(_)))) => {
|
|
|
|
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
|
|
|
|
},
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:02:39 +01:00
|
|
|
#[inline]
|
2019-01-30 16:37:34 +01:00
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
2019-02-14 12:35:24 +02:00
|
|
|
cmp::max(self.proto1.connection_keep_alive(), self.proto2.connection_keep_alive())
|
2019-01-04 12:02:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 14:22:23 +01:00
|
|
|
fn poll(&mut self) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>, Self::Error> {
|
2019-01-29 19:52:22 +01:00
|
|
|
loop {
|
|
|
|
match self.proto1.poll().map_err(EitherError::A)? {
|
|
|
|
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
|
|
|
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(EitherOutput::First(event))));
|
|
|
|
},
|
2019-04-16 15:57:29 +02:00
|
|
|
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
|
|
|
protocol,
|
|
|
|
info,
|
|
|
|
}) => {
|
2019-01-29 19:52:22 +01:00
|
|
|
return Ok(Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
protocol: protocol.map_upgrade(EitherUpgrade::A),
|
2019-01-29 19:52:22 +01:00
|
|
|
info: EitherOutput::First(info),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
Async::NotReady => ()
|
|
|
|
};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
2019-01-29 19:52:22 +01:00
|
|
|
match self.proto2.poll().map_err(EitherError::B)? {
|
|
|
|
Async::Ready(ProtocolsHandlerEvent::Custom(event)) => {
|
|
|
|
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(EitherOutput::Second(event))));
|
|
|
|
},
|
2019-04-16 15:57:29 +02:00
|
|
|
Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
|
|
|
protocol,
|
|
|
|
info,
|
|
|
|
}) => {
|
2019-01-29 19:52:22 +01:00
|
|
|
return Ok(Async::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
protocol: protocol.map_upgrade(EitherUpgrade::B),
|
2019-01-29 19:52:22 +01:00
|
|
|
info: EitherOutput::Second(info),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
Async::NotReady => ()
|
|
|
|
};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
2019-01-29 19:52:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:19:50 +01:00
|
|
|
Ok(Async::NotReady)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|