Example #1
0
        public void ParseMessageInRC5Encrypt(byte[] data)
        {
            byte[] output     = RC5Encrypt.Decrypt(data);
            int    bodyLength = ((int)output[1] << 8) + output[0];

            this.isCompress = false;
            if (((int)output[3] & 0x40) != 0)
            {
                this.isCompress = true;
                byte[] unCompressData = null;
                using (MemoryStream ms = new MemoryStream()) {
                    ms.Write(output, HeadSize, bodyLength);
                    //unCompressData = ZipCompress.Decompress(ms.GetBuffer());
                }
            }

            byte[] nullCmdBytes = new byte[6];
            Array.Copy(output, 4, nullCmdBytes, 0, 6);
            object obj = CmdSerializer.BytesToStruct(nullCmdBytes, typeof(stNullUserCmd));

            if (obj is stNullUserCmd)
            {
                bmdParamTime = (stNullUserCmd)obj;
            }
        }
Example #2
0
        public byte[] CompressAndRC5Encrypt()
        {
            byte[] buffer = CompressCommandAndFillZero();

            if (buffer == null)
            {
                return(null);
            }

            //RC5Crypt
            return(RC5Encrypt.Encrypt(buffer));
        }
Example #3
0
        private void _ReceiveMessage(IByteArray bytes)
        {
            bytes.Retain();
            this.receiveByteArray.AddLast(bytes);
            this.receiveSize += bytes.Size;
            if (this.receiveSize < 8)
            {
                return;
            }

            var decSize = this.receiveSize & (~7);

            this.receiveSize -= decSize;
            var decBytes = this.storage.Alloc(decSize);
            var node     = this.receiveByteArray.First;

            while (decSize > 0 && null != node)
            {
                var current = node;
                node = node.Next;

                if (current.Value.Size <= decSize)
                {
                    decSize -= current.Value.Size;
                    decBytes.WriteByteArray(current.Value);
                }
                else
                {
                    var tmp = current.Value.ReadByteArray(decSize);
                    this.receiveByteArray.AddLast(current.Value.ReadByteArray());
                    decBytes.WriteByteArray(tmp);
                    tmp.Release();
                    decSize = 0;
                }
                this.receiveByteArray.Remove(current);
                current.Value.Release();
            }

            decBytes.Seek();
            byte[] decryptData = null;
            if (this.loginRecv)
            {
                decryptData = RC5Encrypt.Decrypt(decBytes.ReadBytes());
            }
            else
            {
                decryptData = decBytes.ReadBytes();
                DESEncryptorFix.EncDec_DES(decryptData, 0, decryptData.Length, false);
            }
            decBytes.Release();

            var array = this.storage.Alloc(decryptData.Length);

            array.WriteBytes(decryptData, 0, decryptData.Length);
            array.Seek();
            this.decodeByteArray.AddLast(array);
            this.decodeSize += array.Size;

            //解析
            node = this.decodeByteArray.First;
            while (this.decodeSize > 4 && null != node)
            {
                node = _GetHeaderNode(node);
                var byteArray = node.Value;

                //前两字节代表数据长度
                int bodyLength = byteArray.ReadInt16();
                if (bodyLength + 4 > this.decodeSize)
                {
                    byteArray.Seek(-2, SeekOrigin.CURRENT); // 回滚读取索引位置
                    break;
                }

                //第四字节代表是否压缩
                byteArray.ReadByte();
                int compressFlag = (int)byteArray.ReadByte();

                //有一个完整的数据包
                var commandData = this.storage.Alloc(bodyLength);
                while (commandData.Size != bodyLength)
                {
                    var current = node;
                    if ((current.Value.Size - current.Value.Position) <= (bodyLength - commandData.Size))
                    {
                        commandData.WriteByteArray(current.Value);
                        node = node.Next;
                    }
                    else
                    {
                        var tmp = current.Value.ReadByteArray(bodyLength - commandData.Size);
                        commandData.WriteByteArray(tmp);
                        tmp.Release();
                        node = this.decodeByteArray.AddAfter(current, current.Value.ReadByteArray());
                    }
                    current.Value.Release();
                    this.decodeByteArray.Remove(current);
                }

                this.decodeSize -= (bodyLength + 4);

                commandData.Seek();
                IByteArray msgData = null;
                if ((compressFlag & 0x40) != 0)
                {
                    msgData = this.storage.Alloc(BUFFERSIZE);   // 分配storage定义的BufferSize
                    // ZipCompress.Decompress(commandData, msgData);
                }
                else
                {
                    msgData = commandData;
                    msgData.Retain();
                }
                commandData.Release();

                byte command = msgData.ReadByte();
                byte param   = msgData.ReadByte();

                Logger <IGiantFreeServer> .L("<<<<收到返回数据 size = " + msgData.Size + ", byCmd = " + command + ", byParam = " + param);

                ushort sign = (ushort)(((int)command << 8) + param);
                msgData.Seek(6);
                Action <byte[]> callback = null;
                if (this.messageCallbacks.TryGetValue(sign, out callback))
                {
                    callback(msgData.ReadBytes());
                }
                else if (null != this.client)
                {
                    this.client.HandleMessage(command, param, msgData.ReadBytes());
                }
                msgData.Release();
            }
        }