internal static bool DeserializeEvent(string eventMsg, out RequestMessage deserializedMessage) { var msg = Json.Deserialize(eventMsg) as Dictionary <string, object>; deserializedMessage = new RequestMessage(); if (msg == null) { Debug.LogError("Invalid message: " + eventMsg); return(false); } if (!msg.ContainsKey("type")) { Debug.LogError("Message doesn't contain type: " + eventMsg); return(false); } if (!msg.ContainsKey("req")) { Debug.LogError("Message doesn't contain req: " + eventMsg); return(false); } if (!msg.ContainsKey("senderId")) { Debug.LogError("Message doesn't contain senderId: " + eventMsg); return(false); } deserializedMessage.reqType = msg["req"].ToString(); deserializedMessage.eventType = msg["type"].ToString(); deserializedMessage.senderId = Convert.ToInt32(msg["senderId"]); object requestId; if (msg.TryGetValue("requestId", out requestId)) { deserializedMessage.requestId = Convert.ToInt32(requestId); } if (deserializedMessage.senderId == m_Client.GetChannelClientInfo().connectionId) { return(false); } object dataObj = null; msg.TryGetValue("data", out dataObj); object dataInfos = null; if (msg.TryGetValue("dataInfos", out dataInfos)) { deserializedMessage.eventDataSerialization = EventDataSerialization.JsonUtility; } deserializedMessage.data = GetDataArray(dataObj, dataInfos); return(true); }
private static void IncomingEvent(string eventMsg) { //Console.WriteLine("[UMPE] " + eventMsg); var msg = Json.Deserialize(eventMsg) as Dictionary <string, object>; if (msg == null) { Debug.LogError("Invalid message: " + eventMsg); return; } if (!msg.ContainsKey("type")) { Debug.LogError("Message doesn't contain type: " + eventMsg); return; } if (!msg.ContainsKey("req")) { Debug.LogError("Message doesn't contain req: " + eventMsg); return; } if (!msg.ContainsKey("senderId")) { Debug.LogError("Message doesn't contain senderId: " + eventMsg); return; } var reqType = msg["req"].ToString(); var eventType = msg["type"].ToString(); var senderId = Convert.ToInt32(msg["senderId"]); if (senderId == m_Client.GetChannelClientInfo().connectionId) { return; } object dataObj = null; msg.TryGetValue("data", out dataObj); object[] data = null; if (dataObj != null) { data = dataObj is List <object>?(dataObj as List <object>).ToArray() : new[] { dataObj }; } switch (reqType) { case kRequest: // Receiver // We are able to answer this request. Acknowledge it to the sender: if (HasHandlers(eventType)) { var response = CreateRequestMsg(kRequestAcknowledge, eventType, senderId, null); response["requestId"] = msg["requestId"]; m_Client.Send(Json.Serialize(response)); } break; case kRequestAcknowledge: // Request emitter var pendingRequest = GetPendingRequest(eventType, msg); if (pendingRequest != null) { // A client is able to fulfill the request: proceed with request execution: pendingRequest.isAcknowledged = true; pendingRequest.offerStartTime = Stopwatch.GetTimestamp(); var response = CreateRequestMsg(kRequestExecute, eventType, senderId, null); response["requestId"] = msg["requestId"]; response["data"] = Json.Deserialize(pendingRequest.data); m_Client.Send(Json.Serialize(response)); } // else Request might potentially have timed out. break; case kRequestExecute: // Request receiver { // We are fulfilling the request: send the execution results const bool notifyWildcard = false; var results = NotifyLocalListeners(eventType, data, notifyWildcard); var response = CreateRequestMsg(kRequestResult, eventType, senderId, results); response["requestId"] = msg["requestId"]; m_Client.Send(Json.Serialize(response)); break; } case kRequestResult: // Request emitter var pendingRequestAwaitingResult = GetPendingRequest(eventType, msg); if (pendingRequestAwaitingResult != null) { var timeForSuccess = new TimeSpan(Stopwatch.GetTimestamp() - pendingRequestAwaitingResult.offerStartTime).TotalMilliseconds; Console.WriteLine($"[UMPE] Request {eventType} successful in {timeForSuccess} ms"); Resolve(pendingRequestAwaitingResult, data); CleanRequest(eventType); } break; case kEvent: { const bool notifyWildcard = true; NotifyLocalListeners(eventType, data, notifyWildcard); break; } } }