tags : Custom Protocols, Systems, System Design
What?
- Codec stands for
code/decode
- It’s a reversible method for converting interesting things into completely uninteresting and useless numbers … and then converting those uninteresting and useless numbers back into interesting things again.
A streaming codec loop
- Protocols wrap protocols. Binary protocols boils down to:
- Getting how many bytes are coming next
- Writing the buffer with
the meaning of the data(meaningless to the protocol itself)
.
- Start by writing two threads,
TcpListener
and the other has aTcpStream
that connects to the listener - Have them send messages back and forth that begin with 8 byte fixed-size
frame delimiters
.- The
frame delimiter
says how many bytes are coming.
- The
- The
codec loop
is trivial- Read 8 bytes, turn it into a
u64
- Read that many bytes into a new buffer that will be interpreted as the actual message.
- Eg.
2 a b 3 a b c 1 a
will turn into 3 messages:ab
,abc
,a
.
- Read 8 bytes, turn it into a
- A
Frame
is essentially(size, message)
, these can be refereed to as “framed protocols”.