跳到主要内容

95.1、KCP

说明
  1. 使用nugetlinker.kcp,或源码 https://github.com/snltty/linker/tree/master/src/linker.kcp
  2. 以下是简单使用示例
KcpConnection kcpConnection = new KcpConnection(12138, 1500, 8192, 1, 10, 2, 1,
udpSocket, remoteEndPoint, false);

//发送端
await kcpConnection.SendAsync(packet, token).ConfigureAwait(false);

//接收端
kcpConnection.Input(packet);
//接收端独立线程
using IMemoryOwner<byte> owner = MemoryPool<byte>.Shared.Rent(64 * 1024);
while (!cts.IsCancellationRequested)
{
int length = await kcpConnection.ReceiveAsync(owner.Memory, cts.Token).ConfigureAwait(false);
if (length <= 0)
{
return;
}
//[2 length][payload][2 length][payload]
Memory<byte> packets = owner.Memory.Slice(0, length);
}