Пример #1
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);
        }
Пример #2
0
        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());
        }