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
47
48
49
50
51
52
53
54
use std::{io, net};

#[cfg(feature = "quic")]
use xitca_io::net::QuicListenerBuilder;
#[cfg(unix)]
use xitca_io::net::UnixListener;
use xitca_io::net::{Listener, TcpListener};

use tracing::info;

/// Helper trait for convert listener types to tokio types.
/// This is to delay the conversion and make it happen in server thread(s).
/// Otherwise it could panic.
pub trait AsListener: Send {
    fn as_listener(&mut self) -> io::Result<Listener>;
}

impl AsListener for Option<net::TcpListener> {
    fn as_listener(&mut self) -> io::Result<Listener> {
        let this = self.take().unwrap();
        this.set_nonblocking(true)?;

        let tcp = TcpListener::from_std(this)?;

        info!("Started Tcp listening on: {:?}", tcp.local_addr().ok());

        Ok(Listener::Tcp(tcp))
    }
}

#[cfg(unix)]
impl AsListener for Option<std::os::unix::net::UnixListener> {
    fn as_listener(&mut self) -> io::Result<Listener> {
        let this = self.take().unwrap();
        this.set_nonblocking(true)?;

        let unix = UnixListener::from_std(this)?;

        info!("Started Unix listening on: {:?}", unix.local_addr().ok());

        Ok(Listener::Unix(unix))
    }
}

#[cfg(feature = "quic")]
impl AsListener for Option<QuicListenerBuilder> {
    fn as_listener(&mut self) -> io::Result<Listener> {
        let udp = self.take().unwrap().build()?;

        info!("Started Udp listening on: {:?}", udp.endpoint().local_addr().ok());

        Ok(Listener::Udp(udp))
    }
}