/// <summary> /// Registers a method call handler on this channel. /// </summary> /// <param name="handler">A <see cref="MethodCallHandler"/>, or null to deregister.</param> public void SetMethodCallHandler(MethodCallHandler handler) { async Task <byte[]> binaryHandler(byte[] bytes) { MethodCall call = Codec.DecodeMethodCall(bytes); try { return(Codec.EncodeSuccessEnvelope(await handler(call))); } catch (FlutterException e) { return(Codec.EncodeErrorEnvelope(e.Code, e.Message, e.Details, e.StackTrace)); } catch (MissingPluginException) { return(null); } catch (Exception e) { return(Codec.EncodeErrorEnvelope("error", e.Message, null, e.StackTrace)); } } BinaryMessenger.SetMessageHandler(Name, handler == null ? null : (BinaryMessageHandler)binaryHandler); }
public static void ProcessRemoteCallRequest(HttpContext context, MethodCallHandler methodCallHandler) { string protectedPayload = context.Request.Params["ProtectedPayload"]; object[] methodCall = (object[])ObjectProtector.Unprotect(protectedPayload); object[] args = new object[methodCall.Length - 1]; Array.Copy(methodCall, 1, args, 0, args.Length); object retVal = methodCallHandler((string)methodCall[0], args); ArrayList resultsArray = new ArrayList(); resultsArray.Add(retVal); for (int i = 0; i < args.Length; i++) { if (args[i] is ICopyFromObject) resultsArray.Add(args[i]); } string responseBody = ObjectProtector.Protect(resultsArray.ToArray()); context.Response.ContentType = "application/octet-stream"; context.Response.Write(responseBody); context.Response.Flush(); }
/// <summary> /// Handles a call. /// </summary> protected override void Call( OperationContext context, NodeSource target, Delegate methodToCall, IList <object> inputArguments, IList <ServiceResult> argumentErrors, IList <object> outputArguments) { NodeId parentId = (NodeId)inputArguments[0]; NodeId referenceTypeId = (NodeId)inputArguments[1]; QualifiedName browseName = (QualifiedName)inputArguments[2]; NodeId nodeId = null; MethodCallHandler handler = methodToCall as MethodCallHandler; if (handler != null) { handler(context, target, parentId, referenceTypeId, browseName, out nodeId); return; } base.Call(context, target, methodToCall, inputArguments, argumentErrors, outputArguments); }
/// <summary> /// Initializes a new instance of the <see cref="FlutnetBridge"/> class /// specifying how platform code and Flutter code communicate. /// </summary> public FlutnetBridge(FlutterEngine engine, Android.Content.Context context, FlutnetBridgeMode mode) { // Create the named channel for communicating with Flutter module using asynchronous method calls // NOTE: This channel is used to RECEIVE messages/requests FROM Flutter _methodChannelIncoming = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.incoming"); _methodCallHandlerIncoming = new MethodCallHandler(HandleMethodCall); _methodChannelIncoming.SetMethodCallHandler(_methodCallHandlerIncoming); // Create a second named channel for diagnostic use only. // This channel is used, for example, to test if Flutter module is running // embedded into a native Xamarin app or as a standalone app _methodChannelTest = new MethodChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.support"); _methodCallHandlerTest = new MethodCallHandler(HandleMethodCallTest); _methodChannelTest.SetMethodCallHandler(_methodCallHandlerTest); // Create the named channel for communicating with Flutter module using event streams // NOTE: This channel is used to SEND messages/notifications TO Flutter // An event channel is a specialized platform channel intended for the use case of exposing platform events to Flutter as a Dart stream. // The Flutter SDK currently has no support for the symmetrical case of exposing Dart streams to platform code, though that could be built, if the need arises. // see: https://medium.com/flutter/flutter-platform-channels-ce7f540a104e _streamHandler = new StreamHandler(this); _eventChannel = new EventChannel(engine.DartExecutor.BinaryMessenger, "flutnetbridge.outgoing"); _eventChannel.SetStreamHandler(_streamHandler); _context = context; Mode = mode; FlutnetRuntime.OnPlatformEvent += FlutnetRuntimeOnPlatformEvent; if (Mode == FlutnetBridgeMode.WebSocket) { _context.StartService(new Android.Content.Intent(_context, typeof(FlutnetWebSocketService))); } }
public static void ProcessRemoteCallRequest(HttpContext context, MethodCallHandler methodCallHandler) { string protectedPayload = context.Request.Params["ProtectedPayload"]; object[] methodCall = (object[])ObjectProtector.Unprotect(protectedPayload); object[] args = new object[methodCall.Length - 1]; Array.Copy(methodCall, 1, args, 0, args.Length); object retVal = methodCallHandler((string)methodCall[0], args); ArrayList resultsArray = new ArrayList(); resultsArray.Add(retVal); for (int i = 0; i < args.Length; i++) { if (args[i] is ICopyFromObject) { resultsArray.Add(args[i]); } } string responseBody = ObjectProtector.Protect(resultsArray.ToArray()); context.Response.ContentType = "application/octet-stream"; context.Response.Write(responseBody); }
public async Task <Message> HandleMethodCall(Message methodCall) { var key = GetMethodLookupKey(methodCall.Header.Interface, methodCall.Header.Member, methodCall.Header.Signature); MethodCallHandler handler = null; if (!_methodHandlers.TryGetValue(key, out handler)) { if (methodCall.Header.Interface == "org.freedesktop.DBus.Properties") { MessageReader reader = new MessageReader(methodCall, null); var interf = reader.ReadString(); key = GetPropertyLookupKey(interf, methodCall.Header.Member, methodCall.Header.Signature); _methodHandlers.TryGetValue(key, out handler); } } if (handler != null) { if (_synchronizationContext == null) { try { return(await handler(_object, methodCall, _factory).ConfigureAwait(false)); } catch (DBusException be) { return(MessageHelper.ConstructErrorReply(methodCall, be.ErrorName, be.ErrorMessage)); } catch (Exception e) { return(MessageHelper.ConstructErrorReply(methodCall, e.GetType().FullName, e.Message)); } } else { var tcs = new TaskCompletionSource <Message>(); _synchronizationContext.Post(async _ => { Message reply; try { reply = await handler(_object, methodCall, _factory).ConfigureAwait(false); } catch (DBusException be) { reply = MessageHelper.ConstructErrorReply(methodCall, be.ErrorName, be.ErrorMessage); } catch (Exception e) { reply = MessageHelper.ConstructErrorReply(methodCall, e.GetType().FullName, e.Message); } tcs.SetResult(reply); }, null); return(await tcs.Task.ConfigureAwait(false)); } } else { var errorMessage = String.Format("Method \"{0}\" with signature \"{1}\" on interface \"{2}\" doesn't exist", methodCall.Header.Member, methodCall.Header.Signature?.Value, methodCall.Header.Interface); var replyMessage = MessageHelper.ConstructErrorReply(methodCall, "org.freedesktop.DBus.Error.UnknownMethod", errorMessage); return(replyMessage); } }