Example #1
0
        public override void Send(Message message)
        {
            var blockInfo   = NegotiateBlockSize(message);
            var isBlockwise = message.Payload.Length > BlockOption.DecodeSzx(blockInfo.Szx);

            if (isBlockwise)
            {
                if (!message.HasToken)
                {
                    message.Token = _tokenManager.AcquireToken();
                }

                var block        = message.GetBlock(blockInfo);
                var optionNumber = message is Request ? OptionNumber.Block1 : OptionNumber.Block2;

                if (block.GetBlockOption(optionNumber).M > 0)
                {
                    _incomplete.Add(message.GetTransactionKey(), message);
                }

                SendMessageOverLowerLayer(block);
            }
            else
            {
                SendMessageOverLowerLayer(message);
            }
        }
Example #2
0
        public void Should_decode_szx()
        {
            var blockSize1 = BlockOption.DecodeSzx(1);
            var blockSize2 = BlockOption.DecodeSzx(3);

            Assert.AreEqual(32, blockSize1);
            Assert.AreEqual(128, blockSize2);
        }
Example #3
0
        public void Should_construct_block2_option()
        {
            var option = new BlockOption(OptionNumber.Block2, 2, 0, BlockOption.EncodeSzx(32));

            Assert.AreEqual(33, ByteConverter.GetInt(option.Value));
            Assert.AreEqual(2, option.Num);
            Assert.AreEqual(0, option.M);
            Assert.AreEqual(32, BlockOption.DecodeSzx(option.Szx));
        }
Example #4
0
        public void Should_construct_block1_option()
        {
            var option = new BlockOption(OptionNumber.Block1, 3, 1, BlockOption.EncodeSzx(128));

            Assert.AreEqual(59, ByteConverter.GetInt(option.Value));
            Assert.AreEqual(3, option.Num);
            Assert.AreEqual(1, option.M);
            Assert.AreEqual(128, BlockOption.DecodeSzx(option.Szx));
        }
Example #5
0
        public static Message GetBlock(this Message message, int num, int szx)
        {
            var blockSize     = BlockOption.DecodeSzx(szx);
            var payloadOffset = num * blockSize;
            var payloadLeft   = message.Payload.Length - payloadOffset;

            if (payloadLeft > 0)
            {
                var block = message is Request
                    ? (Message) new Request(message.Code, message.Type == MessageType.Confirmable)
                {
                    Id = message.Id
                }
                    : new Response(message.Type, message.Code)
                {
                    Id = message.Id
                };

                foreach (var option in message.Options)
                {
                    block.AddOption(option);
                }

                var m = blockSize < payloadLeft ? 1 : 0;

                if (m == 0)
                {
                    blockSize = payloadLeft;
                }

                var payload = new byte[blockSize];
                Buffer.BlockCopy(message.Payload, payloadOffset, payload, 0, blockSize);
                block.Payload = payload;
                var optionNumber = message is Request ? OptionNumber.Block1 : OptionNumber.Block2;
                block.AddOption(new BlockOption(optionNumber, num, m, szx));
                return(block);
            }

            return(null);
        }