refactor!: Move ConnectionId and PendingPoint to libp2p-swarm (#3346)

Both of these are only needed as part of `libp2p-swarm`. Them residing in `libp2p-core` is a left-over from when `libp2p-core` still contained `Pool`.
This commit is contained in:
Thomas Eizinger
2023-01-18 19:56:32 +11:00
committed by GitHub
parent db2cd43826
commit 475dc80a07
34 changed files with 132 additions and 133 deletions

View File

@ -1,3 +1,10 @@
# 0.39.0 [unreleased]
- Move `ConnectionId` to `libp2p-swarm`. See [PR 3221].
- Move `PendingPoint` to `libp2p-swarm` and make it crate-private. See [PR 3221].
[PR 3221]: https://github.com/libp2p/rust-libp2p/pull/3221
# 0.38.0 # 0.38.0
- Remove deprecated functions `StreamMuxerExt::next_{inbound,outbound}`. See [PR 3031]. - Remove deprecated functions `StreamMuxerExt::next_{inbound,outbound}`. See [PR 3031].

View File

@ -20,29 +20,6 @@
use crate::multiaddr::{Multiaddr, Protocol}; use crate::multiaddr::{Multiaddr, Protocol};
/// Connection identifier.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionId(usize);
impl ConnectionId {
/// Creates a `ConnectionId` from a non-negative integer.
///
/// This is primarily useful for creating connection IDs
/// in test environments. There is in general no guarantee
/// that all connection IDs are based on non-negative integers.
pub fn new(id: usize) -> Self {
Self(id)
}
}
impl std::ops::Add<usize> for ConnectionId {
type Output = Self;
fn add(self, other: usize) -> Self {
Self(self.0 + other)
}
}
/// The endpoint roles associated with a peer-to-peer communication channel. /// The endpoint roles associated with a peer-to-peer communication channel.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Endpoint { pub enum Endpoint {
@ -75,42 +52,6 @@ impl Endpoint {
} }
} }
/// The endpoint roles associated with a pending peer-to-peer connection.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PendingPoint {
/// The socket comes from a dialer.
///
/// There is no single address associated with the Dialer of a pending
/// connection. Addresses are dialed in parallel. Only once the first dial
/// is successful is the address of the connection known.
Dialer {
/// Same as [`ConnectedPoint::Dialer`] `role_override`.
role_override: Endpoint,
},
/// The socket comes from a listener.
Listener {
/// Local connection address.
local_addr: Multiaddr,
/// Address used to send back data to the remote.
send_back_addr: Multiaddr,
},
}
impl From<ConnectedPoint> for PendingPoint {
fn from(endpoint: ConnectedPoint) -> Self {
match endpoint {
ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override },
ConnectedPoint::Listener {
local_addr,
send_back_addr,
} => PendingPoint::Listener {
local_addr,
send_back_addr,
},
}
}
}
/// The endpoint roles associated with an established peer-to-peer connection. /// The endpoint roles associated with an established peer-to-peer connection.
#[derive(PartialEq, Eq, Debug, Clone, Hash)] #[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub enum ConnectedPoint { pub enum ConnectedPoint {

View File

@ -28,9 +28,7 @@ use as_server::AsServer;
pub use as_server::{InboundProbeError, InboundProbeEvent}; pub use as_server::{InboundProbeError, InboundProbeEvent};
use futures_timer::Delay; use futures_timer::Delay;
use instant::Instant; use instant::Instant;
use libp2p_core::{ use libp2p_core::{multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId};
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId,
};
use libp2p_request_response::{ use libp2p_request_response::{
self as request_response, ProtocolSupport, RequestId, ResponseChannel, self as request_response, ProtocolSupport, RequestId, ResponseChannel,
}; };
@ -39,8 +37,8 @@ use libp2p_swarm::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm, ExpiredListenAddr, FromSwarm,
}, },
ConnectionHandler, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, ConnectionHandler, ConnectionId, ExternalAddresses, IntoConnectionHandler, ListenAddresses,
NetworkBehaviourAction, PollParameters, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
}; };
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},

