Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(swarm): remove deprecated inject calls #3264

Merged
merged 16 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protocols/request-response/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ where
Err(oneshot::Canceled) => {
// The inbound upgrade has errored or timed out reading
// or waiting for the request. The handler is informed
// via `inject_listen_upgrade_error`.
// via `on_connection_event` call with `ConnectionEvent::ListenUpgradeError`.
}
}
}
Expand Down
31 changes: 10 additions & 21 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,28 +586,17 @@ pub enum NetworkBehaviourAction<
/// # SubstreamProtocol::new(DeniedUpgrade, ())
/// # }
/// #
/// # fn inject_fully_negotiated_inbound(
/// # &mut self,
/// # _: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::InboundOpenInfo,
/// # ) {
/// # }
/// #
/// # fn inject_fully_negotiated_outbound(
/// # &mut self,
/// # _: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
/// # _: Self::OutboundOpenInfo,
/// # ) {
/// # }
/// #
/// # fn inject_event(&mut self, _event: Self::InEvent) {}
/// # fn on_behaviour_event(&mut self, _event: Self::InEvent) {}
/// #
/// # fn inject_dial_upgrade_error(
/// # &mut self,
/// # _: Self::OutboundOpenInfo,
/// # _: ConnectionHandlerUpgrErr<Void>,
/// # ) {
/// # }
/// # fn on_connection_event(
/// # &mut self,
/// # event: ConnectionEvent<
/// # Self::InboundProtocol,
/// # Self::OutboundProtocol,
/// # Self::InboundOpenInfo,
/// # Self::OutboundOpenInfo,
/// # >,
/// # ) {}
/// #
/// # fn connection_keep_alive(&self) -> KeepAlive {
/// # KeepAlive::Yes
Expand Down
71 changes: 39 additions & 32 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

use crate::behaviour::{inject_from_swarm, FromSwarm};
use crate::handler::{
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr,
DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, IntoConnectionHandler,
KeepAlive, ListenUpgradeError, SubstreamProtocol,
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent,
ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::SendWrapper;
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
Expand Down Expand Up @@ -99,8 +99,7 @@ where
event: crate::THandlerOutEvent<Self>,
) {
if let Some(behaviour) = &mut self.inner {
#[allow(deprecated)]
behaviour.inject_event(peer_id, connection_id, event)
behaviour.on_connection_handler_event(peer_id, connection_id, event)
}
}

Expand Down Expand Up @@ -176,13 +175,17 @@ where
};

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)
.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
FullyNegotiatedInbound {
protocol: out,
info,
},
));
} else {
panic!("Unexpected Either::Right in enabled `inject_fully_negotiated_inbound`.")
panic!("Unexpected Either::Right in enabled `on_fully_negotiated_inbound`.")
}
}

Expand All @@ -199,11 +202,11 @@ where
(None, Either::Right(())) => return,
(Some(_), Either::Right(())) => panic!(
"Unexpected `Either::Right` inbound info through \
`inject_listen_upgrade_error` in enabled state.",
`on_listen_upgrade_error` in enabled state.",
),
(None, Either::Left(_)) => panic!(
"Unexpected `Either::Left` inbound info through \
`inject_listen_upgrade_error` in disabled state.",
`on_listen_upgrade_error` in disabled state.",
),
};

Expand All @@ -218,8 +221,10 @@ where
}
};

#[allow(deprecated)]
inner.inject_listen_upgrade_error(info, err)
inner.on_connection_event(ConnectionEvent::ListenUpgradeError(ListenUpgradeError {
info,
error: err,
}));
}
}

Expand Down Expand Up @@ -251,11 +256,10 @@ where
}

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)
.on_behaviour_event(event)
}

