raptorq/examples/main.rs

57 lines
1.7 KiB
Rust
Raw Normal View History

2023-11-25 19:06:51 +00:00
#[cfg(not(feature = "python"))]
2019-03-24 04:23:57 +00:00
use rand::seq::SliceRandom;
2023-11-25 19:06:51 +00:00
#[cfg(not(feature = "python"))]
2019-03-28 21:13:19 +00:00
use rand::Rng;
2023-11-25 19:06:51 +00:00
#[cfg(not(feature = "python"))]
2019-03-24 04:23:57 +00:00
use raptorq::{Decoder, Encoder, EncodingPacket};
2023-11-25 19:06:51 +00:00
#[cfg(not(feature = "python"))]
2019-03-24 04:23:57 +00:00
fn main() {
// Generate some random data to send
let mut data: Vec<u8> = vec![0; 10_000];
for i in 0..data.len() {
data[i] = rand::thread_rng().gen();
}
// Create the Encoder, with an MTU of 1400 (common for Ethernet)
let encoder = Encoder::with_defaults(&data, 1400);
// Perform the encoding, and serialize to Vec<u8> for transmission
2019-03-28 21:13:19 +00:00
let mut packets: Vec<Vec<u8>> = encoder
.get_encoded_packets(15)
.iter()
2019-03-24 04:23:57 +00:00
.map(|packet| packet.serialize())
.collect();
// Here we simulate losing 10 of the packets randomly. Normally, you would send them over
// (potentially lossy) network here.
packets.shuffle(&mut rand::thread_rng());
// Erase 10 packets at random
let length = packets.len();
packets.truncate(length - 10);
// The Decoder MUST be constructed with the configuration of the Encoder.
// The ObjectTransmissionInformation configuration should be transmitted over a reliable
// channel
let mut decoder = Decoder::new(encoder.get_config());
// Perform the decoding
let mut result = None;
while !packets.is_empty() {
result = decoder.decode(EncodingPacket::deserialize(&packets.pop().unwrap()));
2023-07-03 17:36:03 +00:00
if result.is_some() {
2019-03-24 04:23:57 +00:00
break;
}
}
// Check that even though some of the data was lost we are able to reconstruct the original message
assert_eq!(result.unwrap(), data);
2019-03-28 21:13:19 +00:00
}
2023-11-25 19:06:51 +00:00
#[cfg(feature = "python")]
fn main() {
2023-11-25 19:06:51 +00:00
panic!("This is not indented to compile for `python` feature.");
}