public async Task ProcessMessageAsync(string message) { var packet = OM2MJsonSerializer.Deserialize(message); if (packet is OM2MRequestPrimitive) { var request = packet as OM2MRequestPrimitive; request.From = OriginatorId; var response = CseService.DoBlockingRequest(request); await SendResponseAsync(response); } else if (packet is OM2MResponsePrimitive) { var response = packet as OM2MResponsePrimitive; ResultWait <OM2MResponsePrimitive> resultWait; if (m_waittingResponses.TryGetValue(response.RequestIdentifier, out resultWait)) { resultWait.SetResult(response); } } }
public async Task SendResponseAsync(OM2MResponsePrimitive response) { // Correct primitiveContent if (response.Content != null && response.PrimitiveContent == null) { response.PrimitiveContent = new OM2MPrimitiveContent(); response.PrimitiveContent.Any = new List <object>(); response.PrimitiveContent.Any.Add(response.Content); } var responseMessage = OM2MJsonSerializer.Serialize(response); var responseMessageData = Encoding.UTF8.GetBytes(responseMessage); ArraySegment <byte> buffer = new ArraySegment <byte>(responseMessageData); await WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); }
public OM2MResponsePrimitive SendRequest(OM2MRequestPrimitive request) { // Correct primitiveContent if (request.Content != null && request.PrimitiveContent == null) { request.PrimitiveContent = new OM2MPrimitiveContent(); request.PrimitiveContent.Any = new List <object>(); request.PrimitiveContent.Any.Add(request.Content); } var requestMessage = OM2MJsonSerializer.Serialize(request); var requestMessageData = Encoding.UTF8.GetBytes(requestMessage); ArraySegment <byte> buffer = new ArraySegment <byte>(requestMessageData); using (var resultWait = new ResultWait <OM2MResponsePrimitive>()) { OM2MResponsePrimitive response; m_waittingResponses.Add(request.RequestIdentifier, resultWait); try { Task.WaitAll(WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None)); response = resultWait.Wait(TimeSpan.FromSeconds(30)); } finally { m_waittingResponses.Remove(request.RequestIdentifier); } return(response); } }