ntex_compio/
lib.rs

1use std::{io::Result, net, net::SocketAddr};
2
3use ntex_bytes::PoolRef;
4use ntex_io::Io;
5
6mod io;
7
8/// Tcp stream wrapper for compio TcpStream
9struct TcpStream(compio_net::TcpStream);
10
11#[cfg(unix)]
12/// Tcp stream wrapper for compio UnixStream
13struct UnixStream(compio_net::UnixStream);
14
15/// Opens a TCP connection to a remote host.
16pub async fn tcp_connect(addr: SocketAddr) -> Result<Io> {
17    let sock = compio_net::TcpStream::connect(addr).await?;
18    Ok(Io::new(TcpStream(sock)))
19}
20
21/// Opens a TCP connection to a remote host and use specified memory pool.
22pub async fn tcp_connect_in(addr: SocketAddr, pool: PoolRef) -> Result<Io> {
23    let sock = compio_net::TcpStream::connect(addr).await?;
24    Ok(Io::with_memory_pool(TcpStream(sock), pool))
25}
26
27#[cfg(unix)]
28/// Opens a unix stream connection.
29pub async fn unix_connect<'a, P>(addr: P) -> Result<Io>
30where
31    P: AsRef<std::path::Path> + 'a,
32{
33    let sock = compio_net::UnixStream::connect(addr).await?;
34    Ok(Io::new(UnixStream(sock)))
35}
36
37#[cfg(unix)]
38/// Opens a unix stream connection and specified memory pool.
39pub async fn unix_connect_in<'a, P>(addr: P, pool: PoolRef) -> Result<Io>
40where
41    P: AsRef<std::path::Path> + 'a,
42{
43    let sock = compio_net::UnixStream::connect(addr).await?;
44    Ok(Io::with_memory_pool(UnixStream(sock), pool))
45}
46
47/// Convert std TcpStream to tokio's TcpStream
48pub fn from_tcp_stream(stream: net::TcpStream) -> Result<Io> {
49    stream.set_nodelay(true)?;
50    Ok(Io::new(TcpStream(compio_net::TcpStream::from_std(stream)?)))
51}
52
53#[cfg(unix)]
54/// Convert std UnixStream to tokio's UnixStream
55pub fn from_unix_stream(stream: std::os::unix::net::UnixStream) -> Result<Io> {
56    Ok(Io::new(UnixStream(compio_net::UnixStream::from_std(
57        stream,
58    )?)))
59}