Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 packets is one received packet as lowercase hex text, two digits per byte, with the packet's first byte as the line's first two characters: e803 is the two bytes 0xe8 and 0x03.
  • 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 w bytes b0 b1 ... b(w-1) holds the value b0 + b1 * 256 + b2 * 256^2 + ....
  • The 16-bit value 1000 is 0x03e8, sent as the bytes e8 03. The 32-bit value 19011 is 0x00004a43, sent as 43 4a 00 00.
  • u8, u16, u32, and u64 are 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 value v; the field's value is v when v < 2^31 and v - 2^32 otherwise, so fb ff ff ff is -5.

The wire format

A packet starts with a 6-byte header.

OffsetFieldTypeMeaning
0countu16How many messages the packet carries
2sequ32The sequence number of the packet's first message
  • The count messages follow back to back from offset 6.
  • Message k of the packet, counting from 0, has message number seq + k.
  • Each message is a 3-byte message header followed by length bytes of body. It occupies 3 + length bytes, and the next message starts right after its body.
OffsetFieldTypeMeaning
0typeu8Which message this is
1lengthu16How many body bytes follow the message header
3bodylength bytesLaid out according to type

The decoder knows three message types. Body offsets count from the first body byte.

TypeMessageBody fields (offset: name, type)Body sizePrints
1Add Order0: id u64, 8: side u8, 9: price i32, 13: qty u32, 17: symbol 8 bytes25ADD id side price qty symbol
2Execute0: id u64, 8: qty u3212EXEC id qty
3Delete0: id u648DEL id

In an Add Order:

  • side is 0 for a buy and 1 for a sell, printed as B and S.
  • price is 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.
  • symbol is 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

  1. In Part 1 every packet is complete: it holds exactly count messages, every message has type 1, 2, or 3, and every length equals that type's body size.
  2. A packet with a complete header first prints PACKET seq count, then one line per message in order.
  3. A packet whose count is 0 is a heartbeat, which a feed sends when it has nothing to report. It prints only its PACKET line.

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 + count stays below 232.
  • A packet never holds bytes after its count-th message.
  • id values use all 64 bits, and seq and qty values reach above 231, so a signed 32-bit int cannot hold them. Use std::uint64_t for id and unsigned types for seq and qty.

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

  1. A message whose type is not 1, 2, or 3 prints SKIP type, with type in decimal. Decoding continues with the next message, 3 + length bytes after the start of this one.
  2. A length of 0 is valid.
  3. 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 length body bytes.
  4. A known type's length is never smaller than its body size.

Truncation

A packet can also arrive cut short.

  1. Before decoding each of the count messages, check that the packet still holds the whole message.
  2. If fewer than 3 bytes remain, or the 3-byte message header fits but fewer than length bytes follow it, the message is truncated. Print TRUNCATED and stop decoding that packet.
  3. Lines already printed for the packet stay, and the next packet decodes normally.
  4. A packet whose bytes end at a message boundary before count messages falls under the same rule, because 0 bytes remain for the next message.
  5. The check comes before the type is examined, so a truncated message of an unknown type prints TRUNCATED, not SKIP.
  6. 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.

  1. Keep expected, the number of the next message the decoder expects. It is unset until the first packet with a complete header.
  2. After each PACKET line, compare the packet's seq with expected:
CasePrints after PACKETThen
expected unset, or seq == expectednothingdecode the messages and set expected = seq + count
seq > expectedGAP expected seq-1decode the messages and set expected = seq + count
seq < expectedSTALEdecode nothing and leave expected unchanged
  • GAP first last names 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 expected or higher.
  • A stale packet is not decoded, so it never prints TRUNCATED or SKIP.
  • A truncated packet still sets expected = seq + count from its header: TRUNCATED already reported the lost messages, and the next packet continues after them.
  • A packet shorter than 6 bytes has no seq, so it leaves expected unchanged.
  • Heartbeats follow the same rules, so a heartbeat whose seq is above expected reports 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.

Example
Input
2
0200e9030000011900591b00000000000000434a00002c0100004141504c202020200119005a1b00000000000001474a0000c80000004141504c20202020
0200eb030000020c00591b000000000000640000000308005a1b000000000000
Output
PACKET 1001 2
ADD 7001 B 19011 300 AAPL
ADD 7002 S 19015 200 AAPL
PACKET 1003 2
EXEC 7001 100
DEL 7002
Explanation

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.