1 unstable release
Uses old Rust 2015
0.0.1 | Dec 14, 2018 |
---|
#24 in #datagram
17KB
207 lines
udpsocket2
A more ergonomic Tokio-enabled UDP socket.
In particular, attention is paid to the fact that a UDP socket can both send and receive datagrams, and that a practical consumer would like to be able to do both of these things, interleaved on the same socket, with non-blocking I/O.
extern crate futures;
extern crate udpsocket2;
use udpsocket2::UdpSocket;
fn main() -> std::io::Result<()> {
use futures::{Future, Stream};
use futures::future::ok;
let socket = UdpSocket::bind("127.0.0.1:34254")?;
tokio::run(ok(()).and_then(move |_| {
tokio::spawn(
socket.incoming()
.for_each(|datagram| { println!("{:?}", datagram); Ok(()) })
.map_err(|_| ())
);
tokio::spawn(
socket.send_to(&[0xde, 0xad, 0xbe, 0xef], "127.0.0.1:34254")?
.map_err(|_| ())
);
Ok(())
}).map_err(|_: std::io::Error| ()));
Ok(())
}
lib.rs
:
A more ergonomic Tokio-enabled UDP socket.
In particular, attention is paid to the fact that a UDP socket can both send and receive datagrams, and that a practical consumer would like to be able to do both of these things, interleaved on the same socket, with non-blocking I/O.
See the UdpSocket
struct documentation for more details.
Examples
#
#
#
#
let socket = UdpSocket::bind("127.0.0.1:34254")?;
#
tokio::spawn(
socket.incoming()
.for_each(|datagram| { println!("{:?}", datagram); Ok(()) })
.map_err(|_| ())
);
tokio::spawn(
socket.send_to(&[0xde, 0xad, 0xbe, 0xef], "127.0.0.1:34254")?
.map_err(|_| ())
);
#
#
#
Dependencies
~3.5MB
~47K SLoC