public override void Send(MemoryStream stream) { if (this.kcp != IntPtr.Zero) { // 检查等待发送的消息,如果超出两倍窗口大小,应该断开连接 if (Kcp.KcpWaitsnd(this.kcp) > 256 * 2) { this.OnError(ErrorCode.ERR_KcpWaitSendSizeTooLarge); return; } } ushort size = (ushort)(stream.Length - stream.Position); byte[] bytes; if (this.isConnected) { bytes = stream.GetBuffer(); } else { bytes = new byte[size]; Array.Copy(stream.GetBuffer(), stream.Position, bytes, 0, size); } Send(bytes, 0, size); }
public void Send(long actorId, MemoryStream stream) { if (this.kcp != IntPtr.Zero) { // 检查等待发送的消息,如果超出最大等待大小,应该断开连接 int n = Kcp.KcpWaitsnd(this.kcp); int maxWaitSize = 0; switch (this.Service.ServiceType) { case ServiceType.Inner: maxWaitSize = Kcp.InnerMaxWaitSize; break; case ServiceType.Outer: maxWaitSize = Kcp.OuterMaxWaitSize; break; default: throw new ArgumentOutOfRangeException(); } if (n > maxWaitSize) { Log.Error($"kcp wait snd too large: {n}: {this.Id} {this.RemoteConn}"); this.OnError(ErrorCode.ERR_KcpWaitSendSizeTooLarge); return; } } KcpWaitPacket kcpWaitPacket = new KcpWaitPacket() { ActorId = actorId, MemoryStream = stream }; if (!this.IsConnected) { this.sendBuffer.Enqueue(kcpWaitPacket); return; } this.KcpSend(kcpWaitPacket); }