ntex_glommio/
lib.rs

1#[cfg(target_os = "linux")]
2mod io;
3#[cfg(target_os = "linux")]
4mod signals;
5
6#[cfg(target_os = "linux")]
7pub use self::signals::{signal, Signal};
8
9#[cfg(target_os = "linux")]
10mod net_impl {
11    use std::os::unix::io::{FromRawFd, IntoRawFd};
12    use std::{cell::RefCell, io::Result, net, net::SocketAddr, rc::Rc};
13
14    use ntex_bytes::PoolRef;
15    use ntex_io::Io;
16
17    #[derive(Clone)]
18    pub(crate) struct TcpStream(pub(crate) Rc<RefCell<glommio::net::TcpStream>>);
19
20    impl TcpStream {
21        fn new(io: glommio::net::TcpStream) -> Self {
22            Self(Rc::new(RefCell::new(io)))
23        }
24    }
25
26    #[derive(Clone)]
27    pub(crate) struct UnixStream(pub(crate) Rc<RefCell<glommio::net::UnixStream>>);
28
29    impl UnixStream {
30        fn new(io: glommio::net::UnixStream) -> Self {
31            Self(Rc::new(RefCell::new(io)))
32        }
33    }
34
35    /// Opens a TCP connection to a remote host.
36    pub async fn tcp_connect(addr: SocketAddr) -> Result<Io> {
37        let sock = glommio::net::TcpStream::connect(addr).await?;
38        sock.set_nodelay(true)?;
39        Ok(Io::new(TcpStream::new(sock)))
40    }
41
42    /// Opens a TCP connection to a remote host and use specified memory pool.
43    pub async fn tcp_connect_in(addr: SocketAddr, pool: PoolRef) -> Result<Io> {
44        let sock = glommio::net::TcpStream::connect(addr).await?;
45        sock.set_nodelay(true)?;
46        Ok(Io::with_memory_pool(TcpStream::new(sock), pool))
47    }
48
49    /// Opens a unix stream connection.
50    pub async fn unix_connect<P>(addr: P) -> Result<Io>
51    where
52        P: AsRef<std::path::Path>,
53    {
54        let sock = glommio::net::UnixStream::connect(addr).await?;
55        Ok(Io::new(UnixStream::new(sock)))
56    }
57
58    /// Opens a unix stream connection and specified memory pool.
59    pub async fn unix_connect_in<P>(addr: P, pool: PoolRef) -> Result<Io>
60    where
61        P: AsRef<std::path::Path>,
62    {
63        let sock = glommio::net::UnixStream::connect(addr).await?;
64        Ok(Io::with_memory_pool(UnixStream::new(sock), pool))
65    }
66
67    /// Convert std TcpStream to glommio's TcpStream
68    pub fn from_tcp_stream(stream: net::TcpStream) -> Result<Io> {
69        stream.set_nonblocking(true)?;
70        stream.set_nodelay(true)?;
71        unsafe {
72            Ok(Io::new(TcpStream::new(
73                glommio::net::TcpStream::from_raw_fd(stream.into_raw_fd()),
74            )))
75        }
76    }
77
78    /// Convert std UnixStream to glommio's UnixStream
79    pub fn from_unix_stream(stream: std::os::unix::net::UnixStream) -> Result<Io> {
80        stream.set_nonblocking(true)?;
81        unsafe {
82            Ok(Io::new(UnixStream::new(
83                glommio::net::UnixStream::from_raw_fd(stream.into_raw_fd()),
84            )))
85        }
86    }
87}
88
89#[cfg(target_os = "linux")]
90pub use self::net_impl::*;