Skip to main content

try_frame

Function try_frame 

Source
pub fn try_frame(
    buf: &[u8],
) -> Result<Option<(RawFrame<'_>, usize)>, MessageError>
Expand description

Framing only: validate the 8-byte header and delimit one frame from the front of buf. Ok(None) = need more bytes. Never interprets the payload.

On success the caller holds the frame’s header, its raw payload bytes, and the consumed count — enough to apply NACK/ignore/skip policy itself if the subsequent Payload::decode call fails. Errors from this function are framing-fatal (MessageError::is_framing_fatal returns true): stream sync is lost and the connection should be closed.

§Errors

Returns a MessageError if buf contains a malformed header (e.g. an incorrect inverse protocol version).

§Examples

Frame and decode one message from a byte buffer, with no allocator:

use simple_doip::{try_frame, messages::Payload};

// A complete DoIP NACK frame: 8-byte header + 1-byte body.
let buf = [0x02, 0xFD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03];

let (frame, consumed) = try_frame(&buf)?.expect("buffer holds a complete frame");
assert_eq!(consumed, 9);

let payload = Payload::decode(frame.payload, frame.header.payload_type)?;
assert!(matches!(payload, Payload::DoIPNack(_)));