mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 22:31:33 +00:00
swarm/src: Remove ConnectionHandler (#2519)
The `ConnectionHandler` trait is not exposed to users. The only implementor of `ConnectionHandler` is `NodeHandlerWrapper`. Thus `ConnectionHandler` is a superfluous abstraction. This commit removes `ConnectionHandler`. Next to this large change, this commit removes the `Tmuxer` trait parameter. `Swarm` enforces dynamic dispatching via `StreamMuxerBox` anyways, thus the trait parameter is useless. As a follow up to this commit one could rename `ProtocolsHandler` to `ConnectionHandler` and `NodeHandlerWrapper` to `ConnectionHandlerWrapper` or just `Wrapper`.
This commit is contained in:
@ -19,17 +19,19 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
mod error;
|
||||
pub(crate) mod handler;
|
||||
mod listeners;
|
||||
mod substream;
|
||||
|
||||
pub(crate) mod pool;
|
||||
|
||||
use crate::protocols_handler::{
|
||||
NodeHandlerWrapper, NodeHandlerWrapperError, NodeHandlerWrapperEvent,
|
||||
NodeHandlerWrapperOutboundOpenInfo, ProtocolsHandler,
|
||||
};
|
||||
pub use error::{
|
||||
ConnectionError, PendingConnectionError, PendingInboundConnectionError,
|
||||
PendingOutboundConnectionError,
|
||||
};
|
||||
pub use handler::{ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler};
|
||||
pub use listeners::{ListenersEvent, ListenersStream};
|
||||
pub use pool::{ConnectionCounters, ConnectionLimits};
|
||||
pub use pool::{EstablishedConnection, PendingConnection};
|
||||
@ -37,7 +39,7 @@ pub use substream::{Close, Substream, SubstreamEndpoint};
|
||||
|
||||
use libp2p_core::connection::ConnectedPoint;
|
||||
use libp2p_core::multiaddr::Multiaddr;
|
||||
use libp2p_core::muxing::StreamMuxer;
|
||||
use libp2p_core::muxing::StreamMuxerBox;
|
||||
use libp2p_core::PeerId;
|
||||
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
|
||||
use substream::{Muxing, SubstreamEvent};
|
||||
@ -54,28 +56,26 @@ pub struct Connected {
|
||||
/// Event generated by a [`Connection`].
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Event<T> {
|
||||
/// Event generated by the [`ConnectionHandler`].
|
||||
/// Event generated by the [`NodeHandlerWrapper`].
|
||||
Handler(T),
|
||||
/// Address of the remote has changed.
|
||||
AddressChange(Multiaddr),
|
||||
}
|
||||
|
||||
/// A multiplexed connection to a peer with an associated `ConnectionHandler`.
|
||||
pub struct Connection<TMuxer, THandler>
|
||||
/// A multiplexed connection to a peer with an associated [`NodeHandlerWrapper`].
|
||||
pub struct Connection<THandler>
|
||||
where
|
||||
TMuxer: StreamMuxer,
|
||||
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
|
||||
THandler: ProtocolsHandler,
|
||||
{
|
||||
/// Node that handles the muxing.
|
||||
muxing: substream::Muxing<TMuxer, THandler::OutboundOpenInfo>,
|
||||
muxing: substream::Muxing<StreamMuxerBox, NodeHandlerWrapperOutboundOpenInfo<THandler>>,
|
||||
/// Handler that processes substreams.
|
||||
handler: THandler,
|
||||
handler: NodeHandlerWrapper<THandler>,
|
||||
}
|
||||
|
||||
impl<TMuxer, THandler> fmt::Debug for Connection<TMuxer, THandler>
|
||||
impl<THandler> fmt::Debug for Connection<THandler>
|
||||
where
|
||||
TMuxer: StreamMuxer,
|
||||
THandler: ConnectionHandler<Substream = Substream<TMuxer>> + fmt::Debug,
|
||||
THandler: ProtocolsHandler + fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Connection")
|
||||
@ -85,21 +85,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<TMuxer, THandler> Unpin for Connection<TMuxer, THandler>
|
||||
where
|
||||
TMuxer: StreamMuxer,
|
||||
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
|
||||
{
|
||||
}
|
||||
impl<THandler> Unpin for Connection<THandler> where THandler: ProtocolsHandler {}
|
||||
|
||||
impl<TMuxer, THandler> Connection<TMuxer, THandler>
|
||||
impl<THandler> Connection<THandler>
|
||||
where
|
||||
TMuxer: StreamMuxer,
|
||||
THandler: ConnectionHandler<Substream = Substream<TMuxer>>,
|
||||
THandler: ProtocolsHandler,
|
||||
{
|
||||
/// Builds a new `Connection` from the given substream multiplexer
|
||||
/// and connection handler.
|
||||
pub fn new(muxer: TMuxer, handler: THandler) -> Self {
|
||||
pub fn new(muxer: StreamMuxerBox, handler: NodeHandlerWrapper<THandler>) -> Self {
|
||||
Connection {
|
||||
muxing: Muxing::new(muxer),
|
||||
handler,
|
||||
@ -113,7 +107,7 @@ 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) -> (THandler, Close<TMuxer>) {
|
||||
pub fn close(self) -> (NodeHandlerWrapper<THandler>, Close<StreamMuxerBox>) {
|
||||
(self.handler, self.muxing.close().0)
|
||||
}
|
||||
|
||||
@ -122,7 +116,12 @@ where
|
||||
pub fn poll(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
|
||||
) -> Poll<
|
||||
Result<
|
||||
Event<THandler::OutEvent>,
|
||||
ConnectionError<NodeHandlerWrapperError<THandler::Error>>,
|
||||
>,
|
||||
> {
|
||||
loop {
|
||||
let mut io_pending = false;
|
||||
|
||||
@ -154,10 +153,10 @@ where
|
||||
return Poll::Pending; // Nothing to do
|
||||
}
|
||||
}
|
||||
Poll::Ready(Ok(ConnectionHandlerEvent::OutboundSubstreamRequest(user_data))) => {
|
||||
Poll::Ready(Ok(NodeHandlerWrapperEvent::OutboundSubstreamRequest(user_data))) => {
|
||||
self.muxing.open_substream(user_data);
|
||||
}
|
||||
Poll::Ready(Ok(ConnectionHandlerEvent::Custom(event))) => {
|
||||
Poll::Ready(Ok(NodeHandlerWrapperEvent::Custom(event))) => {
|
||||
return Poll::Ready(Ok(Event::Handler(event)));
|
||||
}
|
||||
Poll::Ready(Err(err)) => return Poll::Ready(Err(ConnectionError::Handler(err))),
|
||||
|
Reference in New Issue
Block a user