aboutsummaryrefslogtreecommitdiff
path: root/net/src/listener.rs
blob: f12f33ee962aa5932f0881cca974642f501fcabc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use async_trait::async_trait;

use crate::{
    transports::{tcp, unix},
    Conn, Endpoint, Error, Result,
};

/// Alias for `Box<dyn ConnListener>`
pub type Listener = Box<dyn ConnListener>;

/// A trait for objects which can be converted to [`Listener`].
pub trait ToListener {
    fn to_listener(self) -> Listener;
}

/// ConnListener is a generic network listener.
#[async_trait]
pub trait ConnListener: Send + Sync {
    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(addr, port) => Ok(Box::new(tcp::listen_tcp(addr, port).await?)),
        Endpoint::Unix(addr) => Ok(Box::new(unix::listen_unix(addr)?)),
        _ => Err(Error::InvalidEndpoint(endpoint.to_string())),
    }
}