public void Update() { if (this.IsDisposed) { return; } uint timeNow = this.Service.TimeNow; // 如果还没连接上,发送连接请求 if (!this.IsConnected) { // 20秒没连接上则报错 if (timeNow - this.CreateTime > 10 * 1000) { Log.Error($"kChannel connect timeout: {this.Id} {this.RemoteConn} {timeNow} {this.CreateTime} {this.ChannelType} {this.RemoteAddress}"); this.OnError(ErrorCode.ERR_KcpConnectTimeout); return; } switch (ChannelType) { case ChannelType.Connect: this.Connect(); break; } return; } try { Kcp.KcpUpdate(this.kcp, timeNow); } catch (Exception e) { Log.Error(e); this.OnError(ErrorCode.ERR_SocketError); return; } if (this.kcp != IntPtr.Zero) { uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow); this.Service.AddToUpdateNextTime(nextUpdateTime, this.Id); } }
private void InitKcp() { switch (this.Service.ServiceType) { case ServiceType.Inner: Kcp.KcpNodelay(kcp, 1, 10, 2, 1); Kcp.KcpWndsize(kcp, 1024 * 100, 1024 * 100); Kcp.KcpSetmtu(kcp, 1400); // 默认1400 Kcp.KcpSetminrto(kcp, 10); break; case ServiceType.Outer: Kcp.KcpNodelay(kcp, 1, 10, 2, 1); Kcp.KcpWndsize(kcp, 128, 128); Kcp.KcpSetmtu(kcp, 470); Kcp.KcpSetminrto(kcp, 10); break; } }
private void KcpSend(KcpWaitPacket kcpWaitPacket) { if (this.IsDisposed) { return; } MemoryStream memoryStream = kcpWaitPacket.MemoryStream; if (this.Service.ServiceType == ServiceType.Inner) { memoryStream.GetBuffer().WriteTo(0, kcpWaitPacket.ActorId); } int count = (int)(memoryStream.Length - memoryStream.Position); Kcp.KcpSend(this.kcp, memoryStream.GetBuffer(), (int)memoryStream.Position, count); this.Service.AddToUpdateNextTime(0, this.Id); }
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); }
public override void Dispose() { if (this.IsDisposed) { return; } uint localConn = this.LocalConn; uint remoteConn = this.RemoteConn; Log.Info($"channel dispose: {this.Id} {localConn} {remoteConn}"); kChannels.Remove(localConn); idLocalRemoteConn.TryRemove(this.Id, out ulong _); long id = this.Id; this.Id = 0; this.Service.Remove(id); try { //this.Service.Disconnect(localConn, remoteConn, this.Error, this.RemoteAddress, 3); } catch (Exception e) { Log.Error(e); } if (this.kcp != IntPtr.Zero) { Kcp.KcpRelease(this.kcp); this.kcp = IntPtr.Zero; } this.socket = null; }
static KService() { //Kcp.KcpSetLog(KcpLog); Kcp.KcpSetoutput(KcpOutput); }