Exemple #1
0
        public void ExtendedBinaryReaderTest()
        {
            var testBytes = new byte[]
            {
                0x15, 0x00, 0x00, 0x00, // Size
                0x02, 0x00, 0x00, 0x00, // Id
                0x03, 0x00, 0x00, 0x00, // Type
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00,
                0x00,
            };

            using (var ms = new MemoryStream(testBytes))
            {
                using (var reader = new BinaryReaderExt(ms))
                {
                    var sizeTestValue = reader.ReadInt32LittleEndian();
                    Assert.Equal(21, sizeTestValue);
                    var idTestValue = reader.ReadInt32LittleEndian();
                    Assert.Equal(2, idTestValue);
                    var typeTestValue = reader.ReadInt32LittleEndian();
                    Assert.Equal(3, typeTestValue);
                    var payloadTestValue = reader.ReadAscii();
                    Assert.Equal("Hello World", payloadTestValue);
                }
            }
        }
Exemple #2
0
        public RemoteConPacket(byte[] packetBytes, bool useUtf8 = false)
        {
            if (useUtf8)
            {
                _packetEncoding = Encoding.UTF8;
            }

            using (var ms = new MemoryStream(packetBytes))
            {
                using (var reader = new BinaryReaderExt(ms))
                {
                    Size = reader.ReadInt32LittleEndian();

                    // The size field (4-Bytes Little Endian Int) is, according to specification, not included.
                    if (Size + 4 != packetBytes.Length)
                    {
                        throw new LengthMismatchException("packet length mismatch");
                    }

                    Id = reader.ReadInt32LittleEndian();

                    var packetType = reader.ReadInt32LittleEndian();
                    if (!Enum.IsDefined(typeof(PacketType), packetType))
                    {
                        throw new InvalidPacketTypeException("Invalid packet type");
                    }
                    Type = (PacketType)Enum.ToObject(typeof(PacketType), packetType);

                    if (!useUtf8)
                    {
                        Payload = reader.ReadAscii();

                        // Get payload length by subtracting 9 bytes (ID 4-Bytes, Type 4-Bytes, Null-terminator 1-Byte)
                        if (Encoding.ASCII.GetByteCount(Payload) > Size - 9)
                        {
                            throw new LengthMismatchException("Payload length mismatch");
                        }
                    }
                    else
                    {
                        Payload = Encoding.UTF8.GetString(reader.ReadBytes(Size - 10));
                        reader.ReadByte();
                    }

                    var nullTerminator = reader.ReadByte();
                    if (nullTerminator != 0x00)
                    {
                        throw new NullTerminatorMissingException("Missing last null-terminator");
                    }

                    if (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        throw new Exception("More data to read");
                    }
                }
            }
        }