use crate::{nonblocking, UnixSocketAddr, ConnCredentials};
use std::io::{self, IoSlice, IoSliceMut};
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::path::Path;
use tokio_crate::io::Interest;
use tokio_crate::io::unix::AsyncFd;
pub struct UnixSeqpacketConn {
io: AsyncFd<nonblocking::UnixSeqpacketConn>,
}
impl UnixSeqpacketConn {
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let conn = nonblocking::UnixSeqpacketConn::connect(path)?;
Self::from_nonblocking(conn)
}
pub fn connect_addr(addr: &UnixSocketAddr) -> io::Result<Self> {
let conn = nonblocking::UnixSeqpacketConn::connect_unix_addr(addr)?;
Self::from_nonblocking(conn)
}
pub fn connect_from_addr(from: &UnixSocketAddr, to: &UnixSocketAddr)
-> io::Result<Self> {
let conn = nonblocking::UnixSeqpacketConn::connect_from_to_unix_addr(from, to)?;
Self::from_nonblocking(conn)
}
pub fn pair() -> Result<(UnixSeqpacketConn, UnixSeqpacketConn), io::Error> {
let (a, b) = nonblocking::UnixSeqpacketConn::pair()?;
let a = Self::from_nonblocking(a)?;
let b = Self::from_nonblocking(b)?;
Ok((a, b))
}
pub fn from_nonblocking(conn: nonblocking::UnixSeqpacketConn) -> Result<Self, io::Error> {
match AsyncFd::new(conn) {
Ok(io) => Ok(Self { io }),
Err(e) => Err(e),
}
}
pub fn into_nonblocking(self) -> nonblocking::UnixSeqpacketConn {
self.io.into_inner()
}
pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Self, io::Error> {
Self::from_nonblocking(nonblocking::UnixSeqpacketConn::from_raw_fd(fd))
}
pub fn shutdown(&self, how: Shutdown) -> Result<(), io::Error> {
self.io.get_ref().shutdown(how)
}
pub fn local_addr(&self) -> Result<UnixSocketAddr, io::Error> {
self.io.get_ref().local_unix_addr()
}
pub fn peer_addr(&self) -> Result<UnixSocketAddr, io::Error> {
self.io.get_ref().peer_unix_addr()
}
pub fn initial_peer_credentials(&self) -> Result<ConnCredentials, io::Error> {
self.io.get_ref().initial_peer_credentials()
}
pub fn initial_peer_selinux_context(&self, buffer: &mut[u8]) -> Result<usize, io::Error> {
self.io.get_ref().initial_peer_selinux_context(buffer)
}
pub fn take_error(&self) -> Result<Option<io::Error>, io::Error> {
self.io.get_ref().take_error()
}
}
impl UnixSeqpacketConn {
pub async fn send(&mut self, packet: &[u8]) -> io::Result<usize> {
self.io.async_io(Interest::WRITABLE, |conn| conn.send(packet) ).await
}
pub async fn recv(&mut self, buffer: &mut[u8]) -> io::Result<usize> {
self.io.async_io(Interest::READABLE, |conn| conn.recv(buffer) ).await
}
pub async fn send_vectored<'a, 'b>
(&'a mut self, slices: &'b [IoSlice<'b>]) -> io::Result<usize> {
self.io.async_io(Interest::WRITABLE, |conn| conn.send_vectored(slices) ).await
}
pub async fn recv_vectored<'a, 'b>
(&'a mut self, buffers: &'b mut [IoSliceMut<'b>]) -> io::Result<usize> {
self.io.async_io(
Interest::READABLE,
|conn| conn.recv_vectored(buffers).map(|(received, _)| received )
).await
}
pub async fn peek(&mut self, buffer: &mut[u8]) -> io::Result<usize> {
self.io.async_io(Interest::READABLE, |conn| conn.peek(buffer) ).await
}
pub async fn peek_vectored<'a, 'b>
(&'a mut self, buffers: &'b mut [IoSliceMut<'b>]) -> io::Result<usize> {
self.io.async_io(
Interest::READABLE,
|conn| conn.peek_vectored(buffers).map(|(received, _)| received )
).await
}
pub async fn send_fds(&mut self, bytes: &[u8], fds: &[RawFd]) -> io::Result<usize> {
self.io.async_io(Interest::WRITABLE, |conn| conn.send_fds(bytes, fds) ).await
}
pub async fn recv_fds(&mut self, byte_buffer: &mut[u8], fd_buffer: &mut[RawFd])
-> io::Result<(usize, bool, usize)> {
self.io.async_io(Interest::READABLE, |conn| conn.recv_fds(byte_buffer, fd_buffer) ).await
}
}
impl AsRef<nonblocking::UnixSeqpacketConn> for UnixSeqpacketConn {
fn as_ref(&self) -> &nonblocking::UnixSeqpacketConn {
self.io.get_ref()
}
}
impl AsRawFd for UnixSeqpacketConn {
fn as_raw_fd(&self) -> RawFd {
self.io.get_ref().as_raw_fd()
}
}
impl IntoRawFd for UnixSeqpacketConn {
fn into_raw_fd(self) -> RawFd {
self.io.into_inner().into_raw_fd()
}
}
pub struct UnixSeqpacketListener {
io: AsyncFd<nonblocking::UnixSeqpacketListener>,
}
impl UnixSeqpacketListener {
pub fn bind<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
match nonblocking::UnixSeqpacketListener::bind(path.as_ref()) {
Ok(listener) => Self::from_nonblocking(listener),
Err(e) => Err(e),
}
}
pub fn bind_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
match nonblocking::UnixSeqpacketListener::bind_unix_addr(addr) {
Ok(listener) => Self::from_nonblocking(listener),
Err(e) => Err(e),
}
}
pub fn from_nonblocking(listener: nonblocking::UnixSeqpacketListener)
-> Result<Self, io::Error> {
match AsyncFd::with_interest(listener, Interest::READABLE) {
Ok(io) => Ok(Self { io }),
Err(e) => Err(e),
}
}
pub fn into_nonblocking(self) -> nonblocking::UnixSeqpacketListener {
self.io.into_inner()
}
pub unsafe fn from_raw_fd(fd: RawFd) -> Result<Self, io::Error> {
Self::from_nonblocking(nonblocking::UnixSeqpacketListener::from_raw_fd(fd))
}
pub async fn accept(&mut self) -> io::Result<(UnixSeqpacketConn, UnixSocketAddr)> {
let (conn, addr) = self.io.async_io(
Interest::READABLE,
|inner| inner.accept_unix_addr()
).await?;
let conn = UnixSeqpacketConn::from_nonblocking(conn)?;
Ok((conn, addr))
}
pub fn local_addr(&self) -> Result<UnixSocketAddr, io::Error> {
self.io.get_ref().local_unix_addr()
}
pub fn take_error(&self) -> Result<Option<io::Error>, io::Error> {
self.io.get_ref().take_error()
}
}
impl AsRef<nonblocking::UnixSeqpacketListener> for UnixSeqpacketListener {
fn as_ref(&self) -> &nonblocking::UnixSeqpacketListener {
self.io.get_ref()
}
}
impl AsRawFd for UnixSeqpacketListener {
fn as_raw_fd(&self) -> RawFd {
self.io.get_ref().as_raw_fd()
}
}
impl IntoRawFd for UnixSeqpacketListener {
fn into_raw_fd(self) -> RawFd {
self.io.into_inner().into_raw_fd()
}
}