swarm/handler: replace inject_* methods (#3085)

Previously, we had one callback for each kind of message that a `ConnectionHandler` would receive from either its `NetworkBehaviour` or the connection itself.

With this patch, we combine these functions, resulting in two callbacks:

- `on_behaviour_event`
- `on_connection_event`

Resolves #3080.
This commit is contained in:
João Oliveira
2022-11-17 17:19:36 +00:00
committed by GitHub
parent 6d49bf4a53
commit 7803524a76
30 changed files with 1718 additions and 1094 deletions

View File

@ -20,10 +20,11 @@
use crate::behaviour::{inject_from_swarm, FromSwarm};
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, SubstreamProtocol,
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, IntoConnectionHandler,
KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper};
use crate::upgrade::SendWrapper;
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use either::Either;
use libp2p_core::{
@ -155,6 +156,73 @@ pub struct ToggleConnectionHandler<TInner> {
inner: Option<TInner>,
}
impl<TInner> ToggleConnectionHandler<TInner>
where
TInner: ConnectionHandler,
{
fn on_fully_negotiated_inbound(
&mut self,
FullyNegotiatedInbound {
protocol: out,
info,
}: FullyNegotiatedInbound<
<Self as ConnectionHandler>::InboundProtocol,
<Self as ConnectionHandler>::InboundOpenInfo,
>,
) {
let out = match out {
EitherOutput::First(out) => out,
EitherOutput::Second(v) => void::unreachable(v),
};
if let Either::Left(info) = info {
#[allow(deprecated)]
self.inner
.as_mut()
.expect("Can't receive an inbound substream if disabled; QED")
.inject_fully_negotiated_inbound(out, info)
} else {
panic!("Unexpected Either::Right in enabled `inject_fully_negotiated_inbound`.")
}
}
fn on_listen_upgrade_error(
&mut self,
ListenUpgradeError { info, error: err }: ListenUpgradeError<
<Self as ConnectionHandler>::InboundOpenInfo,
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
let (inner, info) = match (self.inner.as_mut(), info) {
(Some(inner), Either::Left(info)) => (inner, info),
// Ignore listen upgrade errors in disabled state.
(None, Either::Right(())) => return,
(Some(_), Either::Right(())) => panic!(
"Unexpected `Either::Right` inbound info through \
`inject_listen_upgrade_error` in enabled state.",
),
(None, Either::Left(_)) => panic!(
"Unexpected `Either::Left` inbound info through \
`inject_listen_upgrade_error` in disabled state.",
),
};
let err = match err {
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
ConnectionHandlerUpgrErr::Upgrade(err) => {
ConnectionHandlerUpgrErr::Upgrade(err.map_err(|err| match err {
EitherError::A(e) => e,
EitherError::B(v) => void::unreachable(v),
}))
}
};
#[allow(deprecated)]
inner.inject_listen_upgrade_error(info, err)
}
}
impl<TInner> ConnectionHandler for ToggleConnectionHandler<TInner>
where
TInner: ConnectionHandler,
@ -182,94 +250,14 @@ where
}
}
fn inject_fully_negotiated_inbound(
&mut self,
out: <Self::InboundProtocol as InboundUpgradeSend>::Output,
info: Self::InboundOpenInfo,
) {
let out = match out {
EitherOutput::First(out) => out,
EitherOutput::Second(v) => void::unreachable(v),
};
if let Either::Left(info) = info {
self.inner
.as_mut()
.expect("Can't receive an inbound substream if disabled; QED")
.inject_fully_negotiated_inbound(out, info)
} else {
panic!("Unexpected Either::Right in enabled `inject_fully_negotiated_inbound`.")
}
}
fn inject_fully_negotiated_outbound(
&mut self,
out: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
info: Self::OutboundOpenInfo,
) {
self.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.inject_fully_negotiated_outbound(out, info)
}
fn inject_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::InEvent) {
#[allow(deprecated)]
self.inner
.as_mut()
.expect("Can't receive events if disabled; QED")
.inject_event(event)
}
fn inject_address_change(&mut self, addr: &Multiaddr) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_address_change(addr)
}
}
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
err: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
self.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.inject_dial_upgrade_error(info, err)
}
fn inject_listen_upgrade_error(
&mut self,
info: Self::InboundOpenInfo,
err: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
let (inner, info) = match (self.inner.as_mut(), info) {
(Some(inner), Either::Left(info)) => (inner, info),
// Ignore listen upgrade errors in disabled state.
(None, Either::Right(())) => return,
(Some(_), Either::Right(())) => panic!(
"Unexpected `Either::Right` inbound info through \
`inject_listen_upgrade_error` in enabled state.",
),
(None, Either::Left(_)) => panic!(
"Unexpected `Either::Left` inbound info through \
`inject_listen_upgrade_error` in disabled state.",
),
};
let err = match err {
ConnectionHandlerUpgrErr::Timeout => ConnectionHandlerUpgrErr::Timeout,
ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer,
ConnectionHandlerUpgrErr::Upgrade(err) => {
ConnectionHandlerUpgrErr::Upgrade(err.map_err(|err| match err {
EitherError::A(e) => e,
EitherError::B(v) => void::unreachable(v),
}))
}
};
inner.inject_listen_upgrade_error(info, err)
}
fn connection_keep_alive(&self) -> KeepAlive {
self.inner
.as_ref()
@ -294,6 +282,50 @@ where
Poll::Pending
}
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => {
self.on_fully_negotiated_inbound(fully_negotiated_inbound)
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol: out,
info,
}) =>
{
#[allow(deprecated)]
self.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.inject_fully_negotiated_outbound(out, info)
}
ConnectionEvent::AddressChange(address_change) => {
if let Some(inner) = self.inner.as_mut() {
#[allow(deprecated)]
inner.inject_address_change(address_change.new_address)
}
}
ConnectionEvent::DialUpgradeError(DialUpgradeError { info, error: err }) =>
{
#[allow(deprecated)]
self.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.inject_dial_upgrade_error(info, err)
}
ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => {
self.on_listen_upgrade_error(listen_upgrade_error)
}
}
}
}
#[cfg(test)]
@ -319,6 +351,9 @@ mod tests {
fn ignore_listen_upgrade_error_when_disabled() {
let mut handler = ToggleConnectionHandler::<dummy::ConnectionHandler> { inner: None };
handler.inject_listen_upgrade_error(Either::Right(()), ConnectionHandlerUpgrErr::Timeout);
handler.on_connection_event(ConnectionEvent::ListenUpgradeError(ListenUpgradeError {
info: Either::Right(()),
error: ConnectionHandlerUpgrErr::Timeout,
}));
}
}

