// callback from Flutter with a method... public async void OnMethodCall(MethodCall methodCall, MethodChannel.IResult result) { try { Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "Vistian.Flutter.Remoting.Droid.Example", $"Message received {methodCall.Method} for {_handler.GetType().Name}"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // decode the method details from the method call var arg = methodCall.Argument(DetailsArgumentName); var t0 = stopWatch.ElapsedTicks; var ns = arg.ToString(); var t1 = stopWatch.ElapsedTicks; // invoke the registered handler for this method and the message details var messagingResult = await _handler.Execute(methodCall.Method, ns).ConfigureAwait(false); var t2 = stopWatch.ElapsedTicks; // serialize up the message result, var jsonMessagingResult = JsonConvert.SerializeObject(messagingResult); var t3 = stopWatch.ElapsedTicks; stopWatch.Stop(); Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "Vistian.Flutter.Remoting.Droid.Example", $"Messaging Handling took {stopWatch.ElapsedMilliseconds} {t0} {t1} {t2} {t3} freq: {Stopwatch.Frequency}"); // and send it back result.Success(jsonMessagingResult); } catch (Exception exception) { // report the error back to flutter result.Error(exception.GetType().Name, exception.Message, exception.Source); } }
private void BackgroundHandleMethodCall(FlutnetMethodInfo methodInfo, MethodCall call) { FlutnetRuntime.OperationInfo operation; try { operation = FlutnetRuntime.GetOperation(methodInfo.Instance, methodInfo.Operation); } catch (Exception) { SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationNotImplemented)); return; } if (operation.Parameters.Length > 0 && call.Arguments() == 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; string paramName = param.Name.FirstCharUpper(); object value; if (call.HasArgument(paramName)) { Object argumentValue = call.Argument(paramName); string serializedArg = argumentValue.ToString(); // Deserialize the argument 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); } }