예제 #1
0
 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);
     }
 }
예제 #2
0
        public async Task <CoapResponse> Request(CoapRequest request, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var requestMessage = _requestToMessageConverter.Convert(request);

            requestMessage.Id = _messageIdProvider.Next();

            var requestMessageBuffer = _messageEncoder.Encode(requestMessage);

            await _transportLayer.SendAsync(requestMessageBuffer, cancellationToken).ConfigureAwait(false);

            // TODO: Add proper reqest-response matching like in MQTTnet.

            await _transportLayer.ReceiveAsync(_receiveBuffer, cancellationToken).ConfigureAwait(false);

            var responseMessage = _messageDecoder.Decode(_receiveBuffer);

            return(_messageToResponseConverter.Convert(responseMessage));
        }