• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 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.Reflection;
24 using System.Runtime.InteropServices;
25 using System.Threading;
26 
27 using Grpc.Core.Logging;
28 using Grpc.Core.Utils;
29 
30 namespace Grpc.Core.Internal
31 {
NativeCallbackTestDelegate(bool success)32     internal delegate void NativeCallbackTestDelegate(bool success);
33 
34     /// <summary>
35     /// Provides access to all native methods provided by <c>NativeExtension</c>.
36     /// An extra level of indirection is added to P/Invoke calls to allow intelligent loading
37     /// of the right configuration of the native extension based on current platform, architecture etc.
38     /// </summary>
39     internal partial class NativeMethods
40     {
41         // Signatures of native methods are generated from a template
42         // and can be found in NativeMethods.Generated.cs
43 
44         /// <summary>
45         /// Gets singleton instance of this class.
46         /// </summary>
Get()47         public static NativeMethods Get()
48         {
49             return NativeExtension.Get().NativeMethods;
50         }
51 
52         static T GetMethodDelegate<T>(UnmanagedLibrary library)
53             where T : class
54         {
55             var methodName = RemoveStringSuffix(typeof(T).Name, "_delegate");
56             return library.GetNativeMethodDelegate<T>(methodName);
57         }
58 
RemoveStringSuffix(string str, string toRemove)59         static string RemoveStringSuffix(string str, string toRemove)
60         {
61             if (!str.EndsWith(toRemove))
62             {
63                 return str;
64             }
65             return str.Substring(0, str.Length - toRemove.Length);
66         }
67     }
68 }
69