pub struct UnixStream { /* private fields */ }
Expand description
A structure representing a connected Unix socket with support for passing
RawFd
.
This is the implementation of EnqueueFd
and DequeueFd
that is based
on tokio
UnixStream
. Conceptually the key interfaces
on UnixStream
interact as shown in the following diagram:
EnqueueFd => AsyncWrite => AsyncRead => DequeueFd
That is, you first enqueue a RawFd
to the UnixStream
and then
AsyncWrite
at least one byte. On the other side of the UnixStream
you
then AsyncRead
at least one byte and then dequeue the RawFd
.
This socket can be connected directly with UnixStream::connect
or accepted
from a listener with UnixListener::accept
. Additionally, a pair of
anonymous Unix sockets can be created with UnixStream::pair
.
§Examples
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::fs::File;
let (mut sock1, mut sock2) = UnixStream::pair()?;
// sender side
// let file1: File = ...
sock1.enqueue(&file1).expect("Can't enqueue the file descriptor.");
sock1.write(b"a").await?;
sock1.flush().await?;
// receiver side
let mut buf = [0u8; 1];
sock2.read(&mut buf).await?;
let fd = sock2.dequeue().expect("Can't dequeue the file descriptor.");
let file2 = unsafe { File::from_raw_fd(fd) };
Implementations§
Source§impl UnixStream
impl UnixStream
Sourcepub async fn connect(path: impl AsRef<Path>) -> Result<UnixStream>
pub async fn connect(path: impl AsRef<Path>) -> Result<UnixStream>
Connects to the socket named by path.
This function will create a new socket and connect the the path specified, associating the returned stream with the default event loop’s handle.
§Examples
use fd_queue::tokio::UnixStream;
// let path: Path = ...
UnixStream::connect(path).await?;
Sourcepub fn pair() -> Result<(UnixStream, UnixStream)>
pub fn pair() -> Result<(UnixStream, UnixStream)>
Creates an unnamed pair of connected sockets.
This function will create an unnamed pair of interconnected Unix sockets for communicating back and forth between one another. Each socket will be associated with the default event loop’s handle.
§Examples
use fd_queue::tokio::UnixStream;
let (sock1, sock2) = UnixStream::pair()?;
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address of the local half of this connection.
§Examples
use fd_queue::tokio::UnixStream;
// let path: Path = ...
let sock = UnixStream::connect(path).await?;
sock.local_addr()?;
Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote half of this connection.
§Examples
use fd_queue::tokio::UnixStream;
// let path: Path = ...
let sock = UnixStream::connect(path).await?;
sock.peer_addr()?;
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Returns the value of the SO_ERROR option.
§Examples
use fd_queue::tokio::UnixStream;
// let path: Path = ...
let sock = UnixStream::connect(path).await?;
let err = match sock.take_error() {
Ok(Some(err)) => err,
Ok(None) => {
println!("No error found.");
return Ok(());
}
Err(e) => {
println!("Couldn't take the SO_ERROR option: {}", e);
return Ok(());
}
};
Sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of this connection.
This function will cause all pending and future I/O calls on the specified
portions to immediately return with an appropriate value (see the
documentation of Shutdown
).
§Examples
use std::net::Shutdown;
use tokio::io::AsyncReadExt;
use fd_queue::tokio::UnixStream;
let (mut sock, _) = UnixStream::pair()?;
sock.shutdown(Shutdown::Read)?;
let mut buf = [0u8; 256];
match sock.read(&mut buf).await {
Ok(0) => {},
_ => panic!("Read unexpectedly not shut down."),
}
Trait Implementations§
Source§impl AsRawFd for UnixStream
impl AsRawFd for UnixStream
Source§impl AsyncRead for UnixStream
impl AsyncRead for UnixStream
Source§impl AsyncWrite for UnixStream
impl AsyncWrite for UnixStream
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>
buf
into the object. Read moreSource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Source§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, Error>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>
poll_write
, except that it writes from a slice of buffers. Read moreSource§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. Read moreSource§impl Debug for UnixStream
impl Debug for UnixStream
Source§impl DequeueFd for UnixStream
impl DequeueFd for UnixStream
Source§impl EnqueueFd for UnixStream
impl EnqueueFd for UnixStream
Source§impl From<UnixStream> for UnixStream
impl From<UnixStream> for UnixStream
Source§fn from(inner: TokioUnixStream) -> UnixStream
fn from(inner: TokioUnixStream) -> UnixStream
Source§impl TryFrom<UnixStream> for UnixStream
impl TryFrom<UnixStream> for UnixStream
impl<'pin> Unpin for UnixStreamwhere
__UnixStream<'pin>: Unpin,
impl UnsafeUnpin for UnixStream
Auto Trait Implementations§
impl !Freeze for UnixStream
impl RefUnwindSafe for UnixStream
impl Send for UnixStream
impl Sync for UnixStream
impl UnwindSafe for UnixStream
Blanket Implementations§
Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
Source§fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
Source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf
. Read moreSource§fn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
fn read_u8(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
Source§fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
Source§fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
Source§fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
Source§fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
Source§fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
Source§fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
Source§fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
Source§fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
Source§fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
Source§fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
Source§fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
Source§fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
Source§fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
Source§fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
Source§fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
Source§fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
Source§fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
Source§fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
Source§fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
Source§fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
Source§fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
Source§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
buf
. Read more