View File

@ -27,10 +27,11 @@ use super::{
use futures::FutureExt; use futures::FutureExt;
use futures_timer::Delay; use futures_timer::Delay;
use instant::Instant; use instant::Instant;
use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId}; use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
use libp2p_swarm::{ use libp2p_swarm::{
AddressScore, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, PollParameters, AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviourAction,
PollParameters,
}; };
use rand::{seq::SliceRandom, thread_rng}; use rand::{seq::SliceRandom, thread_rng};
use std::{ use std::{

View File

@ -23,13 +23,13 @@ use super::{
ResponseError, ResponseError,
}; };
use instant::Instant; use instant::Instant;
use libp2p_core::{connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId}; use libp2p_core::{multiaddr::Protocol, Multiaddr, PeerId};
use libp2p_request_response::{ use libp2p_request_response::{
self as request_response, InboundFailure, RequestId, ResponseChannel, self as request_response, InboundFailure, RequestId, ResponseChannel,
}; };
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::{DialOpts, PeerCondition}, dial_opts::{DialOpts, PeerCondition},
DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionId, DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
}; };
use std::{ use std::{
collections::{HashMap, HashSet, VecDeque}, collections::{HashMap, HashSet, VecDeque},

View File

@ -22,11 +22,12 @@
use crate::handler; use crate::handler;
use either::Either; use either::Either;
use libp2p_core::connection::{ConnectedPoint, ConnectionId}; use libp2p_core::connection::ConnectedPoint;
use libp2p_core::multiaddr::Protocol; use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::dial_opts::{self, DialOpts}; use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler, ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,

View File

@ -20,12 +20,11 @@
use crate::protocol; use crate::protocol;
use either::Either; use either::Either;
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::{ConnectedPoint, PeerId}; use libp2p_core::{ConnectedPoint, PeerId};
use libp2p_swarm::dummy; use libp2p_swarm::dummy;
use libp2p_swarm::handler::SendWrapper; use libp2p_swarm::handler::SendWrapper;
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler}; use libp2p_swarm::{ConnectionHandler, ConnectionId, IntoConnectionHandler};
pub mod direct; pub mod direct;
pub mod relayed; pub mod relayed;

View File

@ -20,11 +20,10 @@
//! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection. //! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection.
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_swarm::handler::ConnectionEvent; use libp2p_swarm::handler::ConnectionEvent;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId, KeepAlive,
SubstreamProtocol, SubstreamProtocol,
}; };
use std::task::{Context, Poll}; use std::task::{Context, Poll};

View File

@ -26,11 +26,11 @@ use crate::topic::Topic;
use crate::FloodsubConfig; use crate::FloodsubConfig;
use cuckoofilter::{CuckooError, CuckooFilter}; use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet; use fnv::FnvHashSet;
use libp2p_core::{connection::ConnectionId, PeerId}; use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::DialOpts, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters, OneShotHandler, PollParameters,
}; };
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler}; use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler};
use log::warn; use log::warn;

View File

@ -36,14 +36,13 @@ use prost::Message;
use rand::{seq::SliceRandom, thread_rng}; use rand::{seq::SliceRandom, thread_rng};
use libp2p_core::{ use libp2p_core::{
connection::ConnectionId, identity::Keypair, multiaddr::Protocol::Ip4, identity::Keypair, multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Multiaddr, PeerId,
multiaddr::Protocol::Ip6, Multiaddr, PeerId,
}; };
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm}, behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm},
dial_opts::DialOpts, dial_opts::DialOpts,
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour,
NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters,
}; };
use wasm_timer::Instant; use wasm_timer::Instant;

View File

