Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 35 | using System.Runtime.InteropServices; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 36 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using Grpc.Core.Internal; |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 38 | using Grpc.Core.Logging; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 39 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 40 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 41 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | { |
| 43 | /// <summary> |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 44 | /// Encapsulates initialization and shutdown of gRPC library. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | /// </summary> |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 46 | public class GrpcEnvironment |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 47 | { |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 48 | const int THREAD_POOL_SIZE = 4; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 50 | [DllImport("grpc_csharp_ext.dll")] |
| 51 | static extern void grpcsharp_init(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 52 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 53 | [DllImport("grpc_csharp_ext.dll")] |
| 54 | static extern void grpcsharp_shutdown(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 55 | |
| 56 | static object staticLock = new object(); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 57 | static GrpcEnvironment instance; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 58 | |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 59 | static ILogger logger = new ConsoleLogger(); |
| 60 | |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 61 | readonly GrpcThreadPool threadPool; |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 62 | readonly CompletionRegistry completionRegistry; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 63 | readonly DebugStats debugStats = new DebugStats(); |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 64 | bool isClosed; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 65 | |
| 66 | /// <summary> |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 67 | /// Returns an instance of initialized gRPC environment. |
| 68 | /// Subsequent invocations return the same instance unless Shutdown has been called first. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 69 | /// </summary> |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 70 | internal static GrpcEnvironment GetInstance() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 71 | { |
| 72 | lock (staticLock) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 73 | { |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 74 | if (instance == null) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 75 | { |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 76 | instance = new GrpcEnvironment(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 77 | } |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 78 | return instance; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 83 | /// Shuts down the gRPC environment if it was initialized before. |
| 84 | /// Blocks until the environment has been fully shutdown. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 85 | /// </summary> |
| 86 | public static void Shutdown() |
| 87 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 88 | lock (staticLock) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 89 | { |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 90 | if (instance != null) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 91 | { |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 92 | instance.Close(); |
| 93 | instance = null; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 98 | /// <summary> |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 99 | /// Gets application-wide logger used by gRPC. |
| 100 | /// </summary> |
| 101 | /// <value>The logger.</value> |
| 102 | public static ILogger Logger |
| 103 | { |
| 104 | get |
| 105 | { |
| 106 | return logger; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Sets the application-wide logger that should be used by gRPC. |
| 112 | /// </summary> |
| 113 | public static void SetLogger(ILogger customLogger) |
| 114 | { |
| 115 | Preconditions.CheckNotNull(customLogger); |
| 116 | logger = customLogger; |
| 117 | } |
| 118 | |
| 119 | /// <summary> |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 120 | /// Creates gRPC environment. |
| 121 | /// </summary> |
| 122 | private GrpcEnvironment() |
| 123 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 124 | NativeLogRedirector.Redirect(); |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 125 | grpcsharp_init(); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 126 | completionRegistry = new CompletionRegistry(this); |
| 127 | threadPool = new GrpcThreadPool(this, THREAD_POOL_SIZE); |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 128 | threadPool.Start(); |
| 129 | // TODO: use proper logging here |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 130 | Logger.Info("gRPC initialized."); |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /// <summary> |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 134 | /// Gets the completion registry used by this gRPC environment. |
| 135 | /// </summary> |
| 136 | internal CompletionRegistry CompletionRegistry |
| 137 | { |
| 138 | get |
| 139 | { |
| 140 | return this.completionRegistry; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Gets the completion queue used by this gRPC environment. |
| 146 | /// </summary> |
| 147 | internal CompletionQueueSafeHandle CompletionQueue |
| 148 | { |
| 149 | get |
| 150 | { |
| 151 | return this.threadPool.CompletionQueue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// <summary> |
| 156 | /// Gets the completion queue used by this gRPC environment. |
| 157 | /// </summary> |
| 158 | internal DebugStats DebugStats |
| 159 | { |
| 160 | get |
| 161 | { |
| 162 | return this.debugStats; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// <summary> |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 167 | /// Shuts down this environment. |
| 168 | /// </summary> |
| 169 | private void Close() |
| 170 | { |
| 171 | if (isClosed) |
| 172 | { |
| 173 | throw new InvalidOperationException("Close has already been called"); |
| 174 | } |
| 175 | threadPool.Stop(); |
| 176 | grpcsharp_shutdown(); |
| 177 | isClosed = true; |
| 178 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 179 | debugStats.CheckOK(); |
| 180 | |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 181 | Logger.Info("gRPC shutdown."); |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 182 | } |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 183 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 184 | /// <summary> |
| 185 | /// Shuts down this environment asynchronously. |
| 186 | /// </summary> |
| 187 | private Task CloseAsync() |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 188 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 189 | return Task.Run(() => |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 190 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 191 | try |
| 192 | { |
| 193 | Close(); |
| 194 | } |
| 195 | catch (Exception e) |
| 196 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 197 | Logger.Error(e, "Error occured while shutting down GrpcEnvironment."); |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 198 | } |
| 199 | }); |
Jan Tattermusch | 1b54fcf | 2015-05-01 14:30:16 -0700 | [diff] [blame] | 200 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 201 | } |
| 202 | } |