mKCP Protocol

mKCP is a streaming transport protocol modified from the KCPopen in new window protocol, it can transfer any data stream in order.

Version

mKCP has no version number, compatibility between versions are not guaranteed.

Dependencies

Base Protocol

mKCP is based on the UDP protocol, all communications use UDP.

Functions

  • fnv: FNV-1aopen in new window hash function
    • Input is a string with variable length;
    • Output is an unsigned 32-bit integer;

Communcation Procedure

  1. mKCP splits data stream into many small packets. Each data stream has a unique identifier for classification. Each packet has the same unique indentifier with its parent data stream.
  2. mKCP has no handshake procedure. When a packet is received, its data stream unique identifier will be used to determine whether it is a new connection or a existing connection.
  3. Each data packet includes many segments. There are three types of segment: data, acknowledge (ACK) and ping. Each segment needs to be processed individually.

Data Format

Data Packet

4 Bytes2 BytesL Bytes
Authentication Info ALength LSegments Section S

其中:

  • A = fnv(S), big endian;
  • Segment sections may include multiple segments;

Data Segment

2 Bytes1 Bytes1 Bytes4 Bytes4 Bytes4 Bytes2 BytesL Bytes
Identifier IdCommand CmdOptions OptTimestamp TsSerial Number SnUnconfirmed Serial Number UsnLength LData

Definition:

  • Identifier Id: mKCP data stream identifier
  • Command Cmd: constant 0x01
  • Options Opt: values:
    • 0x00: empty
    • 0x01: all data transfered
  • Timestamp Ts: time when the packet is sent at remote,big endian
  • Serial Number Sn: the position of the segment in the data stream, the serial number of the initial segment is 0, increase 1 on each segment afterwards
  • Unconfirmed Serial Number Una: smallest unconfirmed Serial Number remote host is currently sending

ACK Segment

2 Bytes1 Bytes1 Bytes4 Bytes4 Bytes4 Bytes2 BytesL * 4 Bytes
Identifier IdCommand CmdOptions OptWindow WndNext Serial Number SnTimestamp TsLength LReceived Serial Numbers

Definition:

  • Identifier Conv: mKCP data stream identifier
  • Command Cmd: constant 0x00
  • Options Opt: same as above
  • Window Wnd: largest acceptable serial number of the remote host
  • Next Serial Number Sn: smallest serial number that remote host hasn't receive.
  • Timestamp Ts: timestamp of the newest segment remote host received, can be used to calculate latency
  • Received Serial Numbers: each with length of 4 bytes, implies that the cooresponding data of that Serial Number is received

P.S.:

  • Remote host looks forward to receive data within the range [Sn, Wnd)

Ping Segment

2 Bytes1 Bytes1 Bytes4 Bytes4 Bytes4 Bytes
Identifier ConvCommand CmdOptions OptUnconfirmed Serial Number UnaNext Serial Number SnLatency Rto

Definition:

  • Identifier Conv: mKCP data stream identifier
  • Command Cmd: values:
    • 0x02: connection forcibly closed by the remote host
    • 0x03: normal ping
  • Options Opt: same as above
  • Unconfirmed Serial Number Una: Una of the same segment
  • Next Serial Number Sn: Sn of the same segment
  • Latency Rto: latency calculated by the remote host