TCP Stream Framing
Prerequisites: Socket Programming Basics, TCP vs UDP Trade-Offs, Market Data Feed Handler Shape
Your trading program sends its AAPL orders to the exchange over a TCP connection. The exchange answers on the same connection with one message per event:
- Confirmation: it accepted an order.
- Fill: part of an order traded.
- Reject: it refused an order.
TCP delivers those answers as one continuous byte stream. As Socket Programming Basics showed, one recv is not one message: a read can stop mid-message, even mid-header, or carry several messages back to back.
Write the receive side, which keeps bytes from one read to the next and cuts the stream back into messages.
What to implement
process_stream(max_length, events)returns the printed lines in order.max_lengthis the largest payload length the protocol allows (Part 2).- Each event is one line: either the bytes a single
recvreturned, as lowercase hexadecimal with two digits per byte (0500is the two bytes0x05and0x00), or the wordSTATUS(Part 3). Every read holds at least one byte. - On stdin, the first line is
max_length, the second is the number of events, and each remaining line is one event. - There are three parts, each building on the previous ones. One solution must pass all eleven tests.
| Printed line | When | Part |
|---|---|---|
MSG length payload | a message's last byte has arrived | 1 |
BAD_LENGTH n | a header's length is greater than max_length | 2 |
STATUS retained moved | a STATUS event, while the connection is open | 3 |
Message format
- Every message is a 2-byte header followed by a payload.
- The header holds the payload length as an unsigned 16-bit integer in little-endian byte order, low byte first: the bytes
2c 01mean0x2c + 0x01 * 256 = 300. - The payload is exactly that many bytes, so a length can be anything from 0 to 65535.
- The framing code never looks inside a payload. Test payloads are short ASCII strings such as
ACK 1only so a person can read them.
Part 1: Frame the stream
- Treat the reads as one byte stream: each read's bytes follow the previous read's.
- When a read arrives, append its bytes to the bytes kept from earlier reads, then print every message that is now complete, in stream order.
- A message is complete once its 2 header bytes and all
lengthpayload bytes have arrived. - Bytes that do not yet form a complete message are kept for the next read.
- The header itself can be split: if a read ends after the first header byte, the length stays unknown until the next read supplies the second.
- If the stream ends in the middle of a message, that message is never printed.
Output for each complete message:
MSG length payload
lengthis the payload length in decimal.payloadis the payload bytes in order as lowercase hex, two digits per byte.
Limits
- At most 104 events.
- Each read holds between 1 and 4096 bytes, and all reads together hold at most 106 bytes.
max_lengthis between 0 and 65535.
Tests 1–3 cover this part. Their max_length is 65535, so no length is too long. Every payload in them has at least one byte, and they contain no STATUS events.
Part 2: Bad lengths and empty messages
A length greater than max_length means the stream is corrupt or the two ends disagree about the protocol. On a byte stream every later boundary would be computed from the bad one, so the receiver gives up on the connection.
- As soon as both bytes of such a header have arrived, print
BAD_LENGTH n, wherenis the decoded length in decimal. - Then close the connection: ignore the rest of that read and every later event, and print nothing more.
- The check needs only the header, so
BAD_LENGTHis printed even if no payload byte ever arrives. - A length equal to
max_lengthis allowed. - A length of 0 is valid. Such a message has an empty payload and prints
MSG 0.
The protocol uses these empty messages as heartbeats: messages that carry nothing and only show that the connection is still alive.
Tests 4–6 cover this part. They contain no STATUS events.
Part 3: Buffer compaction
The receiver keeps the unconsumed bytes in one buffer and reads it with a cursor, the index of the first byte of the next message.
Erasing each message from the front as soon as it prints would copy every byte behind it, once per message. Instead, each read follows three steps:
- Append the read's bytes to the end of the buffer and set the cursor to 0.
- Print messages starting at the cursor, moving the cursor past each one, until the bytes after the cursor do not form a complete message.
- Compact the buffer once. If the cursor moved, copy the bytes after the cursor (the start of an unfinished message, possibly none) to the front of the buffer and drop everything before them. If the cursor did not move, copy nothing, because those bytes are already at the front.
Output of a STATUS event:
STATUS retained moved
retainedis the number of bytes in the buffer, which is the start of a message that is not complete yet.movedis the total number of bytes compaction has copied since the first read. A compaction with nothing after the cursor adds 0.- Before any read,
STATUSprintsSTATUS 0 0. After the connection has been closed,STATUSprints nothing.
Tests 7–11 cover this part.
65535 5 050041434b2031 050041434b20320a0046494c4c203120313030 080052454a45 4354203309 0046494c4c2032203530
MSG 5 41434b2031 MSG 5 41434b2032 MSG 10 46494c4c203120313030 MSG 8 52454a4543542033 MSG 9 46494c4c2032203530
The first line is max_length and the second is the number of events. The first read is exactly one message: header 05 00 is length 5, and the payload 41 43 4b 20 31 is the ASCII text "ACK 1".
The second read carries two messages back to back, "ACK 2" and the 10-byte "FILL 1 100" (header 0a 00), so it prints two lines.
The third read holds the header 08 00 and only 4 of the 8 payload bytes, so nothing prints and all 6 bytes are kept.
The fourth read supplies the last 4 payload bytes, completing "REJECT 3", then one more byte, 09. That byte is only the low half of the next header, so the length is still unknown and the byte is kept.
The fifth read starts with the high byte 00, making the length 9, followed by the 9 payload bytes of "FILL 2 50".