Esempio n. 1
0
        private void Check(byte[] data)
        {
            int index = 0;
            var conv  = ConverHelper.GetU32(data, index);
            Tuple <Queue <KCPPackage>, KCP> item = null;

            lock (SessionDic)
            {
                if (!SessionDic.TryGetValue(conv, out item))
                {
                    item = new Tuple <Queue <KCPPackage>, KCP>(new Queue <KCPPackage>(), new KCP(conv));
                    //如果会话中不存在conv则认为这是新连接,创建KCP状态机
                    SessionDic.Add(conv, item);
                    new Thread(Update)
                    {
                        IsBackground = true
                    }.Start(item);
                }
            }

            while (index < data.Length)
            {
                var tempItem = KCPPackage.Parse(data, index);
                if (tempItem.Item2 == 0)
                {
                    break;
                }
                index += tempItem.Item2;
                item.Item1.Enqueue(tempItem.Item1);
            }
        }
Esempio n. 2
0
        public static Tuple <KCPPackage, int> Parse(byte[] data, int index)
        {
            if (data.Length - index < 24)
            {
                return(defaultval);
            }
            var pack = new KCPPackage();

            pack.Conv   = ConverHelper.GetU32(data, index);
            pack.Cmd    = ConverHelper.GetU8(data, index + 4);
            pack.Frg    = ConverHelper.GetU8(data, index + 5);
            pack.Wnd    = ConverHelper.GetU16(data, index + 6);
            pack.Ts     = ConverHelper.GetU16(data, index + 8);
            pack.Sn     = ConverHelper.GetU16(data, index + 12);
            pack.Una    = ConverHelper.GetU16(data, index + 16);
            pack.Length = ConverHelper.GetU16(data, index + 20);
            if ((data.Length - index - headlength) < pack.Length)
            {
                return(defaultval);
            }
            pack.Data = data.Skip(index + headlength).Take((int)pack.Length).ToArray();
            return(new Tuple <KCPPackage, int>(pack, (int)pack.Length + headlength));
        }