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]
fn bind(addr: SocketAddr) -> UdpSocketBind
Makes a future to create a UDP socket binded to the given address.
fn send_to<B: AsRef<[u8]>>(self, buf: B, target: SocketAddr) -> SendTo<B>
Makes a future to send data on the socket to the given address.
fn recv_from<B: AsMut<[u8]>>(self, buf: B) -> RecvFrom<B>
Makes a future to receive data from the socket.
fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address that this socket was created from.
fn take_error(&self) -> Result<Option<Error>>
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.
unsafe fn with_inner<F, T>(&self, f: F) -> T where
F: FnOnce(&UdpSocket) -> T,
F: FnOnce(&UdpSocket) -> T,
Calls f
with the reference to the inner socket.
Trait Implementations
impl Debug for UdpSocket
[src]
impl Clone for UdpSocket
[src]
fn clone(&self) -> UdpSocket
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more