use crate::PeerId;
use crate::upgrade::{
InboundUpgrade,
OutboundUpgrade,
UpgradeError,
};
use futures::prelude::*;
use std::{error, fmt, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
pub use self::dummy::DummyProtocolsHandler;
pub use self::map_in::MapInEvent;
pub use self::map_out::MapOutEvent;
pub use self::node_handler::{NodeHandlerWrapper, NodeHandlerWrapperBuilder};
pub use self::select::{IntoProtocolsHandlerSelect, ProtocolsHandlerSelect};
mod dummy;
mod map_in;
mod map_out;
mod node_handler;
mod select;
pub trait ProtocolsHandler {
type InEvent;
type OutEvent;
type Error: error::Error;
type Substream: AsyncRead + AsyncWrite;
type InboundProtocol: InboundUpgrade<Self::Substream>;
type OutboundProtocol: OutboundUpgrade<Self::Substream>;
type OutboundOpenInfo;
fn listen_protocol(&self) -> Self::InboundProtocol;
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<Self::Substream>>::Output
);
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Output,
info: Self::OutboundOpenInfo
);
fn inject_event(&mut self, event: Self::InEvent);
fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>);
fn inject_inbound_closed(&mut self);
fn connection_keep_alive(&self) -> bool;
fn shutdown(&mut self);
fn poll(&mut self) -> Poll<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>, Self::Error>;
#[inline]
fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap>
where
Self: Sized,
TMap: Fn(&TNewIn) -> Option<&Self::InEvent>,
{
MapInEvent::new(self, map)
}
#[inline]
fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap>
where
Self: Sized,
TMap: FnMut(Self::OutEvent) -> TNewOut,
{
MapOutEvent::new(self, map)
}
#[inline]
fn select<TProto2>(self, other: TProto2) -> ProtocolsHandlerSelect<Self, TProto2>
where
Self: Sized,
{
ProtocolsHandlerSelect::new(self, other)
}
#[inline]
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
where
Self: Sized,
{
IntoProtocolsHandler::into_node_handler_builder(self)
}
#[inline]
#[deprecated(note = "Use into_node_handler_builder instead")]
fn into_node_handler(self) -> NodeHandlerWrapper<Self>
where
Self: Sized,
{
#![allow(deprecated)]
self.into_node_handler_builder().build()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom> {
OutboundSubstreamRequest {
upgrade: TConnectionUpgrade,
info: TOutboundOpenInfo,
},
Shutdown,
Custom(TCustom),
}
impl<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, TCustom>
{
#[inline]
pub fn map_outbound_open_info<F, I>(
self,
map: F,
) -> ProtocolsHandlerEvent<TConnectionUpgrade, I, TCustom>
where
F: FnOnce(TOutboundOpenInfo) -> I,
{
match self {
ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info } => {
ProtocolsHandlerEvent::OutboundSubstreamRequest {
upgrade,
info: map(info),
}
}
ProtocolsHandlerEvent::Shutdown => ProtocolsHandlerEvent::Shutdown,
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
}
}
#[inline]
pub fn map_protocol<F, I>(
self,
map: F,
) -> ProtocolsHandlerEvent<I, TOutboundOpenInfo, TCustom>
where
F: FnOnce(TConnectionUpgrade) -> I,
{
match self {
ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info } => {
ProtocolsHandlerEvent::OutboundSubstreamRequest {
upgrade: map(upgrade),
info,
}
}
ProtocolsHandlerEvent::Shutdown => ProtocolsHandlerEvent::Shutdown,
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(val),
}
}
#[inline]
pub fn map_custom<F, I>(
self,
map: F,
) -> ProtocolsHandlerEvent<TConnectionUpgrade, TOutboundOpenInfo, I>
where
F: FnOnce(TCustom) -> I,
{
match self {
ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info } => {
ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info }
}
ProtocolsHandlerEvent::Shutdown => ProtocolsHandlerEvent::Shutdown,
ProtocolsHandlerEvent::Custom(val) => ProtocolsHandlerEvent::Custom(map(val)),
}
}
}
#[derive(Debug)]
pub enum ProtocolsHandlerUpgrErr<TUpgrErr> {
Timeout,
Timer,
MuxerDeniedSubstream,
Upgrade(UpgradeError<TUpgrErr>),
}
impl<TUpgrErr> fmt::Display for ProtocolsHandlerUpgrErr<TUpgrErr>
where
TUpgrErr: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ProtocolsHandlerUpgrErr::Timeout => {
write!(f, "Timeout error while opening a substream")
},
ProtocolsHandlerUpgrErr::Timer => {
write!(f, "Timer error while opening a substream")
},
ProtocolsHandlerUpgrErr::MuxerDeniedSubstream => {
write!(f, "Remote muxer denied our attempt to open a substream")
},
ProtocolsHandlerUpgrErr::Upgrade(err) => write!(f, "{}", err),
}
}
}
impl<TUpgrErr> error::Error for ProtocolsHandlerUpgrErr<TUpgrErr>
where
TUpgrErr: error::Error + 'static
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
ProtocolsHandlerUpgrErr::Timeout => None,
ProtocolsHandlerUpgrErr::Timer => None,
ProtocolsHandlerUpgrErr::MuxerDeniedSubstream => None,
ProtocolsHandlerUpgrErr::Upgrade(err) => Some(err),
}
}
}
pub trait IntoProtocolsHandler {
type Handler: ProtocolsHandler;
fn into_handler(self, remote_peer_id: &PeerId) -> Self::Handler;
#[inline]
fn select<TProto2>(self, other: TProto2) -> IntoProtocolsHandlerSelect<Self, TProto2>
where
Self: Sized,
{
IntoProtocolsHandlerSelect::new(self, other)
}
#[inline]
fn into_node_handler_builder(self) -> NodeHandlerWrapperBuilder<Self>
where
Self: Sized,
{
NodeHandlerWrapperBuilder::new(self, Duration::from_secs(10), Duration::from_secs(10), Duration::from_secs(5))
}
}
impl<T> IntoProtocolsHandler for T
where T: ProtocolsHandler
{
type Handler = Self;
#[inline]
fn into_handler(self, _: &PeerId) -> Self {
self
}
}