コード例 #1
0
 public Task <CoapPacket> ReceiveAsync()
 => _coapEndpoint.ReceiveAsync();
コード例 #2
0
ファイル: Client.cs プロジェクト: lulzzz/SmartWorld
        /// <summary>
        /// Create Listening point
        /// </summary>
        /// <exception cref=""></exception>
        /// <remarks>N/a</remarks>
        /// <seealso cref=""/>
        /// <see cref=""/>
        /// <permission cref="">This method can be called by: every user</permission>
        public void Listen()
        {
            if (IsListening)
            {
                return;
            }

            receiveCancellationToken = new CancellationTokenSource();

            Task.Factory.StartNew(() =>
            {
                var token = receiveCancellationToken.Token;

                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var payload = transport.ReceiveAsync();
                        payload.Wait(token);
                        if (!payload.IsCompleted || payload.Result == null)
                        {
                            continue;
                        }

                        var message = new CoapMessage(transport.IsMulticast);
                        try
                        {
                            message.Deserialise(payload.Result.Payload);
                        }
                        catch (CoapMessageFormatException)
                        {
                            if (message.Type == CoapMessageType.Confirmable &&
                                !transport.IsMulticast)
                            {
                                Task.Run(() => SendAsync(new CoapMessage
                                {
                                    Id   = message.Id,
                                    Type = CoapMessageType.Reset
                                }, payload.Result.m_Endpoint));
                            }
                            continue;
                        }

                        if (messageReponses.ContainsKey(message.Id))
                        {
                            messageReponses[message.Id].TrySetResult(message);
                        }

                        OnMessageReceived?.Invoke(this, new CoapMessageReceivedEventArgs
                        {
                            m_Message  = message,
                            m_Endpoint = payload.Result.m_Endpoint
                        });
                    }
                    catch (CoapEndpointException)
                    {
                        receiveCancellationToken.Cancel();
                    }
                }
                OnClosed?.Invoke(this, new EventArgs());
            });
        }