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/src/transports | |
parent | 76e952830302271e07a4be9df6dfaa1c11e3e675 (diff) |
net: major cleanup and improvement of the crate api
Diffstat (limited to 'net/src/transports')
-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 |
4 files changed, 15 insertions, 28 deletions
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) } |