Skip to content

13 Zinx Kcp

刘丹冰 edited this page Apr 29, 2024 · 1 revision

1. Introduction to KCP

The following is from the official website: KCP is a fast and reliable protocol that can achieve a transmission effect with an average delay reduction of 30%-40% and a maximum delay reduction of three times at the cost of wasting 10%-20% more bandwidth than TCP. It is purely algorithmically implemented and is not responsible for the sending and receiving of underlying protocols (such as UDP). Users need to define the sending method of lower-layer data themselves and provide it to KCP in a callback manner. Even the clock needs to be passed in externally, and there will be no system calls internally. TCP is designed for throughput (how much KB of data can be transmitted per second), focusing on fully utilizing bandwidth. KCP, on the other hand, is designed for flow rate (how long it takes for a single piece of data to be sent from one end to the other), achieving a transmission speed 30%-40% faster than TCP at the cost of wasting 10%-20% bandwidth.

2. Applicable Scenarios

    As stated above, KCP sacrifices bandwidth to reduce latency, making it very suitable for use in games with frequent operations like MOBA, combined with frame synchronization.

3. Dependencies

    It uses [https://github.com/xtaci/kcp-go](https://github.com/xtaci/kcp-go), a GO language implementation of KCP library.

4. How to Use

    Set **Mode** in the configuration file to **"kcp"**. **KcpPort** is the port number.
{
  "Name":"zinx v-0.10 demoApp",
  "Host":"127.0.0.1",
  "KcpPort":7777,  
  "MaxConn":3,
  "WorkerPoolSize":10,
  "LogDir": "./mylog",
  "LogFile":"zinx.log",
  "Mode":"kcp"   
}

5. Support for KCP Conn Settings

Add the following configuration items. The meanings of the configurations can be referred to at

https://github.com/skywind3000/kcp/blob/master/README.en.md#protocol-configuration

KcpACKNoDelay bool // changes ack flush option, set true to flush ack immediately,
KcpStreamMode bool // toggles the stream mode on/off
KcpNoDelay    int  // Whether nodelay mode is enabled, 0 is not enabled; 1 enabled.
KcpInterval   int  // Protocol internal work interval, in milliseconds, such as 10 ms or 20 ms.
KcpResend     int  // Fast retransmission mode, 0 represents off by default, 2 can be set (2 ACK spans will result in direct retransmission)
KcpNc         int  // Whether to turn off flow control, 0 represents “Do not turn off” by default, 1 represents “Turn off”.
KcpSendWindow int  // SND_BUF, this unit is the packet, default 32.
KcpRecvWindow int  // RCV_BUF, this unit is the packet, default 32.