mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 17:51:35 +00:00
refactor(swarm)!: remove handler
from NetworkBehaviourAction::Dial
(#3328)
We create the `ConnectionId` for the new connection as part of `DialOpts`. This allows `NetworkBehaviour`s to accurately track state regarding their own dial attempts. This patch is the main enabler of https://github.com/libp2p/rust-libp2p/pull/3254. Removing the `handler` field will allow us to deprecate the `NetworkBehaviour::new_handler` function in favor of four new ones that give more control over the connection lifecycle.
This commit is contained in:
@ -20,61 +20,27 @@
|
||||
|
||||
use crate::protocol;
|
||||
use either::Either;
|
||||
use libp2p_core::upgrade::DeniedUpgrade;
|
||||
use libp2p_core::{ConnectedPoint, PeerId};
|
||||
use libp2p_swarm::dummy;
|
||||
use libp2p_swarm::handler::SendWrapper;
|
||||
use libp2p_swarm::{ConnectionHandler, ConnectionId, IntoConnectionHandler};
|
||||
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler};
|
||||
|
||||
pub mod direct;
|
||||
pub mod relayed;
|
||||
|
||||
pub enum Prototype {
|
||||
DirectConnection {
|
||||
role: Role,
|
||||
relayed_connection_id: ConnectionId,
|
||||
},
|
||||
UnknownConnection,
|
||||
}
|
||||
|
||||
pub enum Role {
|
||||
Initiator { attempt: u8 },
|
||||
Listener,
|
||||
}
|
||||
pub struct Prototype;
|
||||
|
||||
impl IntoConnectionHandler for Prototype {
|
||||
type Handler = Either<relayed::Handler, Either<direct::Handler, dummy::ConnectionHandler>>;
|
||||
type Handler = Either<relayed::Handler, direct::Handler>;
|
||||
|
||||
fn into_handler(self, _remote_peer_id: &PeerId, endpoint: &ConnectedPoint) -> Self::Handler {
|
||||
match self {
|
||||
Self::UnknownConnection => {
|
||||
if endpoint.is_relayed() {
|
||||
Either::Left(relayed::Handler::new(endpoint.clone()))
|
||||
} else {
|
||||
Either::Right(Either::Right(dummy::ConnectionHandler))
|
||||
}
|
||||
}
|
||||
Self::DirectConnection {
|
||||
relayed_connection_id,
|
||||
..
|
||||
} => {
|
||||
assert!(
|
||||
!endpoint.is_relayed(),
|
||||
"`Prototype::DirectConnection` is never created for relayed connection."
|
||||
);
|
||||
Either::Right(Either::Left(direct::Handler::new(relayed_connection_id)))
|
||||
}
|
||||
if endpoint.is_relayed() {
|
||||
Either::Left(relayed::Handler::new(endpoint.clone()))
|
||||
} else {
|
||||
Either::Right(direct::Handler::default()) // This is a direct connection. What we don't know is whether it is the one we created or another one that happened accidentally.
|
||||
}
|
||||
}
|
||||
|
||||
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
|
||||
match self {
|
||||
Prototype::UnknownConnection => {
|
||||
Either::Left(SendWrapper(Either::Left(protocol::inbound::Upgrade {})))
|
||||
}
|
||||
Prototype::DirectConnection { .. } => {
|
||||
Either::Left(SendWrapper(Either::Right(DeniedUpgrade)))
|
||||
}
|
||||
}
|
||||
Either::Left(SendWrapper(Either::Left(protocol::inbound::Upgrade {})))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user