@ -21,7 +21,8 @@
//! A collection of types using the Gossipsub system. //! A collection of types using the Gossipsub system.
use crate::rpc_proto; use crate::rpc_proto;
use crate::TopicHash; use crate::TopicHash;
use libp2p_core::{connection::ConnectionId, PeerId}; use libp2p_core::PeerId;
use libp2p_swarm::ConnectionId;
use prometheus_client::encoding::EncodeLabelValue; use prometheus_client::encoding::EncodeLabelValue;
use prost::Message; use prost::Message;
use std::fmt; use std::fmt;

View File

@ -20,10 +20,9 @@
use crate::handler::{self, InEvent, Proto}; use crate::handler::{self, InEvent, Proto};
use crate::protocol::{Info, Protocol, UpgradeError}; use crate::protocol::{Info, Protocol, UpgradeError};
use libp2p_core::{ use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey};
connection::ConnectionId, multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey,
};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError, dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour,

View File

@ -22,11 +22,11 @@ use crate::structs_proto;
use asynchronous_codec::{FramedRead, FramedWrite}; use asynchronous_codec::{FramedRead, FramedWrite};
use futures::{future::BoxFuture, prelude::*}; use futures::{future::BoxFuture, prelude::*};
use libp2p_core::{ use libp2p_core::{
connection::ConnectionId,
identity, multiaddr, identity, multiaddr,
upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo},
Multiaddr, PublicKey, Multiaddr, PublicKey,
}; };
use libp2p_swarm::ConnectionId;
use log::{debug, trace}; use log::{debug, trace};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::{io, iter, pin::Pin}; use std::{io, iter, pin::Pin};

View File

@ -39,14 +39,14 @@ use crate::record::{
use crate::K_VALUE; use crate::K_VALUE;
use fnv::{FnvHashMap, FnvHashSet}; use fnv::{FnvHashMap, FnvHashSet};
use instant::Instant; use instant::Instant;
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId}; use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ use libp2p_swarm::behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm, AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm,
}; };
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::{self, DialOpts}, dial_opts::{self, DialOpts},
DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters,
}; };
use log::{debug, info, warn}; use log::{debug, info, warn};
use smallvec::SmallVec; use smallvec::SmallVec;

View File

@ -28,7 +28,7 @@ use crate::K_VALUE;
use futures::{executor::block_on, future::poll_fn, prelude::*}; use futures::{executor::block_on, future::poll_fn, prelude::*};
use futures_timer::Delay; use futures_timer::Delay;
use libp2p_core::{ use libp2p_core::{
connection::{ConnectedPoint, ConnectionId}, connection::ConnectedPoint,
identity, identity,
multiaddr::{multiaddr, Multiaddr, Protocol}, multiaddr::{multiaddr, Multiaddr, Protocol},
multihash::{Code, Multihash, MultihashDigest}, multihash::{Code, Multihash, MultihashDigest},
@ -36,7 +36,7 @@ use libp2p_core::{
upgrade, Endpoint, PeerId, Transport, upgrade, Endpoint, PeerId, Transport,
}; };
use libp2p_noise as noise; use libp2p_noise as noise;
use libp2p_swarm::{Swarm, SwarmEvent}; use libp2p_swarm::{ConnectionId, Swarm, SwarmEvent};
use libp2p_yamux as yamux; use libp2p_yamux as yamux;
use quickcheck::*; use quickcheck::*;
use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng}; use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng};

View File

