[][src]Struct fd_queue::UnixStream

pub struct UnixStream { /* fields omitted */ }

A structure representing a connected Unix socket with support for passing RawFd.

This is the primary implementation of EnqueueFd and DequeueFd and it is based on a blocking, Unix domain socket. Conceptually the key interfaces on UnixStream interact as shown in the following diagram:

EnqueueFd => Write => Read => DequeueFd

That is, you first endqueue a RawFd to the UnixStream and then Write at least one byte. On the other side of the UnixStream you then Read at least one byte and then dequeue the RawFd.

Examples

use std::fs::File;

let (mut sock1, mut sock2) = UnixStream::pair()?;

// sender side
// let file1: File = ...
sock1.enqueue(&file1).expect("Can't endqueue the file descriptor.");
sock1.write(b"a")?;
sock1.flush()?;

// receiver side
let mut buf = [0u8; 1];
sock2.read(&mut buf)?;
let fd = sock2.dequeue().expect("Can't dequeue the file descriptor.");
let file2 = unsafe { File::from_raw_fd(fd) };

Implementations

impl UnixStream[src]

pub const FD_QUEUE_SIZE: usize[src]

The size of the bounded queue of outbound RawFd.

pub fn connect<P: AsRef<Path>>(path: P) -> Result<UnixStream>[src]

Connects to the socket named by path.

Examples

use fd_queue::UnixStream;

// let path = ...

let sock = match UnixStream::connect(path) {
    Ok(sock) => sock,
    Err(e) => {
        println!("Couldn't connect to a socket: {}", e);
        return Ok(());
    }
};

pub fn pair() -> Result<(UnixStream, UnixStream)>[src]

Creates an unnamed pair of connected sockets.

Returns two UnixStreams which are connected to each other.

Examples

use fd_queue::UnixStream;

let (sock1, sock2) = match UnixStream::pair() {
    Ok((sock1, sock2)) => (sock1, sock2),
    Err(e) => {
        println!("Couldn't create a pair of sockets: {}", e);
        return;
    }
};

pub fn try_clone(&self) -> Result<UnixStream>[src]

Creates a new independently owned handle to the underlying socket.

The returned UnixStream is a reference to the same stream that this object references. Both handles will read and write the same stream of data, and options set on one stream will be propagated to the other stream.

Examples

use fd_queue::UnixStream;

let (sock1, _) = UnixStream::pair()?;

let sock2 = match sock1.try_clone() {
    Ok(sock) => sock,
    Err(e) => {
        println!("Couldn't clone a socket: {}", e);
        return Ok(());
    }
};

pub fn local_addr(&self) -> Result<SocketAddr>[src]

Returns the socket address of the local half of this connection.

Examples

use fd_queue::UnixStream;

// let path = ...
let sock = UnixStream::connect(path)?;

let addr = match sock.local_addr() {
    Ok(addr) => addr,
    Err(e) => {
        println!("Couldn't get the local address: {}", e);
        return Ok(());
    }
};

pub fn peer_addr(&self) -> Result<SocketAddr>[src]

Returns the socket address of the remote half of this connection.

Examples

use fd_queue::UnixStream;

// let path = ...
let sock = UnixStream::connect(path)?;

let addr = match sock.peer_addr() {
    Ok(addr) => addr,
    Err(e) => {
        println!("Couldn't get the local address: {}", e);
        return Ok(());
    }
};

pub fn take_error(&self) -> Result<Option<Error>>[src]

Returns the value of the SO_ERROR option.

Examples

use fd_queue::UnixStream;

let (sock, _) = UnixStream::pair()?;

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(());
    }
};

pub fn shutdown(&self, how: Shutdown) -> Result<()>[src]

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.

Examples

use fd_queue::UnixStream;
use std::net::Shutdown;
use std::io::Read;

let (mut sock, _) = UnixStream::pair()?;

sock.shutdown(Shutdown::Read).expect("Couldn't shutdown.");

let mut buf = [0u8; 256];
match sock.read(buf.as_mut()) {
    Ok(0) => {},
    _ => panic!("Read unexpectly not shut down."),
}

Trait Implementations

impl AsRawFd for UnixStream[src]

impl Debug for UnixStream[src]

impl DequeueFd for UnixStream[src]

Dequeue a RawFd that was previously transmitted across the UnixStream.

The RawFd that are dequeued were transmitted by a previous call to a method of Read.

impl EnqueueFd for UnixStream[src]

Enqueue a RawFd for later transmission across the UnixStream.

The RawFd will be transmitted on a later call to a method of Write. The number of RawFd that can be enqueued before being transmitted is bounded by FD_QUEUE_SIZE.

impl From<UnixStream> for UnixStream[src]

impl FromRawFd for UnixStream[src]

impl IntoRawFd for UnixStream[src]

impl Read for UnixStream[src]

Receive bytes and RawFd that are transmitted across the UnixStream.

The RawFd that are received along with the bytes will be available through the method of the DequeueFd implementation. The number of RawFd that can be received in a single call to one of the Read methods is bounded by FD_QUEUE_SIZE. It is an error if the other side of this UnixStream attempted to send more control messages (including RawFd) than will fit in the buffer that has been sized for receiving up to FD_QUEUE_SIZE RawFd.

impl TryFrom<UnixStream> for UnixStream[src]

type Error = Error

The type returned in the event of a conversion error.

impl Write for UnixStream[src]

Transmit bytes and RawFd across the UnixStream.

The RawFd that are transmitted along with the bytes are ones that were previously enqueued for transmission through the method of EnqueueFd.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.