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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[cfg(target_os = "linux")]
mod io;
#[cfg(target_os = "linux")]
mod signals;

#[cfg(target_os = "linux")]
pub use self::signals::{signal, Signal};

#[cfg(target_os = "linux")]
mod net_impl {
    use std::os::unix::io::{FromRawFd, IntoRawFd};
    use std::{cell::RefCell, io::Result, net, net::SocketAddr, rc::Rc};

    use ntex_bytes::PoolRef;
    use ntex_io::Io;

    #[derive(Clone)]
    pub(crate) struct TcpStream(pub(crate) Rc<RefCell<glommio::net::TcpStream>>);

    impl TcpStream {
        fn new(io: glommio::net::TcpStream) -> Self {
            Self(Rc::new(RefCell::new(io)))
        }
    }

    #[derive(Clone)]
    pub(crate) struct UnixStream(pub(crate) Rc<RefCell<glommio::net::UnixStream>>);

    impl UnixStream {
        fn new(io: glommio::net::UnixStream) -> Self {
            Self(Rc::new(RefCell::new(io)))
        }
    }

    /// Opens a TCP connection to a remote host.
    pub async fn tcp_connect(addr: SocketAddr) -> Result<Io> {
        let sock = glommio::net::TcpStream::connect(addr).await?;
        sock.set_nodelay(true)?;
        Ok(Io::new(TcpStream::new(sock)))
    }

    /// Opens a TCP connection to a remote host and use specified memory pool.
    pub async fn tcp_connect_in(addr: SocketAddr, pool: PoolRef) -> Result<Io> {
        let sock = glommio::net::TcpStream::connect(addr).await?;
        sock.set_nodelay(true)?;
        Ok(Io::with_memory_pool(TcpStream::new(sock), pool))
    }

    /// Opens a unix stream connection.
    pub async fn unix_connect<P>(addr: P) -> Result<Io>
    where
        P: AsRef<std::path::Path>,
    {
        let sock = glommio::net::UnixStream::connect(addr).await?;
        Ok(Io::new(UnixStream::new(sock)))
    }

    /// Opens a unix stream connection and specified memory pool.
    pub async fn unix_connect_in<P>(addr: P, pool: PoolRef) -> Result<Io>
    where
        P: AsRef<std::path::Path>,
    {
        let sock = glommio::net::UnixStream::connect(addr).await?;
        Ok(Io::with_memory_pool(UnixStream::new(sock), pool))
    }

    /// Convert std TcpStream to glommio's TcpStream
    pub fn from_tcp_stream(stream: net::TcpStream) -> Result<Io> {
        stream.set_nonblocking(true)?;
        stream.set_nodelay(true)?;
        unsafe {
            Ok(Io::new(TcpStream::new(
                glommio::net::TcpStream::from_raw_fd(stream.into_raw_fd()),
            )))
        }
    }

    /// Convert std UnixStream to glommio's UnixStream
    pub fn from_unix_stream(stream: std::os::unix::net::UnixStream) -> Result<Io> {
        stream.set_nonblocking(true)?;
        unsafe {
            Ok(Io::new(UnixStream::new(
                glommio::net::UnixStream::from_raw_fd(stream.into_raw_fd()),
            )))
        }
    }
}

#[cfg(target_os = "linux")]
pub use self::net_impl::*;