Ejemplo n.º 1
0
        public Task SendAsync(CoapMessage message, CancellationToken cancellationToken)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var requestMessageBuffer = _messageEncoder.Encode(message);

            return(_transportLayerAdapter.SendAsync(requestMessageBuffer, cancellationToken));
        }
Ejemplo 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());
        }
Ejemplo 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));
        }
Ejemplo n.º 4
0
        public void Encode_Simple_Get_Request()
        {
            var expected      = Convert.FromBase64String("QAHLDnIWEP9wYXlsb2Fk");
            var optionBuilder = new CoapMessageOptionFactory();

            var message = new CoapMessage
            {
                Type    = CoapMessageType.Confirmable,
                Code    = CoapMessageCodes.Get,
                Id      = 0xcb0e,
                Payload = Encoding.UTF8.GetBytes("payload")
            };

            message.Options = new List <CoapMessageOption>
            {
                optionBuilder.CreateUriPort(5648)
            };

            var messageEncoder = new CoapMessageEncoder();
            var messageBuffer  = messageEncoder.Encode(message);

            AssertDataIsEqual(expected, messageBuffer.ToArray());
        }