From a917f75ce052412701213744f30be1aad9f92ecd Mon Sep 17 00:00:00 2001 From: devdesk Date: Sat, 17 Feb 2024 15:56:05 +0200 Subject: [PATCH] avoid reallocating the frame, just use a static buffer --- examples/live.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/examples/live.rs b/examples/live.rs index a0d6701..e0c8b4d 100644 --- a/examples/live.rs +++ b/examples/live.rs @@ -67,7 +67,10 @@ fn main() -> anyhow::Result<()> { .unwrap(); // get a packet and print its bytes - let mut parts = vec![]; + const PACKET_LEN: usize = 6972; + const FRAME_LEN: usize = 288 * 384 * 2; + let mut frame = [0u8; FRAME_LEN]; + let mut len = 0; let mut out = File::create(&output)?; while let Ok(p) = cap.next_packet() { let data = p.data; @@ -80,17 +83,17 @@ fn main() -> anyhow::Result<()> { Err(_) => continue, }; let data = &data[HDR_SIZE..]; - if header.part == 0 && parts.len() > 0 { - let frame = Frame { - header, - raw: parts.concat(), - }; - if frame.raw.len() == 384 * 288 * 2 { - out.write_all(&frame.raw)?; + if (header.part == 0 && len > 0) + // do not write out of bounds - would panic, instead just skip + || (data.len() + len > FRAME_LEN) + { + if len == FRAME_LEN { + out.write_all(&frame[..])?; } - parts.clear(); + len = 0; } - parts.push(data.to_vec()); + frame[len..len + data.len()].copy_from_slice(data); + len += data.len(); } Ok(()) }