View File

@ -149,7 +149,8 @@ where
}
/// Notifies the connection handler of an event.
pub fn inject_event(&mut self, event: THandler::InEvent) {
pub fn on_behaviour_event(&mut self, event: THandler::InEvent) {
#[allow(deprecated)]
self.handler.inject_event(event);
}
@ -180,6 +181,7 @@ where
match requested_substreams.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(()))) => continue,
Poll::Ready(Some(Err(user_data))) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(user_data, ConnectionHandlerUpgrErr::Timeout);
continue;
}
@ -208,10 +210,12 @@ where
match negotiating_out.poll_next_unpin(cx) {
Poll::Pending | Poll::Ready(None) => {}
Poll::Ready(Some((user_data, Ok(upgrade)))) => {
#[allow(deprecated)]
handler.inject_fully_negotiated_outbound(upgrade, user_data);
continue;
}
Poll::Ready(Some((user_data, Err(err)))) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(user_data, err);
continue;
}
@ -222,10 +226,12 @@ where
match negotiating_in.poll_next_unpin(cx) {
Poll::Pending | Poll::Ready(None) => {}
Poll::Ready(Some((user_data, Ok(upgrade)))) => {
#[allow(deprecated)]
handler.inject_fully_negotiated_inbound(upgrade, user_data);
continue;
}
Poll::Ready(Some((user_data, Err(err)))) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(user_data, err);
continue;
}
@ -273,6 +279,7 @@ where
match muxing.poll_unpin(cx)? {
Poll::Pending => {}
Poll::Ready(StreamMuxerEvent::AddressChange(address)) => {
#[allow(deprecated)]
handler.inject_address_change(&address);
return Poll::Ready(Ok(Event::AddressChange(address)));
}

View File

@ -193,7 +193,7 @@ pub async fn new_for_established_connection<THandler>(
.await
{
Either::Left((Some(command), _)) => match command {
Command::NotifyHandler(event) => connection.inject_event(event),
Command::NotifyHandler(event) => connection.on_behaviour_event(event),
Command::Close => {
command_receiver.close();
let (handler, closing_muxer) = connection.close();

View File

@ -1,5 +1,7 @@
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::handler::{InboundUpgradeSend, OutboundUpgradeSend};
use crate::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
};
use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol};
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade;
@ -66,41 +68,10 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
SubstreamProtocol::new(DeniedUpgrade, ())
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
_: Self::InboundOpenInfo,
) {
void::unreachable(protocol)
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
_: Self::OutboundOpenInfo,
) {
void::unreachable(protocol)
}
fn inject_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::InEvent) {
void::unreachable(event)
}
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
match error {
ConnectionHandlerUpgrErr::Timeout => unreachable!(),
ConnectionHandlerUpgrErr::Timer => unreachable!(),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)) => void::unreachable(e),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(_)) => {
unreachable!("Denied upgrade does not support any protocols")
}
}
}
fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::No
}
@ -118,4 +89,32 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
> {
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol, ..
}) => void::unreachable(protocol),
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol, ..
}) => void::unreachable(protocol),
ConnectionEvent::DialUpgradeError(DialUpgradeError { info: _, error }) => match error {
ConnectionHandlerUpgrErr::Timeout => unreachable!(),
ConnectionHandlerUpgrErr::Timer => unreachable!(),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)) => void::unreachable(e),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(_)) => {
unreachable!("Denied upgrade does not support any protocols")
}
},
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
}
}
}

View File

