mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
feat(swarm)!: allow NetworkBehaviour
s to manage connections
Previously, a `ConnectionHandler` was immediately requested from the `NetworkBehaviour` as soon as a new dial was initiated or a new incoming connection accepted. With this patch, we delay the creation of the handler until the connection is actually established and fully upgraded, i.e authenticated and multiplexed. As a consequence, `NetworkBehaviour::new_handler` is now deprecated in favor of a new set of callbacks: - `NetworkBehaviour::handle_pending_inbound_connection` - `NetworkBehaviour::handle_pending_outbound_connection` - `NetworkBehaviour::handle_established_inbound_connection` - `NetworkBehaviour::handle_established_outbound_connection` All callbacks are fallible, allowing the `NetworkBehaviour` to abort the connection either immediately or after it is fully established. All callbacks also receive a `ConnectionId` parameter which uniquely identifies the connection. For example, in case a `NetworkBehaviour` issues a dial via `NetworkBehaviourAction::Dial`, it can unambiguously detect this dial in these lifecycle callbacks via the `ConnectionId`. Finally, `NetworkBehaviour::handle_pending_outbound_connection` also replaces `NetworkBehaviour::addresses_of_peer` by allowing the behaviour to return more addresses to be used for the dial. Resolves #2824. Pull-Request: #3254.
This commit is contained in:
@ -70,12 +70,12 @@ pub use handler::ProtocolSupport;
|
||||
|
||||
use futures::channel::oneshot;
|
||||
use handler::{Handler, RequestProtocol};
|
||||
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
|
||||
use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr, PeerId};
|
||||
use libp2p_swarm::{
|
||||
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
|
||||
dial_opts::DialOpts,
|
||||
ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
|
||||
THandlerInEvent, THandlerOutEvent,
|
||||
ConnectionDenied, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
|
||||
PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
@ -726,25 +726,59 @@ where
|
||||
type ConnectionHandler = Handler<TCodec>;
|
||||
type OutEvent = Event<TCodec::Request, TCodec::Response>;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
Handler::new(
|
||||
fn handle_established_inbound_connection(
|
||||
&mut self,
|
||||
_: ConnectionId,
|
||||
_: PeerId,
|
||||
_: &Multiaddr,
|
||||
_: &Multiaddr,
|
||||
) -> Result<THandler<Self>, ConnectionDenied> {
|
||||
Ok(Handler::new(
|
||||
self.inbound_protocols.clone(),
|
||||
self.codec.clone(),
|
||||
self.config.connection_keep_alive,
|
||||
self.config.request_timeout,
|
||||
self.next_inbound_id.clone(),
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
|
||||
fn handle_pending_outbound_connection(
|
||||
&mut self,
|
||||
_connection_id: ConnectionId,
|
||||
maybe_peer: Option<PeerId>,
|
||||
_addresses: &[Multiaddr],
|
||||
_effective_role: Endpoint,
|
||||
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
|
||||
let peer = match maybe_peer {
|
||||
None => return Ok(vec![]),
|
||||
Some(peer) => peer,
|
||||
};
|
||||
|
||||
let mut addresses = Vec::new();
|
||||
if let Some(connections) = self.connected.get(peer) {
|
||||
if let Some(connections) = self.connected.get(&peer) {
|
||||
addresses.extend(connections.iter().filter_map(|c| c.address.clone()))
|
||||
}
|
||||
if let Some(more) = self.addresses.get(peer) {
|
||||
if let Some(more) = self.addresses.get(&peer) {
|
||||
addresses.extend(more.into_iter().cloned());
|
||||
}
|
||||
addresses
|
||||
|
||||
Ok(addresses)
|
||||
}
|
||||
|
||||
fn handle_established_outbound_connection(
|
||||
&mut self,
|
||||
_: ConnectionId,
|
||||
_: PeerId,
|
||||
_: &Multiaddr,
|
||||
_: Endpoint,
|
||||
) -> Result<THandler<Self>, ConnectionDenied> {
|
||||
Ok(Handler::new(
|
||||
self.inbound_protocols.clone(),
|
||||
self.codec.clone(),
|
||||
self.config.connection_keep_alive,
|
||||
self.config.request_timeout,
|
||||
self.next_inbound_id.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
|
||||
|
Reference in New Issue
Block a user