public void DeserializeTest1() { var content = "{\"m2m:rqp\":{\"op\":1,\"to\":\"//example.net/mncse1234\",\"rqi\":\"A1000\", \"rcn\":7,\"pc\":{\"m2m:ae\":{\"rn\":\"SmartHomeApplication\", \"api\":\"Na56\", \"apn\":\"app1234\"}},\"ty\":2}}"; var rqpObj = OM2MJsonSerializer.Deserialize(content); Assert.IsInstanceOfType(rqpObj, typeof(OM2MRequestPrimitive)); var rqp = rqpObj as OM2MRequestPrimitive; Assert.AreEqual(rqp.Operation, OM2MOperation.Create); Assert.AreEqual(rqp.RequestIdentifier, "A1000"); Assert.AreEqual(rqp.ResourceType, OM2MResourceType.AE); Assert.AreEqual(rqp.ResultContent, OM2MResultContent.OriginalResource); Assert.IsInstanceOfType(rqp.PrimitiveContent.Any[0], typeof(OM2MAE)); var ae = rqp.PrimitiveContent.Any[0] as OM2MAE; Assert.AreEqual(ae.AppName, "app1234"); Assert.AreEqual(ae.AppID, "Na56"); Assert.AreEqual(ae.ResourceName, "SmartHomeApplication"); var text = OM2MJsonSerializer.Serialize(rqpObj); return; }
public async Task SendResponseAsync(OM2MResponsePrimitive response) { var text = OM2MJsonSerializer.Serialize(response); var data = Encoding.UTF8.GetBytes(text); var buffer = new ArraySegment <byte>(data); await WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); }
private async Task MainProcess() { var data = new byte[64 * 1024]; var buffer = new ArraySegment <byte>(data); while (WebSocket.State == WebSocketState.Open) { var r = await WebSocket.ReceiveAsync(buffer, CancellationToken.None); try { var text = Encoding.UTF8.GetString(data, 0, r.Count); var msg = OM2MJsonSerializer.Deserialize(text); if (msg is OM2MResponsePrimitive) { var response = msg as OM2MResponsePrimitive; TaskCompletionSource <OM2MResponsePrimitive> tcs; lock (m_responseWaiters) { if (m_responseWaiters.TryGetValue(response.RequestIdentifier, out tcs)) { m_responseWaiters.Remove(response.RequestIdentifier); } else { continue; } } tcs.SetResult(response); } else if (msg is OM2MRequestPrimitive) { m_recieveQueue.Enqueue(msg as OM2MRequestPrimitive); } } catch (Exception ex) { Console.Error.WriteLine(ex); } } }
public async Task <OM2MResponsePrimitive> SendBlockingRequestAsync(OM2MRequestPrimitive request) { var text = OM2MJsonSerializer.Serialize(request); var data = Encoding.UTF8.GetBytes(text); var buffer = new ArraySegment <byte>(data); var tcs = new TaskCompletionSource <OM2MResponsePrimitive>(); lock (m_responseWaiters) { m_responseWaiters.Add(request.RequestIdentifier, tcs); } await WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); // Wait for response await tcs.Task; return(tcs.Task.Result); }