93 lines
4.0 KiB
Markdown
Raw Normal View History

# mKCP Protocol
2021-05-26 19:05:53 +08:00
mKCP is a stream transfer protocol, modified from the [KCP protocol](https://github.com/skywind3000/kcp), which can transmit any data stream in order.
2021-05-26 19:05:53 +08:00
## Version
2021-05-26 19:05:53 +08:00
mKCP has no version number and does not guarantee compatibility between versions.
2021-05-26 19:05:53 +08:00
## Dependencies
2021-05-26 19:05:53 +08:00
### Underlying Protocol
2021-05-26 19:05:53 +08:00
mKCP is a protocol based on UDP, and all communication uses UDP transmission.
2021-05-26 19:05:53 +08:00
### Functions
2021-05-26 19:05:53 +08:00
- fnv: [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) hash function
- Takes a string of arbitrary length as input parameter;
- Outputs a 32-bit unsigned integer.
2021-05-26 19:05:53 +08:00
## Communication Process
2021-05-26 19:05:53 +08:00
1. mKCP splits data streams into several data packets for transmission. Each data stream has a unique identifier to distinguish it from other data streams. Each data packet in the data stream carries the same identifier.
2. mKCP does not have a handshake process. When receiving a data packet, it determines whether it is a new call or an ongoing call based on the identifier of the data stream it carries.
3. Each data packet contains several segments (Segment), which are divided into three types: data (Data), acknowledgment (ACK), and heartbeat (Ping). Each segment needs to be processed separately.
2021-05-26 19:05:53 +08:00
## Data Format
2021-05-26 19:05:53 +08:00
### Data Packet
2021-05-26 19:05:53 +08:00
| 4 Bytes | 2 Bytes | L Bytes |
| ------- | ---------- | -------- |
| Auth A | Data Len L | Fragment |
2021-05-26 19:05:53 +08:00
as which:
2021-05-26 19:05:53 +08:00
- Authentication information A = fnv(fragment), big endian;
- The fragment may contain multiple sections.
2021-05-26 19:05:53 +08:00
### Data snippet
2021-05-26 19:05:53 +08:00
| 2 bytes | 1 byte | 1 byte | 4 bytes | 4 bytes | 4 bytes | 2 bytes | Len bytes |
| --------- | -------- | -------- | --------- | -------- | -------------- | -------- | --------- |
| Conv flag | Cmd flag | Opt flag | Timestamp | Sequence | Unacknowledged | Len flag | Data |
2021-05-26 19:05:53 +08:00
as which:
2021-05-26 19:05:53 +08:00
- Identifier Conv: Identifier for mKCP data stream
- Command Cmd: Constant 0x01
- Option Opt: Optional values include:
- 0x00: Empty option
- 0x01: Opposite party has sent all data
- Timestamp Ts: Time when the current segment was sent from the remote end, big endian
- Sequence Number Sn: The position of the data segment in the data stream, the sequence number of the starting segment is 0, and each new segment is sequentially added by 1
- Unacknowledged Sequence Number Una: The minimum Sn that the remote host is sending and has not yet received confirmation.
2021-05-26 19:05:53 +08:00
### Confirmation snippet
2021-05-26 19:05:53 +08:00
| 2 bytes | 1 byte | 1 byte | 4 bytes | 4 bytes | 4 bytes | 2 bytes | Len \* 4 bytes |
| ------- | ------ | ------ | ------- | --------------- | --------- | ------- | ------------------- |
| Conv ID | Cmd | Opt | Wnd | Next Seq Number | Timestamp | Length | Received Seq Number |
2021-05-26 19:05:53 +08:00
as which:
2021-05-26 19:05:53 +08:00
- Identifier Conv: Identifier of the mKCP data stream
- Command Cmd: Constant 0x00
- Option Opt: Same as above
- Window Wnd: The maximum sequence number that the remote host can receive
- Next receive sequence number Sn: The smallest sequence number of the data segment that the remote host has not received
- Timestamp Ts: The timestamp of the latest received data segment by the remote host, which can be used to calculate the delay
- Received sequence numbers: Each 4 bytes, indicating that the data of this sequence number has been confirmed received.
2021-05-26 19:05:53 +08:00
as which:
2021-05-26 19:05:53 +08:00
- The remote host expects to receive data within the serial number [Sn, Wnd) range.
2021-05-26 19:05:53 +08:00
### Heartbeat Fragments
2021-05-26 19:05:53 +08:00
| 2 Bytes | 1 Byte | 1 Byte | 4 Bytes | 4 Bytes | 4 Bytes |
| ------- | ------ | ------ | --------------------- | ------------------- | ------- |
| Conv ID | Cmd | Opt | Unacknowledged Seq No | Next Receive Seq No | Rto |
2021-05-26 19:05:53 +08:00
as which:
2021-05-26 19:05:53 +08:00
- Identifier Conv: Identifier for the mKCP data stream
- Command Cmd: Optional values include:
- 0x02: Remote host forcibly terminates the session
- 0x03: Normal heartbeat
- Option Opt: Same as above
- Unacknowledged sequence number Una: Same as the Una of the data fragment
- Next receive sequence number Sn: Same as the Sn of the acknowledgement fragment
- Delay Rto: Delay calculated by the remote host itself