Ejemplo n.º 1
0
        void ApplyUriPort(CoapRequest request, CoapMessage message)
        {
            if (!request.Options.UriPort.HasValue)
            {
                return;
            }

            message.Options.Add(_optionFactory.CreateUriPort((uint)request.Options.UriPort.Value));
        }
Ejemplo n.º 2
0
        void ApplyUriHost(CoapRequest request, CoapMessage message)
        {
            if (string.IsNullOrEmpty(request.Options.UriHost))
            {
                return;
            }

            message.Options.Add(_optionFactory.CreateUriHost(request.Options.UriHost));
        }
Ejemplo n.º 3
0
        void ApplyUriQuery(CoapRequest request, CoapMessage message)
        {
            if (request.Options.UriQuery == null)
            {
                return;
            }

            foreach (var query in request.Options.UriQuery)
            {
                message.Options.Add(_optionFactory.CreateUriQuery(query));
            }
        }
Ejemplo n.º 4
0
        void ApplyUriPath(CoapRequest request, CoapMessage message)
        {
            if (string.IsNullOrEmpty(request.Options.UriPath))
            {
                return;
            }

            var paths = request.Options.UriPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var path in paths)
            {
                message.Options.Add(_optionFactory.CreateUriPath(path));
            }
        }
        public CoapMessage Convert(CoapRequest request)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var message = new CoapMessage
            {
                Type    = CoapMessageType.Confirmable,
                Code    = GetMessageCode(request.Method),
                Options = new List <CoapMessageOption>()
            };

            message.Options.Add(_optionFactory.CreateUriPath(request.Uri));
            message.Options.Add(_optionFactory.CreateUriPort(5648));

            return(message);
        }
Ejemplo n.º 6
0
        public async Task <CoapResponse> RequestAsync(CoapRequest request, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var requestMessage = _requestToMessageConverter.Convert(request);

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

            var payload = responseMessage.Payload;

            if (CoapClientBlockTransferReceiver.IsBlockTransfer(responseMessage))
            {
                payload = await new CoapClientBlockTransferReceiver(requestMessage, responseMessage, this, _logger).ReceiveFullPayload(cancellationToken).ConfigureAwait(false);
            }

            return(_messageToResponseConverter.Convert(responseMessage, payload));
        }
Ejemplo n.º 7
0
        public async Task <CoapObserveResponse> ObserveAsync(CoapObserveOptions options, CancellationToken cancellationToken)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var request = new CoapRequest
            {
                Method  = CoapRequestMethod.Get,
                Options = options.Request.Options,
            };

            var token = _messageTokenProvider.Next();

            var requestMessage = _requestToMessageConverter.Convert(request);

            requestMessage.Token = token.Value;
            requestMessage.Options.Add(new CoapMessageOptionFactory().CreateObserve(CoapObserveOptionValue.Register));

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

            var payload = responseMessage.Payload;

            if (CoapClientBlockTransferReceiver.IsBlockTransfer(responseMessage))
            {
                payload = await new CoapClientBlockTransferReceiver(requestMessage, responseMessage, this, _logger).ReceiveFullPayload(cancellationToken).ConfigureAwait(false);
            }

            _observedResponseHandlers[token] = options.ResponseHandler;

            var response = _messageToResponseConverter.Convert(responseMessage, payload);

            return(new CoapObserveResponse(response, this)
            {
                Token = token,
                Request = request
            });
        }
Ejemplo n.º 8
0
        public CoapMessage Convert(CoapRequest request)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var message = new CoapMessage
            {
                Type    = CoapMessageType.Confirmable,
                Code    = GetMessageCode(request.Method),
                Options = new List <CoapMessageOption>(),
                Payload = request.Payload
            };

            ApplyUriHost(request, message);
            ApplyUriPort(request, message);
            ApplyUriPath(request, message);
            ApplyUriQuery(request, message);

            return(message);
        }
Ejemplo n.º 9
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));
        }