Crates.io | rudp |
lib.rs | rudp |
version | 0.2.1 |
source | src |
created_at | 2018-07-16 15:51:40.505264 |
updated_at | 2018-07-16 16:01:34.86382 |
description | State-wrapper for UdpSockets to implement RUDP protocol. Allows per-message selection of message guarantees on a spectrum from UDP-like to TCP-like. |
homepage | |
repository | https://github.com/sirkibsirkib/rudp |
max_upload_size | |
id | 74524 |
size | 51,503 |
RUDP builds some reliability mechanisms on top of UDP that can drop in as desired at runtime to make it act more like TCP.
If TCP would be too strict but UDP would be too liberal, then RUDP might provide the functionality you need.
An Endpoint
struct wraps some UdpLike
object with a stateful wrapper.
This object must just support send
and recv
(without explcit peer address);
UdpSocket already has this functionality.
// wrap a mio UdpSocket
let sock = mio::net::UdpSocket::bind(addr).unwrap();
sock.connect(peer_addr).unwrap();
let mut endpt = Endpoint::new(sock);
// write a single payload b"Hello, there.".
endpt.write(b"Hello").unwrap();
endpt.write(b", there.").unwrap();
endpt.send_written(Guarantee::Order).unwrap();
// Here's a more convenient form when all the data is in one place
endpt.send_payload(Guarantee::Delivery, b"General Kenobi!").unwrap();
// try to receive the next message. Returns `Ok(None)` if the inner UdpLike
// yields some `WouldBlock` error.
if let Some(msg) = endpt.recv().expect("fatal io error!") {
println!("msg {:?}", msg);
} else {
println!("Nothing ready yet!");
}
// Send three distinct payloads, but allow the peer to yield them
// in any order with respect to one another.
endpt.as_set(|mut s| {
endpt.send_payload(Guarantee::Delivery, b"A")?;
endpt.send_payload(Guarantee::None, b"B")?;
endpt.send_payload(Guarantee::Order, b"C")?;
}).unwrap();
// pass control flow to the endpoint so that it can still send heartbeats,
// clear old references and resend lost packets if you dont want to send or recv.
endpt.maintain();
A RUDP connection is maintained by an Endpoint
structure, built on top
of any Udp-like structure. (We suggest using mio::net::UdpSocket
).
All its functions come in three categories:
send_written
, io::write
, send_payload
, ...recv
maintain
The Endpoint does not have its own thread of control, nor do its calls spin endlessly. The progress of the communication