@ -189,7 +189,7 @@ where
fn on_connection_handler_event( fn on_connection_handler_event(
&mut self, &mut self,
_: PeerId, _: PeerId,
_: libp2p_core::connection::ConnectionId, _: libp2p_swarm::ConnectionId,
ev: <Self::ConnectionHandler as ConnectionHandler>::OutEvent, ev: <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
) { ) {
void::unreachable(ev) void::unreachable(ev)

View File

@ -47,9 +47,9 @@ mod protocol;
use handler::Handler; use handler::Handler;
pub use handler::{Config, Failure, Success}; pub use handler::{Config, Failure, Success};
use libp2p_core::{connection::ConnectionId, PeerId}; use libp2p_core::PeerId;
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters, behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
}; };
use std::{ use std::{
collections::VecDeque, collections::VecDeque,

View File

@ -27,13 +27,12 @@ use crate::message_proto;
use crate::protocol::{inbound_hop, outbound_stop}; use crate::protocol::{inbound_hop, outbound_stop};
use either::Either; use either::Either;
use instant::Instant; use instant::Instant;
use libp2p_core::connection::ConnectionId;
use libp2p_core::multiaddr::Protocol; use libp2p_core::multiaddr::Protocol;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour,
NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters,
}; };
use std::collections::{hash_map, HashMap, HashSet, VecDeque}; use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32; use std::num::NonZeroU32;

View File

@ -30,14 +30,13 @@ use futures::io::AsyncWriteExt;
use futures::stream::{FuturesUnordered, StreamExt}; use futures::stream::{FuturesUnordered, StreamExt};
use futures_timer::Delay; use futures_timer::Delay;
use instant::Instant; use instant::Instant;
use libp2p_core::connection::ConnectionId;
use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::handler::{ use libp2p_swarm::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ListenUpgradeError, SendWrapper, ListenUpgradeError, SendWrapper,
}; };
use libp2p_swarm::{ use libp2p_swarm::{
dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, dummy, ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, ConnectionId,
IntoConnectionHandler, KeepAlive, NegotiatedSubstream, SubstreamProtocol, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, SubstreamProtocol,
}; };
use std::collections::VecDeque; use std::collections::VecDeque;

View File

@ -32,13 +32,12 @@ use futures::future::{BoxFuture, FutureExt};
use futures::io::{AsyncRead, AsyncWrite}; use futures::io::{AsyncRead, AsyncWrite};
use futures::ready; use futures::ready;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use libp2p_core::connection::ConnectionId;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::dial_opts::DialOpts; use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandlerUpgrErr, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction, ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour,
NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters,
}; };
use std::collections::{hash_map, HashMap, VecDeque}; use std::collections::{hash_map, HashMap, VecDeque};
use std::io::{Error, ErrorKind, IoSlice}; use std::io::{Error, ErrorKind, IoSlice};

View File

@ -28,14 +28,13 @@ use futures::future::FutureExt;
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use instant::Duration; use instant::Duration;
use libp2p_core::connection::ConnectionId;
use libp2p_core::identity::error::SigningError; use libp2p_core::identity::error::SigningError;
use libp2p_core::identity::Keypair; use libp2p_core::identity::Keypair;
use libp2p_core::{Multiaddr, PeerId, PeerRecord}; use libp2p_core::{Multiaddr, PeerId, PeerRecord};
use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{ use libp2p_swarm::{
CloseConnection, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
PollParameters, NotifyHandler, PollParameters,
}; };
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::iter::FromIterator; use std::iter::FromIterator;

View File

@ -27,11 +27,11 @@ use futures::future::BoxFuture;
use futures::ready; use futures::ready;
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use libp2p_core::connection::ConnectionId;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{ use libp2p_swarm::{
CloseConnection, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
}; };
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::iter::FromIterator; use std::iter::FromIterator;

View File

