Skip to main content

Module framing

Module framing 

Source
Expand description

Framing: delimiting one checksum-verified FIX message from a byte stream.

A FIX connection is an unbroken stream with no external length prefix, so the only way to know where a message starts is to know where the previous one ended. Scanner::scan answers that one message at a time, borrowing from the caller’s buffer and allocating nothing.

Each call returns one of three outcomes, and each implies a different action: a Frame to consume, Outcome::Incomplete to wait on, or Outcome::Garbled to skip. Driving it is a loop:

use refix_message::framing::{Scanner, Outcome};

// Two heartbeats back to back, SOH-delimited.
let stream = b"8=FIX.4.4\x019=10\x0135=0\x0134=2\x0110=166\x01\
               8=FIX.4.4\x019=10\x0135=0\x0134=2\x0110=166\x01";

let scanner = Scanner::default();
let mut buf = &stream[..];
let mut seen = Vec::new();
loop {
    let advance = match scanner.scan(buf) {
        Outcome::Frame(frame) => {
            seen.push(frame.msg_type.to_vec());
            frame.bytes.len()
        }
        Outcome::Garbled { skipped, .. } => skipped,
        Outcome::Incomplete => break, // read more from the socket
    };
    buf = &buf[advance..];
}
assert_eq!(seen, vec![b"0".to_vec(), b"0".to_vec()]);

§Scope

Framing reads only the fields whose position the frame structure fixes: BeginString, BodyLength, MsgType and CheckSum, each checked positionally at its known offset. Every other header field has no guaranteed wire position and may sit behind a length-prefixed data field, so reading it needs the order-independent, data-field-aware decoding of the layer above.

Structs§

Frame
One well-framed, checksum-verified message.
Scanner
A stateless framing scanner: each scan call attempts to delimit one checksum-verified frame from the start of a buffer.

Enums§

BeginString
The value of BeginString(8), with recognised protocol versions decoded.
GarbledReason
Outcome

Constants§

SOH