private void SendError(FlutnetMethodInfo methodInfo, PlatformOperationException exception)
        {
            FlutnetMessage message = new FlutnetMessage
            {
                MethodInfo = methodInfo,
                // NOTE: Please consider remove ErrorCode and ErrorMessage
                ErrorCode    = FlutnetErrorCode.OperationFailed,
                ErrorMessage = exception.Message,
                Exception    = exception
            };

            Send(message);
        }
        private void SendResult(FlutnetMethodInfo methodInfo, object result)
        {
            Dictionary <string, object> resultValue = new Dictionary <string, object>();

            resultValue.Add("ReturnValue", result);

            FlutnetMessage message = new FlutnetMessage
            {
                MethodInfo = methodInfo,
                Result     = resultValue
            };

            Send(message);
        }
Ejemplo n.º 3
0
        private void SendError(FlutnetMethodInfo methodInfo, PlatformOperationException exception)
        {
            FlutnetMessage message = new FlutnetMessage
            {
                MethodInfo = methodInfo,
                // NOTE: Please consider removing ErrorCode and ErrorMessage
                ErrorCode    = FlutnetErrorCode.OperationFailed,
                ErrorMessage = exception.Message,
                Exception    = exception
            };

            NSObject dartReturnValue = FlutterInterop.ToMethodChannelResult(message);

            Console.WriteLine("Sending error to Flutter...");
            MainThread.BeginInvokeOnMainThread(() => _methodChannelIncoming.InvokeMethod("error", dartReturnValue));
        }
Ejemplo n.º 4
0
        private void SendResult(FlutnetMethodInfo methodInfo, object result)
        {
            Dictionary <string, object> resultValue = new Dictionary <string, object>();

            resultValue.Add("ReturnValue", result);

            FlutnetMessage message = new FlutnetMessage
            {
                MethodInfo = methodInfo,
                Result     = resultValue
            };

            NSObject dartReturnValue = FlutterInterop.ToMethodChannelResult(message);

            Console.WriteLine("Sending result to Flutter...");
            MainThread.BeginInvokeOnMainThread(() => _methodChannelIncoming.InvokeMethod("result", dartReturnValue));
        }
Ejemplo n.º 5
0
        private void BackgroundHandleMethodCall(FlutnetMethodInfo methodInfo, FlutterMethodCall call)
        {
            FlutnetRuntime.OperationInfo operation;
            try
            {
                operation = FlutnetRuntime.GetOperation(methodInfo.Instance, methodInfo.Operation);
            }
            catch (Exception)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationNotImplemented));
                return;
            }

            NSDictionary dartArguments = call.Arguments as NSDictionary;

            if (operation.Parameters.Length > 0 && dartArguments == null)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationArgumentCountMismatch));
                return;
            }

            object[] arguments = new object[operation.Parameters.Length];
            try
            {
                for (int i = 0; i < operation.Parameters.Length; i++)
                {
                    ParameterInfo param     = operation.Parameters[i];
                    Type          paramType = param.IsOut || param.ParameterType.IsByRef
                        ? param.ParameterType.GetElementType()
                        : param.ParameterType;
                    NSString paramName = new NSString(param.Name.FirstCharUpper());

                    object value;
                    if (dartArguments.ContainsKey(paramName))
                    {
                        NSObject argumentValue = dartArguments[paramName];

                        NSString serializedArg = (NSString)argumentValue;

                        // Deserialize the arg value
                        value = JsonConvert.DeserializeObject(serializedArg, paramType, FlutterInterop.JsonSerializerSettings);
                    }
                    else if (param.HasDefaultValue)
                    {
                        value = param.DefaultValue;
                    }
                    else
                    {
                        SendError(methodInfo, new FlutnetException(FlutnetErrorCode.InvalidOperationArguments));
                        return;
                    }

                    arguments[i] = value;
                }
            }
            catch (Exception)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationArgumentParsingError));
                return;
            }

            PlatformOperationResult result = PlatformOperationRunner.Run(operation, arguments);

            if (result.Error != null)
            {
                if (result.Error is PlatformOperationException flutterException)
                {
                    SendError(methodInfo, flutterException);
                }
                else
                {
                    //In case of an unhandled exception, send to Flutter a verbose error message for better diagnostic
                    FlutnetException error = new FlutnetException(FlutnetErrorCode.OperationFailed, result.Error.ToStringCleared(), result.Error);
                    SendError(methodInfo, error);
                }
            }
            else
            {
                SendResult(methodInfo, result.Result);
            }
        }