using System.Buffers; using System.Threading.Tasks; using BufferOwner = System.Buffers.IMemoryOwner; namespace System.Net.Sockets.Kcp { /// /// Kcp回调 /// public interface IKcpCallback { /// /// kcp 发送方向输出 /// /// kcp 交出发送缓冲区控制权,缓冲区来自 /// 数据的有效长度 /// 不需要返回值 /// 通过增加 avalidLength 能够在协议栈中有效的减少数据拷贝 void Output(BufferOwner buffer, int avalidLength); } /// /// Kcp回调 /// /// /// 失败设计,。IMemoryOwner是没有办法代替的。 /// 这里只相当于把 IKcpCallback 和 IRentable 和并。 /// public interface IKcpOutputWriter : IBufferWriter { int UnflushedBytes { get; } void Flush(); } /// /// 外部提供缓冲区,可以在外部链接一个内存池 /// public interface IRentable { /// /// 外部提供缓冲区,可以在外部链接一个内存池 /// BufferOwner RentBuffer(int length); } public interface IKcpSetting { int Interval(int interval); /// /// fastest: ikcp_nodelay(kcp, 1, 20, 2, 1) /// /// 0:disable(default), 1:enable /// internal update timer interval in millisec, default is 100ms /// 0:disable fast resend(default), 1:enable fast resend /// 0:normal congestion control(default), 1:disable congestion control /// int NoDelay(int nodelay, int interval, int resend, int nc); /// /// change MTU size, default is 1400 /// ** 这个方法不是线程安全的。请在没有发送和接收时调用 。 /// /// /// /// /// 如果没有必要,不要修改Mtu。过小的Mtu会导致分片数大于接收窗口,造成kcp阻塞冻结。 /// int SetMtu(int mtu = 1400); /// /// set maximum window size: sndwnd=32, rcvwnd=128 by default /// /// /// /// /// /// 如果没有必要请不要修改。注意确保接收窗口必须大于最大分片数。 /// int WndSize(int sndwnd = 32, int rcvwnd = 128); } public interface IKcpUpdate { void Update(in DateTimeOffset time); } public interface IKcpSendable { /// /// 将要发送到网络的数据Send到kcp协议中 /// /// /// int Send(ReadOnlySpan span, object options = null); /// /// 将要发送到网络的数据Send到kcp协议中 /// /// /// int Send(ReadOnlySequence span, object options = null); } public interface IKcpInputable { /// /// 下层收到数据后添加到kcp协议中 /// /// int Input(ReadOnlySpan span); /// /// 下层收到数据后添加到kcp协议中 /// /// int Input(ReadOnlySequence span); } /// /// kcp协议输入输出标准接口 /// public interface IKcpIO : IKcpSendable, IKcpInputable { /// /// 从kcp中取出一个整合完毕的数据包 /// /// ValueTask RecvAsync(IBufferWriter writer, object options = null); /// /// 从kcp中取出一个整合完毕的数据包 /// /// /// /// 接收数据长度 ValueTask RecvAsync(ArraySegment buffer, object options = null); /// /// 从kcp协议中取出需要发送到网络的数据。 /// /// /// /// ValueTask OutputAsync(IBufferWriter writer, object options = null); } }