diff options
author | hozan23 <hozan23@proton.me> | 2024-03-13 12:33:34 +0100 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2024-03-13 12:33:34 +0100 |
commit | df6aad2be4c6c5d11483f20e62d41e71f0ac989e (patch) | |
tree | bbbc8db330d254eab993f7e3e4bbdbcbb0b3ec6e /net | |
parent | 76e952830302271e07a4be9df6dfaa1c11e3e675 (diff) |
net: major cleanup and improvement of the crate api
Diffstat (limited to 'net')
-rw-r--r-- | net/src/connection.rs | 6 | ||||
-rw-r--r-- | net/src/lib.rs | 7 | ||||
-rw-r--r-- | net/src/listener.rs | 4 | ||||
-rw-r--r-- | net/src/transports/tcp.rs | 4 | ||||
-rw-r--r-- | net/src/transports/tls.rs | 31 | ||||
-rw-r--r-- | net/src/transports/udp.rs | 4 | ||||
-rw-r--r-- | net/src/transports/unix.rs | 4 |
7 files changed, 21 insertions, 39 deletions
diff --git a/net/src/connection.rs b/net/src/connection.rs index ea73a39..fa4640f 100644 --- a/net/src/connection.rs +++ b/net/src/connection.rs @@ -57,9 +57,9 @@ pub trait Connection: Send + Sync { /// pub async fn dial(endpoint: &Endpoint) -> Result<Conn> { match endpoint { - Endpoint::Tcp(_, _) => Ok(Box::new(tcp::dial_tcp(endpoint).await?)), - Endpoint::Udp(_, _) => Ok(Box::new(udp::dial_udp(endpoint).await?)), - Endpoint::Unix(addr) => Ok(Box::new(unix::dial_unix(addr).await?)), + Endpoint::Tcp(_, _) => Ok(Box::new(tcp::dial(endpoint).await?)), + Endpoint::Udp(_, _) => Ok(Box::new(udp::dial(endpoint).await?)), + Endpoint::Unix(addr) => Ok(Box::new(unix::dial(addr).await?)), _ => Err(Error::InvalidEndpoint(endpoint.to_string())), } } diff --git a/net/src/lib.rs b/net/src/lib.rs index 5b9bdd7..5f1c8a6 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -8,12 +8,7 @@ pub use { connection::{dial, Conn, Connection, ToConn}, endpoint::{Addr, Endpoint, Port}, listener::{listen, ConnListener, Listener, ToListener}, - transports::{ - tcp::{dial_tcp, listen_tcp, TcpConn}, - tls, - udp::{dial_udp, listen_udp, UdpConn}, - unix::{dial_unix, listen_unix, UnixConn}, - }, + transports::{tcp, tls, udp, unix}, }; use error::{Error, Result}; diff --git a/net/src/listener.rs b/net/src/listener.rs index 7f6709a..4511212 100644 --- a/net/src/listener.rs +++ b/net/src/listener.rs @@ -39,8 +39,8 @@ pub trait ConnListener: Send + Sync { /// ``` pub async fn listen(endpoint: &Endpoint) -> Result<Box<dyn ConnListener>> { match endpoint { - Endpoint::Tcp(_, _) => Ok(Box::new(tcp::listen_tcp(endpoint).await?)), - Endpoint::Unix(addr) => Ok(Box::new(unix::listen_unix(addr)?)), + Endpoint::Tcp(_, _) => Ok(Box::new(tcp::listen(endpoint).await?)), + Endpoint::Unix(addr) => Ok(Box::new(unix::listen(addr)?)), _ => Err(Error::InvalidEndpoint(endpoint.to_string())), } } 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<TcpConn> { +pub async fn dial(endpoint: &Endpoint) -> Result<TcpConn> { 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<TcpConn> { } /// Listens on the given TCP address and port. -pub async fn listen_tcp(endpoint: &Endpoint) -> Result<TcpListener> { +pub async fn listen(endpoint: &Endpoint) -> Result<TcpListener> { 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<Box<dyn Connection>> { - 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<dyn Connection>) -} - /// 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<Endpoint> { - Ok(Endpoint::new_tls_addr(&self.listener.local_addr()?)) + Ok(Endpoint::new_tls_addr(&self.inner.local_addr()?)) } async fn accept(&self) -> Result<Box<dyn Connection>> { - 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<TlsListener> { +pub async fn listen(endpoint: &Endpoint, config: rustls::ServerConfig) -> Result<TlsListener> { 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<TlsStream<TcpStream>> for Box<dyn Connection> { 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<UdpConn> { +pub async fn dial(endpoint: &Endpoint) -> Result<UdpConn> { 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<UdpConn> { } /// Listens on the given UDP address and port. -pub async fn listen_udp(endpoint: &Endpoint) -> Result<UdpConn> { +pub async fn listen(endpoint: &Endpoint) -> Result<UdpConn> { 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<UnixConn> { +pub async fn dial(path: &String) -> Result<UnixConn> { let conn = UnixStream::connect(path).await?; Ok(UnixConn::new(conn)) } /// Listens on the given Unix socket path. -pub fn listen_unix(path: &String) -> Result<UnixListener> { +pub fn listen(path: &String) -> Result<UnixListener> { let listener = UnixListener::bind(path)?; Ok(listener) } |