This document defines the custom P2P wire protocol used by the Distributed-File-Storage-System.
The protocol uses a Type-Length-Value (TLV) inspired framing system optimized for Go's io.Reader/Writer patterns.
| Byte Offset | Field | Type | Description |
|---|---|---|---|
| 0 | Type Identifier | 1 byte | Indicates the nature of the following payload (Message vs Stream). |
| 1+ | Payload | Body | Either a GOB-encoded message or a raw binary stream. |
To prevent expensive full-packet buffering, we use a single-byte prefix to switch the state of the Decoder.
| Constant | Value | Description |
|---|---|---|
IncomingMessage |
0x1 |
The following payload is a control message. |
IncomingStream |
0x2 |
The following bytes are a raw data stream. |
The DefaultDecoder reads only one byte from the net.Conn.
- If the byte is
IncomingStream, theRPCobject is flagged asStream = trueand returned immediately. This pauses the control loop and allows the binary stream to be piped directly to disk.
If the byte is IncomingMessage, the decoder reads the remaining bytes into a buffer and populates the RPC.Payload. This payload typically contains GOB-encoded structs:
- Used to signify a new file upload to the network.
- Contains:
ID,Key, andSize.
- Used to request a file.
- Contains:
IDandKey.
- Overhead: HTTP headers (User-Agent, Accept, etc.) add significant bloat to small control messages in a P2P network.
- Streaming Control: HTTP/1.1 is strictly Request-Response. Our custom TCP protocol allows for Bidirectional Streaming where a peer can send a control message and immediately follow it with 100GB of raw data without closing the connection or renegotiating headers.
- Simplicity: By working directly with
net.Conn, we leverage Go's internal buffer management more efficiently than high-level web frameworks.