Exemplo n.º 1
0
        public async Task <CoapMessage> ReceiveAsync(CancellationToken cancellationToken)
        {
            var datagramLength = await _transportLayerAdapter.ReceiveAsync(_receiveBuffer, cancellationToken).ConfigureAwait(false);

            if (datagramLength == 0)
            {
                return(null);
            }

            return(_messageDecoder.Decode(new ArraySegment <byte>(_receiveBuffer.Array, 0, datagramLength)));
        }
Exemplo n.º 2
0
        void Enocde_And_Decode_Internal(CoapMessage message)
        {
            var messageEncoder = new CoapMessageEncoder();
            var messageBuffer  = messageEncoder.Encode(message);

            var messageDecoder = new CoapMessageDecoder(new CoapNetLogger());
            var decodedMessage = messageDecoder.Decode(messageBuffer);

            Assert.AreEqual(message.Type, decodedMessage.Type);
            Assert.AreEqual(message.Code, decodedMessage.Code);
            CollectionAssert.AreEqual(message.Token, decodedMessage.Token);
            CollectionAssert.AreEqual(message.Options, decodedMessage.Options);

            if (message.Payload.Array == null && message.Payload.Array == null)
            {
                return;
            }

            CollectionAssert.AreEqual(message.Payload.ToArray(), decodedMessage.Payload.ToArray());
        }
Exemplo n.º 3
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));
        }