|
| 1 | +KCP PROTOCOL SPECIFICATION |
| 2 | + |
| 3 | + |
| 4 | +1. Packet (aka. segment) Structure |
| 5 | + |
| 6 | +KCP has only one kind of segment: both the data and control messages are |
| 7 | +encoded into the same structure and share the same header. |
| 8 | + |
| 9 | +The KCP packet (aka. segment) structure is as following: |
| 10 | + |
| 11 | +0 4 5 6 8 (BYTE) |
| 12 | ++---------------+---+---+-------+ |
| 13 | +| conv |cmd|frg| wnd | |
| 14 | ++---------------+---+---+-------+ 8 |
| 15 | +| ts | sn | |
| 16 | ++---------------+---------------+ 16 |
| 17 | +| una | len | |
| 18 | ++---------------+---------------+ 24 |
| 19 | +| | |
| 20 | +| DATA (optional) | |
| 21 | +| | |
| 22 | ++-------------------------------+ |
| 23 | + |
| 24 | + |
| 25 | +- conv: conversation id (32 bits integer) |
| 26 | + |
| 27 | +The conversation id is used to identify each connection, which will not change |
| 28 | +during the connection life-time. |
| 29 | + |
| 30 | +It is represented by a 32 bits integer which is given at the moment the KCP |
| 31 | +control block (aka. struct ikcpcb, or kcp object) has been created. Each |
| 32 | +packet sent out will carry the conversation id in the first 4 bytes and a |
| 33 | +packet from remote endpoint will not be accepted if it has a different |
| 34 | +conversation id. |
| 35 | + |
| 36 | +The value can be any random number, but in practice, both side between a |
| 37 | +connection will have many KCP objects (or control block) storing in the |
| 38 | +containers like a map or an array. A index is used as the key to look up one |
| 39 | +KCP object from the container. |
| 40 | + |
| 41 | +So, the higher 16 bits of conversation id can be used as caller's index while |
| 42 | +the lower 16 bits can be used as callee's index. KCP will not handle |
| 43 | +handshake, and the index in both side can be decided and exchanged after |
| 44 | +connection establish. |
| 45 | + |
| 46 | +When you receive and accept a remote packet, the local index can be extracted |
| 47 | +from the conversation id and the kcp object which is in charge of this |
| 48 | +connection can be find out from your map or array. |
| 49 | + |
| 50 | + |
| 51 | +- cmd: command |
| 52 | + |
| 53 | +- frg: fragment count |
| 54 | + |
| 55 | +- wnd: window size |
| 56 | + |
| 57 | +- ts: timestamp |
| 58 | + |
| 59 | +- sn: serial number |
| 60 | + |
| 61 | +- una: un-acknowledged serial number |
| 62 | + |
| 63 | + |
| 64 | +# vim: set ts=4 sw=4 tw=0 noet cc=78 wrap textwidth=78 : |
| 65 | + |
0 commit comments