avoid reallocating the frame, just use a static buffer

This commit is contained in:
devdesk 2024-02-17 15:56:05 +02:00
parent 49f9aa98ed
commit a917f75ce0

View File

@ -67,7 +67,10 @@ fn main() -> anyhow::Result<()> {
.unwrap(); .unwrap();
// get a packet and print its bytes // 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)?; let mut out = File::create(&output)?;
while let Ok(p) = cap.next_packet() { while let Ok(p) = cap.next_packet() {
let data = p.data; let data = p.data;
@ -80,17 +83,17 @@ fn main() -> anyhow::Result<()> {
Err(_) => continue, Err(_) => continue,
}; };
let data = &data[HDR_SIZE..]; let data = &data[HDR_SIZE..];
if header.part == 0 && parts.len() > 0 { if (header.part == 0 && len > 0)
let frame = Frame { // do not write out of bounds - would panic, instead just skip
header, || (data.len() + len > FRAME_LEN)
raw: parts.concat(), {
}; if len == FRAME_LEN {
if frame.raw.len() == 384 * 288 * 2 { out.write_all(&frame[..])?;
out.write_all(&frame.raw)?;
} }
parts.clear(); len = 0;
} }
parts.push(data.to_vec()); frame[len..len + data.len()].copy_from_slice(data);
len += data.len();
} }
Ok(()) Ok(())
} }