#466·kcp

[Share] AnLiu: an encrypted, multi-stream, semi-reliable UDP transport inspired by KCP / 基于 KCP 思路的加密多流半可靠传输协议

Author: wanghengwenCreated Oct 1, 2026Updated Oct 1, 2026

Hi, thanks for KCP — it has been the main reference for this project.

I'd like to share AnLiu (暗流), a UDP transport library written in the same spirit as KCP: two files (anliu.h / anliu.c), no external dependencies, no sockets inside (output callback + anl_input), and an application-driven clock.

https://github.com/wanghengwen/AnLiu

It targets real-time audio/video and adds on top of the KCP model:

  • Encryption: whole-datagram authenticated encryption (ChaCha20 + SipHash-2-4, SIV construction) with per-direction keys and random padding; no plaintext protocol fields on the wire.
  • Multiple streams in one connection: open/close from either side, per-stream priority, shared congestion control and ACK packing; reliable streams support half-close.
  • Semi-reliable frames: frames are dropped as a whole when they expire or the backlog grows, with optional key-frame dependency handling.
  • Per-stream Reed-Solomon FEC, fixed or adaptive.
  • BBR-style congestion control with pacing, plus a target_rate for encoders.
  • SACK + RACK loss recovery.

Real-network results are in performance.md, and I'd especially appreciate feedback on them. They cover 600-second rounds over six anonymized servers and side-by-side comparisons with ikcp.c and TCP CUBIC, including the cost side (extra bytes, FEC overhead, retransmissions). For example, on a 200 ms RTT path behind a 5 Mbit TBF with 10% added loss, audio/video on-time delivery was 100% / 99.9% for AnLiu versus 57% / 82% for ikcp (reliable mode), at about 1.5 Mbps vs 1.15 Mbps sent. Behind a 2.4 Mbit policer, 240/240 key frames arrived on time with AnLiu versus 127/240 with ikcp. These ikcp comparisons used an earlier development version under the stated conditions, and the report lists the limits of each result. Test tools are in bench/; KCP's MIT license is kept in bench/LICENSE-ikcp.

Feedback is very welcome. If it's not appropriate to post this here, feel free to close the issue.


想分享一下 AnLiu(暗流):一个按 KCP 思路实现的 UDP 传输库,同样只有 anliu.h / anliu.c 两个文件,不依赖外部库,不直接操作 socket(output 回调 + anl_input),时钟由应用驱动。

https://github.com/wanghengwen/AnLiu

它面向实时音视频,在 KCP 的模型上增加了:

  • 加密:整包认证加密(ChaCha20 + SipHash-2-4 的 SIV 构造),按方向派生密钥,随机填充,线上没有明文协议字段;
  • 多流:一条连接内多个流,两端都可随时开关,按优先级调度,共享拥塞控制与 ACK;可靠流支持半关闭;
  • 半可靠帧传输:过期或积压时整帧丢弃,可选关键帧依赖处理;
  • 按流 Reed-Solomon FEC,支持固定或自适应冗余;
  • BBR 思路的拥塞控制与 pacing,并提供给编码器的 target_rate;
  • SACK + RACK 丢包恢复。

真实网络测试数据见 performance.md,也特别希望大家对这部分数据提意见。报告包含六台匿名服务器上每轮 600 秒的实测,以及与原版 ikcp.c、TCP CUBIC 的同条件对照,也列出了代价(额外发送字节、FEC 开销、重传)。例如在 RTT 200 ms、5 Mbit TBF、额外 10% 丢包的路径上,AnLiu 的音频 / 视频及时率为 100% / 99.9%,ikcp(可靠模式)为 57% / 82%,发送量约 1.5 Mbps 对 1.15 Mbps;在 2.4 Mbit policer 下,关键帧及时数为 240/240 对 127/240。这些与 ikcp 的对照来自较早的开发版本和报告中列出的条件,每项结果的适用范围报告里都有说明。测试工具在 bench/,KCP 的 MIT 许可保留在 bench/LICENSE-ikcp。

欢迎反馈。如果这里不适合发这类内容,请直接关闭,谢谢。