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();
// 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(())
}