diff --git a/core/src/either.rs b/core/src/either.rs index 17827062..e4d7fea0 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -316,3 +316,29 @@ where } } } + +#[derive(Debug, Copy, Clone)] +#[must_use = "futures do nothing unless polled"] +pub enum EitherFuture2 { A(A), B(B) } + +impl Future for EitherFuture2 +where + AFut: Future, + BFut: Future +{ + type Item = EitherOutput; + type Error = EitherError; + + fn poll(&mut self) -> Poll { + match self { + EitherFuture2::A(a) => a.poll() + .map(|v| v.map(EitherOutput::First)) + .map_err(|e| EitherError::A(e)), + + EitherFuture2::B(b) => b.poll() + .map(|v| v.map(EitherOutput::Second)) + .map_err(|e| EitherError::B(e)) + } + } +} + diff --git a/core/src/protocols_handler.rs b/core/src/protocols_handler.rs index b018a6fb..5e8d9128 100644 --- a/core/src/protocols_handler.rs +++ b/core/src/protocols_handler.rs @@ -19,18 +19,19 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherError, EitherOutput}, + either::EitherOutput, nodes::handled_node::{NodeHandler, NodeHandlerEndpoint, NodeHandlerEvent}, upgrade::{ self, InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, - OutboundUpgradeExt, UpgradeInfo, InboundUpgradeApply, OutboundUpgradeApply, - DeniedUpgrade + DeniedUpgrade, + EitherUpgrade, + OrUpgrade } }; use futures::prelude::*; @@ -107,8 +108,6 @@ pub trait ProtocolsHandler { /// > list of supported protocols in a cache in order to avoid spurious queries. fn listen_protocol(&self) -> Self::InboundProtocol; - fn dialer_protocol(&self) -> Self::OutboundProtocol; - /// Injects a fully-negotiated substream in the handler. /// /// This method is called when a substream has been successfully opened and negotiated. @@ -320,11 +319,6 @@ where DeniedUpgrade } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - DeniedUpgrade - } - #[inline] fn inject_fully_negotiated_inbound( &mut self, @@ -393,11 +387,6 @@ where self.inner.listen_protocol() } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - self.inner.dialer_protocol() - } - #[inline] fn inject_fully_negotiated_inbound( &mut self, @@ -471,11 +460,6 @@ where self.inner.listen_protocol() } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - self.inner.dialer_protocol() - } - #[inline] fn inject_fully_negotiated_inbound( &mut self, @@ -766,105 +750,15 @@ where type InEvent = EitherOutput; type OutEvent = EitherOutput; type Substream = TSubstream; - - type InboundProtocol = - upgrade::OrUpgrade< - upgrade::Toggleable< - upgrade::MapInboundUpgradeErr< - upgrade::MapInboundUpgrade< - TProto1::InboundProtocol, - fn(TProto1Out) -> EitherOutput - >, - fn(>::Error) -> - EitherError< - >::Error, - >::Error - > - > - >, - upgrade::Toggleable< - upgrade::MapInboundUpgradeErr< - upgrade::MapInboundUpgrade< - TProto2::InboundProtocol, - fn(TProto2Out) -> EitherOutput - >, - fn(>::Error) -> - EitherError< - >::Error, - >::Error - > - > - > - >; - - type OutboundProtocol = - upgrade::OrUpgrade< - upgrade::Toggleable< - upgrade::MapOutboundUpgradeErr< - upgrade::MapOutboundUpgrade< - TProto1::OutboundProtocol, - fn(TProto1Out) -> EitherOutput - >, - fn(>::Error) -> - EitherError< - >::Error, - >::Error - > - > - >, - upgrade::Toggleable< - upgrade::MapOutboundUpgradeErr< - upgrade::MapOutboundUpgrade< - TProto2::OutboundProtocol, - fn(TProto2Out) -> EitherOutput - >, - fn(>::Error) -> - EitherError< - >::Error, - >::Error - > - > - > - >; - + type InboundProtocol = OrUpgrade; + type OutboundProtocol = EitherUpgrade; type OutboundOpenInfo = EitherOutput; #[inline] fn listen_protocol(&self) -> Self::InboundProtocol { - let proto1 = self.proto1.listen_protocol() - .map_inbound(EitherOutput::First as fn(TProto1Out) -> EitherOutput) - .map_inbound_err(EitherError::A as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - let proto2 = self.proto2.listen_protocol() - .map_inbound(EitherOutput::Second as fn(TProto2Out) -> EitherOutput) - .map_inbound_err(EitherError::B as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - upgrade::toggleable(proto1).or_inbound(upgrade::toggleable(proto2)) - } - - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - let proto1 = self.proto1.dialer_protocol() - .map_outbound(EitherOutput::First as fn(TProto1Out) -> EitherOutput) - .map_outbound_err(EitherError::A as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - let proto2 = self.proto2.dialer_protocol() - .map_outbound(EitherOutput::Second as fn(TProto2Out) -> EitherOutput) - .map_outbound_err(EitherError::B as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - upgrade::toggleable(proto1).or_outbound(upgrade::toggleable(proto2)) + let proto1 = self.proto1.listen_protocol(); + let proto2 = self.proto2.listen_protocol(); + proto1.or_inbound(proto2) } fn inject_fully_negotiated_outbound(&mut self, protocol: >::Output, endpoint: Self::OutboundOpenInfo) { @@ -923,29 +817,8 @@ where return Ok(Async::Ready(Some(ProtocolsHandlerEvent::Custom(EitherOutput::First(event))))); }, Async::Ready(Some(ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info})) => { - let upgrade = { - let proto1 = upgrade - .map_outbound(EitherOutput::First as fn(TProto1Out) -> EitherOutput) - .map_outbound_err(EitherError::A as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - let proto2 = self.proto2.dialer_protocol() - .map_outbound(EitherOutput::Second as fn(TProto2Out) -> EitherOutput) - .map_outbound_err(EitherError::B as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - - let proto1 = upgrade::toggleable(proto1); - let mut proto2 = upgrade::toggleable(proto2); - proto2.disable(); - proto1.or_outbound(proto2) - }; return Ok(Async::Ready(Some(ProtocolsHandlerEvent::OutboundSubstreamRequest { - upgrade, + upgrade: EitherUpgrade::A(upgrade), info: EitherOutput::First(info), }))); }, @@ -958,29 +831,8 @@ where return Ok(Async::Ready(Some(ProtocolsHandlerEvent::Custom(EitherOutput::Second(event))))); }, Async::Ready(Some(ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info })) => { - let upgrade = { - let proto1 = self.proto1.dialer_protocol() - .map_outbound(EitherOutput::First as fn(TProto1Out) -> EitherOutput) - .map_outbound_err(EitherError::A as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - let proto2 = upgrade - .map_outbound(EitherOutput::Second as fn(TProto2Out) -> EitherOutput) - .map_outbound_err(EitherError::B as fn(>::Error) -> - EitherError< - >::Error, - >::Error - >); - - let mut proto1 = upgrade::toggleable(proto1); - proto1.disable(); - let proto2 = upgrade::toggleable(proto2); - proto1.or_outbound(proto2) - }; return Ok(Async::Ready(Some(ProtocolsHandlerEvent::OutboundSubstreamRequest { - upgrade, + upgrade: EitherUpgrade::B(upgrade), info: EitherOutput::Second(info), }))); }, diff --git a/core/src/upgrade/either.rs b/core/src/upgrade/either.rs new file mode 100644 index 00000000..dcf830b3 --- /dev/null +++ b/core/src/upgrade/either.rs @@ -0,0 +1,109 @@ +// 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. + +use bytes::Bytes; +use futures::future::Either; +use crate::{ + either::{EitherOutput, EitherError, EitherFuture2}, + upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo} +}; + +/// A type to represent two possible upgrade types (inbound or outbound). +#[derive(Debug, Clone)] +pub enum EitherUpgrade { A(A), B(B) } + +impl UpgradeInfo for EitherUpgrade +where + A: UpgradeInfo, + B: UpgradeInfo +{ + type UpgradeId = Either; + type NamesIter = EitherIter; + + fn protocol_names(&self) -> Self::NamesIter { + match self { + EitherUpgrade::A(a) => EitherIter::A(a.protocol_names()), + EitherUpgrade::B(b) => EitherIter::B(b.protocol_names()) + } + } +} + +impl InboundUpgrade for EitherUpgrade +where + A: InboundUpgrade, + B: InboundUpgrade, +{ + type Output = EitherOutput; + type Error = EitherError; + type Future = EitherFuture2; + + fn upgrade_inbound(self, sock: C, id: Self::UpgradeId) -> Self::Future { + match (self, id) { + (EitherUpgrade::A(a), Either::A(id)) => EitherFuture2::A(a.upgrade_inbound(sock, id)), + (EitherUpgrade::B(b), Either::B(id)) => EitherFuture2::B(b.upgrade_inbound(sock, id)), + _ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound") + } + } +} + +impl OutboundUpgrade for EitherUpgrade +where + A: OutboundUpgrade, + B: OutboundUpgrade, +{ + type Output = EitherOutput; + type Error = EitherError; + type Future = EitherFuture2; + + fn upgrade_outbound(self, sock: C, id: Self::UpgradeId) -> Self::Future { + match (self, id) { + (EitherUpgrade::A(a), Either::A(id)) => EitherFuture2::A(a.upgrade_outbound(sock, id)), + (EitherUpgrade::B(b), Either::B(id)) => EitherFuture2::B(b.upgrade_outbound(sock, id)), + _ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound") + } + } +} + +/// A type to represent two possible `Iterator` types. +#[derive(Debug, Clone)] +pub enum EitherIter { A(A), B(B) } + +impl Iterator for EitherIter +where + A: Iterator, + B: Iterator, +{ + type Item = (Bytes, Either); + + fn next(&mut self) -> Option { + match self { + EitherIter::A(a) => a.next().map(|(name, id)| (name, Either::A(id))), + EitherIter::B(b) => b.next().map(|(name, id)| (name, Either::B(id))) + } + } + + fn size_hint(&self) -> (usize, Option) { + match self { + EitherIter::A(a) => a.size_hint(), + EitherIter::B(b) => b.size_hint() + } + } +} + diff --git a/core/src/upgrade/mod.rs b/core/src/upgrade/mod.rs index 5783ec42..96ac41ea 100644 --- a/core/src/upgrade/mod.rs +++ b/core/src/upgrade/mod.rs @@ -59,10 +59,10 @@ mod apply; mod denied; +mod either; mod error; mod map; mod or; -mod toggleable; use bytes::Bytes; use futures::future::Future; @@ -70,10 +70,10 @@ use futures::future::Future; pub use self::{ apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, denied::DeniedUpgrade, + either::EitherUpgrade, error::UpgradeError, map::{MapInboundUpgrade, MapOutboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgradeErr}, or::OrUpgrade, - toggleable::{toggleable, Toggleable} }; /// Common trait for upgrades that can be applied on inbound substreams, outbound substreams, @@ -132,7 +132,7 @@ pub trait InboundUpgradeExt: InboundUpgrade { fn or_inbound(self, upgrade: U) -> OrUpgrade where Self: Sized, - U: InboundUpgrade + U: InboundUpgrade { OrUpgrade::new(self, upgrade) } @@ -182,7 +182,7 @@ pub trait OutboundUpgradeExt: OutboundUpgrade { fn or_outbound(self, upgrade: U) -> OrUpgrade where Self: Sized, - U: OutboundUpgrade + U: OutboundUpgrade { OrUpgrade::new(self, upgrade) } diff --git a/core/src/upgrade/or.rs b/core/src/upgrade/or.rs index 318b337f..e775d80e 100644 --- a/core/src/upgrade/or.rs +++ b/core/src/upgrade/or.rs @@ -20,7 +20,10 @@ use bytes::Bytes; use futures::future::Either; -use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; +use crate::{ + either::{EitherOutput, EitherError, EitherFuture2}, + upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo} +}; /// Upgrade that combines two upgrades into one. Supports all the protocols supported by either /// sub-upgrade. @@ -51,36 +54,36 @@ where } } -impl InboundUpgrade for OrUpgrade +impl InboundUpgrade for OrUpgrade where - A: InboundUpgrade, - B: InboundUpgrade, + A: InboundUpgrade, + B: InboundUpgrade, { - type Output = T; // TODO: different output types - type Error = E; // TODO: different error types - type Future = Either; + type Output = EitherOutput; + type Error = EitherError; + type Future = EitherFuture2; fn upgrade_inbound(self, sock: C, id: Self::UpgradeId) -> Self::Future { match id { - Either::A(id) => Either::A(self.0.upgrade_inbound(sock, id)), - Either::B(id) => Either::B(self.1.upgrade_inbound(sock, id)) + Either::A(id) => EitherFuture2::A(self.0.upgrade_inbound(sock, id)), + Either::B(id) => EitherFuture2::B(self.1.upgrade_inbound(sock, id)) } } } -impl OutboundUpgrade for OrUpgrade +impl OutboundUpgrade for OrUpgrade where - A: OutboundUpgrade, - B: OutboundUpgrade, + A: OutboundUpgrade, + B: OutboundUpgrade, { - type Output = T; // TODO: different output types - type Error = E; // TODO: different error types - type Future = Either; + type Output = EitherOutput; + type Error = EitherError; + type Future = EitherFuture2; fn upgrade_outbound(self, sock: C, id: Self::UpgradeId) -> Self::Future { match id { - Either::A(id) => Either::A(self.0.upgrade_outbound(sock, id)), - Either::B(id) => Either::B(self.1.upgrade_outbound(sock, id)) + Either::A(id) => EitherFuture2::A(self.0.upgrade_outbound(sock, id)), + Either::B(id) => EitherFuture2::B(self.1.upgrade_outbound(sock, id)) } } } diff --git a/core/src/upgrade/toggleable.rs b/core/src/upgrade/toggleable.rs deleted file mode 100644 index e694e26d..00000000 --- a/core/src/upgrade/toggleable.rs +++ /dev/null @@ -1,153 +0,0 @@ -// 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. - -use crate::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; -use futures::future; - -/// Wraps around a `InboundUpgrade` or `OutboundUpgrade` and makes it possible -/// to enable or disable the upgrade. -#[inline] -pub fn toggleable(upgrade: U) -> Toggleable { - Toggleable { - inner: upgrade, - enabled: true, - } -} - -/// See `toggleable`. -#[derive(Debug, Copy, Clone)] -pub struct Toggleable { - inner: U, - enabled: bool, -} - -impl Toggleable { - /// Toggles the upgrade. - #[inline] - pub fn toggle(&mut self) { - self.enabled = !self.enabled; - } - - /// Returns true if the upgrade is enabled. - #[inline] - pub fn enabled(&self) -> bool { - self.enabled - } - - /// Enables the upgrade. - #[inline] - pub fn enable(&mut self) { - self.enabled = true; - } - - /// Disables the upgrade. - #[inline] - pub fn disable(&mut self) { - self.enabled = false; - } -} - -impl UpgradeInfo for Toggleable -where - U: UpgradeInfo -{ - type UpgradeId = U::UpgradeId; - type NamesIter = ToggleableIter; - - #[inline] - fn protocol_names(&self) -> Self::NamesIter { - ToggleableIter { - inner: self.inner.protocol_names(), - enabled: self.enabled, - } - } -} - -impl InboundUpgrade for Toggleable -where - U: InboundUpgrade -{ - type Output = U::Output; - type Error = U::Error; - type Future = future::Either, U::Future>; - - #[inline] - fn upgrade_inbound(self, socket: C, id: Self::UpgradeId) -> Self::Future { - if self.enabled { - future::Either::B(self.inner.upgrade_inbound(socket, id)) - } else { - future::Either::A(future::empty()) - } - } -} - -impl OutboundUpgrade for Toggleable -where - U: OutboundUpgrade -{ - type Output = U::Output; - type Error = U::Error; - type Future = future::Either, U::Future>; - - #[inline] - fn upgrade_outbound(self, socket: C, id: Self::UpgradeId) -> Self::Future { - if self.enabled { - future::Either::B(self.inner.upgrade_outbound(socket, id)) - } else { - future::Either::A(future::empty()) - } - } -} - -/// Iterator that is toggleable. -#[derive(Debug, Clone)] -pub struct ToggleableIter { - inner: I, - // It is expected that `enabled` doesn't change once the iterator has been created. - enabled: bool, -} - -impl Iterator for ToggleableIter -where I: Iterator -{ - type Item = I::Item; - - fn next(&mut self) -> Option { - if self.enabled { - self.inner.next() - } else { - None - } - } - - fn size_hint(&self) -> (usize, Option) { - if self.enabled { - self.inner.size_hint() - } else { - (0, Some(0)) - } - } -} - -impl ExactSizeIterator for ToggleableIter -where - I: ExactSizeIterator -{} - diff --git a/protocols/floodsub/src/handler.rs b/protocols/floodsub/src/handler.rs index ad8d0d2d..531f50b6 100644 --- a/protocols/floodsub/src/handler.rs +++ b/protocols/floodsub/src/handler.rs @@ -114,11 +114,6 @@ where self.config.clone() } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - self.config.clone() - } - fn inject_fully_negotiated_inbound( &mut self, protocol: >::Output diff --git a/protocols/identify/src/listen_handler.rs b/protocols/identify/src/listen_handler.rs index a4a23396..65f9f6ce 100644 --- a/protocols/identify/src/listen_handler.rs +++ b/protocols/identify/src/listen_handler.rs @@ -69,11 +69,6 @@ where self.config.clone() } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - DeniedUpgrade - } - fn inject_fully_negotiated_inbound( &mut self, protocol: >::Output diff --git a/protocols/identify/src/periodic_id_handler.rs b/protocols/identify/src/periodic_id_handler.rs index c13aaa5b..76551fd0 100644 --- a/protocols/identify/src/periodic_id_handler.rs +++ b/protocols/identify/src/periodic_id_handler.rs @@ -22,7 +22,7 @@ use crate::{RemoteInfo, IdentifyProtocolConfig}; use futures::prelude::*; use libp2p_core::{ protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent}, - upgrade::{self, DeniedUpgrade, OutboundUpgrade, Toggleable} + upgrade::{DeniedUpgrade, OutboundUpgrade} }; use std::{io, marker::PhantomData, time::{Duration, Instant}}; use tokio_io::{AsyncRead, AsyncWrite}; @@ -39,7 +39,7 @@ const TRY_AGAIN_ON_ERR: Duration = Duration::from_secs(60 * 60); /// Protocol handler that identifies the remote at a regular period. pub struct PeriodicIdentification { /// Configuration for the protocol. - config: Toggleable, + config: IdentifyProtocolConfig, /// If `Some`, we successfully generated an `PeriodicIdentificationEvent` and we will produce /// it the next time `poll()` is invoked. @@ -67,7 +67,7 @@ impl PeriodicIdentification { #[inline] pub fn new() -> Self { PeriodicIdentification { - config: upgrade::toggleable(IdentifyProtocolConfig), + config: IdentifyProtocolConfig, pending_result: None, next_id: Some(Delay::new(Instant::now() + DELAY_TO_FIRST_ID)), marker: PhantomData, @@ -83,7 +83,7 @@ where type OutEvent = PeriodicIdentificationEvent; type Substream = TSubstream; type InboundProtocol = DeniedUpgrade; - type OutboundProtocol = Toggleable; + type OutboundProtocol = IdentifyProtocolConfig; type OutboundOpenInfo = (); #[inline] @@ -91,11 +91,6 @@ where DeniedUpgrade } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - self.config.clone() - } - fn inject_fully_negotiated_inbound(&mut self, protocol: Void) { unreachable(protocol) } @@ -155,8 +150,7 @@ where Ok(Async::NotReady) => Ok(Async::NotReady), Ok(Async::Ready(())) => { next_id.reset(Instant::now() + DELAY_TO_NEXT_ID); - let mut upgrade = self.config.clone(); - upgrade.enable(); + let upgrade = self.config.clone(); let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info: () }; Ok(Async::Ready(Some(ev))) } diff --git a/protocols/ping/src/dial_handler.rs b/protocols/ping/src/dial_handler.rs index e92df15b..21ce38b0 100644 --- a/protocols/ping/src/dial_handler.rs +++ b/protocols/ping/src/dial_handler.rs @@ -23,7 +23,7 @@ use libp2p_core::{ OutboundUpgrade, ProtocolsHandler, ProtocolsHandlerEvent, - upgrade::{self, DeniedUpgrade} + upgrade::DeniedUpgrade }; use log::warn; use protocol::{Ping, PingDialer}; @@ -40,7 +40,7 @@ use void::{Void, unreachable}; /// If the remote doesn't respond, produces `Unresponsive` and closes the connection. pub struct PeriodicPingHandler { /// Configuration for the ping protocol. - ping_config: upgrade::Toggleable>, + ping_config: Ping, /// State of the outgoing ping. out_state: OutState, @@ -128,7 +128,7 @@ impl PeriodicPingHandler { let ping_timeout = Duration::from_secs(30); PeriodicPingHandler { - ping_config: upgrade::toggleable(Default::default()), + ping_config: Default::default(), out_state: OutState::NeedToOpen { expires: Delay::new(Instant::now() + ping_timeout), }, @@ -154,7 +154,7 @@ where type OutEvent = OutEvent; type Substream = TSubstream; type InboundProtocol = DeniedUpgrade; - type OutboundProtocol = upgrade::Toggleable>; + type OutboundProtocol = Ping; type OutboundOpenInfo = (); #[inline] @@ -162,11 +162,6 @@ where DeniedUpgrade } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - self.ping_config - } - fn inject_fully_negotiated_inbound(&mut self, protocol: Void) { unreachable(protocol) } diff --git a/protocols/ping/src/listen_handler.rs b/protocols/ping/src/listen_handler.rs index 1270e933..3c89dc4c 100644 --- a/protocols/ping/src/listen_handler.rs +++ b/protocols/ping/src/listen_handler.rs @@ -80,11 +80,6 @@ where self.ping_config } - #[inline] - fn dialer_protocol(&self) -> Self::OutboundProtocol { - DeniedUpgrade - } - fn inject_fully_negotiated_inbound( &mut self, protocol: >::Output