A crypto-secure Reliable-UDP library for Golang with FEC support.
翻译准备中
开发工具GitHub @xtaci自行安装运营者未确认
试用过了吗? 点一下就好,无需登录。
尚未收集仓库信息。
尚未检测到更新。GitHub 发布版本后会显示在这里。
制作者的介绍仅有上面的一句话。 更多详情请阅读仓库 README。 仓库 README ↗
正在加载完整 README。
查看完整 README ↗kcp-go is a Reliable-UDP library for golang.
This library provides a reliable, ordered byte-stream transport over UDP. Optional FEC and packet encryption are available. Encryption can hide packet contents, but random per-packet nonces do not make network traffic anonymous: endpoints, packet sizes, timing, and traffic volume may still be observable. It has been deployed across a range of devices and applications, including online games, live broadcasting, file synchronization, and network acceleration.
kcp-go implements net.Conn and net.Listener, so applications can use familiar stream I/O. These standalone examples show a basic echo server and client; they use no packet encryption or FEC.
Echo server:
package main
import (
"io"
"log"
"net"
"github.com/xtaci/kcp-go/v5"
)
func main() {
ln, err := kcp.Listen("127.0.0.1:12345")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go func(conn net.Conn) {
defer conn.Close()
if _, err := io.Copy(conn, conn); err != nil {
log.Println(err)
}
}(conn)
}
}
Echo client:
package main
import (
"fmt"
"io"
"log"
"github.com/xtaci/kcp-go/v5"
)
func main() {
conn, err := kcp.Dial("127.0.0.1:12345")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
payload := []byte("hello over KCP")
if _, err := conn.Write(payload); err != nil {
log.Fatal(err)
}
reply := make([]byte, len(payload))
if _, err := io.ReadFull(conn, reply); err != nil {
log.Fatal(err)
}
fmt.Println(string(reply))
}
For optional packet encryption or FEC, use ListenWithOptions and DialWithOptions; both peers must use matching encryption keys and FEC shard settings. See Godoc for configuration details.
net.Conn and net.Listener APIs; the familiar interface does not make the transport TCP.go build -tags kcp_no_snmp ./...; in this build, Snmp is an empty placeholder and its statistics fields and reporting methods are unavailable.For complete documentation, see the associated Godoc.
The KCP flush path scans the in-flight send buffer to find segments that need transmission or retransmission. The ring-buffer representation supports sequential traversal with good locality; the scan cost depends on the number of in-flight segments and how often the session is updated.
I wrote a benchmark comparing sequential loops through a slice and a container/list here:
BenchmarkLoopSlice-4 2000000000 0.39 ns/op
BenchmarkLoopList-4 100000000 54.6 ns/op
The list structure introduces heavy cache misses compared to the slice, which offers better locality. In the linked benchmark, for 5,000 connections with a 32-window size and a 20 ms update interval, using a slice costs 6 μs (0.03% CPU) per kcp.flush(), whereas using a list costs 8.7 ms (43.5% CPU). These figures are benchmark-specific; actual cost depends on workload and hardware.
Timing is critical for the RTT estimator. Inaccurate timing can cause premature or delayed retransmissions. KCP uses currentMs(), which derives elapsed monotonic time from a package reference point. The time.Now() numbers below are measurements from a historical benchmark on the listed hardware, not portable performance guarantees.
The benchmark for time.Now() is here:
以上为 README 的开头部分。其余内容请阅读原文。 查看完整 README ↗