public async Task SendAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken)
 {
     try
     {
         _logger.Trace(nameof(CoapTransportLayerAdapter), $"Sending {buffer.Count} bytes...");
         await _transportLayer.SendAsync(buffer, cancellationToken).ConfigureAwait(false);
     }
     catch (Exception exception)
     {
         throw new CoapCommunicationException("Error while sending CoAP message.", exception);
     }
 }
Exemple #2
0
        async Task ReceiveMessages(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var message = await _lowLevelClient.ReceiveAsync(cancellationToken).ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (message == null)
                    {
                        continue;
                    }

                    if (!_messageDispatcher.TryHandleReceivedMessage(message))
                    {
                        if (!await _observationManager.TryHandleReceivedMessage(message).ConfigureAwait(false))
                        {
                            _logger.Trace(nameof(CoapClient), "Received an unexpected message ({0}).", message.Id);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception exception)
                {
                    _logger.Error(nameof(CoapClient), exception, "Error while receiving messages.");
                }
            }
        }
        public async Task <ArraySegment <byte> > ReceiveFullPayload(CancellationToken cancellationToken)
        {
            var receivedBlock2Option      = _firstResponseMessage.Options.First(o => o.Number == CoapMessageOptionNumber.Block2);
            var receivedBlock2OptionValue = _blockValueDecoder.Decode(((CoapMessageOptionUintValue)receivedBlock2Option.Value).Value);

            _logger.Trace(nameof(CoapClientBlockTransferReceiver), "Received Block2 {0}.", FormatBlock2OptionValue(receivedBlock2OptionValue));

            var requestMessage = new CoapMessage
            {
                Type    = CoapMessageType.Confirmable,
                Code    = _requestMessage.Code,
                Token   = _requestMessage.Token,
                Options = new List <CoapMessageOption>(_requestMessage.Options)
            };

            var requestBlock2Option = new CoapMessageOption(CoapMessageOptionNumber.Block2, new CoapMessageOptionUintValue(0));

            requestMessage.Options.Add(requestBlock2Option);

            // Crate a buffer which is pre sized to at least 4 blocks.
            using (var buffer = new MemoryBuffer(receivedBlock2OptionValue.Size * 4))
            {
                buffer.Write(_firstResponseMessage.Payload);

                while (receivedBlock2OptionValue.HasFollowingBlocks)
                {
                    // Patch Block1 so that we get the next chunk.
                    // TODO: Move custom size to client connect options!
                    //receivedBlock2OptionValue.Size = 1024;
                    receivedBlock2OptionValue.Number++;

                    // TODO: Avoid setting value. Create new instead.
                    requestBlock2Option.Value = new CoapMessageOptionUintValue(_blockValueEncoder.Encode(receivedBlock2OptionValue));

                    var response = await _client.RequestAsync(requestMessage, cancellationToken).ConfigureAwait(false);

                    receivedBlock2Option      = response.Options.First(o => o.Number == CoapMessageOptionNumber.Block2);
                    receivedBlock2OptionValue = _blockValueDecoder.Decode(((CoapMessageOptionUintValue)receivedBlock2Option.Value).Value);

                    _logger.Trace(nameof(CoapClientBlockTransferReceiver), "Received Block2 {0}.", FormatBlock2OptionValue(receivedBlock2OptionValue));

                    buffer.Write(response.Payload);
                }

                return(buffer.GetBuffer());
            }
        }