@ -70,15 +70,16 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect};
/// 1. Dialing by initiating a new outbound substream. In order to do so,
/// [`ConnectionHandler::poll()`] must return an [`ConnectionHandlerEvent::OutboundSubstreamRequest`],
/// providing an instance of [`libp2p_core::upgrade::OutboundUpgrade`] that is used to negotiate the
/// protocol(s). Upon success, [`ConnectionHandler::inject_fully_negotiated_outbound`]
/// is called with the final output of the upgrade.
/// protocol(s). Upon success, [`ConnectionHandler::on_connection_event`] is called with
/// [`ConnectionEvent::FullyNegotiatedOutbound`] translating the final output of the upgrade.
///
/// 2. Listening by accepting a new inbound substream. When a new inbound substream
/// is created on a connection, [`ConnectionHandler::listen_protocol`] is called
/// to obtain an instance of [`libp2p_core::upgrade::InboundUpgrade`] that is used to
/// negotiate the protocol(s). Upon success,
/// [`ConnectionHandler::inject_fully_negotiated_inbound`] is called with the final
/// output of the upgrade.
/// [`ConnectionHandler::on_connection_event`] is called with [`ConnectionEvent::FullyNegotiatedInbound`]
/// translating the final output of the upgrade.
///
///
/// # Connection Keep-Alive
///
@ -122,41 +123,93 @@ pub trait ConnectionHandler: Send + 'static {
/// of simultaneously open negotiated inbound substreams. In other words it is up to the
/// [`ConnectionHandler`] implementation to stop a malicious remote node to open and keep alive
/// an excessive amount of inbound substreams.
#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::FullyNegotiatedInbound` on `ConnectionHandler::on_connection_event` instead.
The default implemention of this `inject_*` method delegates to it."
)]
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
info: Self::InboundOpenInfo,
);
) {
self.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
FullyNegotiatedInbound { protocol, info },
))
}
/// Injects the output of a successful upgrade on a new outbound substream.
///
/// The second argument is the information that was previously passed to
/// [`ConnectionHandlerEvent::OutboundSubstreamRequest`].
#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::FullyNegotiatedOutbound` on `ConnectionHandler::on_connection_event` instead.
The default implemention of this `inject_*` method delegates to it."
)]
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
info: Self::OutboundOpenInfo,
);
) {
self.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
FullyNegotiatedOutbound { protocol, info },
))
}
/// Injects an event coming from the outside in the handler.
fn inject_event(&mut self, event: Self::InEvent);
#[deprecated(
since = "0.41.0",
note = "Implement `ConnectionHandler::on_behaviour_event` instead. The default implementation of `inject_event` delegates to it."
)]
fn inject_event(&mut self, event: Self::InEvent) {
self.on_behaviour_event(event);
}
/// Notifies the handler of a change in the address of the remote.
fn inject_address_change(&mut self, _new_address: &Multiaddr) {}
#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::AddressChange` on `ConnectionHandler::on_connection_event` instead.
The default implemention of this `inject_*` method delegates to it."
)]
fn inject_address_change(&mut self, new_address: &Multiaddr) {
self.on_connection_event(ConnectionEvent::AddressChange(AddressChange {
new_address,
}))
}
/// Indicates to the handler that upgrading an outbound substream to the given protocol has failed.
#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::DialUpgradeError` on `ConnectionHandler::on_connection_event` instead.
The default implemention of this `inject_*` method delegates to it."
)]
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
);
) {
self.on_connection_event(ConnectionEvent::DialUpgradeError(DialUpgradeError {
info,
error,
}))
}
/// Indicates to the handler that upgrading an inbound substream to the given protocol has failed.
#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::ListenUpgradeError` on `ConnectionHandler::on_connection_event` instead.
The default implemention of this `inject_*` method delegates to it."
)]
fn inject_listen_upgrade_error(
&mut self,
_: Self::InboundOpenInfo,
_: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
info: Self::InboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
self.on_connection_event(ConnectionEvent::ListenUpgradeError(ListenUpgradeError {
info,
error,
}))
}
/// Returns until when the connection should be kept alive.
@ -224,6 +277,76 @@ pub trait ConnectionHandler: Send + 'static {
{
ConnectionHandlerSelect::new(self, other)
}
/// Informs the handler about an event from the [`NetworkBehaviour`](super::NetworkBehaviour).
fn on_behaviour_event(&mut self, _event: Self::InEvent) {}
fn on_connection_event(
&mut self,
_event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
}
}
/// Enumeration with the list of the possible stream events
/// to pass to [`on_connection_event`](ConnectionHandler::on_connection_event).
pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
/// Informs the handler about the output of a successful upgrade on a new inbound substream.
FullyNegotiatedInbound(FullyNegotiatedInbound<IP, IOI>),
/// Informs the handler about the output of a successful upgrade on a new outbound stream.
FullyNegotiatedOutbound(FullyNegotiatedOutbound<OP, OOI>),
/// Informs the handler about a change in the address of the remote.
AddressChange(AddressChange<'a>),
/// Informs the handler that upgrading an outbound substream to the given protocol has failed.
DialUpgradeError(DialUpgradeError<OOI, OP>),
/// Informs the handler that upgrading an inbound substream to the given protocol has failed.
ListenUpgradeError(ListenUpgradeError<IOI, IP>),
}
/// [`ConnectionEvent`] variant that informs the handler about
/// the output of a successful upgrade on a new inbound substream.
///
/// Note that it is up to the [`ConnectionHandler`] implementation to manage the lifetime of the
/// negotiated inbound substreams. E.g. the implementation has to enforce a limit on the number
/// of simultaneously open negotiated inbound substreams. In other words it is up to the
/// [`ConnectionHandler`] implementation to stop a malicious remote node to open and keep alive
/// an excessive amount of inbound substreams.
pub struct FullyNegotiatedInbound<IP: InboundUpgradeSend, IOI> {
pub protocol: IP::Output,
pub info: IOI,
}
/// [`ConnectionEvent`] variant that informs the handler about successful upgrade on a new outbound stream.
///
/// The `protocol` field is the information that was previously passed to
/// [`ConnectionHandlerEvent::OutboundSubstreamRequest`].
pub struct FullyNegotiatedOutbound<OP: OutboundUpgradeSend, OOI> {
pub protocol: OP::Output,
pub info: OOI,
}
/// [`ConnectionEvent`] variant that informs the handler about a change in the address of the remote.
pub struct AddressChange<'a> {
pub new_address: &'a Multiaddr,
}
/// [`ConnectionEvent`] variant that informs the handler
/// that upgrading an outbound substream to the given protocol has failed.
pub struct DialUpgradeError<OOI, OP: OutboundUpgradeSend> {
pub info: OOI,
pub error: ConnectionHandlerUpgrErr<OP::Error>,
}
/// [`ConnectionEvent`] variant that informs the handler
/// that upgrading an inbound substream to the given protocol has failed.
pub struct ListenUpgradeError<IOI, IP: InboundUpgradeSend> {
pub info: IOI,
pub error: ConnectionHandlerUpgrErr<IP::Error>,
}
/// Configuration of inbound or outbound substream protocol(s)

