Struct UdStreamListener

Source
pub struct UdStreamListener { /* private fields */ }
Available on Unix only.
Expand description

A Unix domain byte stream socket server, listening for connections.

All such sockets have the SOCK_STREAM socket type; in other words, this is the Unix domain version of a TCP server.

§Examples

Basic server:

use interprocess::os::unix::udsocket::{UdStream, UdStreamListener};
use std::{io::{self, prelude::*}, net::Shutdown};

fn handle_error(result: io::Result<UdStream>) -> Option<UdStream> {
    match result {
        Ok(val) => Some(val),
        Err(error) => {
            eprintln!("There was an error with an incoming connection: {}", error);
            None
        }
    }
}

let listener = UdStreamListener::bind("/tmp/example.sock")?;
// Outside the loop so that we could reuse the memory allocation for every client
let mut input_string = String::new();
for mut conn in listener.incoming()
    // Use filter_map to report all errors with connections and skip those connections in the loop,
    // making the actual server loop part much cleaner than if it contained error handling as well.
    .filter_map(handle_error) {
    conn.write_all(b"Hello from server!")?;
    conn.shutdown(Shutdown::Write)?;
    conn.read_to_string(&mut input_string)?;
    println!("Client answered: {}", input_string);
    input_string.clear();
}

Implementations§

Source§

impl UdStreamListener

Source

pub fn bind<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>

Creates a new listener socket at the specified address.

If the socket path exceeds the maximum socket path length (which includes the first 0 byte when using the socket namespace), an error is returned. Errors can also be produced for different reasons, i.e. errors should always be handled regardless of whether the path is known to be short enough or not.

After the socket is dropped, the socket file will be left over. Use bind_with_drop_guard() to mitigate this automatically, even during panics (if unwinding is enabled).

§Example

See ToUdSocketPath.

§System calls
  • socket
  • bind
Source

pub fn bind_with_drop_guard<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>

Creates a new listener socket at the specified address, remembers the address, and installs a drop guard that will delete the socket file once the socket is dropped.

See the documentation of bind().

Source

pub fn accept(&self) -> Result<UdStream>

Listens for incoming connections to the socket, blocking until a client is connected.

See incoming for a convenient way to create a main loop for a server.

§Example
use interprocess::os::unix::udsocket::UdStreamListener;

let listener = UdStreamListener::bind("/tmp/example.sock")?;
loop {
    match listener.accept() {
        Ok(connection) => {
            println!("New client!");
        },
        Err(error) => {
            println!("Incoming connection failed: {}", error);
        },
    }
}
§System calls
  • accept
Source

pub fn incoming(&self) -> Incoming<'_>

Creates an infinite iterator which calls accept() with each iteration. Used together with for loops to conveniently create a main loop for a socket server.

§Example
use interprocess::os::unix::udsocket::UdStreamListener;

let listener = UdStreamListener::bind("/tmp/example.sock")?;
// Thanks to incoming(), you get a simple self-documenting infinite server loop
for connection in listener.incoming()
    .map(|conn| if let Err(error) = conn {
        eprintln!("Incoming connection failed: {}", error);
    }) {
    eprintln!("New client!");
}
Source

pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>

Enables or disables the nonblocking mode for the listener. By default, it is disabled.

In nonblocking mode, calls to accept, and, by extension, iteration through incoming will never wait for a client to become available to connect and will instead return a WouldBlock error immediately, allowing the thread to perform other useful operations while there are no new client connections to accept.

Source

pub fn is_nonblocking(&self) -> Result<bool>

Checks whether the socket is currently in nonblocking mode or not.

Trait Implementations§

Source§

impl AsRawFd for UdStreamListener

Source§

fn as_raw_fd(&self) -> c_int

Extracts the raw file descriptor. Read more
Source§

impl Debug for UdStreamListener

Source§

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

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

impl<'a> From<&'a UdStreamListener> for Incoming<'a>

Source§

fn from(listener: &'a UdStreamListener) -> Self

Converts to this type from the input type.
Source§

impl FromRawFd for UdStreamListener

Source§

unsafe fn from_raw_fd(fd: c_int) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for UdStreamListener

Source§

fn into_raw_fd(self) -> c_int

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

impl TryFrom<UdStreamListener> for UdStreamListener

Available on crate feature tokio_support only.
Source§

type Error = Error

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

fn try_from(x: UdStreamListener) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<UdStreamListener> for UdStreamListener

Available on crate feature tokio_support only.
Source§

type Error = Error

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

fn try_from(sync: SyncUdStreamListener) -> Result<Self, Self::Error>

Performs the conversion.

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> To for T
where T: ?Sized,

Source§

fn to<T>(self) -> T
where Self: Into<T>,

Converts to T by calling Into<T>::into.
Source§

fn try_to<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
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.