Binary Market Data Decoder
Prerequisites: Market Data Feed Handler Shape, Network Stack Latency, Copies, Moves, and Object Layout
The AAPL feed handler from the feed handler article receives datagrams in a binary wire format. That article's demo assumed the wire matched the host's MarketUpdate layout and cast a pointer onto each record.
This format packs its fields with no padding, at offsets a C++ compiler would not choose, and its messages vary in length. Implement the frame and parse stages for it.
What to implement
decode_packets(packets)returns the decoded lines in order.- Each element of
packetsis one received packet as lowercase hex text, two digits per byte, with the packet's first byte as the line's first two characters:e803is the two bytes0xe8and0x03. - On stdin, the first line is the number of packets, and each remaining line is one packet.
- There are three parts, each adding behavior. One solution must pass all eleven tests.
Byte order
Every integer wider than one byte is little-endian: its least significant byte comes first.
- A field of
wbytesb0 b1 ... b(w-1)holds the valueb0 + b1 * 256 + b2 * 256^2 + .... - The 16-bit value 1000 is
0x03e8, sent as the bytese8 03. The 32-bit value 19011 is0x00004a43, sent as43 4a 00 00. u8,u16,u32, andu64are unsigned integers of 1, 2, 4, and 8 bytes.i32, the one signed type, uses two's complement. Read the four bytes as an unsigned valuev; the field's value isvwhenv < 2^31andv - 2^32otherwise, sofb ff ff ffis -5.
The wire format
A packet starts with a 6-byte header.
| Offset | Field | Type | Meaning |
|---|---|---|---|
| 0 | count | u16 | How many messages the packet carries |
| 2 | seq | u32 | The sequence number of the packet's first message |
- The
countmessages follow back to back from offset 6. - Message
kof the packet, counting from 0, has message numberseq + k. - Each message is a 3-byte message header followed by
lengthbytes of body. It occupies3 + lengthbytes, and the next message starts right after its body.
| Offset | Field | Type | Meaning |
|---|---|---|---|
| 0 | type | u8 | Which message this is |
| 1 | length | u16 | How many body bytes follow the message header |
| 3 | body | length bytes | Laid out according to type |
The decoder knows three message types. Body offsets count from the first body byte.
| Type | Message | Body fields (offset: name, type) | Body size | Prints |
|---|---|---|---|---|
| 1 | Add Order | 0: id u64, 8: side u8, 9: price i32, 13: qty u32, 17: symbol 8 bytes | 25 | ADD id side price qty symbol |
| 2 | Execute | 0: id u64, 8: qty u32 | 12 | EXEC id qty |
| 3 | Delete | 0: id u64 | 8 | DEL id |
In an Add Order:
sideis 0 for a buy and 1 for a sell, printed asBandS.priceis in integer ticks and is signed, so the same message can carry an instrument whose price can fall below zero, such as a spread quoted as the difference between two prices.symbolis 1 to 8 ASCII uppercase letters, padded on the right with spaces to 8 bytes. Print it without the padding.
Part 1: Decode well-formed packets
- In Part 1 every packet is complete: it holds exactly
countmessages, every message has type 1, 2, or 3, and everylengthequals that type's body size. - A packet with a complete header first prints
PACKET seq count, then one line per message in order. - A packet whose
countis 0 is a heartbeat, which a feed sends when it has nothing to report. It prints only itsPACKETline.
Output
PACKET seq count
ADD id side price qty symbol
EXEC id qty
DEL id
- Numbers print in decimal, and a negative price prints with a leading
-.
Limits
- At most 104 packets, each 1 to 1,000 bytes long.
seq + countstays below 232.- A packet never holds bytes after its
count-th message. idvalues use all 64 bits, andseqandqtyvalues reach above 231, so a signed 32-bitintcannot hold them. Usestd::uint64_tforidand unsigned types forseqandqty.
Tests 1–3 cover this part.
Part 2: Unknown types, longer messages, and truncation
The length field lets a decoder step over a message it does not understand. That is how a feed can add message types without breaking older decoders.
Unknown types and longer messages
- A message whose
typeis not 1, 2, or 3 printsSKIP type, withtypein decimal. Decoding continues with the next message,3 + lengthbytes after the start of this one. - A
lengthof 0 is valid. - A known message may be longer than its body size in the table: a newer version of the format may append fields. Decode the listed fields from the front of the body, ignore the rest, and continue after
lengthbody bytes. - A known type's
lengthis never smaller than its body size.
Truncation
A packet can also arrive cut short.
- Before decoding each of the
countmessages, check that the packet still holds the whole message. - If fewer than 3 bytes remain, or the 3-byte message header fits but fewer than
lengthbytes follow it, the message is truncated. PrintTRUNCATEDand stop decoding that packet. - Lines already printed for the packet stay, and the next packet decodes normally.
- A packet whose bytes end at a message boundary before
countmessages falls under the same rule, because 0 bytes remain for the next message. - The check comes before the type is examined, so a truncated message of an unknown type prints
TRUNCATED, notSKIP. - A packet shorter than 6 bytes has no complete header and prints only
TRUNCATED.
Tests 4–6 and 11 cover this part.
Part 3: Sequence gaps and stale packets
Message numbers increase by one across the whole feed, so the decoder can tell when messages went missing, as in the feed handler article's gap check.
- Keep
expected, the number of the next message the decoder expects. It is unset until the first packet with a complete header. - After each
PACKETline, compare the packet'sseqwithexpected:
| Case | Prints after PACKET | Then |
|---|---|---|
expected unset, or seq == expected | nothing | decode the messages and set expected = seq + count |
seq > expected | GAP expected seq-1 | decode the messages and set expected = seq + count |
seq < expected | STALE | decode nothing and leave expected unchanged |
GAP first lastnames the inclusive range of message numbers that never arrived.- A stale packet carries messages the decoder has already moved past. Either it decoded them before, because feeds resend packets and a network can deliver one packet twice, or they fell inside a gap it already reported, which recovery refetches separately.
- In the tests, a stale packet never carries a message numbered
expectedor higher. - A stale packet is not decoded, so it never prints
TRUNCATEDorSKIP. - A truncated packet still sets
expected = seq + countfrom its header:TRUNCATEDalready reported the lost messages, and the next packet continues after them. - A packet shorter than 6 bytes has no
seq, so it leavesexpectedunchanged. - Heartbeats follow the same rules, so a heartbeat whose
seqis aboveexpectedreports a gap even though it carries no messages.
Tests 1–6 and 11 send packets in order with no gaps or repeats, so Part 3 adds nothing to their output. Tests 7–10 cover this part.
2 0200e9030000011900591b00000000000000434a00002c0100004141504c202020200119005a1b00000000000001474a0000c80000004141504c20202020 0200eb030000020c00591b000000000000640000000308005a1b000000000000
PACKET 1001 2 ADD 7001 B 19011 300 AAPL ADD 7002 S 19015 200 AAPL PACKET 1003 2 EXEC 7001 100 DEL 7002
The first packet's header is 02 00 (count 2) and e9 03 00 00, which is 0x000003e9 = 1001. The first message starts at offset 6 with type 01 and length 19 00 = 25, an Add Order.
Its body reads id 59 1b 00 00 00 00 00 00 = 0x1b59 = 7001, side 00 = B, and price 43 4a 00 00 = 0x4a43 = 19011.
Then come qty 2c 01 00 00 = 0x012c = 300 and symbol 41 41 50 4c 20 20 20 20, which is "AAPL" and four spaces of padding.
The second message starts at 6 + 3 + 25 = 34: id 7002, side 01 = S, price 0x4a47 = 19015, qty 0xc8 = 200.
The second packet has count 2 and seq 0x3eb = 1003, then an Execute (type 02, length 0c 00 = 12) of 0x64 = 100 on order 7001 and a Delete (type 03, length 08 00 = 8) of order 7002.