Episode 46
The Soup with a Byte Count
Duck gets text in pieces so needs to wait for the whole text before serving it. The toy prepends a length digit+colon to the text. The length digit lets you know the number of letter bytes in the text body. E.g. 3: indicates that the text body has 3 letter bytes.
















Episode transcript
A broken menu delivery
Saved bytes 3:S Question mark on output.
Diagram values: TOY ASCII PROTOCOL; FIRST READ 3:S
Duck: First read 3:S Can I serve it already?
Robot splits out read-chunk bracket from incomplete message bracket.
Diagram values: READ CHUNK != MESSAGE
Robot: One read chunk isn't always a complete message.
Header 3 is displayed prior to the letter S. There are two empty body slots. Digit 3 is linked to three body slots by an arrow. Header digit + colon are outside the body slots.
Diagram values: FRAME = DIGIT : PAYLOAD; DIGIT 0..9; LENGTH COUNTS PAYLOAD BYTES ONLY
Duck: Read one ascii digit as size of payload.
A duck perplexed with a bowl with an S card in it. Unfinished payload (empty dotted places).
Diagram values: EXPECT3; HAVE1: S; MISSING ?
Duck: Missing how many payload bytes?
Wait for the whole bowl
Duck is hanging around next to unfinished buffer. Output tray is empty.
Diagram values: MISSING2; OUTPUT EMPTY
Duck: We've still got a pair of bytes to go.
Robot adds UN2: onto saved bytes, so it becomes 3:SUN2:.
Diagram values: NEXT READ UN2:; BUFFER 3:SUN2:
Robot: Next read: UN2: And add it to the saved bytes.
Duck takes whole 3:SUN frame and outputs SUN. 2: bytes still in the buffer.
Diagram values: EMIT SUN; REMAINDER 2:
Duck: S,U and N cover three payload slots. First frame is done.
Robot waits for 2 byte payload for next header 2: Emitted SUN remains isolated.
Diagram values: EXPECT2; HAVE0; BUFFER2:
Robot: Keep the 2: below - it's waiting for the 2 payload bytes to arrive.
Two messages, three reads
Duck adds last read OK... making the buffered frame 2:OK.
Diagram values: NEXT READ OK; BUFFER2:OK
Duck: Last read is OK. so we have the second frame filled.
Robot puts OK behind SUN output tray. Receive buffer is empty.
Diagram values: EMIT OK; OUTPUT SUN | OK; BUFFER EMPTY
Robot: We have OK from the complete frame. No buffered bytes left.
Duck compares the read chunks. We have three of them and two full messages.
Diagram values: READS 3:S / UN2: / OK; STREAM3:SUN2:OK; MESSAGES SUN / OK
Duck: We got 2 messages. The boundaries were different. We read it 3 times.
Robot points at toy framing rule that's next to byte stream pipe.
Diagram values: ONE ASCII DIGIT; MAX PAYLOAD9; REQUIRED COLON
Robot: Here's our mini application protocol. TCP doesn't provide these message cuts.
A fresh little stream
The new receiver's buffer is empty. The read cards are 1:, A2:B, C.
Diagram values: PRACTICE READ1 1:; READ2 A2:B; READ3 C
Duck: Fresh stream. It comes 1: then A2:B then C. After each?
First read leaves in buffer: 1: no message. Second read: emits A, leaves in buffer 2:B.
Diagram values: AFTER1 BUFFER1: OUTPUTNONE; AFTER2 BUFFER2:B OUTPUTA
Robot: After 1: wait, then after A2:B, emit A and retain 2:B.
Appending C is sufficient for completing 2:BC. Duck appends BC after A to the output tray and clears the buffer.
Diagram values: AFTER3 OUTPUT A | BC; BUFFER EMPTY
Duck: C completes 2-byte BC payload. Buffer is empty.
Robot is pointing to the wait-and-recheck mechanism of the receiver.
Diagram values: WAIT FOR COMPLETE FRAME; RECHECK REMAINDER
Robot: Output only complete frames.
The lesson
Diagram values: SAME NINE-BYTE STREAM, SINGLE READ; INITIAL BUFFER = EMPTY; READ 1 = "3:SUN2:OK" (9 ASCII bytes); FRAME 1 = "3:SUN"; EMIT "SUN"; REMAINDER = "2:OK"; RECHECK SAME BUFFER; FRAME 2 = "2:OK"; EMIT "OK"; REMAINDER = EMPTY; OUTPUT = "SUN", THEN "OK"; READ COUNT = 1; NO SECOND READ
TCP is an ordered byte stream. It doesn't keep the message boundaries of the application.
A single read may return part of a message or multiple messages.
So we need some application rule to know where complete messages are. In our made up ascii rule, we have some decimal number digit 0..9, then a colon then that number of payload bytes.
Only payload bytes count in the number value. The digit and colon constitute 2 bytes of header.
We buffer up the bytes as we get them. After adding in a read, we look at the first header. If we're missing the header or the promised payload, then we just keep them around and wait. If we see a complete frame, we pull out the frame and write out the payload. Then look again at what's left. In our story, 3:S is incomplete. If we add on UN2: we get 3:SUN2:. So we write out SUN and hold onto 2:. If we add OK onto it to get 2:OK, then we write out OK and the buffer is empty. The byte stream could be divided up differently for different reads. Still we'll figure out we have the same two messages using the same framing rule. This is a teaching protocol, where we just do a single ascii length digit and up to 9 payload bytes. For a real receiver, we'd have to have rules for bad input, resource constraints, and if we get cut off in the middle of a frame when the connection is ending.
Try it
Diagram values: READ 1 = "2:H"; READ 2 = "I1"; READ 3 = ":Z"
Same toy framing rule (One ascii digit for number of payload bytes, then colon then payload) Buffer is initially empty
After each read, what new messages are possible to emit? What's the remaining contents of the buffer?
Answer
2:H Don't emit anything. Keep 2:H. Add I1, 2:HI1. Emit HI. Keep incomplete next header 1. Add :Z, 1:Z. Emit Z. No buffered bytes left. Message: HI, then Z.
Sources
RFC 9293: TCP segmentation and application boundaries (opens in a new tab)