View File

@ -19,14 +19,15 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, SubstreamProtocol,
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent,
ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper};
use crate::upgrade::SendWrapper;
use either::Either;
use libp2p_core::either::{EitherError, EitherOutput};
use libp2p_core::upgrade::{EitherUpgrade, UpgradeError};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_core::{ConnectedPoint, PeerId};
use std::task::{Context, Poll};
/// Auxiliary type to allow implementing [`IntoConnectionHandler`]. As [`IntoConnectionHandler`] is
@ -119,181 +120,16 @@ where
}
}
fn inject_fully_negotiated_outbound(
&mut self,
output: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
info: Self::OutboundOpenInfo,
) {
match (self, output, info) {
(Either::Left(handler), EitherOutput::First(output), Either::Left(info)) => {
handler.inject_fully_negotiated_outbound(output, info)
}
(Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) => {
handler.inject_fully_negotiated_outbound(output, info)
}
_ => unreachable!(),
}
}
fn inject_fully_negotiated_inbound(
&mut self,
output: <Self::InboundProtocol as InboundUpgradeSend>::Output,
info: Self::InboundOpenInfo,
) {
match (self, output, info) {
(Either::Left(handler), EitherOutput::First(output), Either::Left(info)) => {
handler.inject_fully_negotiated_inbound(output, info)
}
(Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) => {
handler.inject_fully_negotiated_inbound(output, info)
}
_ => unreachable!(),
}
}
fn inject_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::InEvent) {
match (self, event) {
#[allow(deprecated)]
(Either::Left(handler), Either::Left(event)) => handler.inject_event(event),
#[allow(deprecated)]
(Either::Right(handler), Either::Right(event)) => handler.inject_event(event),
_ => unreachable!(),
}
}
fn inject_address_change(&mut self, addr: &Multiaddr) {
match self {
Either::Left(handler) => handler.inject_address_change(addr),
Either::Right(handler) => handler.inject_address_change(addr),
}
}
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
match error {
ConnectionHandlerUpgrErr::Timer => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Timeout => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => {
match (self, info) {
(Either::Right(handler), Either::Right(info)) => {
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
}
}
fn inject_listen_upgrade_error(
&mut self,
info: Self::InboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
match error {
ConnectionHandlerUpgrErr::Timer => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Timeout => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
(Either::Right(handler), Either::Right(info)) => {
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => {
match (self, info) {
(Either::Right(handler), Either::Right(info)) => {
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
}
}
fn connection_keep_alive(&self) -> KeepAlive {
match self {
Either::Left(handler) => handler.connection_keep_alive(),
@ -327,4 +163,199 @@ where
Poll::Ready(event)
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol: output,
info,
}) => match (self, output, info) {
(Either::Left(handler), EitherOutput::First(output), Either::Left(info)) =>
{
#[allow(deprecated)]
handler.inject_fully_negotiated_outbound(output, info)
}
(Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) =>
{
#[allow(deprecated)]
handler.inject_fully_negotiated_outbound(output, info)
}
_ => unreachable!(),
},
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol: output,
info,
}) => match (self, output, info) {
(Either::Left(handler), EitherOutput::First(output), Either::Left(info)) =>
{
#[allow(deprecated)]
handler.inject_fully_negotiated_inbound(output, info)
}
(Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) =>
{
#[allow(deprecated)]
handler.inject_fully_negotiated_inbound(output, info)
}
_ => unreachable!(),
},
ConnectionEvent::AddressChange(AddressChange { new_address: addr }) => match self {
#[allow(deprecated)]
Either::Left(handler) => handler.inject_address_change(addr),
#[allow(deprecated)]
Either::Right(handler) => handler.inject_address_change(addr),
},
ConnectionEvent::DialUpgradeError(DialUpgradeError { info, error }) => match error {
ConnectionHandlerUpgrErr::Timer => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Timeout => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => {
match (self, info) {
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
},
ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info, error }) => {
match error {
ConnectionHandlerUpgrErr::Timer => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler
.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler
.inject_listen_upgrade_error(info, ConnectionHandlerUpgrErr::Timer);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Timeout => match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Timeout,
);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Timeout,
);
}
_ => unreachable!(),
},
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(error)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => {
match (self, info) {
(Either::Left(handler), Either::Left(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => {
match (self, info) {
(Either::Right(handler), Either::Right(info)) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
);
}
_ => unreachable!(),
}
}
}
}
}
}
}

