Ejemplo n.º 1
0
        public void Read(BinaryReader br)
        {
            if (br.BaseStream.Length < 4)
            {
                throw new Exception("Fragmented receive, should not happen! (4 size header)");
            }

            Size    = br.ReadUInt16();
            Channel = br.ReadByte();

            br.ReadByte(); // padding

            if (Size > br.BaseStream.Length)
            {
                Debugger.Break();

                throw new Exception($"Fragmented receive, should not happen! Packet size: {Size} <-> Buffer length: {br.BaseStream.Length}");
            }

            if (Channel == 0xFF) // Internal channel: Send timeout checking, ignore the packet
            {
                return;
            }

            if (Channel != 0)                     // 0 == ReliableStreamChannel (no extra data), Move message uses channels
            {
                SequenceNumber = br.ReadUInt32(); // Sequence number? if (previousValue - newValue < 0) { process packet; previousValue = newValue; }
                br.ReadInt32();                   // 0xDEADBEEF
                br.ReadInt32();                   // skip
            }

            var packetBeginPosition = br.BaseStream.Position;

            using (var reader = new ProtocolBufferReader(br, ProtocolBufferFlags.DontFragment))
            {
                reader.ReadProtocolFlags();


                reader.ReadPacketType(out ushort type, out bool compress);

                Type     = (ClientMessageOpcode)type;
                Compress = compress;

                reader.ReadXORCheck((int)(br.BaseStream.Position - packetBeginPosition));
            }

            var xorCheckPosition = (int)br.BaseStream.Position;

            var readBr = br;

            BufferData buffer = null;

            if (Compress)
            {
                var someType = br.ReadByte(); // 0 = No compression
                if (someType >= 2)
                {
                    throw new Exception("Invalid compress type received!");
                }

                if (someType == 1)
                {
                    Debugger.Break(); // TODO: test

                    var uncompressedSize = br.ReadInt32();

                    byte[] uncompressedData;
                    var    offset = 0;

                    if (uncompressedSize > BufferManager.BlockSize)
                    {
                        uncompressedData = new byte[uncompressedSize];
                    }
                    else
                    {
                        buffer = BufferManager.RequestBuffer();

                        uncompressedData = buffer.Buffer;
                        offset           = buffer.BaseOffset;
                    }

                    using (var deflateStream = new DeflateStream(br.BaseStream, CompressionMode.Decompress, true)) // TODO: test if the br.BaseStream is cool as the Stream input for the DeflateStream
                        deflateStream.Read(uncompressedData, offset, uncompressedSize);

                    readBr = buffer != null?buffer.GetReader() : new BinaryReader(new MemoryStream(uncompressedData, 0, uncompressedSize, false), Encoding.UTF8, false);
                }
            }

            // ReSharper disable SwitchStatementMissingSomeCases
            switch (Type)
            {
            case ClientMessageOpcode.Login:
                Message = new LoginMessage();
                break;

            case ClientMessageOpcode.Move:
                Message = new MoveMessage();
                break;

            case ClientMessageOpcode.CallServerMethod:
                Message = new CallServerMethodMessage();
                break;

            case ClientMessageOpcode.Ping:
                Message = new PingMessage();
                break;

            default:
                throw new Exception($"Unable to handle packet type {Type}, because it's a Server -> Client packet!");
            }
            // ReSharper restore SwitchStatementMissingSomeCases

            using (var reader = new ProtocolBufferReader(readBr, ProtocolBufferFlags.DontFragment))
            {
                reader.ReadProtocolFlags();

                // Subtype and Message.Read()
                reader.ReadDebugByte(41);

                if ((Message.SubtypeFlags & ClientMessageSubtypeFlag.HasSubtype) == ClientMessageSubtypeFlag.HasSubtype)
                {
                    Message.RawSubtype = reader.ReadByte();
                    if (Message.RawSubtype < Message.MinSubtype || Message.RawSubtype > Message.MaxSubtype)
                    {
                        throw new Exception("Invalid Subtype found!");
                    }
                }

                Message.Read(reader);

                reader.ReadDebugByte(42);

                reader.ReadXORCheck((int)br.BaseStream.Position - xorCheckPosition);
            }

            if (buffer != null) // If we requested a buffer for decompressing, free it
            {
                BufferManager.FreeBuffer(buffer);
            }
        }
 public void Read(ProtocolBufferReader reader)
 {
     ChannelId        = reader.ReadByte();
     UnkMaybeEntityId = reader.ReadULong();
 }
Ejemplo n.º 3
0
 public void Read(ProtocolBufferReader reader)
 {
     UnkByte = reader.ReadByte();
     /*var movementData = */ reader.ReadMovement();
 }
Ejemplo n.º 4
0
 public void Read(ProtocolBufferReader reader)
 {
     UnkByte  = reader.ReadByte();
     Movement = reader.ReadMovement();
 }