use std::{io::Result, net, net::SocketAddr};
use ntex_bytes::PoolRef;
use ntex_io::Io;
mod io;
struct TcpStream(compio::net::TcpStream);
#[cfg(unix)]
struct UnixStream(compio::net::UnixStream);
pub async fn tcp_connect(addr: SocketAddr) -> Result<Io> {
let sock = compio::net::TcpStream::connect(addr).await?;
Ok(Io::new(TcpStream(sock)))
}
pub async fn tcp_connect_in(addr: SocketAddr, pool: PoolRef) -> Result<Io> {
let sock = compio::net::TcpStream::connect(addr).await?;
Ok(Io::with_memory_pool(TcpStream(sock), pool))
}
#[cfg(unix)]
pub async fn unix_connect<'a, P>(addr: P) -> Result<Io>
where
P: AsRef<std::path::Path> + 'a,
{
let sock = compio::net::UnixStream::connect(addr).await?;
Ok(Io::new(UnixStream(sock)))
}
#[cfg(unix)]
pub async fn unix_connect_in<'a, P>(addr: P, pool: PoolRef) -> Result<Io>
where
P: AsRef<std::path::Path> + 'a,
{
let sock = compio::net::UnixStream::connect(addr).await?;
Ok(Io::with_memory_pool(UnixStream(sock), pool))
}
pub fn from_tcp_stream(stream: net::TcpStream) -> Result<Io> {
stream.set_nodelay(true)?;
Ok(Io::new(TcpStream(compio::net::TcpStream::from_std(
stream,
)?)))
}
#[cfg(unix)]
pub fn from_unix_stream(stream: std::os::unix::net::UnixStream) -> Result<Io> {
Ok(Io::new(UnixStream(compio::net::UnixStream::from_std(
stream,
)?)))
}