Exemple #1
0
        public override void Update()
        {
            TimeNow = (uint)(TimeHelper.ClientNow() - StartTime);

            Recv();

            TimerOut();

            foreach (long id in updateChannels)
            {
                KChannel kChannel = GetKChannel(id);
                if (kChannel == null)
                {
                    continue;
                }
                if (kChannel.Id == 0)
                {
                    continue;
                }
                kChannel.Update();
            }
            updateChannels.Clear();

            while (removedChannels.Count > 0)
            {
                long     id = removedChannels.Dequeue();
                KChannel channel;
                if (!localConnChannels.TryGetValue(id, out channel))
                {
                    continue;
                }
                localConnChannels.Remove(id);
                channel.Dispose();
            }
        }
Exemple #2
0
        public override AChannel ConnectChannel(IPEndPoint remoteEndPoint)
        {
            uint     localConn = (uint)RandomHelper.RandomNumber(1000, int.MaxValue);
            KChannel oldChannel;

            if (localConnChannels.TryGetValue(localConn, out oldChannel))
            {
                localConnChannels.Remove(oldChannel.LocalConn);
                oldChannel.Dispose();
            }

            KChannel channel = new KChannel(localConn, socket, remoteEndPoint, this);

            localConnChannels[channel.LocalConn] = channel;
            return(channel);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public void Recv()
        {
            if (socket == null)
            {
                return;
            }

            while (socket != null && socket.Available > 0)
            {
                int messageLength = 0;
                try
                {
                    messageLength = socket.ReceiveFrom(cache, ref ipEndPoint);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    continue;
                }

                // 长度小于1,不是正常的消息
                if (messageLength < 1)
                {
                    continue;
                }
                // accept
                byte flag = 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)ipEndPoint;
                    ipEndPoint = new IPEndPoint(0, 0);

                    remoteConn = BitConverter.ToUInt32(cache, 1);

                    // 如果已经收到连接,则忽略
                    if (waitConnectChannels.TryGetValue(remoteConn, out kChannel))
                    {
                        break;
                    }

                    localConn = ++IdGenerater;
                    kChannel  = new KChannel(localConn, remoteConn, socket, acceptIpEndPoint, this);
                    localConnChannels[kChannel.LocalConn] = kChannel;
                    waitConnectChannels[remoteConn]       = kChannel;

                    OnAccept(kChannel);

                    break;

                case KcpProtocalType.ACK:                          // connect返回
                    // 长度!=9,不是connect消息
                    if (messageLength != 9)
                    {
                        break;
                    }
                    remoteConn = BitConverter.ToUInt32(cache, 1);
                    localConn  = BitConverter.ToUInt32(cache, 5);

                    kChannel = GetKChannel(localConn);
                    if (kChannel != null)
                    {
                        kChannel.HandleConnnect(remoteConn);
                    }
                    break;

                case KcpProtocalType.FIN:                          // 断开
                    // 长度!=13,不是DisConnect消息
                    if (messageLength != 13)
                    {
                        break;
                    }

                    remoteConn = BitConverter.ToUInt32(cache, 1);
                    localConn  = BitConverter.ToUInt32(cache, 5);

                    // 处理chanel
                    kChannel = 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(cache, 1);
                    localConn  = BitConverter.ToUInt32(cache, 5);

                    waitConnectChannels.Remove(remoteConn);

                    kChannel = GetKChannel(localConn);
                    if (kChannel != null)
                    {
                        // 校验remoteConn,防止第三方攻击
                        if (kChannel.RemoteConn == remoteConn)
                        {
                            kChannel.HandleRecv(cache, 5, messageLength - 5);
                        }
                    }
                    break;
                }
            }
        }