From 83b6a98872212863d5d4f3c2fe80a0a96a01c1cd Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 11 Mar 2019 18:51:49 +0100 Subject: [PATCH] allow return value from native callbacks --- .../Grpc.Core/Internal/NativeCallbackDispatcher.cs | 13 +++++++------ .../Internal/NativeMetadataCredentialsPlugin.cs | 3 ++- src/csharp/ext/grpc_csharp_ext.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/NativeCallbackDispatcher.cs b/src/csharp/Grpc.Core/Internal/NativeCallbackDispatcher.cs index 53f8b7c6489..36df8a5ede1 100644 --- a/src/csharp/Grpc.Core/Internal/NativeCallbackDispatcher.cs +++ b/src/csharp/Grpc.Core/Internal/NativeCallbackDispatcher.cs @@ -27,9 +27,9 @@ using Grpc.Core.Logging; namespace Grpc.Core.Internal { - internal delegate void UniversalNativeCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); + internal delegate int UniversalNativeCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); - internal delegate void NativeCallbackDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); + internal delegate int NativeCallbackDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5); internal class NativeCallbackDispatcher { @@ -81,7 +81,7 @@ namespace Grpc.Core.Internal } [MonoPInvokeCallback(typeof(NativeCallbackDispatcherCallback))] - private static void HandleDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5) + private static int HandleDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5) { try { @@ -89,13 +89,15 @@ namespace Grpc.Core.Internal if (!TryGetCallback(tag, out callback)) { Logger.Error("No native callback handler registered for tag {0}.", tag); + return 0; } - callback(arg0, arg1, arg2, arg3, arg4, arg5); + return callback(arg0, arg1, arg2, arg3, arg4, arg5); } catch (Exception e) { // eat the exception, we must not throw when inside callback from native code. Logger.Error(e, "Caught exception inside callback from native callback."); + return 0; } } } @@ -103,9 +105,8 @@ namespace Grpc.Core.Internal internal class NativeCallbackRegistration : IDisposable { readonly IntPtr tag; - readonly Action disposeAction; - public NativeCallbackRegistration(IntPtr tag, Action disposeAction) + public NativeCallbackRegistration(IntPtr tag) { this.tag = tag; } diff --git a/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs b/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs index d01d4ef287a..f47988f7f76 100644 --- a/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs +++ b/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs @@ -46,9 +46,10 @@ namespace Grpc.Core.Internal get { return credentials; } } - private void HandleUniversalCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5) + private int HandleUniversalCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5) { NativeMetadataInterceptorHandler(arg0, arg1, arg2, arg3, arg4 != IntPtr.Zero); + return 0; } private void NativeMetadataInterceptorHandler(IntPtr serviceUrlPtr, IntPtr methodNamePtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy) diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index b034bafe86c..fcd4caf5f49 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -1012,7 +1012,7 @@ grpcsharp_composite_call_credentials_create(grpc_call_credentials* creds1, /* Native callback dispatcher */ -typedef void(GPR_CALLTYPE* grpcsharp_native_callback_dispatcher_func)( +typedef int(GPR_CALLTYPE* grpcsharp_native_callback_dispatcher_func)( void* tag, void* arg0, void* arg1, void* arg2, void* arg3, void* arg4, void *arg5); static grpcsharp_native_callback_dispatcher_func native_callback_dispatcher = NULL;