mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-08-01 01:11:58 +00:00
Full support for multiple connections per peer in libp2p-swarm. (#1519)
* [libp2p-swarm] Make the multiple connections per peer first-class. This commit makes the notion of multiple connections per peer first-class in the API of libp2p-swarm, introducing the new callbacks `inject_connection_established` and `inject_connection_closed`. The `endpoint` parameter from `inject_connected` and `inject_disconnected` is removed, since the first connection to open may not be the last connection to close, i.e. it cannot be guaranteed, as was previously the case, that the endpoints passed to these callbacks match up. * Have identify track all addresses. So that identify requests can be answered with the correct observed address of the connection on which the request arrives. * Cleanup * Cleanup * Improve the `Peer` state API. * Remove connection ID from `SwarmEvent::Dialing`. * Mark `DialPeerCondition` non-exhaustive. * Re-encapsulate `NetworkConfig`. To retain the possibility of not re-exposing all network configuration choices, thereby providing a more convenient API on the \`SwarmBuilder\`. * Rework Swarm::dial API. * Update CHANGELOG. * Doc formatting tweaks.
This commit is contained in:
@@ -114,6 +114,8 @@ where
|
||||
///
|
||||
/// The connection is closed as a result of the error.
|
||||
ConnectionError {
|
||||
/// The ID of the connection that encountered an error.
|
||||
id: ConnectionId,
|
||||
/// Information about the connection that encountered the error.
|
||||
connected: Connected<TConnInfo>,
|
||||
/// The error that occurred.
|
||||
|
@@ -174,28 +174,66 @@ where
|
||||
TConnInfo: fmt::Debug + ConnectionInfo<PeerId = TPeerId> + Send + 'static,
|
||||
TPeerId: Eq + Hash + Clone + Send + 'static,
|
||||
{
|
||||
/// Checks whether the peer is currently connected.
|
||||
///
|
||||
/// Returns `true` iff [`Peer::into_connected`] returns `Some`.
|
||||
pub fn is_connected(&self) -> bool {
|
||||
match self {
|
||||
Peer::Connected(..) => true,
|
||||
Peer::Dialing(peer) => peer.is_connected(),
|
||||
Peer::Disconnected(..) => false,
|
||||
Peer::Local => false
|
||||
}
|
||||
}
|
||||
|
||||
/// If we are connected, returns the `ConnectedPeer`.
|
||||
/// Checks whether the peer is currently being dialed.
|
||||
///
|
||||
/// Returns `true` iff [`Peer::into_dialing`] returns `Some`.
|
||||
pub fn is_dialing(&self) -> bool {
|
||||
match self {
|
||||
Peer::Dialing(_) => true,
|
||||
Peer::Connected(peer) => peer.is_dialing(),
|
||||
Peer::Disconnected(..) => false,
|
||||
Peer::Local => false
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether the peer is currently disconnected.
|
||||
///
|
||||
/// Returns `true` iff [`Peer::into_disconnected`] returns `Some`.
|
||||
pub fn is_disconnected(&self) -> bool {
|
||||
match self {
|
||||
Peer::Disconnected(..) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the peer into a `ConnectedPeer`, if there an established connection exists.
|
||||
pub fn into_connected(self) -> Option<
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
> {
|
||||
match self {
|
||||
Peer::Connected(peer) => Some(peer),
|
||||
_ => None,
|
||||
Peer::Dialing(peer) => peer.into_connected(),
|
||||
Peer::Disconnected(..) => None,
|
||||
Peer::Local => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// If a connection is pending, returns the `DialingPeer`.
|
||||
/// Converts the peer into a `DialingPeer`, if a dialing attempt exists.
|
||||
pub fn into_dialing(self) -> Option<
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
> {
|
||||
match self {
|
||||
Peer::Dialing(peer) => Some(peer),
|
||||
_ => None,
|
||||
Peer::Connected(peer) => peer.into_dialing(),
|
||||
Peer::Disconnected(..) => None,
|
||||
Peer::Local => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If we are not connected, returns the `DisconnectedPeer`.
|
||||
/// Converts the peer into a `DisconnectedPeer`, if neither an established connection
|
||||
/// nor a dialing attempt exists.
|
||||
pub fn into_disconnected(self) -> Option<
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
> {
|
||||
@@ -225,6 +263,10 @@ where
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: Eq + Hash + Clone,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Attempts to establish a new connection to this peer using the given addresses,
|
||||
/// if there is currently no ongoing dialing attempt.
|
||||
///
|
||||
@@ -294,7 +336,7 @@ where
|
||||
self.network.dialing.contains_key(&self.peer_id)
|
||||
}
|
||||
|
||||
/// Turns this peer into a [`DialingPeer`], if there is an ongoing
|
||||
/// Converts this peer into a [`DialingPeer`], if there is an ongoing
|
||||
/// dialing attempt, `None` otherwise.
|
||||
pub fn into_dialing(self) -> Option<
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
@@ -373,12 +415,34 @@ where
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: Eq + Hash + Clone,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Disconnects from this peer, closing all pending connections.
|
||||
pub fn disconnect(self) -> DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> {
|
||||
self.network.disconnect(&self.peer_id);
|
||||
DisconnectedPeer { network: self.network, peer_id: self.peer_id }
|
||||
}
|
||||
|
||||
/// Checks whether there is an established connection to the peer.
|
||||
///
|
||||
/// Returns `true` iff [`DialingPeer::into_connected`] returns `Some`.
|
||||
pub fn is_connected(&self) -> bool {
|
||||
self.network.pool.is_connected(&self.peer_id)
|
||||
}
|
||||
|
||||
/// Converts the peer into a `ConnectedPeer`, if an established connection exists.
|
||||
pub fn into_connected(self)
|
||||
-> Option<ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>>
|
||||
{
|
||||
if self.is_connected() {
|
||||
Some(ConnectedPeer { peer_id: self.peer_id, network: self.network })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Obtains the connection that is currently being established.
|
||||
pub fn connection<'b>(&'b mut self) -> DialingConnection<'b, TInEvent, TConnInfo, TPeerId> {
|
||||
let attempt = match self.network.dialing.entry(self.peer_id.clone()) {
|
||||
@@ -452,6 +516,10 @@ where
|
||||
TInEvent: Send + 'static,
|
||||
TOutEvent: Send + 'static,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Attempts to connect to this peer using the given addresses.
|
||||
pub fn connect<TIter>(self, first: Multiaddr, rest: TIter, handler: THandler)
|
||||
-> Result<DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
|
Reference in New Issue
Block a user