From df6aad2be4c6c5d11483f20e62d41e71f0ac989e Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 13 Mar 2024 12:33:34 +0100 Subject: net: major cleanup and improvement of the crate api --- net/src/transports/tcp.rs | 4 ++-- net/src/transports/tls.rs | 31 +++++++++---------------------- net/src/transports/udp.rs | 4 ++-- net/src/transports/unix.rs | 4 ++-- 4 files changed, 15 insertions(+), 28 deletions(-) (limited to 'net/src/transports') diff --git a/net/src/transports/tcp.rs b/net/src/transports/tcp.rs index af50c10..21fce3d 100644 --- a/net/src/transports/tcp.rs +++ b/net/src/transports/tcp.rs @@ -71,7 +71,7 @@ impl ConnListener for TcpListener { } /// Connects to the given TCP address and port. -pub async fn dial_tcp(endpoint: &Endpoint) -> Result { +pub async fn dial(endpoint: &Endpoint) -> Result { let addr = SocketAddr::try_from(endpoint.clone())?; let conn = TcpStream::connect(addr).await?; conn.set_nodelay(true)?; @@ -79,7 +79,7 @@ pub async fn dial_tcp(endpoint: &Endpoint) -> Result { } /// Listens on the given TCP address and port. -pub async fn listen_tcp(endpoint: &Endpoint) -> Result { +pub async fn listen(endpoint: &Endpoint) -> Result { let addr = SocketAddr::try_from(endpoint.clone())?; let listener = TcpListener::bind(addr).await?; Ok(listener) diff --git a/net/src/transports/tls.rs b/net/src/transports/tls.rs index 53b4566..476f495 100644 --- a/net/src/transports/tls.rs +++ b/net/src/transports/tls.rs @@ -59,7 +59,7 @@ impl Connection for TlsConn { } /// Connects to the given TLS address and port. -pub async fn dial_tls( +pub async fn dial( endpoint: &Endpoint, config: rustls::ClientConfig, dns_name: &'static str, @@ -76,36 +76,20 @@ pub async fn dial_tls( Ok(TlsConn::new(sock, TlsStream::Client(conn))) } -/// Connects to the given TLS endpoint, returns `Conn` ([`Connection`]). -pub async fn dial( - endpoint: &Endpoint, - config: rustls::ClientConfig, - dns_name: &'static str, -) -> Result> { - match endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) => {} - _ => return Err(Error::InvalidEndpoint(endpoint.to_string())), - } - - dial_tls(endpoint, config, dns_name) - .await - .map(|c| Box::new(c) as Box) -} - /// Tls network listener implementation of the `Listener` [`ConnListener`] trait. pub struct TlsListener { + inner: TcpListener, acceptor: TlsAcceptor, - listener: TcpListener, } #[async_trait] impl ConnListener for TlsListener { fn local_endpoint(&self) -> Result { - Ok(Endpoint::new_tls_addr(&self.listener.local_addr()?)) + Ok(Endpoint::new_tls_addr(&self.inner.local_addr()?)) } async fn accept(&self) -> Result> { - let (sock, _) = self.listener.accept().await?; + let (sock, _) = self.inner.accept().await?; sock.set_nodelay(true)?; let conn = self.acceptor.accept(sock.clone()).await?; Ok(Box::new(TlsConn::new(sock, TlsStream::Server(conn)))) @@ -113,11 +97,14 @@ impl ConnListener for TlsListener { } /// Listens on the given TLS address and port. -pub async fn listen_tls(endpoint: &Endpoint, config: rustls::ServerConfig) -> Result { +pub async fn listen(endpoint: &Endpoint, config: rustls::ServerConfig) -> Result { let addr = SocketAddr::try_from(endpoint.clone())?; let acceptor = TlsAcceptor::from(Arc::new(config)); let listener = TcpListener::bind(addr).await?; - Ok(TlsListener { acceptor, listener }) + Ok(TlsListener { + acceptor, + inner: listener, + }) } impl From> for Box { diff --git a/net/src/transports/udp.rs b/net/src/transports/udp.rs index 991b1fd..bd1fe83 100644 --- a/net/src/transports/udp.rs +++ b/net/src/transports/udp.rs @@ -57,7 +57,7 @@ impl Connection for UdpConn { } /// Connects to the given UDP address and port. -pub async fn dial_udp(endpoint: &Endpoint) -> Result { +pub async fn dial(endpoint: &Endpoint) -> Result { let addr = SocketAddr::try_from(endpoint.clone())?; // Let the operating system assign an available port to this socket @@ -67,7 +67,7 @@ pub async fn dial_udp(endpoint: &Endpoint) -> Result { } /// Listens on the given UDP address and port. -pub async fn listen_udp(endpoint: &Endpoint) -> Result { +pub async fn listen(endpoint: &Endpoint) -> Result { let addr = SocketAddr::try_from(endpoint.clone())?; let conn = UdpSocket::bind(addr).await?; let udp_conn = UdpConn::new(conn); diff --git a/net/src/transports/unix.rs b/net/src/transports/unix.rs index 3867040..494e104 100644 --- a/net/src/transports/unix.rs +++ b/net/src/transports/unix.rs @@ -69,13 +69,13 @@ impl ConnListener for UnixListener { } /// Connects to the given Unix socket path. -pub async fn dial_unix(path: &String) -> Result { +pub async fn dial(path: &String) -> Result { let conn = UnixStream::connect(path).await?; Ok(UnixConn::new(conn)) } /// Listens on the given Unix socket path. -pub fn listen_unix(path: &String) -> Result { +pub fn listen(path: &String) -> Result { let listener = UnixListener::bind(path)?; Ok(listener) } -- cgit v1.2.3