[][src]Struct fibers::net::UdpSocket

pub struct UdpSocket { /* fields omitted */ }

A User Datagram Protocol socket.

Examples

// See also: fibers/examples/udp_example.rs
use fibers::{Executor, InPlaceExecutor, Spawn};
use fibers::net::UdpSocket;
use fibers::sync::oneshot;
use futures::Future;

let mut executor = InPlaceExecutor::new().unwrap();
let (addr_tx, addr_rx) = oneshot::channel();

// Spawns receiver
let mut monitor = executor.spawn_monitor(UdpSocket::bind("127.0.0.1:0".parse().unwrap())
    .and_then(|socket| {
        addr_tx.send(socket.local_addr().unwrap()).unwrap();
        socket.recv_from(vec![0; 32]).map_err(|e| panic!("{:?}", e))
    })
    .and_then(|(_, mut buf, len, _)| {
        buf.truncate(len);
        assert_eq!(buf, b"hello world");
        Ok(())
    }));

// Spawns sender
executor.spawn(addr_rx.map_err(|e| panic!("{:?}", e))
    .and_then(|receiver_addr| {
        UdpSocket::bind("127.0.0.1:0".parse().unwrap())
            .and_then(move |socket| {
                socket.send_to(b"hello world", receiver_addr).map_err(|e| panic!("{:?}", e))
            })
            .then(|r| Ok(assert!(r.is_ok())))
    }));

// Runs until the monitored fiber (i.e., receiver) exits.
while monitor.poll().unwrap().is_not_ready() {
    executor.run_once().unwrap();
}

Methods

impl UdpSocket[src]

pub fn bind(addr: SocketAddr) -> UdpSocketBind[src]

Makes a future to create a UDP socket binded to the given address.

pub fn send_to<B: AsRef<[u8]>>(self, buf: B, target: SocketAddr) -> SendTo<B>[src]

Makes a future to send data on the socket to the given address.

pub fn recv_from<B: AsMut<[u8]>>(self, buf: B) -> RecvFrom<B>[src]

Makes a future to receive data from the socket.

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

Returns the socket address that this socket was created from.

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

Get the value of the SO_ERROR option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

pub fn with_inner<F, T>(&self, f: F) -> T where
    F: FnOnce(&MioUdpSocket) -> T, 
[src]

Calls f with the reference to the inner socket.

Trait Implementations

impl Clone for UdpSocket[src]

impl Debug for UdpSocket[src]

Auto Trait Implementations

Blanket Implementations

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

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.

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

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

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