swarm/: Make NodeHandlerWrapper an impl detail of connection (#2523)

Previously one would wrap a `ProtocolsHandler` into a
`NodeHandlerWrapper` as early as possible, even though the functionality
of `NodeHandlerWrapper` is only needed within `mod connection`.

This commit makes `NodeHandlerWrapper` an implementation detail of `mod
connection`, thus neither `mod protocols_handler`, `mod pool` nor the
root level (`libp2p-swarm`) need to bother about the abstraction.

In addition to the above, this commit:

- Renames `NodeHandlerWrapper` to `HandlerWrapper`. The word `Node` is
  outdated.
- Removes `NodeHandlerWrapperBuilder`. With this simplification it is no
  longer needed.
- Folds `NodeHandlerWrapperError` into `ConnectionError`. No need for
  upper layers to be aware of the fact that `ProtocolHandler`s are
  wrapped.
This commit is contained in:
Max Inden
2022-02-18 11:32:58 +01:00
committed by GitHub
parent eeb3504d5f
commit 2ff9acee22
7 changed files with 123 additions and 195 deletions

View File

@ -19,15 +19,12 @@
// DEALINGS IN THE SOFTWARE.
mod error;
mod handler_wrapper;
mod listeners;
mod substream;
pub(crate) mod pool;
use crate::protocols_handler::{
NodeHandlerWrapper, NodeHandlerWrapperError, NodeHandlerWrapperEvent,
NodeHandlerWrapperOutboundOpenInfo, ProtocolsHandler,
};
pub use error::{
ConnectionError, PendingConnectionError, PendingInboundConnectionError,
PendingOutboundConnectionError,
@ -37,9 +34,12 @@ pub use pool::{ConnectionCounters, ConnectionLimits};
pub use pool::{EstablishedConnection, PendingConnection};
pub use substream::{Close, Substream, SubstreamEndpoint};
use crate::protocols_handler::ProtocolsHandler;
use handler_wrapper::HandlerWrapper;
use libp2p_core::connection::ConnectedPoint;
use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::muxing::StreamMuxerBox;
use libp2p_core::upgrade;
use libp2p_core::PeerId;
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
use substream::{Muxing, SubstreamEvent};
@ -56,21 +56,21 @@ pub struct Connected {
/// Event generated by a [`Connection`].
#[derive(Debug, Clone)]
pub enum Event<T> {
/// Event generated by the [`NodeHandlerWrapper`].
/// Event generated by the [`ProtocolsHandler`].
Handler(T),
/// Address of the remote has changed.
AddressChange(Multiaddr),
}
/// A multiplexed connection to a peer with an associated [`NodeHandlerWrapper`].
/// A multiplexed connection to a peer with an associated [`ProtocolsHandler`].
pub struct Connection<THandler>
where
THandler: ProtocolsHandler,
{
/// Node that handles the muxing.
muxing: substream::Muxing<StreamMuxerBox, NodeHandlerWrapperOutboundOpenInfo<THandler>>,
muxing: substream::Muxing<StreamMuxerBox, handler_wrapper::OutboundOpenInfo<THandler>>,
/// Handler that processes substreams.
handler: NodeHandlerWrapper<THandler>,
handler: HandlerWrapper<THandler>,
}
impl<THandler> fmt::Debug for Connection<THandler>
@ -93,10 +93,15 @@ where
{
/// Builds a new `Connection` from the given substream multiplexer
/// and connection handler.
pub fn new(muxer: StreamMuxerBox, handler: NodeHandlerWrapper<THandler>) -> Self {
pub fn new(
muxer: StreamMuxerBox,
handler: THandler,
substream_upgrade_protocol_override: Option<upgrade::Version>,
) -> Self {
let wrapped_handler = HandlerWrapper::new(handler, substream_upgrade_protocol_override);
Connection {
muxing: Muxing::new(muxer),
handler,
handler: wrapped_handler,
}
}
@ -107,8 +112,8 @@ where
/// Begins an orderly shutdown of the connection, returning the connection
/// handler and a `Future` that resolves when connection shutdown is complete.
pub fn close(self) -> (NodeHandlerWrapper<THandler>, Close<StreamMuxerBox>) {
(self.handler, self.muxing.close().0)
pub fn close(self) -> (THandler, Close<StreamMuxerBox>) {
(self.handler.into_protocols_handler(), self.muxing.close().0)
}
/// Polls the connection for events produced by the associated handler
@ -116,12 +121,7 @@ where
pub fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<
Result<
Event<THandler::OutEvent>,
ConnectionError<NodeHandlerWrapperError<THandler::Error>>,
>,
> {
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
loop {
let mut io_pending = false;
@ -153,13 +153,13 @@ where
return Poll::Pending; // Nothing to do
}
}
Poll::Ready(Ok(NodeHandlerWrapperEvent::OutboundSubstreamRequest(user_data))) => {
Poll::Ready(Ok(handler_wrapper::Event::OutboundSubstreamRequest(user_data))) => {
self.muxing.open_substream(user_data);
}
Poll::Ready(Ok(NodeHandlerWrapperEvent::Custom(event))) => {
Poll::Ready(Ok(handler_wrapper::Event::Custom(event))) => {
return Poll::Ready(Ok(Event::Handler(event)));
}
Poll::Ready(Err(err)) => return Poll::Ready(Err(ConnectionError::Handler(err))),
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
}
}
}