View File

@ -19,11 +19,10 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, ListenUpgradeError,
SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_core::Multiaddr;
use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll};
/// Wrapper around a protocol handler that turns the input event into something else.
@ -64,48 +63,13 @@ where
self.inner.listen_protocol()
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
info: Self::InboundOpenInfo,
) {
self.inner.inject_fully_negotiated_inbound(protocol, info)
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
info: Self::OutboundOpenInfo,
) {
self.inner.inject_fully_negotiated_outbound(protocol, info)
}
fn inject_event(&mut self, event: TNewIn) {
fn on_behaviour_event(&mut self, event: TNewIn) {
if let Some(event) = (self.map)(event) {
#[allow(deprecated)]
self.inner.inject_event(event);
}
}
fn inject_address_change(&mut self, addr: &Multiaddr) {
self.inner.inject_address_change(addr)
}
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
self.inner.inject_dial_upgrade_error(info, error)
}
fn inject_listen_upgrade_error(
&mut self,
info: Self::InboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
self.inner.inject_listen_upgrade_error(info, error)
}
fn connection_keep_alive(&self) -> KeepAlive {
self.inner.connection_keep_alive()
}
@ -123,4 +87,45 @@ where
> {
self.inner.poll(cx)
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, info }) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_inbound(protocol, info)
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol,
info,
}) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_outbound(protocol, info)
}
ConnectionEvent::AddressChange(AddressChange { new_address }) =>
{
#[allow(deprecated)]
self.inner.inject_address_change(new_address)
}
ConnectionEvent::DialUpgradeError(DialUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_dial_upgrade_error(info, error)
}
ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_listen_upgrade_error(info, error)
}
}
}
}

View File

@ -19,11 +19,10 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, ListenUpgradeError,
SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_core::Multiaddr;
use std::fmt::Debug;
use std::task::{Context, Poll};
@ -59,46 +58,11 @@ where
self.inner.listen_protocol()
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
info: Self::InboundOpenInfo,
) {
self.inner.inject_fully_negotiated_inbound(protocol, info)
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
info: Self::OutboundOpenInfo,
) {
self.inner.inject_fully_negotiated_outbound(protocol, info)
}
fn inject_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::InEvent) {
#[allow(deprecated)]
self.inner.inject_event(event)
}
fn inject_address_change(&mut self, addr: &Multiaddr) {
self.inner.inject_address_change(addr)
}
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
self.inner.inject_dial_upgrade_error(info, error)
}
fn inject_listen_upgrade_error(
&mut self,
info: Self::InboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
) {
self.inner.inject_listen_upgrade_error(info, error)
}
fn connection_keep_alive(&self) -> KeepAlive {
self.inner.connection_keep_alive()
}
@ -122,4 +86,45 @@ where
}
})
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, info }) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_inbound(protocol, info)
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol,
info,
}) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_outbound(protocol, info)
}
ConnectionEvent::AddressChange(AddressChange { new_address }) =>
{
#[allow(deprecated)]
self.inner.inject_address_change(new_address)
}
ConnectionEvent::DialUpgradeError(DialUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_dial_upgrade_error(info, error)
}
ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_listen_upgrade_error(info, error)
}
}
}
}

View File

