use crate::{
multiaddr::{Multiaddr, Protocol},
transport::PortUse,
};
#[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(PartialEq, Eq, Debug, Clone, Hash)]
pub enum ConnectedPoint {
Dialer {
address: Multiaddr,
role_override: Endpoint,
port_use: PortUse,
},
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 {
matches!(self, ConnectedPoint::Dialer { .. })
}
pub fn is_listener(&self) -> bool {
matches!(self, ConnectedPoint::Listener { .. })
}
pub fn is_relayed(&self) -> bool {
match self {
ConnectedPoint::Dialer { address, .. } => 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,
}
}
}