public UnityRequestException(UnityMessage errorMessage, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) : this(null, null, null, errorMessage, memberName, sourceFilePath, sourceLineNumber) { }
/// <summary> /// Tries to find and resolve awaiting outbound request handler. /// </summary> /// <param name="id">The unity response message.</param> private void TryResolveOutboundRequest(UnityMessage unityMessage) { var uuid = unityMessage.uuid.Value; lock (this.stateLock) { // Response (success or failure) to sent request if (this.RemoveOutboundRequest(uuid, out UniTaskCompletionSource <UnityMessage> awaiter)) { if (unityMessage.IsResponse) { awaiter.TrySetResult(unityMessage); } else if (unityMessage.IsCanceled) { awaiter.TrySetCanceled(); } else if (unityMessage.IsError) { awaiter.TrySetException(new UnityRequestException(unityMessage)); } else { Debug.unityLogger.LogError("messaging", $"Unknown response message type: {unityMessage.type}", instance); } } else { Debug.unityLogger.LogError("messaging", $"Unknown outbound request to resolve [uuid: {unityMessage.uuid}]", instance); } } }
/// <summary> /// Send serialized message to React Native app (MOCK) /// </summary> /// <param name="message">The serialized message to send.</param> private static void onUnityMessage(string message) { if (!message.StartsWith(MessagePrefix)) { Debug.unityLogger.Log("messaging", $"Unknown type: {message}"); return; } message = message.Substring(MessagePrefix.Length); UnityMessage unityMessage = JsonConvert.DeserializeObject <UnityMessage>(message); if (unityMessage.IsRequestCompletion) { Debug.unityLogger.Log("messaging", $"response[{unityMessage.uuid}] {message}"); if (instance.sentRequests.ContainsKey(unityMessage.uuid.Value)) { instance.TryResolveOutboundRequest(unityMessage); } else { instance.MOCK_TryResolveInjectedRequest(unityMessage); } } else if (unityMessage.IsCancel) { Debug.unityLogger.Log("messaging", $"cancel[{unityMessage.uuid}] {message}"); instance.TryCancelRequest(unityMessage); } else { Debug.unityLogger.Log("messaging", $"request[{unityMessage.uuid}] {message}"); UnityMessageManager.OnMessageSent?.Invoke(instance, unityMessage); } }
private UnityRequestException(string message, Exception innerException, UnityMessage errorMessage, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) : base(message, innerException) { this.error = errorMessage; this.memberName = memberName; this.sourceFilePath = sourceFilePath; this.sourceLineNumber = sourceLineNumber; }
private UnityRequestException(string message, string rawInput, Exception innerException, UnityMessage errorMessage, string memberName = "", string sourceFilePath = "", int sourceLineNumber = 0) : base(message, innerException) { this.rawInput = rawInput; this.error = errorMessage; this.memberName = memberName; this.sourceFilePath = sourceFilePath; this.sourceLineNumber = sourceLineNumber; }
private void TryCancelRequest(UnityMessage unityMessage) { var uuid = unityMessage.uuid.Value; lock (this.stateLock) { // Cancellation of received request if (this.RemoveIncommingRequest(uuid, out UnityMessageHandlerImpl handler)) { handler.NotifyCancelled(); } else if (this.RemoveOutboundRequest(uuid, out UniTaskCompletionSource <UnityMessage> awaiter)) { awaiter.TrySetCanceled(); } else { Debug.unityLogger.LogError("messaging", $"Unknown request to cancel. [uuid: {uuid}]", this); } } }
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); } }
public UnityMessageHandlerImpl(UnityMessage message, string raw) { this.raw = raw; this.Message = message; }
public UnityMessageHandlerImpl(UnityMessage message) { this.Message = message; }