2018-11-26 14:01:08 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-07-04 14:47:59 +02:00
|
|
|
use crate::protocols_handler::{
|
2021-08-11 13:12:12 +02:00
|
|
|
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
2018-11-26 14:01:08 +01:00
|
|
|
};
|
2021-08-11 13:12:12 +02:00
|
|
|
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
2020-08-18 16:27:02 +02:00
|
|
|
use libp2p_core::Multiaddr;
|
2021-08-11 12:41:28 +02:00
|
|
|
use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll};
|
2018-11-26 14:01:08 +01:00
|
|
|
|
|
|
|
/// Wrapper around a protocol handler that turns the input event into something else.
|
|
|
|
pub struct MapInEvent<TProtoHandler, TNewIn, TMap> {
|
|
|
|
inner: TProtoHandler,
|
|
|
|
map: TMap,
|
|
|
|
marker: PhantomData<TNewIn>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TProtoHandler, TMap, TNewIn> MapInEvent<TProtoHandler, TNewIn, TMap> {
|
|
|
|
/// Creates a `MapInEvent`.
|
|
|
|
pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
|
|
|
|
MapInEvent {
|
|
|
|
inner,
|
|
|
|
map,
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TProtoHandler, TMap, TNewIn> ProtocolsHandler for MapInEvent<TProtoHandler, TNewIn, TMap>
|
|
|
|
where
|
|
|
|
TProtoHandler: ProtocolsHandler,
|
|
|
|
TMap: Fn(TNewIn) -> Option<TProtoHandler::InEvent>,
|
2021-08-11 12:41:28 +02:00
|
|
|
TNewIn: Debug + Send + 'static,
|
2020-02-07 16:29:30 +01:00
|
|
|
TMap: Send + 'static,
|
2018-11-26 14:01:08 +01:00
|
|
|
{
|
|
|
|
type InEvent = TNewIn;
|
|
|
|
type OutEvent = TProtoHandler::OutEvent;
|
2018-12-28 15:11:35 +01:00
|
|
|
type Error = TProtoHandler::Error;
|
2018-11-26 14:01:08 +01:00
|
|
|
type InboundProtocol = TProtoHandler::InboundProtocol;
|
|
|
|
type OutboundProtocol = TProtoHandler::OutboundProtocol;
|
2020-08-23 16:57:20 +02:00
|
|
|
type InboundOpenInfo = TProtoHandler::InboundOpenInfo;
|
2018-11-26 14:01:08 +01:00
|
|
|
type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
|
|
|
|
|
2020-08-23 16:57:20 +02:00
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
|
2018-11-26 14:01:08 +01:00
|
|
|
self.inner.listen_protocol()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_inbound(
|
|
|
|
&mut self,
|
2020-08-23 16:57:20 +02:00
|
|
|
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
|
2021-08-11 13:12:12 +02:00
|
|
|
info: Self::InboundOpenInfo,
|
2018-11-26 14:01:08 +01:00
|
|
|
) {
|
2020-08-23 16:57:20 +02:00
|
|
|
self.inner.inject_fully_negotiated_inbound(protocol, info)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_outbound(
|
|
|
|
&mut self,
|
2020-02-07 16:29:30 +01:00
|
|
|
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
|
2021-08-11 13:12:12 +02:00
|
|
|
info: Self::OutboundOpenInfo,
|
2018-11-26 14:01:08 +01:00
|
|
|
) {
|
|
|
|
self.inner.inject_fully_negotiated_outbound(protocol, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_event(&mut self, event: TNewIn) {
|
|
|
|
if let Some(event) = (self.map)(event) {
|
|
|
|
self.inner.inject_event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:27:02 +02:00
|
|
|
fn inject_address_change(&mut self, addr: &Multiaddr) {
|
|
|
|
self.inner.inject_address_change(addr)
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
fn inject_dial_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
info: Self::OutboundOpenInfo,
|
|
|
|
error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
|
|
|
) {
|
2018-11-26 14:01:08 +01:00
|
|
|
self.inner.inject_dial_upgrade_error(info, error)
|
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
fn inject_listen_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
info: Self::InboundOpenInfo,
|
|
|
|
error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
|
|
|
) {
|
2020-08-23 16:57:20 +02:00
|
|
|
self.inner.inject_listen_upgrade_error(info, error)
|
2020-08-18 16:27:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:37:34 +01:00
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
2019-01-04 12:02:39 +01:00
|
|
|
self.inner.connection_keep_alive()
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:01:08 +01:00
|
|
|
fn poll(
|
|
|
|
&mut self,
|
2020-07-27 20:27:33 +00:00
|
|
|
cx: &mut Context<'_>,
|
2018-11-26 14:01:08 +01:00
|
|
|
) -> Poll<
|
2021-08-11 13:12:12 +02:00
|
|
|
ProtocolsHandlerEvent<
|
|
|
|
Self::OutboundProtocol,
|
|
|
|
Self::OutboundOpenInfo,
|
|
|
|
Self::OutEvent,
|
|
|
|
Self::Error,
|
|
|
|
>,
|
2018-11-26 14:01:08 +01:00
|
|
|
> {
|
2019-09-16 11:08:44 +02:00
|
|
|
self.inner.poll(cx)
|
2018-11-26 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|