aboutsummaryrefslogtreecommitdiff
path: root/net/src/listener.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-04-11 10:19:20 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-19 13:51:30 +0200
commit0992071a7f1a36424bcfaf1fbc84541ea041df1a (patch)
tree961d73218af672797d49f899289bef295bc56493 /net/src/listener.rs
parenta69917ecd8272a4946cfd12c75bf8f8c075b0e50 (diff)
add support for tokio & improve net crate api
Diffstat (limited to 'net/src/listener.rs')
-rw-r--r--net/src/listener.rs41
1 files changed, 8 insertions, 33 deletions
diff --git a/net/src/listener.rs b/net/src/listener.rs
index 4511212..469f5e9 100644
--- a/net/src/listener.rs
+++ b/net/src/listener.rs
@@ -1,46 +1,21 @@
use async_trait::async_trait;
-use crate::{
- transports::{tcp, unix},
- Conn, Endpoint, Error, Result,
-};
+use crate::{Conn, Endpoint, Result};
/// Alias for `Box<dyn ConnListener>`
-pub type Listener = Box<dyn ConnListener>;
+pub type Listener<T> = Box<dyn ConnListener<Item = T>>;
/// A trait for objects which can be converted to [`Listener`].
pub trait ToListener {
- fn to_listener(self) -> Listener;
+ type Item;
+ fn to_listener(self) -> Listener<Self::Item>;
}
-/// ConnListener is a generic network listener.
+/// ConnListener is a generic network listener interface for
+/// [`tcp::TcpConn`], [`tls::TlsConn`], [`ws::WsConn`], and [`unix::UnixConn`].
#[async_trait]
pub trait ConnListener: Send + Sync {
+ type Item;
fn local_endpoint(&self) -> Result<Endpoint>;
- async fn accept(&self) -> Result<Conn>;
-}
-
-/// Listens to the provided endpoint.
-///
-/// it only supports `tcp4/6`, and `unix`.
-///
-/// #Example
-///
-/// ```
-/// use karyon_net::{Endpoint, listen};
-///
-/// async {
-/// let endpoint: Endpoint = "tcp://127.0.0.1:3000".parse().unwrap();
-///
-/// let listener = listen(&endpoint).await.unwrap();
-/// let conn = listener.accept().await.unwrap();
-/// };
-///
-/// ```
-pub async fn listen(endpoint: &Endpoint) -> Result<Box<dyn ConnListener>> {
- match endpoint {
- Endpoint::Tcp(_, _) => Ok(Box::new(tcp::listen(endpoint).await?)),
- Endpoint::Unix(addr) => Ok(Box::new(unix::listen(addr)?)),
- _ => Err(Error::InvalidEndpoint(endpoint.to_string())),
- }
+ async fn accept(&self) -> Result<Conn<Self::Item>>;
}