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

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]

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

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

Makes a future to receive data from the socket.

Returns the socket address that this socket was created from.

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.

Calls f with the reference to the inner socket.

Trait Implementations

impl Debug for UdpSocket
[src]

Formats the value using the given formatter.

impl Clone for UdpSocket
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more