@ -127,6 +127,7 @@ where
(key, arg): Self::OutboundOpenInfo,
) {
if let Some(h) = self.handlers.get_mut(&key) {
#[allow(deprecated)]
h.inject_fully_negotiated_outbound(protocol, arg)
} else {
log::error!("inject_fully_negotiated_outbound: no handler for key")
@ -140,6 +141,7 @@ where
) {
if let Some(h) = self.handlers.get_mut(&key) {
if let Some(i) = info.take(&key) {
#[allow(deprecated)]
h.inject_fully_negotiated_inbound(arg, i)
}
} else {
@ -147,8 +149,9 @@ where
}
}
fn inject_event(&mut self, (key, event): Self::InEvent) {
fn on_behaviour_event(&mut self, (key, event): Self::InEvent) {
if let Some(h) = self.handlers.get_mut(&key) {
#[allow(deprecated)]
h.inject_event(event)
} else {
log::error!("inject_event: no handler for key")
@ -157,6 +160,7 @@ where
fn inject_address_change(&mut self, addr: &Multiaddr) {
for h in self.handlers.values_mut() {
#[allow(deprecated)]
h.inject_address_change(addr)
}
}
@ -167,6 +171,7 @@ where
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
if let Some(h) = self.handlers.get_mut(&key) {
#[allow(deprecated)]
h.inject_dial_upgrade_error(arg, error)
} else {
log::error!("inject_dial_upgrade_error: no handler for protocol")
@ -182,6 +187,7 @@ where
ConnectionHandlerUpgrErr::Timer => {
for (k, h) in &mut self.handlers {
if let Some(i) = info.take(k) {
#[allow(deprecated)]
h.inject_listen_upgrade_error(i, ConnectionHandlerUpgrErr::Timer)
}
}
@ -189,6 +195,7 @@ where
ConnectionHandlerUpgrErr::Timeout => {
for (k, h) in &mut self.handlers {
if let Some(i) = info.take(k) {
#[allow(deprecated)]
h.inject_listen_upgrade_error(i, ConnectionHandlerUpgrErr::Timeout)
}
}
@ -196,6 +203,7 @@ where
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
for (k, h) in &mut self.handlers {
if let Some(i) = info.take(k) {
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
@ -214,6 +222,7 @@ where
let e = NegotiationError::ProtocolError(ProtocolError::IoError(
e.kind().into(),
));
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e)),
@ -225,6 +234,7 @@ where
for (k, h) in &mut self.handlers {
if let Some(i) = info.take(k) {
let e = NegotiationError::ProtocolError(ProtocolError::InvalidMessage);
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e)),
@ -236,6 +246,7 @@ where
for (k, h) in &mut self.handlers {
if let Some(i) = info.take(k) {
let e = NegotiationError::ProtocolError(ProtocolError::InvalidProtocol);
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e)),
@ -248,6 +259,7 @@ where
if let Some(i) = info.take(k) {
let e =
NegotiationError::ProtocolError(ProtocolError::TooManyProtocols);
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e)),
@ -259,6 +271,7 @@ where
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply((k, e))) => {
if let Some(h) = self.handlers.get_mut(&k) {
if let Some(i) = info.take(&k) {
#[allow(deprecated)]
h.inject_listen_upgrade_error(
i,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),

View File

@ -19,7 +19,8 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive,
SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
@ -132,42 +133,10 @@ where
self.listen_protocol.clone()
}
fn inject_fully_negotiated_inbound(
&mut self,
out: <Self::InboundProtocol as InboundUpgradeSend>::Output,
(): Self::InboundOpenInfo,
) {
// If we're shutting down the connection for inactivity, reset the timeout.
if !self.keep_alive.is_yes() {
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout);
}
self.events_out.push(out.into());
}
fn inject_fully_negotiated_outbound(
&mut self,
out: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
_: Self::OutboundOpenInfo,
) {
self.dial_negotiated -= 1;
self.events_out.push(out.into());
}
fn inject_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::InEvent) {
self.send_request(event);
}
fn inject_dial_upgrade_error(
&mut self,
_info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
) {
if self.pending_error.is_none() {
self.pending_error = Some(error);
}
}
fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive
}
@ -212,6 +181,44 @@ where
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol: out,
..
}) => {
// If we're shutting down the connection for inactivity, reset the timeout.
if !self.keep_alive.is_yes() {
self.keep_alive =
KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout);
}
self.events_out.push(out.into());
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol: out,
..
}) => {
self.dial_negotiated -= 1;
self.events_out.push(out.into());
}
ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => {
if self.pending_error.is_none() {
self.pending_error = Some(error);
}
}
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
}
}
}
/// Configuration parameters for the `OneShotHandler`

View File

@ -20,14 +20,10 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
SubstreamProtocol,
};
use crate::NegotiatedSubstream;
use libp2p_core::{
upgrade::{InboundUpgrade, OutboundUpgrade, PendingUpgrade},
Multiaddr,
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, FullyNegotiatedInbound,
FullyNegotiatedOutbound, KeepAlive, SubstreamProtocol,
};
use libp2p_core::upgrade::PendingUpgrade;
use std::task::{Context, Poll};
use void::Void;
@ -56,50 +52,10 @@ impl ConnectionHandler for PendingConnectionHandler {
SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ())
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
_: Self::InboundOpenInfo,
) {
void::unreachable(protocol)
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
_info: Self::OutboundOpenInfo,
) {
void::unreachable(protocol);
#[allow(unreachable_code)]
{
void::unreachable(_info);
}
}
fn inject_event(&mut self, v: Self::InEvent) {
fn on_behaviour_event(&mut self, v: Self::InEvent) {
void::unreachable(v)
}
fn inject_address_change(&mut self, _: &Multiaddr) {}
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
_: ConnectionHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
}
fn inject_listen_upgrade_error(
&mut self,
_: Self::InboundOpenInfo,
_: ConnectionHandlerUpgrErr<
<Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
}
fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::No
}
@ -117,4 +73,33 @@ impl ConnectionHandler for PendingConnectionHandler {
> {
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol, ..
}) => void::unreachable(protocol),
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol,
info: _info,
}) => {
void::unreachable(protocol);
#[allow(unreachable_code)]
{
void::unreachable(_info);
}
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::DialUpgradeError(_)
| ConnectionEvent::ListenUpgradeError(_) => {}
}
}
}

View File

