OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Globalization; |
| 36 using System.IO; |
| 37 using System.Reflection; |
| 38 |
| 39 using Grpc.Core.Logging; |
| 40 |
| 41 namespace Grpc.Core.Internal |
| 42 { |
| 43 /// <summary> |
| 44 /// Takes care of loading C# native extension and provides access to PInvoke
calls the library exports. |
| 45 /// </summary> |
| 46 internal sealed class NativeExtension |
| 47 { |
| 48 const string NativeLibrariesDir = "nativelibs"; |
| 49 |
| 50 static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeEx
tension>(); |
| 51 static readonly object staticLock = new object(); |
| 52 static volatile NativeExtension instance; |
| 53 |
| 54 readonly NativeMethods nativeMethods; |
| 55 |
| 56 private NativeExtension() |
| 57 { |
| 58 this.nativeMethods = new NativeMethods(Load()); |
| 59 |
| 60 // Redirect the the native logs as the very first thing after loadin
g the native extension |
| 61 // to make sure we don't lose any logs. |
| 62 NativeLogRedirector.Redirect(this.nativeMethods); |
| 63 |
| 64 DefaultSslRootsOverride.Override(this.nativeMethods); |
| 65 |
| 66 Logger.Debug("gRPC native library loaded successfully."); |
| 67 } |
| 68 |
| 69 /// <summary> |
| 70 /// Gets singleton instance of this class. |
| 71 /// The native extension is loaded when called for the first time. |
| 72 /// </summary> |
| 73 public static NativeExtension Get() |
| 74 { |
| 75 if (instance == null) |
| 76 { |
| 77 lock (staticLock) |
| 78 { |
| 79 if (instance == null) { |
| 80 instance = new NativeExtension(); |
| 81 } |
| 82 } |
| 83 } |
| 84 return instance; |
| 85 } |
| 86 |
| 87 /// <summary> |
| 88 /// Provides access to the exported native methods. |
| 89 /// </summary> |
| 90 public NativeMethods NativeMethods |
| 91 { |
| 92 get { return this.nativeMethods; } |
| 93 } |
| 94 |
| 95 /// <summary> |
| 96 /// Detects which configuration of native extension to load and load it. |
| 97 /// </summary> |
| 98 private static UnmanagedLibrary Load() |
| 99 { |
| 100 // TODO: allow customizing path to native extension (possibly throug
h exposing a GrpcEnvironment property). |
| 101 |
| 102 var libraryFlavor = string.Format("{0}_{1}", GetPlatformString(), Ge
tArchitectureString()); |
| 103 var fullPath = Path.Combine(Path.GetDirectoryName(GetAssemblyPath())
, |
| 104 NativeLibrariesDir, libraryFlavor, GetNativeLibraryFilename()); |
| 105 return new UnmanagedLibrary(fullPath); |
| 106 } |
| 107 |
| 108 private static string GetAssemblyPath() |
| 109 { |
| 110 var assembly = typeof(NativeExtension).GetTypeInfo().Assembly; |
| 111 |
| 112 // If assembly is shadowed (e.g. in a webapp), EscapedCodeBase is po
inting |
| 113 // to the original location of the assembly, and Location is pointin
g |
| 114 // to the shadow copy. We care about the original location because |
| 115 // the native dlls don't get shadowed. |
| 116 var escapedCodeBase = assembly.EscapedCodeBase; |
| 117 if (IsFileUri(escapedCodeBase)) |
| 118 { |
| 119 return new Uri(escapedCodeBase).LocalPath; |
| 120 } |
| 121 return assembly.Location; |
| 122 } |
| 123 |
| 124 private static bool IsFileUri(string uri) |
| 125 { |
| 126 return uri.ToLowerInvariant().StartsWith(Uri.UriSchemeFile); |
| 127 } |
| 128 |
| 129 private static string GetPlatformString() |
| 130 { |
| 131 if (PlatformApis.IsWindows) |
| 132 { |
| 133 return "windows"; |
| 134 } |
| 135 if (PlatformApis.IsLinux) |
| 136 { |
| 137 return "linux"; |
| 138 } |
| 139 if (PlatformApis.IsMacOSX) |
| 140 { |
| 141 return "macosx"; |
| 142 } |
| 143 throw new InvalidOperationException("Unsupported platform."); |
| 144 } |
| 145 |
| 146 // Currently, only Intel platform is supported. |
| 147 private static string GetArchitectureString() |
| 148 { |
| 149 if (PlatformApis.Is64Bit) |
| 150 { |
| 151 return "x64"; |
| 152 } |
| 153 else |
| 154 { |
| 155 return "x86"; |
| 156 } |
| 157 } |
| 158 |
| 159 // platform specific file name of the extension library |
| 160 private static string GetNativeLibraryFilename() |
| 161 { |
| 162 if (PlatformApis.IsWindows) |
| 163 { |
| 164 return "grpc_csharp_ext.dll"; |
| 165 } |
| 166 if (PlatformApis.IsLinux) |
| 167 { |
| 168 return "libgrpc_csharp_ext.so"; |
| 169 } |
| 170 if (PlatformApis.IsMacOSX) |
| 171 { |
| 172 return "libgrpc_csharp_ext.dylib"; |
| 173 } |
| 174 throw new InvalidOperationException("Unsupported platform."); |
| 175 } |
| 176 } |
| 177 } |
OLD | NEW |