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

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_length is the largest payload length the protocol allows (Part 2).
  • Each event is one line: either the bytes a single recv returned, as lowercase hexadecimal with two digits per byte (0500 is the two bytes 0x05 and 0x00), or the word STATUS (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 lineWhenPart
MSG length payloada message's last byte has arrived1
BAD_LENGTH na header's length is greater than max_length2
STATUS retained moveda STATUS event, while the connection is open3

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 01 mean 0x2c + 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 1 only so a person can read them.

Part 1: Frame the stream

  1. Treat the reads as one byte stream: each read's bytes follow the previous read's.
  2. 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.
  3. A message is complete once its 2 header bytes and all length payload bytes have arrived.
  4. Bytes that do not yet form a complete message are kept for the next read.
  5. 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.
  6. If the stream ends in the middle of a message, that message is never printed.

Output for each complete message:

MSG length payload
  • length is the payload length in decimal.
  • payload is 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_length is 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.

  1. As soon as both bytes of such a header have arrived, print BAD_LENGTH n, where n is the decoded length in decimal.
  2. Then close the connection: ignore the rest of that read and every later event, and print nothing more.
  3. The check needs only the header, so BAD_LENGTH is printed even if no payload byte ever arrives.
  4. A length equal to max_length is allowed.
  5. 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:

  1. Append the read's bytes to the end of the buffer and set the cursor to 0.
  2. Print messages starting at the cursor, moving the cursor past each one, until the bytes after the cursor do not form a complete message.
  3. 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
  • retained is the number of bytes in the buffer, which is the start of a message that is not complete yet.
  • moved is the total number of bytes compaction has copied since the first read. A compaction with nothing after the cursor adds 0.
  • Before any read, STATUS prints STATUS 0 0. After the connection has been closed, STATUS prints nothing.

Tests 7–11 cover this part.

Example
Input
65535
5
050041434b2031
050041434b20320a0046494c4c203120313030
080052454a45
4354203309
0046494c4c2032203530
Output
MSG 5 41434b2031
MSG 5 41434b2032
MSG 10 46494c4c203120313030
MSG 8 52454a4543542033
MSG 9 46494c4c2032203530
Explanation

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".