mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-08-01 01:11:58 +00:00
Remove ConnectionInfo trait (#1813)
In all cases, we pass the PeerId directly as the connection info. The flexbility of doing something different here was originally envisioned but turned out to be never needed. For reference see: https://github.com/libp2p/rust-libp2p/issues/1798#issuecomment-714526056 Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@@ -34,14 +34,15 @@ use crate::{
|
||||
PendingConnectionError,
|
||||
},
|
||||
transport::Transport,
|
||||
PeerId
|
||||
};
|
||||
use std::{fmt, num::NonZeroU32};
|
||||
|
||||
/// Event that can happen on the `Network`.
|
||||
pub enum NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
pub enum NetworkEvent<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
/// One of the listeners gracefully closed.
|
||||
ListenerClosed {
|
||||
@@ -104,7 +105,7 @@ where
|
||||
/// A new connection to a peer has been established.
|
||||
ConnectionEstablished {
|
||||
/// The newly established connection.
|
||||
connection: EstablishedConnection<'a, TInEvent, TConnInfo>,
|
||||
connection: EstablishedConnection<'a, TInEvent>,
|
||||
/// The total number of established connections to the same peer,
|
||||
/// including the one that has just been opened.
|
||||
num_established: NonZeroU32,
|
||||
@@ -126,7 +127,7 @@ where
|
||||
/// The ID of the connection that encountered an error.
|
||||
id: ConnectionId,
|
||||
/// Information about the connection that encountered the error.
|
||||
connected: Connected<TConnInfo>,
|
||||
connected: Connected,
|
||||
/// The error that occurred.
|
||||
error: Option<ConnectionError<<THandler::Handler as ConnectionHandler>::Error>>,
|
||||
/// The remaining number of established connections to the same peer.
|
||||
@@ -139,7 +140,7 @@ where
|
||||
attempts_remaining: u32,
|
||||
|
||||
/// Id of the peer we were trying to dial.
|
||||
peer_id: TPeerId,
|
||||
peer_id: PeerId,
|
||||
|
||||
/// The multiaddr we failed to reach.
|
||||
multiaddr: Multiaddr,
|
||||
@@ -160,7 +161,7 @@ where
|
||||
/// An established connection produced an event.
|
||||
ConnectionEvent {
|
||||
/// The connection on which the event occurred.
|
||||
connection: EstablishedConnection<'a, TInEvent, TConnInfo>,
|
||||
connection: EstablishedConnection<'a, TInEvent>,
|
||||
/// Event that was produced by the node.
|
||||
event: TOutEvent,
|
||||
},
|
||||
@@ -168,7 +169,7 @@ where
|
||||
/// An established connection has changed its address.
|
||||
AddressChange {
|
||||
/// The connection whose address has changed.
|
||||
connection: EstablishedConnection<'a, TInEvent, TConnInfo>,
|
||||
connection: EstablishedConnection<'a, TInEvent>,
|
||||
/// New endpoint of this connection.
|
||||
new_endpoint: ConnectedPoint,
|
||||
/// Old endpoint of this connection.
|
||||
@@ -176,17 +177,15 @@ where
|
||||
},
|
||||
}
|
||||
|
||||
impl<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> fmt::Debug for
|
||||
NetworkEvent<'_, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<TTrans, TInEvent, TOutEvent, THandler> fmt::Debug for
|
||||
NetworkEvent<'_, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TInEvent: fmt::Debug,
|
||||
TOutEvent: fmt::Debug,
|
||||
TTrans: Transport,
|
||||
TTrans::Error: fmt::Debug,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
<THandler::Handler as ConnectionHandler>::Error: fmt::Debug,
|
||||
TConnInfo: fmt::Debug,
|
||||
TPeerId: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
|
@@ -26,7 +26,6 @@ use crate::{
|
||||
Connected,
|
||||
ConnectedPoint,
|
||||
ConnectionHandler,
|
||||
ConnectionInfo,
|
||||
Connection,
|
||||
ConnectionId,
|
||||
ConnectionLimit,
|
||||
@@ -37,6 +36,7 @@ use crate::{
|
||||
Substream,
|
||||
pool::Pool,
|
||||
},
|
||||
PeerId
|
||||
};
|
||||
use fnv::FnvHashMap;
|
||||
use smallvec::SmallVec;
|
||||
@@ -44,7 +44,6 @@ use std::{
|
||||
collections::hash_map,
|
||||
error,
|
||||
fmt,
|
||||
hash::Hash,
|
||||
};
|
||||
use super::{Network, DialingOpts};
|
||||
|
||||
@@ -54,35 +53,33 @@ use super::{Network, DialingOpts};
|
||||
/// > **Note**: In any state there may always be a pending incoming
|
||||
/// > connection attempt from the peer, however, the remote identity
|
||||
/// > of a peer is only known once a connection is fully established.
|
||||
pub enum Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
pub enum Peer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>
|
||||
THandler: IntoConnectionHandler
|
||||
{
|
||||
/// At least one established connection exists to the peer.
|
||||
Connected(ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>),
|
||||
Connected(ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>),
|
||||
|
||||
/// There is an ongoing dialing (i.e. outgoing connection) attempt
|
||||
/// to the peer. There may already be other established connections
|
||||
/// to the peer.
|
||||
Dialing(DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>),
|
||||
Dialing(DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>),
|
||||
|
||||
/// There exists no established connection to the peer and there is
|
||||
/// currently no ongoing dialing (i.e. outgoing connection) attempt
|
||||
/// in progress.
|
||||
Disconnected(DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>),
|
||||
Disconnected(DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>),
|
||||
|
||||
/// The peer represents the local node.
|
||||
Local,
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> fmt::Debug for
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler> fmt::Debug for
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TConnInfo: fmt::Debug + ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: fmt::Debug + Eq + Hash,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
@@ -109,17 +106,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TPeerId: Eq + Hash,
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
pub(super) fn new(
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId
|
||||
) -> Self {
|
||||
if peer_id == network.local_peer_id {
|
||||
return Peer::Local;
|
||||
@@ -138,43 +133,41 @@ where
|
||||
|
||||
|
||||
fn disconnected(
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId
|
||||
) -> Self {
|
||||
Peer::Disconnected(DisconnectedPeer { network, peer_id })
|
||||
}
|
||||
|
||||
fn connected(
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId
|
||||
) -> Self {
|
||||
Peer::Connected(ConnectedPeer { network, peer_id })
|
||||
}
|
||||
|
||||
fn dialing(
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId
|
||||
) -> Self {
|
||||
Peer::Dialing(DialingPeer { network, peer_id })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TMuxer, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TMuxer, TInEvent, TOutEvent, THandler>
|
||||
Peer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport<Output = (TConnInfo, TMuxer)> + Clone,
|
||||
TTrans: Transport<Output = (PeerId, TMuxer)> + Clone,
|
||||
TTrans::Error: Send + 'static,
|
||||
TTrans::Dial: Send + 'static,
|
||||
TMuxer: StreamMuxer + Send + Sync + 'static,
|
||||
TMuxer::OutboundSubstream: Send,
|
||||
TInEvent: Send + 'static,
|
||||
TOutEvent: Send + 'static,
|
||||
THandler: IntoConnectionHandler<TConnInfo> + Send + 'static,
|
||||
THandler: IntoConnectionHandler + Send + 'static,
|
||||
THandler::Handler: ConnectionHandler<Substream = Substream<TMuxer>, InEvent = TInEvent, OutEvent = TOutEvent> + Send,
|
||||
<THandler::Handler as ConnectionHandler>::OutboundOpenInfo: Send,
|
||||
<THandler::Handler as ConnectionHandler>::Error: error::Error + Send + 'static,
|
||||
TConnInfo: fmt::Debug + ConnectionInfo<PeerId = TPeerId> + Send + 'static,
|
||||
TPeerId: Eq + Hash + Clone + Send + 'static,
|
||||
{
|
||||
/// Checks whether the peer is currently connected.
|
||||
///
|
||||
@@ -219,7 +212,7 @@ where
|
||||
/// attempt to the first address fails.
|
||||
pub fn dial<I>(self, address: Multiaddr, remaining: I, handler: THandler)
|
||||
-> Result<
|
||||
(ConnectionId, DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>),
|
||||
(ConnectionId, DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>),
|
||||
ConnectionLimit
|
||||
>
|
||||
where
|
||||
@@ -246,7 +239,7 @@ where
|
||||
///
|
||||
/// Succeeds if the there is at least one established connection to the peer.
|
||||
pub fn into_connected(self) -> Option<
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
> {
|
||||
match self {
|
||||
Peer::Connected(peer) => Some(peer),
|
||||
@@ -260,7 +253,7 @@ where
|
||||
///
|
||||
/// Succeeds if the there is at least one pending outgoing connection to the peer.
|
||||
pub fn into_dialing(self) -> Option<
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
> {
|
||||
match self {
|
||||
Peer::Dialing(peer) => Some(peer),
|
||||
@@ -273,7 +266,7 @@ where
|
||||
/// 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>
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
> {
|
||||
match self {
|
||||
Peer::Disconnected(peer) => Some(peer),
|
||||
@@ -285,35 +278,33 @@ where
|
||||
/// The representation of a peer in a [`Network`] to whom at least
|
||||
/// one established connection exists. There may also be additional ongoing
|
||||
/// dialing attempts to the peer.
|
||||
pub struct ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
pub struct ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId,
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId,
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: Eq + Hash + Clone,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
pub fn id(&self) -> &PeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Returns the `ConnectedPeer` into a `Peer`.
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> {
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler> {
|
||||
Peer::Connected(self)
|
||||
}
|
||||
|
||||
/// Obtains an established connection to the peer by ID.
|
||||
pub fn connection<'b>(&'b mut self, id: ConnectionId)
|
||||
-> Option<EstablishedConnection<'b, TInEvent, TConnInfo>>
|
||||
-> Option<EstablishedConnection<'b, TInEvent>>
|
||||
{
|
||||
self.network.pool.get_established(id)
|
||||
}
|
||||
@@ -333,7 +324,7 @@ where
|
||||
/// 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>
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
> {
|
||||
if self.network.dialing.contains_key(&self.peer_id) {
|
||||
Some(DialingPeer { network: self.network, peer_id: self.peer_id })
|
||||
@@ -350,16 +341,14 @@ where
|
||||
TOutEvent,
|
||||
THandler,
|
||||
TTrans::Error,
|
||||
<THandler::Handler as ConnectionHandler>::Error,
|
||||
TConnInfo,
|
||||
TPeerId>
|
||||
<THandler::Handler as ConnectionHandler>::Error>
|
||||
{
|
||||
self.network.pool.iter_peer_established(&self.peer_id)
|
||||
}
|
||||
|
||||
/// Obtains some established connection to the peer.
|
||||
pub fn some_connection<'b>(&'b mut self)
|
||||
-> EstablishedConnection<'b, TInEvent, TConnInfo>
|
||||
-> EstablishedConnection<'b, TInEvent>
|
||||
{
|
||||
self.connections()
|
||||
.into_first()
|
||||
@@ -368,19 +357,18 @@ where
|
||||
|
||||
/// Disconnects from the peer, closing all connections.
|
||||
pub fn disconnect(self)
|
||||
-> DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
-> DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
{
|
||||
self.network.disconnect(&self.peer_id);
|
||||
DisconnectedPeer { network: self.network, peer_id: self.peer_id }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> fmt::Debug for
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler> fmt::Debug for
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TPeerId: Eq + Hash + fmt::Debug,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.debug_struct("ConnectedPeer")
|
||||
@@ -394,36 +382,34 @@ where
|
||||
/// The representation of a peer in a [`Network`] to whom a dialing
|
||||
/// attempt is ongoing. There may already exist other established
|
||||
/// connections to this peer.
|
||||
pub struct DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
pub struct DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: TPeerId,
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
peer_id: PeerId,
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: Eq + Hash + Clone,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
pub fn id(&self) -> &PeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Returns the `DialingPeer` into a `Peer`.
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> {
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler> {
|
||||
Peer::Dialing(self)
|
||||
}
|
||||
|
||||
/// Disconnects from this peer, closing all established connections and
|
||||
/// aborting all dialing attempts.
|
||||
pub fn disconnect(self)
|
||||
-> DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
-> DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
{
|
||||
self.network.disconnect(&self.peer_id);
|
||||
DisconnectedPeer { network: self.network, peer_id: self.peer_id }
|
||||
@@ -438,7 +424,7 @@ where
|
||||
|
||||
/// 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>>
|
||||
-> Option<ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>>
|
||||
{
|
||||
if self.is_connected() {
|
||||
Some(ConnectedPeer { peer_id: self.peer_id, network: self.network })
|
||||
@@ -450,7 +436,7 @@ where
|
||||
/// Obtains a dialing attempt to the peer by connection ID of
|
||||
/// the current connection attempt.
|
||||
pub fn attempt<'b>(&'b mut self, id: ConnectionId)
|
||||
-> Option<DialingAttempt<'b, TInEvent, TConnInfo, TPeerId>>
|
||||
-> Option<DialingAttempt<'b, TInEvent>>
|
||||
{
|
||||
if let hash_map::Entry::Occupied(attempts) = self.network.dialing.entry(self.peer_id.clone()) {
|
||||
if let Some(pos) = attempts.get().iter().position(|s| s.current.0 == id) {
|
||||
@@ -475,9 +461,7 @@ where
|
||||
TOutEvent,
|
||||
THandler,
|
||||
TTrans::Error,
|
||||
<THandler::Handler as ConnectionHandler>::Error,
|
||||
TConnInfo,
|
||||
TPeerId>
|
||||
<THandler::Handler as ConnectionHandler>::Error>
|
||||
{
|
||||
DialingAttemptIter::new(&self.peer_id, &mut self.network.pool, &mut self.network.dialing)
|
||||
}
|
||||
@@ -486,7 +470,7 @@ where
|
||||
///
|
||||
/// At least one dialing connection is guaranteed to exist on a `DialingPeer`.
|
||||
pub fn some_attempt<'b>(&'b mut self)
|
||||
-> DialingAttempt<'b, TInEvent, TConnInfo, TPeerId>
|
||||
-> DialingAttempt<'b, TInEvent>
|
||||
{
|
||||
self.attempts()
|
||||
.into_first()
|
||||
@@ -494,12 +478,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> fmt::Debug for
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler> fmt::Debug for
|
||||
DialingPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TPeerId: Eq + Hash + fmt::Debug,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.debug_struct("DialingPeer")
|
||||
@@ -513,21 +496,20 @@ where
|
||||
/// The representation of a peer to whom the `Network` has currently
|
||||
/// neither an established connection, nor an ongoing dialing attempt
|
||||
/// initiated by the local peer.
|
||||
pub struct DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
pub struct DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
peer_id: TPeerId,
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
peer_id: PeerId,
|
||||
network: &'a mut Network<TTrans, TInEvent, TOutEvent, THandler>,
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> fmt::Debug for
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler> fmt::Debug for
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
TPeerId: fmt::Debug,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.debug_struct("DisconnectedPeer")
|
||||
@@ -536,18 +518,18 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>
|
||||
impl<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
DisconnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>
|
||||
where
|
||||
TTrans: Transport,
|
||||
THandler: IntoConnectionHandler<TConnInfo>,
|
||||
THandler: IntoConnectionHandler,
|
||||
{
|
||||
pub fn id(&self) -> &TPeerId {
|
||||
pub fn id(&self) -> &PeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Returns the `DisconnectedPeer` into a `Peer`.
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId> {
|
||||
pub fn into_peer(self) -> Peer<'a, TTrans, TInEvent, TOutEvent, THandler> {
|
||||
Peer::Disconnected(self)
|
||||
}
|
||||
|
||||
@@ -558,13 +540,13 @@ where
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `connected.peer_id()` does not identify the current peer.
|
||||
/// Panics if `connected.peer_id` does not identify the current peer.
|
||||
pub fn set_connected<TMuxer>(
|
||||
self,
|
||||
connected: Connected<TConnInfo>,
|
||||
connected: Connected,
|
||||
connection: Connection<TMuxer, THandler::Handler>,
|
||||
) -> Result<
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler, TConnInfo, TPeerId>,
|
||||
ConnectedPeer<'a, TTrans, TInEvent, TOutEvent, THandler>,
|
||||
ConnectionLimit
|
||||
> where
|
||||
TInEvent: Send + 'static,
|
||||
@@ -574,13 +556,11 @@ where
|
||||
THandler::Handler: ConnectionHandler<Substream = Substream<TMuxer>, InEvent = TInEvent, OutEvent = TOutEvent> + Send,
|
||||
<THandler::Handler as ConnectionHandler>::OutboundOpenInfo: Send,
|
||||
<THandler::Handler as ConnectionHandler>::Error: error::Error + Send + 'static,
|
||||
TConnInfo: fmt::Debug + ConnectionInfo<PeerId = TPeerId> + Clone + Send + 'static,
|
||||
TPeerId: Eq + Hash + Clone + Send + fmt::Debug + 'static,
|
||||
TMuxer: StreamMuxer + Send + Sync + 'static,
|
||||
TMuxer::OutboundSubstream: Send,
|
||||
{
|
||||
if connected.peer_id() != &self.peer_id {
|
||||
panic!("Invalid peer ID given: {:?}. Expected: {:?}", connected.peer_id(), self.peer_id)
|
||||
if connected.peer_id != self.peer_id {
|
||||
panic!("Invalid peer ID given: {:?}. Expected: {:?}", connected.peer_id, self.peer_id)
|
||||
}
|
||||
|
||||
self.network.pool.add(connection, connected)
|
||||
@@ -604,17 +584,17 @@ pub(super) struct DialingState {
|
||||
/// A `DialingAttempt` is an ongoing outgoing connection attempt to
|
||||
/// a known / expected remote peer ID and a list of alternative addresses
|
||||
/// to connect to, if the current connection attempt fails.
|
||||
pub struct DialingAttempt<'a, TInEvent, TConnInfo, TPeerId> {
|
||||
pub struct DialingAttempt<'a, TInEvent> {
|
||||
/// The underlying pending connection in the `Pool`.
|
||||
inner: PendingConnection<'a, TInEvent, TConnInfo, TPeerId>,
|
||||
inner: PendingConnection<'a, TInEvent>,
|
||||
/// All current dialing attempts of the peer.
|
||||
attempts: hash_map::OccupiedEntry<'a, TPeerId, SmallVec<[DialingState; 10]>>,
|
||||
attempts: hash_map::OccupiedEntry<'a, PeerId, SmallVec<[DialingState; 10]>>,
|
||||
/// The position of the current `DialingState` of this connection in the `attempts`.
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'a, TInEvent, TConnInfo, TPeerId>
|
||||
DialingAttempt<'a, TInEvent, TConnInfo, TPeerId>
|
||||
impl<'a, TInEvent>
|
||||
DialingAttempt<'a, TInEvent>
|
||||
{
|
||||
/// Returns the ID of the current connection attempt.
|
||||
pub fn id(&self) -> ConnectionId {
|
||||
@@ -622,7 +602,7 @@ impl<'a, TInEvent, TConnInfo, TPeerId>
|
||||
}
|
||||
|
||||
/// Returns the (expected) peer ID of the dialing attempt.
|
||||
pub fn peer_id(&self) -> &TPeerId {
|
||||
pub fn peer_id(&self) -> &PeerId {
|
||||
self.attempts.key()
|
||||
}
|
||||
|
||||
@@ -658,17 +638,17 @@ impl<'a, TInEvent, TConnInfo, TPeerId>
|
||||
}
|
||||
|
||||
/// An iterator over the ongoing dialing attempts to a peer.
|
||||
pub struct DialingAttemptIter<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId> {
|
||||
pub struct DialingAttemptIter<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr> {
|
||||
/// The peer whose dialing attempts are being iterated.
|
||||
peer_id: &'a TPeerId,
|
||||
peer_id: &'a PeerId,
|
||||
/// The underlying connection `Pool` of the `Network`.
|
||||
pool: &'a mut Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId>,
|
||||
pool: &'a mut Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr>,
|
||||
/// The state of all current dialing attempts known to the `Network`.
|
||||
///
|
||||
/// Ownership of the `OccupiedEntry` for `peer_id` containing all attempts must be
|
||||
/// borrowed to each `DialingAttempt` in order for it to remove the entry if the
|
||||
/// last dialing attempt is aborted.
|
||||
dialing: &'a mut FnvHashMap<TPeerId, SmallVec<[DialingState; 10]>>,
|
||||
dialing: &'a mut FnvHashMap<PeerId, SmallVec<[DialingState; 10]>>,
|
||||
/// The current position of the iterator in `dialing[peer_id]`.
|
||||
pos: usize,
|
||||
/// The total number of elements in `dialing[peer_id]` to iterate over.
|
||||
@@ -678,23 +658,20 @@ pub struct DialingAttemptIter<'a, TInEvent, TOutEvent, THandler, TTransErr, THan
|
||||
// Note: Ideally this would be an implementation of `Iterator`, but that
|
||||
// requires GATs (cf. https://github.com/rust-lang/rust/issues/44265) and
|
||||
// a different definition of `Iterator`.
|
||||
impl<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId>
|
||||
DialingAttemptIter<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId>
|
||||
where
|
||||
TConnInfo: ConnectionInfo<PeerId = TPeerId>,
|
||||
TPeerId: Eq + Hash + Clone,
|
||||
impl<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr>
|
||||
DialingAttemptIter<'a, TInEvent, TOutEvent, THandler, TTransErr, THandlerErr>
|
||||
{
|
||||
fn new(
|
||||
peer_id: &'a TPeerId,
|
||||
pool: &'a mut Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr, TConnInfo, TPeerId>,
|
||||
dialing: &'a mut FnvHashMap<TPeerId, SmallVec<[DialingState; 10]>>,
|
||||
peer_id: &'a PeerId,
|
||||
pool: &'a mut Pool<TInEvent, TOutEvent, THandler, TTransErr, THandlerErr>,
|
||||
dialing: &'a mut FnvHashMap<PeerId, SmallVec<[DialingState; 10]>>,
|
||||
) -> Self {
|
||||
let end = dialing.get(peer_id).map_or(0, |conns| conns.len());
|
||||
Self { pos: 0, end, pool, dialing, peer_id }
|
||||
}
|
||||
|
||||
/// Obtains the next dialing connection, if any.
|
||||
pub fn next<'b>(&'b mut self) -> Option<DialingAttempt<'b, TInEvent, TConnInfo, TPeerId>> {
|
||||
pub fn next<'b>(&'b mut self) -> Option<DialingAttempt<'b, TInEvent>> {
|
||||
if self.pos == self.end {
|
||||
return None
|
||||
}
|
||||
@@ -713,7 +690,7 @@ where
|
||||
|
||||
/// Returns the first connection, if any, consuming the iterator.
|
||||
pub fn into_first<'b>(self)
|
||||
-> Option<DialingAttempt<'b, TInEvent, TConnInfo, TPeerId>>
|
||||
-> Option<DialingAttempt<'b, TInEvent>>
|
||||
where 'a: 'b
|
||||
{
|
||||
if self.pos == self.end {
|
||||
|
Reference in New Issue
Block a user