2021-08-17 18:02:06 +02:00
|
|
|
// Copyright 2021 Protocol Labs.
|
|
|
|
//
|
|
|
|
// 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::{
|
2023-01-12 11:21:02 +00:00
|
|
|
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
|
2023-01-23 23:31:30 +11:00
|
|
|
FullyNegotiatedInbound, InboundUpgradeSend, IntoConnectionHandler, KeepAlive,
|
|
|
|
ListenUpgradeError, SubstreamProtocol,
|
2021-08-17 18:02:06 +02:00
|
|
|
};
|
2022-11-17 17:19:36 +00:00
|
|
|
use crate::upgrade::SendWrapper;
|
2021-08-17 18:02:06 +02:00
|
|
|
use either::Either;
|
2023-01-23 23:31:30 +11:00
|
|
|
use futures::future;
|
2023-01-18 13:35:07 +11:00
|
|
|
use libp2p_core::upgrade::UpgradeError;
|
2022-11-17 17:19:36 +00:00
|
|
|
use libp2p_core::{ConnectedPoint, PeerId};
|
2021-08-17 18:02:06 +02:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2022-11-17 09:28:40 +00:00
|
|
|
/// Auxiliary type to allow implementing [`IntoConnectionHandler`]. As [`IntoConnectionHandler`] is
|
|
|
|
/// already implemented for T, we cannot implement it for Either<A, B>.
|
2021-08-17 18:02:06 +02:00
|
|
|
pub enum IntoEitherHandler<L, R> {
|
|
|
|
Left(L),
|
|
|
|
Right(R),
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
/// Implementation of a [`IntoConnectionHandler`] that represents either of two [`IntoConnectionHandler`]
|
2021-08-17 18:02:06 +02:00
|
|
|
/// implementations.
|
2022-02-21 13:32:24 +01:00
|
|
|
impl<L, R> IntoConnectionHandler for IntoEitherHandler<L, R>
|
2021-08-17 18:02:06 +02:00
|
|
|
where
|
2022-02-21 13:32:24 +01:00
|
|
|
L: IntoConnectionHandler,
|
|
|
|
R: IntoConnectionHandler,
|
2021-08-17 18:02:06 +02:00
|
|
|
{
|
|
|
|
type Handler = Either<L::Handler, R::Handler>;
|
|
|
|
|
|
|
|
fn into_handler(self, p: &PeerId, c: &ConnectedPoint) -> Self::Handler {
|
|
|
|
match self {
|
|
|
|
IntoEitherHandler::Left(into_handler) => Either::Left(into_handler.into_handler(p, c)),
|
|
|
|
IntoEitherHandler::Right(into_handler) => {
|
|
|
|
Either::Right(into_handler.into_handler(p, c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
2021-08-17 18:02:06 +02:00
|
|
|
match self {
|
|
|
|
IntoEitherHandler::Left(into_handler) => {
|
2023-01-18 13:35:07 +11:00
|
|
|
Either::Left(SendWrapper(into_handler.inbound_protocol()))
|
2021-08-17 18:02:06 +02:00
|
|
|
}
|
|
|
|
IntoEitherHandler::Right(into_handler) => {
|
2023-01-18 13:35:07 +11:00
|
|
|
Either::Right(SendWrapper(into_handler.inbound_protocol()))
|
2021-08-17 18:02:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 09:28:40 +00:00
|
|
|
// Taken from https://github.com/bluss/either.
|
|
|
|
impl<L, R> IntoEitherHandler<L, R> {
|
|
|
|
/// Returns the left value.
|
|
|
|
pub fn unwrap_left(self) -> L {
|
|
|
|
match self {
|
|
|
|
IntoEitherHandler::Left(l) => l,
|
|
|
|
IntoEitherHandler::Right(_) => {
|
|
|
|
panic!("called `IntoEitherHandler::unwrap_left()` on a `Right` value.",)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the right value.
|
|
|
|
pub fn unwrap_right(self) -> R {
|
|
|
|
match self {
|
|
|
|
IntoEitherHandler::Right(r) => r,
|
|
|
|
IntoEitherHandler::Left(_) => {
|
|
|
|
panic!("called `IntoEitherHandler::unwrap_right()` on a `Left` value.",)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 11:21:02 +00:00
|
|
|
impl<LIP, RIP, LIOI, RIOI>
|
2023-01-18 13:35:07 +11:00
|
|
|
FullyNegotiatedInbound<Either<SendWrapper<LIP>, SendWrapper<RIP>>, Either<LIOI, RIOI>>
|
2023-01-12 11:21:02 +00:00
|
|
|
where
|
|
|
|
RIP: InboundUpgradeSend,
|
|
|
|
LIP: InboundUpgradeSend,
|
|
|
|
{
|
|
|
|
fn transpose(
|
|
|
|
self,
|
|
|
|
) -> Either<FullyNegotiatedInbound<LIP, LIOI>, FullyNegotiatedInbound<RIP, RIOI>> {
|
|
|
|
match self {
|
|
|
|
FullyNegotiatedInbound {
|
2023-01-23 23:31:30 +11:00
|
|
|
protocol: future::Either::Left(protocol),
|
2023-01-12 11:21:02 +00:00
|
|
|
info: Either::Left(info),
|
|
|
|
} => Either::Left(FullyNegotiatedInbound { protocol, info }),
|
|
|
|
FullyNegotiatedInbound {
|
2023-01-23 23:31:30 +11:00
|
|
|
protocol: future::Either::Right(protocol),
|
2023-01-12 11:21:02 +00:00
|
|
|
info: Either::Right(info),
|
|
|
|
} => Either::Right(FullyNegotiatedInbound { protocol, info }),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LIP, RIP, LIOI, RIOI>
|
2023-01-18 13:35:07 +11:00
|
|
|
ListenUpgradeError<Either<LIOI, RIOI>, Either<SendWrapper<LIP>, SendWrapper<RIP>>>
|
2023-01-12 11:21:02 +00:00
|
|
|
where
|
|
|
|
RIP: InboundUpgradeSend,
|
|
|
|
LIP: InboundUpgradeSend,
|
|
|
|
{
|
|
|
|
fn transpose(self) -> Either<ListenUpgradeError<LIOI, LIP>, ListenUpgradeError<RIOI, RIP>> {
|
|
|
|
match self {
|
|
|
|
ListenUpgradeError {
|
2023-01-18 10:05:59 +11:00
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(error))),
|
2023-01-12 11:21:02 +00:00
|
|
|
info: Either::Left(info),
|
|
|
|
} => Either::Left(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)),
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
2023-01-18 10:05:59 +11:00
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(error))),
|
2023-01-12 11:21:02 +00:00
|
|
|
info: Either::Right(info),
|
|
|
|
} => Either::Right(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)),
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
|
|
|
|
info: Either::Left(info),
|
|
|
|
} => Either::Left(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
|
|
|
|
info: Either::Right(info),
|
|
|
|
} => Either::Right(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timer,
|
|
|
|
info: Either::Left(info),
|
|
|
|
} => Either::Left(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timer,
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timer,
|
|
|
|
info: Either::Right(info),
|
|
|
|
} => Either::Right(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timer,
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timeout,
|
|
|
|
info: Either::Left(info),
|
|
|
|
} => Either::Left(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timeout,
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timeout,
|
|
|
|
info: Either::Right(info),
|
|
|
|
} => Either::Right(ListenUpgradeError {
|
|
|
|
error: ConnectionHandlerUpgrErr::Timeout,
|
|
|
|
info,
|
|
|
|
}),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
/// Implementation of a [`ConnectionHandler`] that represents either of two [`ConnectionHandler`]
|
2021-08-17 18:02:06 +02:00
|
|
|
/// implementations.
|
2022-02-21 13:32:24 +01:00
|
|
|
impl<L, R> ConnectionHandler for Either<L, R>
|
2021-08-17 18:02:06 +02:00
|
|
|
where
|
2022-02-21 13:32:24 +01:00
|
|
|
L: ConnectionHandler,
|
|
|
|
R: ConnectionHandler,
|
2021-08-17 18:02:06 +02:00
|
|
|
{
|
|
|
|
type InEvent = Either<L::InEvent, R::InEvent>;
|
|
|
|
type OutEvent = Either<L::OutEvent, R::OutEvent>;
|
|
|
|
type Error = Either<L::Error, R::Error>;
|
2023-01-18 13:35:07 +11:00
|
|
|
type InboundProtocol = Either<SendWrapper<L::InboundProtocol>, SendWrapper<R::InboundProtocol>>;
|
2021-08-17 18:02:06 +02:00
|
|
|
type OutboundProtocol =
|
2023-01-18 13:35:07 +11:00
|
|
|
Either<SendWrapper<L::OutboundProtocol>, SendWrapper<R::OutboundProtocol>>;
|
2021-08-17 18:02:06 +02:00
|
|
|
type InboundOpenInfo = Either<L::InboundOpenInfo, R::InboundOpenInfo>;
|
|
|
|
type OutboundOpenInfo = Either<L::OutboundOpenInfo, R::OutboundOpenInfo>;
|
|
|
|
|
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
|
|
|
|
match self {
|
|
|
|
Either::Left(a) => a
|
|
|
|
.listen_protocol()
|
2023-01-18 13:35:07 +11:00
|
|
|
.map_upgrade(|u| Either::Left(SendWrapper(u)))
|
2021-08-17 18:02:06 +02:00
|
|
|
.map_info(Either::Left),
|
|
|
|
Either::Right(b) => b
|
|
|
|
.listen_protocol()
|
2023-01-18 13:35:07 +11:00
|
|
|
.map_upgrade(|u| Either::Right(SendWrapper(u)))
|
2021-08-17 18:02:06 +02:00
|
|
|
.map_info(Either::Right),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:19:36 +00:00
|
|
|
fn on_behaviour_event(&mut self, event: Self::InEvent) {
|
2021-08-17 18:02:06 +02:00
|
|
|
match (self, event) {
|
2023-01-12 11:21:02 +00:00
|
|
|
(Either::Left(handler), Either::Left(event)) => handler.on_behaviour_event(event),
|
|
|
|
(Either::Right(handler), Either::Right(event)) => handler.on_behaviour_event(event),
|
2021-08-17 18:02:06 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
|
|
|
match self {
|
|
|
|
Either::Left(handler) => handler.connection_keep_alive(),
|
|
|
|
Either::Right(handler) => handler.connection_keep_alive(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
) -> Poll<
|
2022-02-21 13:32:24 +01:00
|
|
|
ConnectionHandlerEvent<
|
2021-08-17 18:02:06 +02:00
|
|
|
Self::OutboundProtocol,
|
|
|
|
Self::OutboundOpenInfo,
|
|
|
|
Self::OutEvent,
|
|
|
|
Self::Error,
|
|
|
|
>,
|
|
|
|
> {
|
|
|
|
let event = match self {
|
|
|
|
Either::Left(handler) => futures::ready!(handler.poll(cx))
|
|
|
|
.map_custom(Either::Left)
|
|
|
|
.map_close(Either::Left)
|
2023-01-18 13:35:07 +11:00
|
|
|
.map_protocol(|p| Either::Left(SendWrapper(p)))
|
2021-08-17 18:02:06 +02:00
|
|
|
.map_outbound_open_info(Either::Left),
|
|
|
|
Either::Right(handler) => futures::ready!(handler.poll(cx))
|
|
|
|
.map_custom(Either::Right)
|
|
|
|
.map_close(Either::Right)
|
2023-01-18 13:35:07 +11:00
|
|
|
.map_protocol(|p| Either::Right(SendWrapper(p)))
|
2021-08-17 18:02:06 +02:00
|
|
|
.map_outbound_open_info(Either::Right),
|
|
|
|
};
|
|
|
|
|
|
|
|
Poll::Ready(event)
|
|
|
|
}
|
2022-11-17 17:19:36 +00:00
|
|
|
|
|
|
|
fn on_connection_event(
|
|
|
|
&mut self,
|
|
|
|
event: ConnectionEvent<
|
|
|
|
Self::InboundProtocol,
|
|
|
|
Self::OutboundProtocol,
|
|
|
|
Self::InboundOpenInfo,
|
|
|
|
Self::OutboundOpenInfo,
|
|
|
|
>,
|
|
|
|
) {
|
|
|
|
match event {
|
2023-01-12 11:21:02 +00:00
|
|
|
ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => {
|
|
|
|
match (fully_negotiated_inbound.transpose(), self) {
|
|
|
|
(Either::Left(fully_negotiated_inbound), Either::Left(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
|
|
|
|
fully_negotiated_inbound,
|
|
|
|
)),
|
|
|
|
(Either::Right(fully_negotiated_inbound), Either::Right(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
|
|
|
|
fully_negotiated_inbound,
|
|
|
|
)),
|
|
|
|
_ => unreachable!(),
|
2022-11-17 17:19:36 +00:00
|
|
|
}
|
2023-01-12 11:21:02 +00:00
|
|
|
}
|
|
|
|
ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => {
|
|
|
|
match (fully_negotiated_outbound.transpose(), self) {
|
|
|
|
(Either::Left(fully_negotiated_outbound), Either::Left(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
|
|
|
|
fully_negotiated_outbound,
|
|
|
|
)),
|
|
|
|
(Either::Right(fully_negotiated_outbound), Either::Right(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
|
|
|
|
fully_negotiated_outbound,
|
|
|
|
)),
|
|
|
|
_ => unreachable!(),
|
2022-11-17 17:19:36 +00:00
|
|
|
}
|
2023-01-12 11:21:02 +00:00
|
|
|
}
|
|
|
|
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
|
|
|
|
match (dial_upgrade_error.transpose(), self) {
|
|
|
|
(Either::Left(dial_upgrade_error), Either::Left(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::DialUpgradeError(dial_upgrade_error)),
|
|
|
|
(Either::Right(dial_upgrade_error), Either::Right(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::DialUpgradeError(dial_upgrade_error)),
|
2022-11-17 17:19:36 +00:00
|
|
|
_ => unreachable!(),
|
2023-01-12 11:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => {
|
|
|
|
match (listen_upgrade_error.transpose(), self) {
|
|
|
|
(Either::Left(listen_upgrade_error), Either::Left(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::ListenUpgradeError(
|
|
|
|
listen_upgrade_error,
|
|
|
|
)),
|
|
|
|
(Either::Right(listen_upgrade_error), Either::Right(handler)) => handler
|
|
|
|
.on_connection_event(ConnectionEvent::ListenUpgradeError(
|
|
|
|
listen_upgrade_error,
|
|
|
|
)),
|
2022-11-17 17:19:36 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2023-01-12 11:21:02 +00:00
|
|
|
}
|
|
|
|
ConnectionEvent::AddressChange(address_change) => match self {
|
|
|
|
Either::Left(handler) => {
|
|
|
|
handler.on_connection_event(ConnectionEvent::AddressChange(address_change))
|
2022-11-17 17:19:36 +00:00
|
|
|
}
|
2023-01-12 11:21:02 +00:00
|
|
|
Either::Right(handler) => {
|
|
|
|
handler.on_connection_event(ConnectionEvent::AddressChange(address_change))
|
2022-11-17 17:19:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 18:02:06 +02:00
|
|
|
}
|