public override void Update() { this.TimeNow = (uint)(TimeHelper.ClientNow() - this.StartTime); this.Recv(); this.CheckWaitTimeout(); this.TimerOut(); foreach (long id in updateChannels) { KChannel kChannel = this.GetKChannel(id); if (kChannel == null) { continue; } if (kChannel.Id == 0) { continue; } kChannel.Update(); } this.updateChannels.Clear(); while (true) { if (this.removedChannels.Count <= 0) { break; } long id = this.removedChannels.Dequeue(); this.localConnChannels.Remove(id); } }
public override void Update() { this.TimeNow = (uint)(TimeHelper.ClientNow() - this.StartTime); this.Recv(); this.TimerOut(); foreach (long id in updateChannels) { KChannel kChannel = this.GetKChannel(id); if (kChannel == null) { continue; } if (kChannel.Id == 0) { continue; } kChannel.Update(); } this.updateChannels.Clear(); while (this.removedChannels.Count > 0) { long id = this.removedChannels.Dequeue(); KChannel channel; if (!this.localConnChannels.TryGetValue(id, out channel)) { continue; } this.localConnChannels.Remove(id); channel.Dispose(); } }
private KChannel CreateAcceptChannel(IPEndPoint remoteEndPoint, uint remoteConn) { KChannel channel = new KChannel(++this.IdGenerater, remoteConn, this.socket, remoteEndPoint, this); KChannel oldChannel; if (this.idChannels.TryGetValue(channel.Id, out oldChannel)) { this.idChannels.Remove(oldChannel.Id); oldChannel.Dispose(); } this.idChannels[channel.Id] = channel; return(channel); }
public override AChannel ConnectChannel(IPEndPoint remoteEndPoint) { uint conv = (uint)RandomHelper.RandomNumber(1000, int.MaxValue); KChannel channel = new KChannel(conv, this.socket, remoteEndPoint, this); KChannel oldChannel; if (this.idChannels.TryGetValue(channel.Id, out oldChannel)) { this.idChannels.Remove(oldChannel.Id); oldChannel.Dispose(); } this.idChannels[channel.Id] = channel; return(channel); }
public override AChannel ConnectChannel(IPEndPoint remoteEndPoint) { uint localConn = (uint)RandomHelper.RandomNumber(1000, int.MaxValue); KChannel oldChannel; if (this.localConnChannels.TryGetValue(localConn, out oldChannel)) { this.localConnChannels.Remove(oldChannel.LocalConn); oldChannel.Dispose(); } KChannel channel = new KChannel(localConn, this.socket, remoteEndPoint, this); this.localConnChannels[channel.LocalConn] = channel; return(channel); }
public override void Update() { this.TimeNow = (uint)(TimeHelper.ClientNow() - this.StartTime); this.Recv(); this.TimerOut(); while (this.removedChannels.Count > 0) { long id = this.removedChannels.Dequeue(); KChannel channel; if (!this.localConnChannels.TryGetValue(id, out channel)) { continue; } this.localConnChannels.Remove(id); channel.Dispose(); } foreach (long id in updateChannels) { KChannel kChannel = this.GetKChannel(id); if (kChannel == null) { continue; } if (kChannel.Id == 0) { continue; } kChannel.Update(); } this.updateChannels.Clear(); if (CheckHearBeat && timeHearBeat.IsPassSet()) { foreach (KChannel channel in localConnChannels.Values) { if (channel.CheckHearBeat()) { channel.OnError(ErrorCode.ERR_KcpChannelTimeout); Remove(channel.Id); } } } }
public override AChannel ConnectChannel(IPEndPoint remoteEndPoint) { uint localConn = (uint)RandomHelper.RandomNumber(1000, int.MaxValue); KChannel oldChannel; if (this.localConnChannels.TryGetValue(localConn, out oldChannel)) { this.localConnChannels.Remove(oldChannel.LocalConn); oldChannel.Dispose(); } #if !SERVER this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); #endif KChannel channel = new KChannel(localConn, this.socket, remoteEndPoint, this); this.localConnChannels[channel.LocalConn] = channel; return(channel); }
public static void Output(IntPtr bytes, int count, IntPtr user) { if (Instance == null) { return; } AChannel aChannel = Instance.GetChannel((uint)user); if (aChannel == null) { Debug.LogError($"not found kchannel, {(uint)user}"); } KChannel kChannel = aChannel as KChannel; kChannel.Output(bytes, count); }
public /*static*/ void Output(IntPtr bytes, int count, IntPtr user) { //if (Instance == null) //{ // return; //} AChannel aChannel = /*Instance.*/ GetChannel((uint)user); if (aChannel == null) { Log.Error($"not found kchannel, {(uint)user}"); return; } KChannel kChannel = aChannel as KChannel; kChannel.Output(bytes, count); }
public void Recv() { if (this.socket == null) { return; } while (socket != null && this.socket.Available > 0) { int messageLength = 0; try { messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint); } catch (Exception e) { Log.Error(e); continue; } // 长度小于1,不是正常的消息 if (messageLength < 1) { continue; } // accept byte flag = this.cache[0]; // conn从1000开始,如果为1,2,3则是特殊包 uint remoteConn = 0; uint localConn = 0; KChannel kChannel = null; switch (flag) { case KcpProtocalType.SYN: // accept // 长度!=5,不是accpet消息 if (messageLength != 5) { break; } IPEndPoint acceptIpEndPoint = (IPEndPoint)this.ipEndPoint; this.ipEndPoint = new IPEndPoint(0, 0); remoteConn = BitConverter.ToUInt32(this.cache, 1); // 如果等待连接状态,则重新响应请求 if (this.waitConnectChannels.TryGetValue(remoteConn, out kChannel)) { kChannel.HandleAccept(remoteConn); break; } localConn = ++this.IdGenerater; kChannel = new KChannel(localConn, remoteConn, this.socket, acceptIpEndPoint, this); this.localConnChannels[kChannel.LocalConn] = kChannel; this.waitConnectChannels[remoteConn] = kChannel; kChannel.HandleAccept(remoteConn); this.OnAccept(kChannel); break; case KcpProtocalType.ACK: // connect返回 // 长度!=9,不是connect消息 if (messageLength != 9) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetKChannel(localConn); if (kChannel != null) { kChannel.HandleConnnect(remoteConn); } break; case KcpProtocalType.FIN: // 断开 // 长度!=13,不是DisConnect消息 if (messageLength != 13) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); // 处理chanel kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { kChannel.Disconnect(ErrorCode.ERR_PeerDisconnect); } } break; case KcpProtocalType.MSG: // 断开 // 长度<9,不是Msg消息 if (messageLength < 9) { break; } // 处理chanel remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { kChannel.HandleRecv(this.cache, 5, messageLength - 5); } } break; } } }
public override AChannel ConnectChannel(IPEndPoint ipEndPoint) { KChannel channel = this.CreateConnectChannel(ipEndPoint); return(channel); }
public void Recv() { //如果当前使用的是非阻止 Socket,一种较好的做法是在调用 Receive 之前使用 Available //来确定数据是否排队等待读取。可用的数据即网络缓冲区中排队等待读取的全部数据。 //如果在网络缓冲区中没有排队的数据,则 Available 返回 0。 if (this.socket == null) { return; } while (socket != null && this.socket.Available > 0) { int messageLength = 0; try { //接收到通讯数据 ref this.ipEndPoint 保存远程端IP messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint); } catch (Exception e) { Log.Error(e); continue; } // 长度小于1,不是正常的消息 if (messageLength < 1) { continue; } // accept byte flag = this.cache[0]; // conn从1000开始,如果为1,2,3则是特殊包 uint remoteConn = 0; uint localConn = 0; KChannel kChannel = null; switch (flag) { case KcpProtocalType.SYN: // accept //收到连接请求 // 长度!=5,不是accpet消息 if (messageLength != 5) { break; } IPEndPoint acceptIpEndPoint = (IPEndPoint)this.ipEndPoint; this.ipEndPoint = new IPEndPoint(0, 0); remoteConn = BitConverter.ToUInt32(this.cache, 1); // 如果已经收到连接,则忽略 if (this.waitConnectChannels.TryGetValue(remoteConn, out kChannel)) { break; } localConn = ++this.IdGenerater; kChannel = new KChannel(localConn, remoteConn, this.socket, acceptIpEndPoint, this); this.localConnChannels[kChannel.LocalConn] = kChannel; this.waitConnectChannels[remoteConn] = kChannel; this.OnAccept(kChannel); break; case KcpProtocalType.ACK: // connect返回 //2次握手 // 长度!=9,不是connect消息 if (messageLength != 9) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); //请求的KChannel ID localConn = BitConverter.ToUInt32(this.cache, 5); kChannel = this.GetKChannel(localConn); //如果字典里面没有 说明没有连接过 没必要2次握手 退出不处理 if (kChannel != null) { kChannel.HandleConnnect(remoteConn); } break; case KcpProtocalType.FIN: // 断开 //收到断开请求 // 长度!=13,不是DisConnect消息 if (messageLength != 13) { break; } remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); // 处理chanel kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { kChannel.Disconnect(ErrorCode.ERR_PeerDisconnect); } } break; case KcpProtocalType.MSG: // 断开? // 长度<9,不是Msg消息 if (messageLength < 9) { break; } // 处理chanel remoteConn = BitConverter.ToUInt32(this.cache, 1); localConn = BitConverter.ToUInt32(this.cache, 5); this.waitConnectChannels.Remove(remoteConn); kChannel = this.GetKChannel(localConn); if (kChannel != null) { // 校验remoteConn,防止第三方攻击 if (kChannel.RemoteConn == remoteConn) { //处理普通消息 kChannel.HandleRecv(this.cache, 5, messageLength - 5); } } break; } } }