public void Should_read_bytes()
 {
     var reader = new DatagramReader(new byte[] { 0, 255 });
     Assert.AreEqual(0, reader.Read(4));
     Assert.AreEqual(0, reader.Read(4));
     Assert.AreEqual(new byte[] { 255 }, reader.ReadAllBytes());
 }
 public void Should_read_8_bits()
 {
     var reader = new DatagramReader(new byte[] { 80 });
     Assert.AreEqual(1, reader.Read(2));
     Assert.AreEqual(1, reader.Read(2));
     Assert.AreEqual(0, reader.Read(4));
 }
예제 #3
0
        public virtual Message Deserialize(byte[] bytes)
        {
            var reader = new DatagramReader(bytes);
            var factory = new MessageFactory();
            var version = reader.Read(Message.VersionBits);

            if (version != Message.Version) {
                throw new SerializationException("incorrect version");
            }

            var type = (MessageType) reader.Read(Message.TypeBits);
            var optionCount = reader.Read(Message.OptionCountBits);
            var code = (CodeRegistry) reader.Read(Message.CodeBits);
            var id = reader.Read(Message.IdBits);
            var message = factory.Create(type, code, id);
            var currentOption = 0;

            for (var i = 0; i < optionCount; i++) {
                var delta = reader.Read(Message.OptionDeltaBits);
                var length = reader.Read(Message.OptionLengthBits);
                currentOption += delta;
                var option = new Option((OptionNumber) currentOption) { Value = reader.ReadBytes(length) };
                message.AddOption(option);
            }

            message.Payload = reader.ReadAllBytes();
            return message;
        }
 public void Should_read_16_bits()
 {
     var reader = new DatagramReader(new byte[] { 66, 69 });
     Assert.AreEqual(66, reader.Read(8));
     Assert.AreEqual(69, reader.Read(8));
 }