mio::net

Struct UnixDatagram

Source
pub struct UnixDatagram { /* private fields */ }
Available on crate feature net only.
Expand description

A Unix datagram socket.

Implementations§

Source§

impl UnixDatagram

Source

pub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixDatagram>

Creates a Unix datagram socket bound to the given path.

Source

pub fn bind_addr(address: &SocketAddr) -> Result<UnixDatagram>

Creates a new UnixDatagram bound to the specified socket address.

Source

pub fn from_std(socket: UnixDatagram) -> UnixDatagram

Creates a new UnixDatagram from a standard net::UnixDatagram.

This function is intended to be used to wrap a Unix datagram from the standard library in the Mio equivalent. The conversion assumes nothing about the underlying datagram; it is left up to the user to set it in non-blocking mode.

Source

pub fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>

Connects the socket to the specified address.

This may return a WouldBlock in which case the socket connection cannot be completed immediately.

Source

pub fn unbound() -> Result<UnixDatagram>

Creates a Unix Datagram socket which is not bound to any address.

Source

pub fn pair() -> Result<(UnixDatagram, UnixDatagram)>

Create an unnamed pair of connected sockets.

Source

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

Returns the address of this socket.

Source

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

Returns the address of this socket’s peer.

The connect method will connect the socket to a peer.

Source

pub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Receives data from the socket.

On success, returns the number of bytes read and the address from whence the data came.

Source

pub fn recv(&self, buf: &mut [u8]) -> Result<usize>

Receives data from the socket.

On success, returns the number of bytes read.

Source

pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> Result<usize>

Sends data on the socket to the specified address.

On success, returns the number of bytes written.

Source

pub fn send(&self, buf: &[u8]) -> Result<usize>

Sends data on the socket to the socket’s peer.

The peer address may be set by the connect method, and this method will return an error if the socket has not already been connected.

On success, returns the number of bytes written.

Source

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

Returns the value of the SO_ERROR option.

Source

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

Shut 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).

Source

pub fn try_io<F, T>(&self, f: F) -> Result<T>
where F: FnOnce() -> Result<T>,

Execute an I/O operation ensuring that the socket receives more events if it hits a WouldBlock error.

§Notes

This method is required to be called for all I/O operations to ensure the user will receive events once the socket is ready again after returning a WouldBlock error.

§Examples
use std::io;
use std::os::fd::AsRawFd;
use mio::net::UnixDatagram;

let (dgram1, dgram2) = UnixDatagram::pair()?;

// Wait until the dgram is writable...

// Write to the dgram using a direct libc call, of course the
// `io::Write` implementation would be easier to use.
let buf = b"hello";
let n = dgram1.try_io(|| {
    let buf_ptr = &buf as *const _ as *const _;
    let res = unsafe { libc::send(dgram1.as_raw_fd(), buf_ptr, buf.len(), 0) };
    if res != -1 {
        Ok(res as usize)
    } else {
        // If EAGAIN or EWOULDBLOCK is set by libc::send, the closure
        // should return `WouldBlock` error.
        Err(io::Error::last_os_error())
    }
})?;
eprintln!("write {} bytes", n);

// Wait until the dgram is readable...

// Read from the dgram using a direct libc call, of course the
// `io::Read` implementation would be easier to use.
let mut buf = [0; 512];
let n = dgram2.try_io(|| {
    let buf_ptr = &mut buf as *mut _ as *mut _;
    let res = unsafe { libc::recv(dgram2.as_raw_fd(), buf_ptr, buf.len(), 0) };
    if res != -1 {
        Ok(res as usize)
    } else {
        // If EAGAIN or EWOULDBLOCK is set by libc::recv, the closure
        // should return `WouldBlock` error.
        Err(io::Error::last_os_error())
    }
})?;
eprintln!("read {} bytes", n);

Trait Implementations§

Source§

impl AsFd for UnixDatagram

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for UnixDatagram

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for UnixDatagram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<UnixDatagram> for UnixDatagram

Source§

fn from(datagram: UnixDatagram) -> Self

Converts to this type from the input type.
Source§

impl FromRawFd for UnixDatagram

Source§

unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram

Converts a RawFd to a UnixDatagram.

§Notes

The caller is responsible for ensuring that the socket is in non-blocking mode.

Source§

impl IntoRawFd for UnixDatagram

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
Source§

impl Source for UnixDatagram

Source§

fn register( &mut self, registry: &Registry, token: Token, interests: Interest, ) -> Result<()>

Register self with the given Registry instance. Read more
Source§

fn reregister( &mut self, registry: &Registry, token: Token, interests: Interest, ) -> Result<()>

Re-register self with the given Registry instance. Read more
Source§

fn deregister(&mut self, registry: &Registry) -> Result<()>

Deregister self from the given Registry instance. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 16 bytes