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.
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
use crate::handler::{
|
|
|
|
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
|
|
|
|
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.
|
2022-05-18 02:52:50 -05:00
|
|
|
pub struct MapInEvent<TConnectionHandler, TNewIn, TMap> {
|
|
|
|
inner: TConnectionHandler,
|
2018-11-26 14:01:08 +01:00
|
|
|
map: TMap,
|
|
|
|
marker: PhantomData<TNewIn>,
|
|
|
|
}
|
|
|
|
|
2022-05-18 02:52:50 -05:00
|
|
|
impl<TConnectionHandler, TMap, TNewIn> MapInEvent<TConnectionHandler, TNewIn, TMap> {
|
2018-11-26 14:01:08 +01:00
|
|
|
/// Creates a `MapInEvent`.
|
2022-05-18 02:52:50 -05:00
|
|
|
pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self {
|
2018-11-26 14:01:08 +01:00
|
|
|
MapInEvent {
|
|
|
|
inner,
|
|
|
|
map,
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 02:52:50 -05:00
|
|
|
impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
|
|
|
|
for MapInEvent<TConnectionHandler, TNewIn, TMap>
|
2018-11-26 14:01:08 +01:00
|
|
|
where
|
2022-05-18 02:52:50 -05:00
|
|
|
TConnectionHandler: ConnectionHandler,
|
|
|
|
TMap: Fn(TNewIn) -> Option<TConnectionHandler::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;
|
2022-05-18 02:52:50 -05:00
|
|
|
type OutEvent = TConnectionHandler::OutEvent;
|
|
|
|
type Error = TConnectionHandler::Error;
|
|
|
|
type InboundProtocol = TConnectionHandler::InboundProtocol;
|
|
|
|
type OutboundProtocol = TConnectionHandler::OutboundProtocol;
|
|
|
|
type InboundOpenInfo = TConnectionHandler::InboundOpenInfo;
|
|
|
|
type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo;
|
2018-11-26 14:01:08 +01:00
|
|
|
|
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,
|
2022-02-21 13:32:24 +01:00
|
|
|
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
|
2021-08-11 13:12:12 +02:00
|
|
|
) {
|
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,
|
2022-02-21 13:32:24 +01:00
|
|
|
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
|
2021-08-11 13:12:12 +02:00
|
|
|
) {
|
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<
|
2022-02-21 13:32:24 +01:00
|
|
|
ConnectionHandlerEvent<
|
2021-08-11 13:12:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|