use crate::multiaddr::{Multiaddr, Protocol};
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConnectionId(usize);
impl ConnectionId {
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)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ListenerId(u64);
impl ListenerId {
pub fn new(id: u64) -> Self {
Self(id)
}
}
impl std::ops::Add<u64> for ListenerId {
type Output = Self;
fn add(self, other: u64) -> Self {
Self(self.0 + other)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Endpoint {
Dialer,
Listener,
}
impl std::ops::Not for Endpoint {
type Output = Endpoint;
fn not(self) -> Self::Output {
match self {
Endpoint::Dialer => Endpoint::Listener,
Endpoint::Listener => Endpoint::Dialer,
}
}
}
impl Endpoint {
pub fn is_dialer(self) -> bool {
matches!(self, Endpoint::Dialer)
}
pub fn is_listener(self) -> bool {
matches!(self, Endpoint::Listener)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PendingPoint {
Dialer {
role_override: Endpoint,
},
Listener {
local_addr: Multiaddr,
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,
},
}
}
}
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub enum ConnectedPoint {
Dialer {
address: Multiaddr,
role_override: Endpoint,
},
Listener {
local_addr: Multiaddr,
send_back_addr: Multiaddr,
},
}
impl From<&'_ ConnectedPoint> for Endpoint {
fn from(endpoint: &'_ ConnectedPoint) -> Endpoint {
endpoint.to_endpoint()
}
}
impl From<ConnectedPoint> for Endpoint {
fn from(endpoint: ConnectedPoint) -> Endpoint {
endpoint.to_endpoint()
}
}
impl ConnectedPoint {
pub fn to_endpoint(&self) -> Endpoint {
match self {
ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
ConnectedPoint::Listener { .. } => Endpoint::Listener,
}
}
pub fn is_dialer(&self) -> bool {
match self {
ConnectedPoint::Dialer { .. } => true,
ConnectedPoint::Listener { .. } => false,
}
}
pub fn is_listener(&self) -> bool {
match self {
ConnectedPoint::Dialer { .. } => false,
ConnectedPoint::Listener { .. } => true,
}
}
pub fn is_relayed(&self) -> bool {
match self {
ConnectedPoint::Dialer {
address,
role_override: _,
} => address,
ConnectedPoint::Listener { local_addr, .. } => local_addr,
}
.iter()
.any(|p| p == Protocol::P2pCircuit)
}
pub fn get_remote_address(&self) -> &Multiaddr {
match self {
ConnectedPoint::Dialer { address, .. } => address,
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr,
}
}
pub fn set_remote_address(&mut self, new_address: Multiaddr) {
match self {
ConnectedPoint::Dialer { address, .. } => *address = new_address,
ConnectedPoint::Listener { send_back_addr, .. } => *send_back_addr = new_address,
}
}
}