2019-01-22 14:45:03 +01:00
|
|
|
// Copyright 2019 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-05 06:28:18 +11:00
|
|
|
use crate::protocols_handler::{
|
|
|
|
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr,
|
2019-04-16 15:57:29 +02:00
|
|
|
SubstreamProtocol
|
2019-04-05 06:28:18 +11:00
|
|
|
};
|
2019-01-22 14:45:03 +01:00
|
|
|
use crate::upgrade::{InboundUpgrade, OutboundUpgrade};
|
|
|
|
use futures::prelude::*;
|
|
|
|
use smallvec::SmallVec;
|
2019-04-25 15:08:06 +02:00
|
|
|
use std::{error, marker::PhantomData, time::Duration};
|
2019-01-22 14:45:03 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2019-04-25 15:08:06 +02:00
|
|
|
use wasm_timer::Instant;
|
2019-01-22 14:45:03 +01:00
|
|
|
|
|
|
|
/// Implementation of `ProtocolsHandler` that opens a new substream for each individual message.
|
|
|
|
///
|
|
|
|
/// This struct is meant to be a helper for other implementations to use.
|
|
|
|
// TODO: Debug
|
|
|
|
pub struct OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
2019-04-05 06:28:18 +11:00
|
|
|
where
|
|
|
|
TOutProto: OutboundUpgrade<TSubstream>,
|
2019-01-22 14:45:03 +01:00
|
|
|
{
|
|
|
|
/// The upgrade for inbound substreams.
|
2019-04-16 15:57:29 +02:00
|
|
|
listen_protocol: SubstreamProtocol<TInProto>,
|
2019-01-22 14:45:03 +01:00
|
|
|
/// If `Some`, something bad happened and we should shut down the handler with an error.
|
2019-04-05 06:28:18 +11:00
|
|
|
pending_error:
|
|
|
|
Option<ProtocolsHandlerUpgrErr<<TOutProto as OutboundUpgrade<TSubstream>>::Error>>,
|
2019-01-22 14:45:03 +01:00
|
|
|
/// Queue of events to produce in `poll()`.
|
|
|
|
events_out: SmallVec<[TOutEvent; 4]>,
|
|
|
|
/// Queue of outbound substreams to open.
|
|
|
|
dial_queue: SmallVec<[TOutProto; 4]>,
|
|
|
|
/// Current number of concurrent outbound substreams being opened.
|
|
|
|
dial_negotiated: u32,
|
|
|
|
/// Maximum number of concurrent outbound substreams being opened. Value is never modified.
|
|
|
|
max_dial_negotiated: u32,
|
2019-01-30 16:37:34 +01:00
|
|
|
/// Value to return from `connection_keep_alive`.
|
|
|
|
keep_alive: KeepAlive,
|
|
|
|
/// After the given duration has elapsed, an inactive connection will shutdown.
|
|
|
|
inactive_timeout: Duration,
|
2019-01-22 14:45:03 +01:00
|
|
|
/// Pin the `TSubstream` generic.
|
|
|
|
marker: PhantomData<TSubstream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TSubstream, TInProto, TOutProto, TOutEvent>
|
|
|
|
OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
2019-04-05 06:28:18 +11:00
|
|
|
where
|
|
|
|
TOutProto: OutboundUpgrade<TSubstream>,
|
2019-01-22 14:45:03 +01:00
|
|
|
{
|
|
|
|
/// Creates a `OneShotHandler`.
|
|
|
|
#[inline]
|
2019-04-16 15:57:29 +02:00
|
|
|
pub fn new(
|
|
|
|
listen_protocol: SubstreamProtocol<TInProto>,
|
|
|
|
inactive_timeout: Duration
|
|
|
|
) -> Self {
|
2019-01-22 14:45:03 +01:00
|
|
|
OneShotHandler {
|
|
|
|
listen_protocol,
|
|
|
|
pending_error: None,
|
|
|
|
events_out: SmallVec::new(),
|
|
|
|
dial_queue: SmallVec::new(),
|
|
|
|
dial_negotiated: 0,
|
|
|
|
max_dial_negotiated: 8,
|
2019-04-23 11:58:49 +02:00
|
|
|
keep_alive: KeepAlive::Yes,
|
2019-04-05 06:28:18 +11:00
|
|
|
inactive_timeout,
|
2019-01-22 14:45:03 +01:00
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of pending requests.
|
|
|
|
#[inline]
|
|
|
|
pub fn pending_requests(&self) -> u32 {
|
|
|
|
self.dial_negotiated + self.dial_queue.len() as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the listen protocol configuration.
|
|
|
|
///
|
|
|
|
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
|
|
|
|
/// > substreams, not the ones already being negotiated.
|
|
|
|
#[inline]
|
2019-04-16 15:57:29 +02:00
|
|
|
pub fn listen_protocol_ref(&self) -> &SubstreamProtocol<TInProto> {
|
2019-01-22 14:45:03 +01:00
|
|
|
&self.listen_protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the listen protocol configuration.
|
|
|
|
///
|
|
|
|
/// > **Note**: If you modify the protocol, modifications will only applies to future inbound
|
|
|
|
/// > substreams, not the ones already being negotiated.
|
|
|
|
#[inline]
|
2019-04-16 15:57:29 +02:00
|
|
|
pub fn listen_protocol_mut(&mut self) -> &mut SubstreamProtocol<TInProto> {
|
2019-01-22 14:45:03 +01:00
|
|
|
&mut self.listen_protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Opens an outbound substream with `upgrade`.
|
|
|
|
#[inline]
|
|
|
|
pub fn send_request(&mut self, upgrade: TOutProto) {
|
2019-04-23 11:58:49 +02:00
|
|
|
self.keep_alive = KeepAlive::Yes;
|
2019-01-22 14:45:03 +01:00
|
|
|
self.dial_queue.push(upgrade);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 06:28:18 +11:00
|
|
|
impl<TSubstream, TInProto, TOutProto, TOutEvent> Default
|
|
|
|
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
|
|
|
where
|
|
|
|
TOutProto: OutboundUpgrade<TSubstream>,
|
2019-04-16 15:57:29 +02:00
|
|
|
TInProto: InboundUpgrade<TSubstream> + Default,
|
2019-01-22 14:45:03 +01:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
2019-04-16 15:57:29 +02:00
|
|
|
OneShotHandler::new(SubstreamProtocol::new(Default::default()), Duration::from_secs(10))
|
2019-01-22 14:45:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 06:28:18 +11:00
|
|
|
impl<TSubstream, TInProto, TOutProto, TOutEvent> ProtocolsHandler
|
|
|
|
for OneShotHandler<TSubstream, TInProto, TOutProto, TOutEvent>
|
2019-01-22 14:45:03 +01:00
|
|
|
where
|
|
|
|
TSubstream: AsyncRead + AsyncWrite,
|
2019-04-16 15:57:29 +02:00
|
|
|
TInProto: InboundUpgrade<TSubstream>,
|
2019-01-22 14:45:03 +01:00
|
|
|
TOutProto: OutboundUpgrade<TSubstream>,
|
|
|
|
TInProto::Output: Into<TOutEvent>,
|
|
|
|
TOutProto::Output: Into<TOutEvent>,
|
|
|
|
TOutProto::Error: error::Error + 'static,
|
2019-04-16 15:57:29 +02:00
|
|
|
SubstreamProtocol<TInProto>: Clone,
|
2019-01-22 14:45:03 +01:00
|
|
|
{
|
|
|
|
type InEvent = TOutProto;
|
|
|
|
type OutEvent = TOutEvent;
|
2019-04-05 06:28:18 +11:00
|
|
|
type Error = ProtocolsHandlerUpgrErr<
|
|
|
|
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error,
|
|
|
|
>;
|
2019-01-22 14:45:03 +01:00
|
|
|
type Substream = TSubstream;
|
|
|
|
type InboundProtocol = TInProto;
|
|
|
|
type OutboundProtocol = TOutProto;
|
|
|
|
type OutboundOpenInfo = ();
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-16 15:57:29 +02:00
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
|
2019-01-22 14:45:03 +01:00
|
|
|
self.listen_protocol.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inject_fully_negotiated_inbound(
|
|
|
|
&mut self,
|
2019-04-05 06:28:18 +11:00
|
|
|
out: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output,
|
2019-01-22 14:45:03 +01:00
|
|
|
) {
|
2019-01-30 16:37:34 +01:00
|
|
|
// If we're shutting down the connection for inactivity, reset the timeout.
|
2019-04-23 11:58:49 +02:00
|
|
|
if !self.keep_alive.is_yes() {
|
2019-01-30 16:37:34 +01:00
|
|
|
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
|
|
|
|
}
|
|
|
|
|
2019-01-22 14:45:03 +01:00
|
|
|
self.events_out.push(out.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inject_fully_negotiated_outbound(
|
|
|
|
&mut self,
|
|
|
|
out: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
|
2019-04-05 06:28:18 +11:00
|
|
|
_: Self::OutboundOpenInfo,
|
2019-01-22 14:45:03 +01:00
|
|
|
) {
|
|
|
|
self.dial_negotiated -= 1;
|
|
|
|
|
2019-01-30 16:37:34 +01:00
|
|
|
if self.dial_negotiated == 0 && self.dial_queue.is_empty() {
|
|
|
|
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
|
|
|
|
}
|
|
|
|
|
2019-01-22 14:45:03 +01:00
|
|
|
self.events_out.push(out.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inject_event(&mut self, event: Self::InEvent) {
|
|
|
|
self.send_request(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-05 06:28:18 +11:00
|
|
|
fn inject_dial_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
_: Self::OutboundOpenInfo,
|
|
|
|
error: ProtocolsHandlerUpgrErr<
|
|
|
|
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error,
|
|
|
|
>,
|
|
|
|
) {
|
2019-01-22 14:45:03 +01:00
|
|
|
if self.pending_error.is_none() {
|
|
|
|
self.pending_error = Some(error);
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 17:27:30 +01:00
|
|
|
|
2019-01-22 14:45:03 +01:00
|
|
|
#[inline]
|
2019-01-30 16:37:34 +01:00
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
|
|
|
self.keep_alive
|
2019-01-22 14:45:03 +01:00
|
|
|
}
|
|
|
|
|
2019-04-05 06:28:18 +11:00
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
) -> Poll<
|
|
|
|
ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>,
|
|
|
|
Self::Error,
|
|
|
|
> {
|
2019-01-22 14:45:03 +01:00
|
|
|
if let Some(err) = self.pending_error.take() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.events_out.is_empty() {
|
2019-04-05 06:28:18 +11:00
|
|
|
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(
|
|
|
|
self.events_out.remove(0),
|
|
|
|
)));
|
2019-01-22 14:45:03 +01:00
|
|
|
} else {
|
|
|
|
self.events_out.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.dial_queue.is_empty() {
|
2019-03-28 15:36:26 -03:00
|
|
|
if self.dial_negotiated < self.max_dial_negotiated {
|
2019-01-22 14:45:03 +01:00
|
|
|
self.dial_negotiated += 1;
|
2019-04-05 06:28:18 +11:00
|
|
|
return Ok(Async::Ready(
|
|
|
|
ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
2019-04-16 15:57:29 +02:00
|
|
|
protocol: SubstreamProtocol::new(self.dial_queue.remove(0)),
|
2019-04-05 06:28:18 +11:00
|
|
|
info: (),
|
|
|
|
},
|
|
|
|
));
|
2019-01-22 14:45:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.dial_queue.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|