diff options
author | hozan23 <hozan23@proton.me> | 2024-03-13 03:35:09 +0100 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2024-03-13 03:35:09 +0100 |
commit | 76e952830302271e07a4be9df6dfaa1c11e3e675 (patch) | |
tree | 41202b50d07821caeee8fc2332bf9b5cc0f5f413 /net/src/endpoint.rs | |
parent | 5f403823fa3c2ef0c85dd81fe3601d084d0067e5 (diff) |
net: pass `Endpoint` to dial and listen functions
Diffstat (limited to 'net/src/endpoint.rs')
-rw-r--r-- | net/src/endpoint.rs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/net/src/endpoint.rs b/net/src/endpoint.rs index fdb2735..9193628 100644 --- a/net/src/endpoint.rs +++ b/net/src/endpoint.rs @@ -34,6 +34,7 @@ pub enum Endpoint { Udp(Addr, Port), Tcp(Addr, Port), Tls(Addr, Port), + Ws(Addr, Port), Unix(String), } @@ -49,6 +50,9 @@ impl std::fmt::Display for Endpoint { Endpoint::Tls(ip, port) => { write!(f, "tls://{}:{}", ip, port) } + Endpoint::Ws(ip, port) => { + write!(f, "ws://{}:{}", ip, port) + } Endpoint::Unix(path) => { if path.is_empty() { write!(f, "unix:/UNNAMED") @@ -64,9 +68,10 @@ impl TryFrom<Endpoint> for SocketAddr { type Error = Error; fn try_from(endpoint: Endpoint) -> std::result::Result<SocketAddr, Self::Error> { match endpoint { - Endpoint::Udp(ip, port) | Endpoint::Tcp(ip, port) | Endpoint::Tls(ip, port) => { - Ok(SocketAddr::new(ip.try_into()?, port)) - } + Endpoint::Udp(ip, port) + | Endpoint::Tcp(ip, port) + | Endpoint::Tls(ip, port) + | Endpoint::Ws(ip, port) => Ok(SocketAddr::new(ip.try_into()?, port)), Endpoint::Unix(_) => Err(Error::TryFromEndpoint), } } @@ -118,6 +123,7 @@ impl FromStr for Endpoint { "tcp" => Ok(Endpoint::Tcp(addr, port)), "udp" => Ok(Endpoint::Udp(addr, port)), "tls" => Ok(Endpoint::Tls(addr, port)), + "ws" => Ok(Endpoint::Ws(addr, port)), _ => Err(Error::InvalidEndpoint(s.to_string())), } } else { @@ -139,14 +145,19 @@ impl Endpoint { Endpoint::Tcp(Addr::Ip(addr.ip()), addr.port()) } + /// Creates a new UDP endpoint from a `SocketAddr`. + pub fn new_udp_addr(addr: &SocketAddr) -> Endpoint { + Endpoint::Udp(Addr::Ip(addr.ip()), addr.port()) + } + /// Creates a new TLS endpoint from a `SocketAddr`. pub fn new_tls_addr(addr: &SocketAddr) -> Endpoint { Endpoint::Tls(Addr::Ip(addr.ip()), addr.port()) } - /// Creates a new UDP endpoint from a `SocketAddr`. - pub fn new_udp_addr(addr: &SocketAddr) -> Endpoint { - Endpoint::Udp(Addr::Ip(addr.ip()), addr.port()) + /// Creates a new WS endpoint from a `SocketAddr`. + pub fn new_ws_addr(addr: &SocketAddr) -> Endpoint { + Endpoint::Ws(Addr::Ip(addr.ip()), addr.port()) } /// Creates a new Unix endpoint from a `UnixSocketAddress`. @@ -162,7 +173,10 @@ impl Endpoint { /// Returns the `Port` of the endpoint. pub fn port(&self) -> Result<&Port> { match self { - Endpoint::Udp(_, port) | Endpoint::Tcp(_, port) | Endpoint::Tls(_, port) => Ok(port), + Endpoint::Tcp(_, port) + | Endpoint::Udp(_, port) + | Endpoint::Tls(_, port) + | Endpoint::Ws(_, port) => Ok(port), _ => Err(Error::TryFromEndpoint), } } @@ -170,7 +184,10 @@ impl Endpoint { /// Returns the `Addr` of the endpoint. pub fn addr(&self) -> Result<&Addr> { match self { - Endpoint::Udp(addr, _) | Endpoint::Tcp(addr, _) | Endpoint::Tls(addr, _) => Ok(addr), + Endpoint::Tcp(addr, _) + | Endpoint::Udp(addr, _) + | Endpoint::Tls(addr, _) + | Endpoint::Ws(addr, _) => Ok(addr), _ => Err(Error::TryFromEndpoint), } } |