use std::io::{Error as IoError, Result as IoResult};
use std::net::TcpStream;
use std::os::unix::io::FromRawFd;
pub fn create_tcp_pair() -> IoResult<(TcpStream, TcpStream)> {
let mut fds: [libc::c_int; 2] = [0; 2];
unsafe {
if libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) == 0 {
Ok((TcpStream::from_raw_fd(fds[0]), TcpStream::from_raw_fd(fds[1])))
} else {
Err(IoError::last_os_error())
}
}
}
#[cfg(feature = "tokio")]
pub fn create_tcp_pair_async() -> IoResult<(tokio::net::TcpStream, tokio::net::TcpStream)> {
let (c, s) = create_tcp_pair()?;
c.set_nonblocking(true)?;
s.set_nonblocking(true)?;
Ok((tokio::net::TcpStream::from_std(c)?, tokio::net::TcpStream::from_std(s)?))
}