예제 #1
0
 private void AddIncommingRequest(int uuid, UnityMessageHandlerImpl handler)
 {
     lock (this.stateLock)
     {
         this.receivedRequests[uuid] = handler;
     }
 }
예제 #2
0
        private bool RemoveIncommingRequest(int uuid, out UnityMessageHandlerImpl handler)
        {
            lock (this.stateLock)
            {
                if (this.receivedRequests.TryGetValue(uuid, out handler))
                {
                    return(this.receivedRequests.Remove(uuid));
                }

                return(false);
            }
        }
예제 #3
0
        private void onRNMessage(string message)
        {
            try
            {
                if (!message.StartsWith(MessagePrefix))
                {
                    Debug.unityLogger.LogWarning("messaging", $"Invalid message format.");
                    return;
                }

                message = message.Substring(MessagePrefix.Length);

                Subscription[] subscriptionList;
                UnityMessage   unityMessage = JsonConvert.DeserializeObject <UnityMessage>(message);
                if (unityMessage.IsRequestCompletion)
                {
                    // Handle as request response/error/cancellation
                    this.TryResolveOutboundRequest(unityMessage);
                }
                else if (unityMessage.IsCancel)
                {
                    // Handle as request cancellation
                    this.TryCancelRequest(unityMessage);
                }
                else
                {
                    lock (this.stateLock)
                    {
                        var args = new UnityMessageHandlerImpl(unityMessage, message);

                        try
                        {
                            // Handle as incomming message or request
                            if (this.subscriptions.TryGetValue(unityMessage.id, out subscriptionList) && subscriptionList.Length > 0)
                            {
                                if (unityMessage.IsRequest)
                                {
                                    // Remember request for incomming cancelation handling
                                    this.AddIncommingRequest(unityMessage.uuid.Value, args);
                                }

                                try
                                {
                                    foreach (Subscription s in subscriptionList)
                                    {
                                        s.handler.Invoke(args);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Debug.unityLogger.LogError("messaging", $"Failed to handle incoming message:\n{e}", this);

                                    if (args.IsRequest && !args.IsDeferred && !args.ResponseSent)
                                    {
                                        args.SendError(e);
                                    }
                                }
                            }
                            else
                            {
                                if (args.IsRequest)
                                {
                                    args.SendError(new ArgumentException("Invalid message ID.", nameof(UnityMessage.id)));
                                }

                                Debug.unityLogger.LogError("messaging", $"Unknown message id: {unityMessage.id}.", this);
                            }
                        }
                        finally
                        {
                            if (!args.IsDeferred)
                            {
                                args.Dispose();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.unityLogger.LogError("messaging", $"Failed to parse incoming message:\n{e}", this);
            }
        }