@ -19,15 +19,16 @@
// DEALINGS IN THE SOFTWARE.
use crate::handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, SubstreamProtocol,
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, IntoConnectionHandler,
KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper};
use crate::upgrade::SendWrapper;
use libp2p_core::{
either::{EitherError, EitherOutput},
upgrade::{EitherUpgrade, NegotiationError, ProtocolError, SelectUpgrade, UpgradeError},
ConnectedPoint, Multiaddr, PeerId,
ConnectedPoint, PeerId,
};
use std::{cmp, task::Context, task::Poll};
@ -97,45 +98,30 @@ impl<TProto1, TProto2> ConnectionHandlerSelect<TProto1, TProto2> {
}
}
impl<TProto1, TProto2> ConnectionHandler for ConnectionHandlerSelect<TProto1, TProto2>
impl<TProto1, TProto2> ConnectionHandlerSelect<TProto1, TProto2>
where
TProto1: ConnectionHandler,
TProto2: ConnectionHandler,
{
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
type Error = EitherError<TProto1::Error, TProto2::Error>;
type InboundProtocol = SelectUpgrade<
SendWrapper<<TProto1 as ConnectionHandler>::InboundProtocol>,
SendWrapper<<TProto2 as ConnectionHandler>::InboundProtocol>,
>;
type OutboundProtocol = EitherUpgrade<
SendWrapper<TProto1::OutboundProtocol>,
SendWrapper<TProto2::OutboundProtocol>,
>;
type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>;
type InboundOpenInfo = (TProto1::InboundOpenInfo, TProto2::InboundOpenInfo);
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
let proto1 = self.proto1.listen_protocol();
let proto2 = self.proto2.listen_protocol();
let timeout = *std::cmp::max(proto1.timeout(), proto2.timeout());
let (u1, i1) = proto1.into_upgrade();
let (u2, i2) = proto2.into_upgrade();
let choice = SelectUpgrade::new(SendWrapper(u1), SendWrapper(u2));
SubstreamProtocol::new(choice, (i1, i2)).with_timeout(timeout)
}
fn inject_fully_negotiated_outbound(
fn on_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
endpoint: Self::OutboundOpenInfo,
FullyNegotiatedOutbound {
protocol,
info: endpoint,
}: FullyNegotiatedOutbound<
<Self as ConnectionHandler>::OutboundProtocol,
<Self as ConnectionHandler>::OutboundOpenInfo,
>,
) {
match (protocol, endpoint) {
(EitherOutput::First(protocol), EitherOutput::First(info)) => {
(EitherOutput::First(protocol), EitherOutput::First(info)) =>
{
#[allow(deprecated)]
self.proto1.inject_fully_negotiated_outbound(protocol, info)
}
(EitherOutput::Second(protocol), EitherOutput::Second(info)) => {
(EitherOutput::Second(protocol), EitherOutput::Second(info)) =>
{
#[allow(deprecated)]
self.proto2.inject_fully_negotiated_outbound(protocol, info)
}
(EitherOutput::First(_), EitherOutput::Second(_)) => {
@ -147,45 +133,47 @@ where
}
}
fn inject_fully_negotiated_inbound(
fn on_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
(i1, i2): Self::InboundOpenInfo,
FullyNegotiatedInbound {
protocol,
info: (i1, i2),
}: FullyNegotiatedInbound<
<Self as ConnectionHandler>::InboundProtocol,
<Self as ConnectionHandler>::InboundOpenInfo,
>,
) {
match protocol {
EitherOutput::First(protocol) => {
EitherOutput::First(protocol) =>
{
#[allow(deprecated)]
self.proto1.inject_fully_negotiated_inbound(protocol, i1)
}
EitherOutput::Second(protocol) => {
EitherOutput::Second(protocol) =>
{
#[allow(deprecated)]
self.proto2.inject_fully_negotiated_inbound(protocol, i2)
}
}
}
fn inject_event(&mut self, event: Self::InEvent) {
match event {
EitherOutput::First(event) => self.proto1.inject_event(event),
EitherOutput::Second(event) => self.proto2.inject_event(event),
}
}
fn inject_address_change(&mut self, new_address: &Multiaddr) {
self.proto1.inject_address_change(new_address);
self.proto2.inject_address_change(new_address)
}
fn inject_dial_upgrade_error(
fn on_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>,
DialUpgradeError { info, error }: DialUpgradeError<
<Self as ConnectionHandler>::OutboundOpenInfo,
<Self as ConnectionHandler>::OutboundProtocol,
>,
) {
match (info, error) {
#[allow(deprecated)]
(EitherOutput::First(info), ConnectionHandlerUpgrErr::Timer) => self
.proto1
.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer),
#[allow(deprecated)]
(EitherOutput::First(info), ConnectionHandlerUpgrErr::Timeout) => self
.proto1
.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout),
#[allow(deprecated)]
(
EitherOutput::First(info),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
@ -193,6 +181,7 @@ where
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
),
#[allow(deprecated)]
(
EitherOutput::First(info),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(err))),
@ -206,12 +195,15 @@ where
) => {
panic!("Wrong API usage; the upgrade error doesn't match the outbound open info");
}
#[allow(deprecated)]
(EitherOutput::Second(info), ConnectionHandlerUpgrErr::Timeout) => self
.proto2
.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timeout),
#[allow(deprecated)]
(EitherOutput::Second(info), ConnectionHandlerUpgrErr::Timer) => self
.proto2
.inject_dial_upgrade_error(info, ConnectionHandlerUpgrErr::Timer),
#[allow(deprecated)]
(
EitherOutput::Second(info),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
@ -219,6 +211,7 @@ where
info,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(err)),
),
#[allow(deprecated)]
(
EitherOutput::Second(info),
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(err))),
@ -235,31 +228,42 @@ where
}
}
fn inject_listen_upgrade_error(
fn on_listen_upgrade_error(
&mut self,
(i1, i2): Self::InboundOpenInfo,
error: ConnectionHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>,
ListenUpgradeError {
info: (i1, i2),
error,
}: ListenUpgradeError<
<Self as ConnectionHandler>::InboundOpenInfo,
<Self as ConnectionHandler>::InboundProtocol,
>,
) {
match error {
ConnectionHandlerUpgrErr::Timer => {
#[allow(deprecated)]
self.proto1
.inject_listen_upgrade_error(i1, ConnectionHandlerUpgrErr::Timer);
#[allow(deprecated)]
self.proto2
.inject_listen_upgrade_error(i2, ConnectionHandlerUpgrErr::Timer)
}
ConnectionHandlerUpgrErr::Timeout => {
#[allow(deprecated)]
self.proto1
.inject_listen_upgrade_error(i1, ConnectionHandlerUpgrErr::Timeout);
#[allow(deprecated)]
self.proto2
.inject_listen_upgrade_error(i2, ConnectionHandlerUpgrErr::Timeout)
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
#[allow(deprecated)]
self.proto1.inject_listen_upgrade_error(
i1,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
NegotiationError::Failed,
)),
);
#[allow(deprecated)]
self.proto2.inject_listen_upgrade_error(
i2,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(
@ -291,22 +295,28 @@ where
e2 = NegotiationError::ProtocolError(ProtocolError::TooManyProtocols)
}
}
#[allow(deprecated)]
self.proto1.inject_listen_upgrade_error(
i1,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e1)),
);
#[allow(deprecated)]
self.proto2.inject_listen_upgrade_error(
i2,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e2)),
)
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => {
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) =>
{
#[allow(deprecated)]
self.proto1.inject_listen_upgrade_error(
i1,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
)
}
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => {
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) =>
{
#[allow(deprecated)]
self.proto2.inject_listen_upgrade_error(
i2,
ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)),
@ -314,6 +324,45 @@ where
}
}
}
}
impl<TProto1, TProto2> ConnectionHandler for ConnectionHandlerSelect<TProto1, TProto2>
where
TProto1: ConnectionHandler,
TProto2: ConnectionHandler,
{
type InEvent = EitherOutput<TProto1::InEvent, TProto2::InEvent>;
type OutEvent = EitherOutput<TProto1::OutEvent, TProto2::OutEvent>;
type Error = EitherError<TProto1::Error, TProto2::Error>;
type InboundProtocol = SelectUpgrade<
SendWrapper<<TProto1 as ConnectionHandler>::InboundProtocol>,
SendWrapper<<TProto2 as ConnectionHandler>::InboundProtocol>,
>;
type OutboundProtocol = EitherUpgrade<
SendWrapper<TProto1::OutboundProtocol>,
SendWrapper<TProto2::OutboundProtocol>,
>;
type OutboundOpenInfo = EitherOutput<TProto1::OutboundOpenInfo, TProto2::OutboundOpenInfo>;
type InboundOpenInfo = (TProto1::InboundOpenInfo, TProto2::InboundOpenInfo);
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
let proto1 = self.proto1.listen_protocol();
let proto2 = self.proto2.listen_protocol();
let timeout = *std::cmp::max(proto1.timeout(), proto2.timeout());
let (u1, i1) = proto1.into_upgrade();
let (u2, i2) = proto2.into_upgrade();
let choice = SelectUpgrade::new(SendWrapper(u1), SendWrapper(u2));
SubstreamProtocol::new(choice, (i1, i2)).with_timeout(timeout)
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
match event {
#[allow(deprecated)]
EitherOutput::First(event) => self.proto1.inject_event(event),
#[allow(deprecated)]
EitherOutput::Second(event) => self.proto2.inject_event(event),
}
}
fn connection_keep_alive(&self) -> KeepAlive {
cmp::max(
@ -369,4 +418,35 @@ where
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => {
self.on_fully_negotiated_outbound(fully_negotiated_outbound)
}
ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => {
self.on_fully_negotiated_inbound(fully_negotiated_inbound)
}
ConnectionEvent::AddressChange(address) => {
#[allow(deprecated)]
self.proto1.inject_address_change(address.new_address);
#[allow(deprecated)]
self.proto2.inject_address_change(address.new_address)
}
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
self.on_dial_upgrade_error(dial_upgrade_error)
}
ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => {
self.on_listen_upgrade_error(listen_upgrade_error)
}
}
}
}