fn connection_keep_alive(&self) -> KeepAlive {
Expand Down Expand Up @@ -299,28 +303,31 @@ where
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)
}
}) => self
.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
FullyNegotiatedOutbound {
protocol: out,
info,
},
)),
ConnectionEvent::AddressChange(address_change) => {
if let Some(inner) = self.inner.as_mut() {
#[allow(deprecated)]
inner.inject_address_change(address_change.new_address)
inner.on_connection_event(ConnectionEvent::AddressChange(AddressChange {
new_address: 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::DialUpgradeError(DialUpgradeError { info, error: err }) => self
.inner
.as_mut()
.expect("Can't receive an outbound substream if disabled; QED")
.on_connection_event(ConnectionEvent::DialUpgradeError(DialUpgradeError {
info,
error: err,
})),
ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => {
self.on_listen_upgrade_error(listen_upgrade_error)
}
Expand Down
94 changes: 53 additions & 41 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ pub use error::{
PendingOutboundConnectionError,
};

use crate::handler::ConnectionHandler;
use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, DialUpgradeError, FullyNegotiatedInbound,
FullyNegotiatedOutbound, ListenUpgradeError,
};
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper};
use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol};
use futures::stream::FuturesUnordered;
Expand Down Expand Up @@ -150,8 +153,7 @@ where

/// Notifies the connection handler of an event.
pub fn on_behaviour_event(&mut self, event: THandler::InEvent) {
#[allow(deprecated)]
self.handler.inject_event(event);
self.handler.on_behaviour_event(event);
}

/// Begins an orderly shutdown of the connection, returning the connection
Expand Down Expand Up @@ -180,9 +182,13 @@ where
loop {
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);
Poll::Ready(Some(Err(info))) => {
handler.on_connection_event(ConnectionEvent::DialUpgradeError(
DialUpgradeError {
info,
error: ConnectionHandlerUpgrErr::Timeout,
},
));
continue;
}
Poll::Ready(None) | Poll::Pending => {}
Expand All @@ -209,14 +215,16 @@ where
// In case the [`ConnectionHandler`] can not make any more progress, poll the negotiating outbound streams.
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);
Poll::Ready(Some((info, Ok(protocol)))) => {
handler.on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
FullyNegotiatedOutbound { protocol, info },
));
continue;
}
Poll::Ready(Some((user_data, Err(err)))) => {
#[allow(deprecated)]
handler.inject_dial_upgrade_error(user_data, err);
Poll::Ready(Some((info, Err(error)))) => {
handler.on_connection_event(ConnectionEvent::DialUpgradeError(
DialUpgradeError { info, error },
));
continue;
}
}
Expand All @@ -225,14 +233,16 @@ where
// make any more progress, poll the negotiating inbound streams.
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);
Poll::Ready(Some((info, Ok(protocol)))) => {
handler.on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
FullyNegotiatedInbound { protocol, info },
));
continue;
}
Poll::Ready(Some((user_data, Err(err)))) => {
#[allow(deprecated)]
handler.inject_listen_upgrade_error(user_data, err);
Poll::Ready(Some((info, Err(error)))) => {
handler.on_connection_event(ConnectionEvent::ListenUpgradeError(
ListenUpgradeError { info, error },
));
continue;
}
}
Expand Down Expand Up @@ -279,8 +289,9 @@ where
match muxing.poll_unpin(cx)? {
Poll::Pending => {}
Poll::Ready(StreamMuxerEvent::AddressChange(address)) => {
#[allow(deprecated)]
handler.inject_address_change(&address);
handler.on_connection_event(ConnectionEvent::AddressChange(AddressChange {
new_address: &address,
}));
return Poll::Ready(Ok(Event::AddressChange(address)));
}
}
Expand Down Expand Up @@ -757,34 +768,35 @@ mod tests {
SubstreamProtocol::new(DeniedUpgrade, ()).with_timeout(self.upgrade_timeout)
}

fn inject_fully_negotiated_inbound(
fn on_connection_event(
&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,
event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
void::unreachable(protocol)
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
protocol,
..
}) => void::unreachable(protocol),
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol,
..
}) => void::unreachable(protocol),
ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => {
self.error = Some(error)
}
ConnectionEvent::AddressChange(_) | ConnectionEvent::ListenUpgradeError(_) => {}
}
}

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>,
) {
self.error = Some(error)
}

fn connection_keep_alive(&self) -> KeepAlive {
KeepAlive::Yes
}
Expand Down
Loading