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.Collections.Concurrent; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 36 | using System.Collections.Generic; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using System.Diagnostics; |
| 38 | using System.Runtime.InteropServices; |
| 39 | using System.Threading.Tasks; |
| 40 | using Grpc.Core.Internal; |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 41 | using Grpc.Core.Logging; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 42 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 43 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 44 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | { |
| 46 | /// <summary> |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 47 | /// A gRPC server. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 48 | /// </summary> |
| 49 | public class Server |
| 50 | { |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 51 | /// <summary> |
| 52 | /// Pass this value as port to have the server choose an unused listening port for you. |
| 53 | /// </summary> |
| 54 | public const int PickUnusedPort = 0; |
| 55 | |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 56 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Server>(); |
| 57 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 58 | readonly GrpcEnvironment environment; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 59 | readonly List<ChannelOption> options; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 60 | readonly ServerSafeHandle handle; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 61 | readonly object myLock = new object(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 62 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 63 | readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>(); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 64 | readonly TaskCompletionSource<object> shutdownTcs = new TaskCompletionSource<object>(); |
| 65 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 66 | bool startRequested; |
| 67 | bool shutdownRequested; |
| 68 | |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 69 | /// <summary> |
| 70 | /// Create a new server. |
| 71 | /// </summary> |
| 72 | /// <param name="options">Channel options.</param> |
| 73 | public Server(IEnumerable<ChannelOption> options = null) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 74 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 75 | this.environment = GrpcEnvironment.GetInstance(); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 76 | this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); |
| 77 | using (var channelArgs = ChannelOptions.CreateChannelArgs(this.options)) |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 78 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 79 | this.handle = ServerSafeHandle.NewServer(environment.CompletionQueue, channelArgs); |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 80 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 83 | /// <summary> |
| 84 | /// Adds a service definition to the server. This is how you register |
| 85 | /// handlers for a service with the server. |
| 86 | /// Only call this before Start(). |
| 87 | /// </summary> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 88 | public void AddServiceDefinition(ServerServiceDefinition serviceDefinition) |
| 89 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 90 | lock (myLock) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 91 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 92 | Preconditions.CheckState(!startRequested); |
| 93 | foreach (var entry in serviceDefinition.CallHandlers) |
| 94 | { |
| 95 | callHandlers.Add(entry.Key, entry.Value); |
| 96 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 97 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 98 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 99 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 100 | /// <summary> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 101 | /// Add a port on which server should listen. |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 102 | /// Only call this before Start(). |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 103 | /// </summary> |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 104 | /// <returns>The port on which server will be listening.</returns> |
| 105 | /// <param name="host">the host</param> |
| 106 | /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 107 | public int AddPort(string host, int port, ServerCredentials credentials) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 108 | { |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 109 | lock (myLock) |
| 110 | { |
| 111 | Preconditions.CheckNotNull(credentials); |
| 112 | Preconditions.CheckState(!startRequested); |
| 113 | var address = string.Format("{0}:{1}", host, port); |
| 114 | using (var nativeCredentials = credentials.ToNativeCredentials()) |
| 115 | { |
| 116 | if (nativeCredentials != null) |
| 117 | { |
| 118 | return handle.AddSecurePort(address, nativeCredentials); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | return handle.AddInsecurePort(address); |
| 123 | } |
| 124 | } |
| 125 | } |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /// <summary> |
| 129 | /// Starts the server. |
| 130 | /// </summary> |
| 131 | public void Start() |
| 132 | { |
| 133 | lock (myLock) |
| 134 | { |
| 135 | Preconditions.CheckState(!startRequested); |
| 136 | startRequested = true; |
| 137 | |
| 138 | handle.Start(); |
| 139 | AllowOneRpc(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 143 | /// <summary> |
| 144 | /// Requests server shutdown and when there are no more calls being serviced, |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 145 | /// cleans up used resources. The returned task finishes when shutdown procedure |
| 146 | /// is complete. |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 147 | /// </summary> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 148 | public async Task ShutdownAsync() |
| 149 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 150 | lock (myLock) |
| 151 | { |
| 152 | Preconditions.CheckState(startRequested); |
| 153 | Preconditions.CheckState(!shutdownRequested); |
| 154 | shutdownRequested = true; |
| 155 | } |
| 156 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 157 | handle.ShutdownAndNotify(HandleServerShutdown, environment); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 158 | await shutdownTcs.Task; |
| 159 | handle.Dispose(); |
| 160 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 161 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 162 | /// <summary> |
| 163 | /// To allow awaiting termination of the server. |
| 164 | /// </summary> |
| 165 | public Task ShutdownTask |
| 166 | { |
| 167 | get |
| 168 | { |
| 169 | return shutdownTcs.Task; |
| 170 | } |
| 171 | } |
| 172 | |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 173 | /// <summary> |
| 174 | /// Requests server shutdown while cancelling all the in-progress calls. |
| 175 | /// The returned task finishes when shutdown procedure is complete. |
| 176 | /// </summary> |
| 177 | public async Task KillAsync() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 178 | { |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 179 | lock (myLock) |
| 180 | { |
| 181 | Preconditions.CheckState(startRequested); |
| 182 | Preconditions.CheckState(!shutdownRequested); |
| 183 | shutdownRequested = true; |
| 184 | } |
| 185 | |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 186 | handle.ShutdownAndNotify(HandleServerShutdown, environment); |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 187 | handle.CancelAllCalls(); |
| 188 | await shutdownTcs.Task; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 189 | handle.Dispose(); |
| 190 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 191 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 192 | /// <summary> |
| 193 | /// Allows one new RPC call to be received by server. |
| 194 | /// </summary> |
| 195 | private void AllowOneRpc() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 196 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 197 | lock (myLock) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 198 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 199 | if (!shutdownRequested) |
| 200 | { |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 201 | handle.RequestCall(HandleNewServerRpc, environment); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 202 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 203 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 206 | /// <summary> |
| 207 | /// Selects corresponding handler for given call and handles the call. |
| 208 | /// </summary> |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 209 | private async Task HandleCallAsync(ServerRpcNew newRpc) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 210 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 211 | try |
| 212 | { |
| 213 | IServerCallHandler callHandler; |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 214 | if (!callHandlers.TryGetValue(newRpc.Method, out callHandler)) |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 215 | { |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 216 | callHandler = NoSuchMethodCallHandler.Instance; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 217 | } |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 218 | await callHandler.HandleCall(newRpc, environment); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 219 | } |
| 220 | catch (Exception e) |
| 221 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 222 | Logger.Warning(e, "Exception while handling RPC."); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 223 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 226 | /// <summary> |
| 227 | /// Handles the native callback. |
| 228 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 229 | private void HandleNewServerRpc(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 230 | { |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 231 | if (success) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 232 | { |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 233 | ServerRpcNew newRpc = ctx.GetServerRpcNew(); |
| 234 | |
| 235 | // after server shutdown, the callback returns with null call |
| 236 | if (!newRpc.Call.IsInvalid) |
| 237 | { |
| 238 | Task.Run(async () => await HandleCallAsync(newRpc)); |
| 239 | } |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 240 | } |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 241 | |
| 242 | AllowOneRpc(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 245 | /// <summary> |
| 246 | /// Handles native callback. |
| 247 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 248 | private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 249 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 250 | shutdownTcs.SetResult(null); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 251 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 252 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 253 | } |