View File

@ -1,14 +1,11 @@
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::handler::{
ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol,
ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound,
KeepAlive, SubstreamProtocol,
};
use crate::NegotiatedSubstream;
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::PeerId;
use libp2p_core::{
upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade},
Multiaddr,
};
use std::task::{Context, Poll};
use void::Void;
@ -76,46 +73,10 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
SubstreamProtocol::new(DeniedUpgrade, ())
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
_: Self::InboundOpenInfo,
) {
void::unreachable(protocol);
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
_: Self::OutboundOpenInfo,
) {
void::unreachable(protocol)
}
fn inject_event(&mut self, v: Self::InEvent) {
fn on_behaviour_event(&mut self, v: Self::InEvent) {
void::unreachable(v)
}
fn inject_address_change(&mut self, _: &Multiaddr) {}
fn inject_dial_upgrade_error(
&mut self,
_: Self::OutboundOpenInfo,
_: ConnectionHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
}
fn inject_listen_upgrade_error(
&mut self,
_: Self::InboundOpenInfo,
_: ConnectionHandlerUpgrErr<
<Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Error,
>,
) {
}
fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::Yes
}
@ -133,4 +94,26 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
> {
Poll::Pending
}
fn on_connection_event(
&mut self,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol, ..
}) => void::unreachable(protocol),
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol, ..
}) => void::unreachable(protocol),
ConnectionEvent::DialUpgradeError(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::AddressChange(_) => {}
}
}
}