@ -70,11 +70,12 @@ pub use handler::ProtocolSupport;
use futures::channel::oneshot; use futures::channel::oneshot;
use handler::{Handler, RequestProtocol}; use handler::{Handler, RequestProtocol};
use libp2p_core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId}; use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
dial_opts::DialOpts, dial_opts::DialOpts,
IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, ConnectionId, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{

View File

@ -26,12 +26,11 @@ pub mod toggle;
pub use external_addresses::ExternalAddresses; pub use external_addresses::ExternalAddresses;
pub use listen_addresses::ListenAddresses; pub use listen_addresses::ListenAddresses;
use crate::connection::ConnectionId;
use crate::dial_opts::DialOpts; use crate::dial_opts::DialOpts;
use crate::handler::{ConnectionHandler, IntoConnectionHandler}; use crate::handler::{ConnectionHandler, IntoConnectionHandler};
use crate::{AddressRecord, AddressScore, DialError}; use crate::{AddressRecord, AddressScore, DialError};
use libp2p_core::{ use libp2p_core::{transport::ListenerId, ConnectedPoint, Multiaddr, PeerId};
connection::ConnectionId, transport::ListenerId, ConnectedPoint, Multiaddr, PeerId,
};
use std::{task::Context, task::Poll}; use std::{task::Context, task::Poll};
/// Custom event that can be received by the [`ConnectionHandler`]. /// Custom event that can be received by the [`ConnectionHandler`].
@ -259,14 +258,14 @@ pub enum NetworkBehaviourAction<
/// ```rust /// ```rust
/// # use futures::executor::block_on; /// # use futures::executor::block_on;
/// # use futures::stream::StreamExt; /// # use futures::stream::StreamExt;
/// # use libp2p_core::connection::ConnectionId;
/// # use libp2p_core::identity; /// # use libp2p_core::identity;
/// # use libp2p_core::transport::{MemoryTransport, Transport}; /// # use libp2p_core::transport::{MemoryTransport, Transport};
/// # use libp2p_core::upgrade::{self, DeniedUpgrade, InboundUpgrade, OutboundUpgrade}; /// # use libp2p_core::upgrade::{self, DeniedUpgrade, InboundUpgrade, OutboundUpgrade};
/// # use libp2p_core::PeerId; /// # use libp2p_core::PeerId;
/// # use libp2p_plaintext::PlainText2Config; /// # use libp2p_plaintext::PlainText2Config;
/// # use libp2p_swarm::{ /// # use libp2p_swarm::{
/// # FromSwarm, DialFailure, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, /// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream,
/// # FromSwarm, DialFailure,
/// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler, /// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler,
/// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent, /// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent,
/// # }; /// # };

View File

@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::connection::ConnectionId;
use crate::handler::either::IntoEitherHandler; use crate::handler::either::IntoEitherHandler;
use either::Either; use either::Either;
use libp2p_core::{Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
@ -69,7 +70,7 @@ where
fn on_connection_handler_event( fn on_connection_handler_event(
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: libp2p_core::connection::ConnectionId, connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>, event: crate::THandlerOutEvent<Self>,
) { ) {
match (self, event) { match (self, event) {

View File

@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use crate::behaviour::FromSwarm; use crate::behaviour::FromSwarm;
use crate::connection::ConnectionId;
use crate::handler::{ use crate::handler::{
AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, AddressChange, ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent,
ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, ConnectionHandlerUpgrErr, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
@ -93,7 +94,7 @@ where
fn on_connection_handler_event( fn on_connection_handler_event(
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: libp2p_core::connection::ConnectionId, connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>, event: crate::THandlerOutEvent<Self>,
) { ) {
if let Some(behaviour) = &mut self.inner { if let Some(behaviour) = &mut self.inner {

View File

@ -42,13 +42,36 @@ use libp2p_core::connection::ConnectedPoint;
use libp2p_core::multiaddr::Multiaddr; use libp2p_core::multiaddr::Multiaddr;
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerEvent, StreamMuxerExt, SubstreamBox}; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerEvent, StreamMuxerExt, SubstreamBox};
use libp2p_core::upgrade::{InboundUpgradeApply, OutboundUpgradeApply}; use libp2p_core::upgrade::{InboundUpgradeApply, OutboundUpgradeApply};
use libp2p_core::PeerId;
use libp2p_core::{upgrade, UpgradeError}; use libp2p_core::{upgrade, UpgradeError};
use libp2p_core::{Endpoint, PeerId};
use std::future::Future; use std::future::Future;
use std::task::Waker; use std::task::Waker;
use std::time::Duration; use std::time::Duration;
use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll}; use std::{fmt, io, mem, pin::Pin, task::Context, task::Poll};
/// Connection identifier.
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionId(usize);
impl ConnectionId {
/// Creates a `ConnectionId` from a non-negative integer.
///
/// This is primarily useful for creating connection IDs
/// in test environments. There is in general no guarantee
/// that all connection IDs are based on non-negative integers.
pub fn new(id: usize) -> Self {
Self(id)
}
}
impl std::ops::Add<usize> for ConnectionId {
type Output = Self;
fn add(self, other: usize) -> Self {
Self(self.0 + other)
}
}
/// Information about a successfully established connection. /// Information about a successfully established connection.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Connected { pub struct Connected {
@ -824,3 +847,39 @@ mod tests {
} }
} }
} }
/// The endpoint roles associated with a pending peer-to-peer connection.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum PendingPoint {
/// The socket comes from a dialer.
///
/// There is no single address associated with the Dialer of a pending
/// connection. Addresses are dialed in parallel. Only once the first dial
/// is successful is the address of the connection known.
Dialer {
/// Same as [`ConnectedPoint::Dialer`] `role_override`.
role_override: Endpoint,
},
/// The socket comes from a listener.
Listener {
/// Local connection address.
local_addr: Multiaddr,
/// Address used to send back data to the remote.
send_back_addr: Multiaddr,
},
}
impl From<ConnectedPoint> for PendingPoint {
fn from(endpoint: ConnectedPoint) -> Self {
match endpoint {
ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override },
ConnectedPoint::Listener {
local_addr,
send_back_addr,
} => PendingPoint::Listener {
local_addr,
send_back_addr,
},
}
}
}

View File

@ -19,7 +19,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use crate::connection::Connection; use crate::connection::{Connection, ConnectionId, PendingPoint};
use crate::{ use crate::{
behaviour::{THandlerInEvent, THandlerOutEvent}, behaviour::{THandlerInEvent, THandlerOutEvent},
connection::{ connection::{
@ -39,7 +39,7 @@ use futures::{
stream::FuturesUnordered, stream::FuturesUnordered,
}; };
use instant::Instant; use instant::Instant;
use libp2p_core::connection::{ConnectionId, Endpoint, PendingPoint}; use libp2p_core::connection::Endpoint;
use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt}; use libp2p_core::muxing::{StreamMuxerBox, StreamMuxerExt};
use std::{ use std::{
collections::{hash_map, HashMap}, collections::{hash_map, HashMap},

View File

@ -24,7 +24,8 @@
use super::concurrent_dial::ConcurrentDial; use super::concurrent_dial::ConcurrentDial;
use crate::{ use crate::{
connection::{ connection::{
self, ConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, self, ConnectionError, ConnectionId, PendingInboundConnectionError,
PendingOutboundConnectionError,
}, },
transport::TransportError, transport::TransportError,
ConnectionHandler, Multiaddr, PeerId, ConnectionHandler, Multiaddr, PeerId,
@ -34,7 +35,6 @@ use futures::{
future::{poll_fn, Either, Future}, future::{poll_fn, Either, Future},
SinkExt, StreamExt, SinkExt, StreamExt,
}; };
use libp2p_core::connection::ConnectionId;
use libp2p_core::muxing::StreamMuxerBox; use libp2p_core::muxing::StreamMuxerBox;
use std::pin::Pin; use std::pin::Pin;
use void::Void; use void::Void;

View File

@ -1,9 +1,9 @@
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::connection::ConnectionId;
use crate::handler::{ use crate::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
}; };
use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol}; use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol};
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_core::UpgradeError; use libp2p_core::UpgradeError;

View File

@ -1,9 +1,9 @@
use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::behaviour::{FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::connection::ConnectionId;
use crate::handler::{ use crate::handler::{
ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound, ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound,
KeepAlive, SubstreamProtocol, KeepAlive, SubstreamProtocol,
}; };
use libp2p_core::connection::ConnectionId;
use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use std::task::{Context, Poll}; use std::task::{Context, Poll};

View File

@ -84,6 +84,7 @@ pub mod derive_prelude {
pub use crate::behaviour::NewExternalAddr; pub use crate::behaviour::NewExternalAddr;
pub use crate::behaviour::NewListenAddr; pub use crate::behaviour::NewListenAddr;
pub use crate::behaviour::NewListener; pub use crate::behaviour::NewListener;
pub use crate::connection::ConnectionId;
pub use crate::ConnectionHandler; pub use crate::ConnectionHandler;
pub use crate::DialError; pub use crate::DialError;
pub use crate::IntoConnectionHandler; pub use crate::IntoConnectionHandler;
@ -92,7 +93,6 @@ pub mod derive_prelude {
pub use crate::NetworkBehaviourAction; pub use crate::NetworkBehaviourAction;
pub use crate::PollParameters; pub use crate::PollParameters;
pub use futures::prelude as futures; pub use futures::prelude as futures;
pub use libp2p_core::connection::ConnectionId;
pub use libp2p_core::either::EitherOutput; pub use libp2p_core::either::EitherOutput;
pub use libp2p_core::transport::ListenerId; pub use libp2p_core::transport::ListenerId;
pub use libp2p_core::ConnectedPoint; pub use libp2p_core::ConnectedPoint;
@ -108,8 +108,8 @@ pub use behaviour::{
}; };
pub use connection::pool::{ConnectionCounters, ConnectionLimits}; pub use connection::pool::{ConnectionCounters, ConnectionLimits};
pub use connection::{ pub use connection::{
ConnectionError, ConnectionLimit, PendingConnectionError, PendingInboundConnectionError, ConnectionError, ConnectionId, ConnectionLimit, PendingConnectionError,
PendingOutboundConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError,
}; };
pub use executor::Executor; pub use executor::Executor;
pub use handler::{ pub use handler::{
@ -125,7 +125,6 @@ use connection::pool::{EstablishedConnection, Pool, PoolConfig, PoolEvent};
use connection::IncomingInfo; use connection::IncomingInfo;
use dial_opts::{DialOpts, PeerCondition}; use dial_opts::{DialOpts, PeerCondition};
use futures::{executor::ThreadPoolBuilder, prelude::*, stream::FusedStream}; use futures::{executor::ThreadPoolBuilder, prelude::*, stream::FusedStream};
use libp2p_core::connection::ConnectionId;
use libp2p_core::muxing::SubstreamBox; use libp2p_core::muxing::SubstreamBox;
use libp2p_core::{ use libp2p_core::{
connection::ConnectedPoint, connection::ConnectedPoint,

View File

@ -23,12 +23,10 @@ use crate::behaviour::{
FromSwarm, ListenerClosed, ListenerError, NewExternalAddr, NewListenAddr, NewListener, FromSwarm, ListenerClosed, ListenerError, NewExternalAddr, NewListenAddr, NewListener,
}; };
use crate::{ use crate::{
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour,
PollParameters, NetworkBehaviourAction, PollParameters,
};
use libp2p_core::{
connection::ConnectionId, multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId,
}; };
use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId};
use std::collections::HashMap; use std::collections::HashMap;
use std::task::{Context, Poll}; use std::task::{Context, Poll};

View File

@ -368,10 +368,10 @@ fn generated_out_event_derive_debug() {
#[test] #[test]
fn custom_out_event_no_type_parameters() { fn custom_out_event_no_type_parameters() {
use libp2p_core::connection::ConnectionId;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandler, IntoConnectionHandler, NetworkBehaviourAction, PollParameters, ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviourAction,
PollParameters,
}; };
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;