• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2019 The gRPC Authors
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Collections.Concurrent;
21 using System.Diagnostics;
22 using System.IO;
23 using System.Runtime.InteropServices;
24 using System.Threading;
25 using System.Collections.Generic;
26 using Grpc.Core.Logging;
27 using Grpc.Core.Utils;
28 
29 namespace Grpc.Core.Internal
30 {
UniversalNativeCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5)31     internal delegate int UniversalNativeCallback(IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
32 
NativeCallbackDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5)33     internal delegate int NativeCallbackDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
34 
35     internal class NativeCallbackDispatcher
36     {
37         static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeCallbackDispatcher>();
38 
39         static NativeCallbackDispatcherCallback dispatcherCallback;
40 
Init(NativeMethods native)41         public static void Init(NativeMethods native)
42         {
43             GrpcPreconditions.CheckState(dispatcherCallback == null);
44             dispatcherCallback = new NativeCallbackDispatcherCallback(HandleDispatcherCallback);
45             native.grpcsharp_native_callback_dispatcher_init(dispatcherCallback);
46         }
47 
RegisterCallback(UniversalNativeCallback callback)48         public static NativeCallbackRegistration RegisterCallback(UniversalNativeCallback callback)
49         {
50             var gcHandle = GCHandle.Alloc(callback);
51             return new NativeCallbackRegistration(gcHandle);
52         }
53 
54         [MonoPInvokeCallback(typeof(NativeCallbackDispatcherCallback))]
HandleDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5)55         private static int HandleDispatcherCallback(IntPtr tag, IntPtr arg0, IntPtr arg1, IntPtr arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5)
56         {
57             try
58             {
59                 var gcHandle = GCHandle.FromIntPtr(tag);
60                 var callback = (UniversalNativeCallback) gcHandle.Target;
61                 return callback(arg0, arg1, arg2, arg3, arg4, arg5);
62             }
63             catch (Exception e)
64             {
65                 // eat the exception, we must not throw when inside callback from native code.
66                 Logger.Error(e, "Caught exception inside callback from native code.");
67                 return 0;
68             }
69         }
70     }
71 
72     internal class NativeCallbackRegistration : IDisposable
73     {
74         readonly GCHandle handle;
75 
NativeCallbackRegistration(GCHandle handle)76         public NativeCallbackRegistration(GCHandle handle)
77         {
78             this.handle = handle;
79         }
80 
81         public IntPtr Tag => GCHandle.ToIntPtr(handle);
82 
Dispose()83         public void Dispose()
84         {
85             if (handle.IsAllocated)
86             {
87                 handle.Free();
88             }
89         }
90     }
91 }
92