pub fn scan_frame(buf: &[u8]) -> FrameScanExpand description
Scan buf for a complete frame at offset 0 without allocating.
Examples found in repository?
examples/cli.rs (line 306)
288fn tcp_reader_loop(
289 stream: Arc<TcpStream>,
290 layout: Shared<Option<Layout>>,
291 root: Shared<Option<Node>>,
292 watch: Arc<AtomicBool>,
293) {
294 let mut buf = Vec::<u8>::with_capacity(8192);
295 let mut chunk = [0u8; 4096];
296 loop {
297 let mut s: &TcpStream = &stream;
298 let n = match s.read(&mut chunk) {
299 Ok(0) => return,
300 Ok(n) => n,
301 Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
302 Err(_) => return,
303 };
304 buf.extend_from_slice(&chunk[..n]);
305 loop {
306 match frame::scan_frame(&buf) {
307 FrameScan::Incomplete => break,
308 FrameScan::Desync => {
309 // TCP frames are byte-aligned (no report stride);
310 // slide one byte and rescan to resync on a fresh magic.
311 buf.drain(..1);
312 }
313 FrameScan::Complete { len } => {
314 let Some((packet, consumed)) = frame::Packet::from_bytes(&buf[..len]) else {
315 buf.drain(..len);
316 continue;
317 };
318 buf.drain(..consumed);
319 handle_body(packet.payload, &layout, &root, &watch);
320 }
321 }
322 }
323 }
324}