From fe0135fc2314913250590be164c0ff1f1d970255 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Sat, 30 Dec 2023 20:47:49 +0800 Subject: [PATCH] feat(viz): implement Accept for tls --- viz/src/tls/native_tls.rs | 19 +++++++++++++++---- viz/src/tls/rustls.rs | 13 +++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/viz/src/tls/native_tls.rs b/viz/src/tls/native_tls.rs index 73b4d796..6c335d3a 100644 --- a/viz/src/tls/native_tls.rs +++ b/viz/src/tls/native_tls.rs @@ -1,4 +1,8 @@ -use std::{fmt, net::SocketAddr}; +use std::{ + fmt, + io::{Error as IoError, ErrorKind}, + net::SocketAddr, +}; use tokio::net::{TcpListener, TcpStream}; use tokio_native_tls::{native_tls::TlsAcceptor as TlsAcceptorWrapper, TlsStream}; @@ -38,15 +42,22 @@ impl Config { } } -impl Listener { +impl crate::Accept for Listener { + type Conn = TlsStream; + type Addr = SocketAddr; + /// A [`TlsStream`] and [`SocketAddr`] part for accepting TLS. /// /// # Errors /// /// Will return `Err` if accepting the stream fails. - pub async fn accept(&self) -> Result<(TlsStream, SocketAddr)> { + async fn accept(&self) -> std::io::Result<(Self::Conn, Self::Addr)> { let (stream, addr) = self.inner.accept().await?; - let tls_stream = self.acceptor.accept(stream).await.map_err(Error::boxed)?; + let tls_stream = self + .acceptor + .accept(stream) + .await + .map_err(|e| IoError::new(ErrorKind::Other, e))?; Ok((tls_stream, addr)) } } diff --git a/viz/src/tls/rustls.rs b/viz/src/tls/rustls.rs index 7540e7c5..168d2461 100644 --- a/viz/src/tls/rustls.rs +++ b/viz/src/tls/rustls.rs @@ -149,15 +149,16 @@ impl Config { } } -impl Listener { - /// Accepts a new incoming connection from this listener. - /// - /// Returns a [`TlsStream`] and [`SocketAddr`] part. +impl crate::Accept for Listener { + type Conn = TlsStream; + type Addr = SocketAddr; + + /// A [`TlsStream`] and [`SocketAddr`] part for accepting TLS. /// /// # Errors /// - /// This function throws if it is not accepted from the listener. - pub async fn accept(&self) -> Result<(TlsStream, SocketAddr)> { + /// Will return `Err` if accepting the stream fails. + async fn accept(&self) -> std::io::Result<(Self::Conn, Self::Addr)> { let (stream, addr) = self.inner.accept().await?; let tls_stream = self.acceptor.accept(stream).await?; Ok((tls_stream, addr))