Add EitherUpgrade and generalise OrUpgrade. (#662)

This commit is contained in:
Toralf Wittner
2018-11-20 15:09:59 +01:00
committed by GitHub
parent b213fd7bd7
commit 938b91742f
11 changed files with 179 additions and 368 deletions

View File

@ -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: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output

View File

@ -69,11 +69,6 @@ where
self.config.clone()
}
#[inline]
fn dialer_protocol(&self) -> Self::OutboundProtocol {
DeniedUpgrade
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output

View File

@ -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<TSubstream> {
/// Configuration for the protocol.
config: Toggleable<IdentifyProtocolConfig>,
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<TSubstream> PeriodicIdentification<TSubstream> {
#[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<IdentifyProtocolConfig>;
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)))
}

View File

@ -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<TSubstream> {
/// Configuration for the ping protocol.
ping_config: upgrade::Toggleable<Ping<Instant>>,
ping_config: Ping<Instant>,
/// State of the outgoing ping.
out_state: OutState<TSubstream>,
@ -128,7 +128,7 @@ impl<TSubstream> PeriodicPingHandler<TSubstream> {
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<Ping<Instant>>;
type OutboundProtocol = Ping<Instant>;
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)
}

View File

@ -80,11 +80,6 @@ where
self.ping_config
}
#[inline]
fn dialer_protocol(&self) -> Self::OutboundProtocol {
DeniedUpgrade
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output