예제 #1
0
        public async Task <bool> TryHandleReceivedMessage(CoapMessage message)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                var observeOption = message.Options.FirstOrDefault(o => o.Number == CoapMessageOptionNumber.Observe);
                if (observeOption == null)
                {
                    return(false);
                }

                if (!_observedResponseHandlers.TryGetValue(new CoapMessageToken(message.Token), out var responseHandler))
                {
                    await DeregisterObservation(message).ConfigureAwait(false);

                    return(true);
                }

                if (message.Type == CoapMessageType.Confirmable)
                {
                    var ackMessage = new CoapMessage
                    {
                        Type = CoapMessageType.Acknowledgement,
                        Code = CoapMessageCodes.Empty,
                        Id   = message.Id
                    };

                    await _client.SendAsync(ackMessage, CancellationToken.None).ConfigureAwait(false);
                }

                var payload = message.Payload;

                // TODO: Check if supported etc.
                //if (CoapClientBlockTransferReceiver.IsBlockTransfer(coapMessage))
                //{
                //    payload = await new CoapClientBlockTransferReceiver(requestMessage, coapMessage, this, _logger).ReceiveFullPayload(CancellationToken.None).ConfigureAwait(false);
                //}

                var sequenceNumber = ((CoapMessageOptionUintValue)observeOption.Value).Value;
                var response       = _messageToResponseConverter.Convert(message, payload);

                await responseHandler.HandleResponseAsync(new HandleResponseContext
                {
                    SequenceNumber = sequenceNumber,
                    Response       = response
                }).ConfigureAwait(false);

                return(true);
            }
            catch (Exception exception)
            {
                _logger.Error(nameof(CoapClient), exception, "Error while handling observed message.");

                return(false);
            }
        }
예제 #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.");
                }
            }
        }
예제 #3
0
        async Task ReceiveMessages(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var message = await _lowLevelClient.ReceiveAsync(cancellationToken).ConfigureAwait(false);

                    if (!_messageDispatcher.TryDispatch(message))
                    {
                        if (!await TryHandleObservedMessage(message).ConfigureAwait(false))
                        {
                            await DeregisterObservation(message).ConfigureAwait(false);
                        }
                    }
                }
                catch (Exception exception)
                {
                    _logger.Error(nameof(CoapClient), exception, "Error while receiving message.");
                }
            }
        }