public Task <CoapMessage> PostAsync(CoapMessage message) { TaskCompletionSource <CoapMessage> tcs = new TaskCompletionSource <CoapMessage>(); CoapUri uri = new CoapUri(message.ResourceUri.ToString()); ResponseMessageType rmt = message.MessageType == CoapMessageType.Confirmable ? ResponseMessageType.Acknowledgement : ResponseMessageType.NonConfirmable; registry.GetAction("POST", uri.Resource)?.Invoke(MediaTypeConverter.ConvertFromMediaType(message.ContentType), message.Payload); CoapMessage response = new CoapResponse(message.MessageId, rmt, ResponseCodeType.Created, message.Token); tcs.SetResult(response); return(tcs.Task); }
private void Channel_OnReceive(object sender, ChannelReceivedEventArgs e) { try { CoapMessage message = CoapMessage.DecodeMessage(e.Message); if (!session.IsAuthenticated || forcePerReceiveAuthn) { session.EnsureAuthentication(message, forcePerReceiveAuthn); UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity, session.Config.IdentityClaimType, Channel.TypeId, "COAP", "Granted", DateTime.UtcNow); userAuditor?.WriteAuditRecordAsync(record).Ignore(); } OnObserve?.Invoke(this, new ChannelObserverEventArgs(Channel.Id, message.ResourceUri.ToString(), MediaTypeConverter.ConvertFromMediaType(message.ContentType), message.Payload)); Task task = Task.Factory.StartNew(async() => { CoapMessageHandler handler = CoapMessageHandler.Create(session, message, dispatcher); CoapMessage msg = await handler.ProcessAsync(); if (msg != null) { byte[] payload = msg.Encode(); await Channel.SendAsync(payload); } }); task.LogExceptions(); } catch (Exception ex) { logger?.LogErrorAsync(ex, $"CoAP adapter receiveing on channel '{Channel.Id}'.").GetAwaiter(); OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex)); Channel.CloseAsync().Ignore(); } }