Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 19 | using System; |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 20 | using System.Collections; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 21 | using System.Collections.Generic; |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 22 | using System.IO; |
Jan Tattermusch | e6d1de6 | 2016-05-25 19:32:15 -0400 | [diff] [blame] | 23 | using System.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 24 | using System.Threading.Tasks; |
| 25 | using Grpc.Core.Internal; |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 26 | using Grpc.Core.Logging; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 27 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 28 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 29 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 30 | { |
| 31 | /// <summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 32 | /// gRPC server. A single server can server arbitrary number of services and can listen on more than one ports. |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 33 | /// </summary> |
| 34 | public class Server |
| 35 | { |
Jan Tattermusch | fac33ad | 2016-10-21 01:42:10 +0200 | [diff] [blame] | 36 | const int DefaultRequestCallTokensPerCq = 2000; |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 37 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Server>(); |
| 38 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 39 | readonly AtomicCounter activeCallCounter = new AtomicCounter(); |
| 40 | |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 41 | readonly ServiceDefinitionCollection serviceDefinitions; |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 42 | readonly ServerPortCollection ports; |
Jan Tattermusch | 04eb89c | 2015-06-12 13:03:05 -0700 | [diff] [blame] | 43 | readonly GrpcEnvironment environment; |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 44 | readonly List<ChannelOption> options; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | readonly ServerSafeHandle handle; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 46 | readonly object myLock = new object(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 47 | |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 48 | readonly List<ServerServiceDefinition> serviceDefinitionsList = new List<ServerServiceDefinition>(); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 49 | readonly List<ServerPort> serverPortList = new List<ServerPort>(); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 50 | readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>(); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 51 | readonly TaskCompletionSource<object> shutdownTcs = new TaskCompletionSource<object>(); |
| 52 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 53 | bool startRequested; |
Jan Tattermusch | 1e1fa08 | 2016-05-09 15:18:11 -0700 | [diff] [blame] | 54 | volatile bool shutdownRequested; |
Jan Tattermusch | fac33ad | 2016-10-21 01:42:10 +0200 | [diff] [blame] | 55 | int requestCallTokensPerCq = DefaultRequestCallTokensPerCq; |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 56 | |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 57 | /// <summary> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 58 | /// Creates a new server. |
| 59 | /// </summary> |
| 60 | public Server() : this(null) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Creates a new server. |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 66 | /// </summary> |
| 67 | /// <param name="options">Channel options.</param> |
Jan Tattermusch | 8d829d0 | 2016-06-06 16:43:54 -0700 | [diff] [blame] | 68 | public Server(IEnumerable<ChannelOption> options) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 69 | { |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 70 | this.serviceDefinitions = new ServiceDefinitionCollection(this); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 71 | this.ports = new ServerPortCollection(this); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 72 | this.environment = GrpcEnvironment.AddRef(); |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 73 | this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>(); |
| 74 | using (var channelArgs = ChannelOptions.CreateChannelArgs(this.options)) |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 75 | { |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 76 | this.handle = ServerSafeHandle.NewServer(channelArgs); |
| 77 | } |
| 78 | |
| 79 | foreach (var cq in environment.CompletionQueues) |
| 80 | { |
| 81 | this.handle.RegisterCompletionQueue(cq); |
Jan Tattermusch | 6d53a5c | 2015-06-08 18:03:05 -0700 | [diff] [blame] | 82 | } |
Jan Tattermusch | 739ee1b | 2016-06-01 14:08:26 -0700 | [diff] [blame] | 83 | GrpcEnvironment.RegisterServer(this); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 86 | /// <summary> |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 87 | /// Services that will be exported by the server once started. Register a service with this |
| 88 | /// server by adding its definition to this collection. |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 89 | /// </summary> |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 90 | public ServiceDefinitionCollection Services |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 91 | { |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 92 | get |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 93 | { |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 94 | return serviceDefinitions; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 95 | } |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 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 | /// <summary> |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 99 | /// Ports on which the server will listen once started. Register a port with this |
| 100 | /// server by adding its definition to this collection. |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 101 | /// </summary> |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 102 | public ServerPortCollection Ports |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 103 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 104 | get |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 105 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 106 | return ports; |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 107 | } |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /// <summary> |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 111 | /// To allow awaiting termination of the server. |
| 112 | /// </summary> |
| 113 | public Task ShutdownTask |
| 114 | { |
| 115 | get |
| 116 | { |
| 117 | return shutdownTcs.Task; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// <summary> |
Jan Tattermusch | fac33ad | 2016-10-21 01:42:10 +0200 | [diff] [blame] | 122 | /// Experimental API. Might anytime change without prior notice. |
| 123 | /// Number or calls requested via grpc_server_request_call at any given time for each completion queue. |
| 124 | /// </summary> |
| 125 | public int RequestCallTokensPerCompletionQueue |
| 126 | { |
| 127 | get |
| 128 | { |
| 129 | return requestCallTokensPerCq; |
| 130 | } |
| 131 | set |
| 132 | { |
| 133 | lock (myLock) |
| 134 | { |
| 135 | GrpcPreconditions.CheckState(!startRequested); |
| 136 | GrpcPreconditions.CheckArgument(value > 0); |
| 137 | requestCallTokensPerCq = value; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// <summary> |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 143 | /// Starts the server. |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 144 | /// Throws <c>IOException</c> if not successful. |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 145 | /// </summary> |
| 146 | public void Start() |
| 147 | { |
| 148 | lock (myLock) |
| 149 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 150 | GrpcPreconditions.CheckState(!startRequested); |
Jan Tattermusch | 8e93533 | 2016-06-08 19:45:54 -0700 | [diff] [blame] | 151 | GrpcPreconditions.CheckState(!shutdownRequested); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 152 | startRequested = true; |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 153 | |
| 154 | CheckPortsBoundSuccessfully(); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 155 | handle.Start(); |
Jan Tattermusch | 26cc142 | 2016-05-04 17:21:17 -0700 | [diff] [blame] | 156 | |
Jan Tattermusch | fac33ad | 2016-10-21 01:42:10 +0200 | [diff] [blame] | 157 | for (int i = 0; i < requestCallTokensPerCq; i++) |
Jan Tattermusch | 26cc142 | 2016-05-04 17:21:17 -0700 | [diff] [blame] | 158 | { |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 159 | foreach (var cq in environment.CompletionQueues) |
| 160 | { |
| 161 | AllowOneRpc(cq); |
| 162 | } |
Jan Tattermusch | 26cc142 | 2016-05-04 17:21:17 -0700 | [diff] [blame] | 163 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 167 | /// <summary> |
| 168 | /// Requests server shutdown and when there are no more calls being serviced, |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 169 | /// cleans up used resources. The returned task finishes when shutdown procedure |
| 170 | /// is complete. |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 171 | /// </summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 172 | /// <remarks> |
| 173 | /// It is strongly recommended to shutdown all previously created servers before exiting from the process. |
| 174 | /// </remarks> |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 175 | public Task ShutdownAsync() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 176 | { |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 177 | return ShutdownInternalAsync(false); |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 180 | /// <summary> |
| 181 | /// Requests server shutdown while cancelling all the in-progress calls. |
| 182 | /// The returned task finishes when shutdown procedure is complete. |
| 183 | /// </summary> |
Jan Tattermusch | a134fa7 | 2016-06-03 17:24:50 -0700 | [diff] [blame] | 184 | /// <remarks> |
| 185 | /// It is strongly recommended to shutdown all previously created servers before exiting from the process. |
| 186 | /// </remarks> |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 187 | public Task KillAsync() |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 188 | { |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 189 | return ShutdownInternalAsync(true); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | internal void AddCallReference(object call) |
| 193 | { |
| 194 | activeCallCounter.Increment(); |
| 195 | |
| 196 | bool success = false; |
| 197 | handle.DangerousAddRef(ref success); |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 198 | GrpcPreconditions.CheckState(success); |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | internal void RemoveCallReference(object call) |
| 202 | { |
| 203 | handle.DangerousRelease(); |
| 204 | activeCallCounter.Decrement(); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 205 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 206 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 207 | /// <summary> |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 208 | /// Shuts down the server. |
| 209 | /// </summary> |
| 210 | private async Task ShutdownInternalAsync(bool kill) |
| 211 | { |
| 212 | lock (myLock) |
| 213 | { |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 214 | GrpcPreconditions.CheckState(!shutdownRequested); |
| 215 | shutdownRequested = true; |
| 216 | } |
Jan Tattermusch | 739ee1b | 2016-06-01 14:08:26 -0700 | [diff] [blame] | 217 | GrpcEnvironment.UnregisterServer(this); |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 218 | |
| 219 | var cq = environment.CompletionQueues.First(); // any cq will do |
| 220 | handle.ShutdownAndNotify(HandleServerShutdown, cq); |
| 221 | if (kill) |
| 222 | { |
| 223 | handle.CancelAllCalls(); |
| 224 | } |
Jan Tattermusch | 739ee1b | 2016-06-01 14:08:26 -0700 | [diff] [blame] | 225 | await ShutdownCompleteOrEnvironmentDeadAsync().ConfigureAwait(false); |
| 226 | |
Jan Tattermusch | 63386a1 | 2016-06-01 12:47:46 -0700 | [diff] [blame] | 227 | DisposeHandle(); |
| 228 | |
| 229 | await GrpcEnvironment.ReleaseAsync().ConfigureAwait(false); |
| 230 | } |
| 231 | |
| 232 | /// <summary> |
Jan Tattermusch | 739ee1b | 2016-06-01 14:08:26 -0700 | [diff] [blame] | 233 | /// In case the environment's threadpool becomes dead, the shutdown completion will |
| 234 | /// never be delivered, but we need to release the environment's handle anyway. |
| 235 | /// </summary> |
| 236 | private async Task ShutdownCompleteOrEnvironmentDeadAsync() |
| 237 | { |
| 238 | while (true) |
| 239 | { |
| 240 | var task = await Task.WhenAny(shutdownTcs.Task, Task.Delay(20)).ConfigureAwait(false); |
| 241 | if (shutdownTcs.Task == task) |
| 242 | { |
| 243 | return; |
| 244 | } |
| 245 | if (!environment.IsAlive) |
| 246 | { |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// <summary> |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 253 | /// Adds a service definition. |
| 254 | /// </summary> |
| 255 | private void AddServiceDefinitionInternal(ServerServiceDefinition serviceDefinition) |
| 256 | { |
| 257 | lock (myLock) |
| 258 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 259 | GrpcPreconditions.CheckState(!startRequested); |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 260 | foreach (var entry in serviceDefinition.CallHandlers) |
| 261 | { |
| 262 | callHandlers.Add(entry.Key, entry.Value); |
| 263 | } |
| 264 | serviceDefinitionsList.Add(serviceDefinition); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /// <summary> |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 269 | /// Adds a listening port. |
| 270 | /// </summary> |
| 271 | private int AddPortInternal(ServerPort serverPort) |
| 272 | { |
| 273 | lock (myLock) |
| 274 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 275 | GrpcPreconditions.CheckNotNull(serverPort.Credentials, "serverPort"); |
| 276 | GrpcPreconditions.CheckState(!startRequested); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 277 | var address = string.Format("{0}:{1}", serverPort.Host, serverPort.Port); |
| 278 | int boundPort; |
| 279 | using (var nativeCredentials = serverPort.Credentials.ToNativeCredentials()) |
| 280 | { |
| 281 | if (nativeCredentials != null) |
| 282 | { |
| 283 | boundPort = handle.AddSecurePort(address, nativeCredentials); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | boundPort = handle.AddInsecurePort(address); |
| 288 | } |
| 289 | } |
| 290 | var newServerPort = new ServerPort(serverPort, boundPort); |
| 291 | this.serverPortList.Add(newServerPort); |
| 292 | return boundPort; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /// <summary> |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 297 | /// Allows one new RPC call to be received by server. |
| 298 | /// </summary> |
Jan Tattermusch | 5ee8e77 | 2016-05-24 16:17:10 -0400 | [diff] [blame] | 299 | private void AllowOneRpc(CompletionQueueSafeHandle cq) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 300 | { |
Jan Tattermusch | 1e1fa08 | 2016-05-09 15:18:11 -0700 | [diff] [blame] | 301 | if (!shutdownRequested) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 302 | { |
Jan Tattermusch | e8b3313 | 2017-11-28 13:54:13 +0100 | [diff] [blame] | 303 | // TODO(jtattermusch): avoid unnecessary delegate allocation |
Jan Tattermusch | e6d1de6 | 2016-05-25 19:32:15 -0400 | [diff] [blame] | 304 | handle.RequestCall((success, ctx) => HandleNewServerRpc(success, ctx, cq), cq); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 305 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 308 | /// <summary> |
| 309 | /// Checks that all ports have been bound successfully. |
| 310 | /// </summary> |
| 311 | private void CheckPortsBoundSuccessfully() |
| 312 | { |
| 313 | lock (myLock) |
| 314 | { |
Jan Tattermusch | c409646 | 2017-06-01 21:56:24 +0200 | [diff] [blame] | 315 | var unboundPort = ports.FirstOrDefault(port => port.BoundPort == 0); |
| 316 | if (unboundPort != null) |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 317 | { |
Jan Tattermusch | c409646 | 2017-06-01 21:56:24 +0200 | [diff] [blame] | 318 | throw new IOException( |
| 319 | string.Format("Failed to bind port \"{0}:{1}\"", unboundPort.Host, unboundPort.Port)); |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 324 | private void DisposeHandle() |
| 325 | { |
| 326 | var activeCallCount = activeCallCounter.Count; |
| 327 | if (activeCallCount > 0) |
| 328 | { |
| 329 | Logger.Warning("Server shutdown has finished but there are still {0} active calls for that server.", activeCallCount); |
| 330 | } |
| 331 | handle.Dispose(); |
| 332 | } |
| 333 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 334 | /// <summary> |
| 335 | /// Selects corresponding handler for given call and handles the call. |
| 336 | /// </summary> |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 337 | private async Task HandleCallAsync(ServerRpcNew newRpc, CompletionQueueSafeHandle cq, Action continuation) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 338 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 339 | try |
| 340 | { |
| 341 | IServerCallHandler callHandler; |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 342 | if (!callHandlers.TryGetValue(newRpc.Method, out callHandler)) |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 343 | { |
Jan Tattermusch | f72ead3 | 2016-10-18 19:02:46 +0200 | [diff] [blame] | 344 | callHandler = UnimplementedMethodCallHandler.Instance; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 345 | } |
Jan Tattermusch | b089320 | 2016-05-25 19:20:39 -0400 | [diff] [blame] | 346 | await callHandler.HandleCall(newRpc, cq).ConfigureAwait(false); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 347 | } |
| 348 | catch (Exception e) |
| 349 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 350 | Logger.Warning(e, "Exception while handling RPC."); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 351 | } |
Jan Tattermusch | ba09884 | 2016-10-24 11:19:41 +0200 | [diff] [blame] | 352 | finally |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 353 | { |
| 354 | continuation(); |
| 355 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 358 | /// <summary> |
| 359 | /// Handles the native callback. |
| 360 | /// </summary> |
Jan Tattermusch | 6e90171 | 2016-10-21 00:42:37 +0200 | [diff] [blame] | 361 | private void HandleNewServerRpc(bool success, RequestCallContextSafeHandle ctx, CompletionQueueSafeHandle cq) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 362 | { |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 363 | bool nextRpcRequested = false; |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 364 | if (success) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 365 | { |
Jan Tattermusch | 6e90171 | 2016-10-21 00:42:37 +0200 | [diff] [blame] | 366 | var newRpc = ctx.GetServerRpcNew(this); |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 367 | |
| 368 | // after server shutdown, the callback returns with null call |
| 369 | if (!newRpc.Call.IsInvalid) |
| 370 | { |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 371 | nextRpcRequested = true; |
| 372 | |
| 373 | // Start asynchronous handler for the call. |
| 374 | // Don't await, the continuations will run on gRPC thread pool once triggered |
| 375 | // by cq.Next(). |
| 376 | #pragma warning disable 4014 |
| 377 | HandleCallAsync(newRpc, cq, () => AllowOneRpc(cq)); |
| 378 | #pragma warning restore 4014 |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 379 | } |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 380 | } |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 381 | |
| 382 | if (!nextRpcRequested) |
| 383 | { |
| 384 | AllowOneRpc(cq); |
| 385 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 388 | /// <summary> |
| 389 | /// Handles native callback. |
| 390 | /// </summary> |
Jan Tattermusch | 8b451ed | 2017-11-20 11:46:48 +0100 | [diff] [blame] | 391 | private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx, object state) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 392 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 393 | shutdownTcs.SetResult(null); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 394 | } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 395 | |
| 396 | /// <summary> |
| 397 | /// Collection of service definitions. |
| 398 | /// </summary> |
| 399 | public class ServiceDefinitionCollection : IEnumerable<ServerServiceDefinition> |
| 400 | { |
| 401 | readonly Server server; |
| 402 | |
| 403 | internal ServiceDefinitionCollection(Server server) |
| 404 | { |
| 405 | this.server = server; |
| 406 | } |
| 407 | |
| 408 | /// <summary> |
| 409 | /// Adds a service definition to the server. This is how you register |
| 410 | /// handlers for a service with the server. Only call this before Start(). |
| 411 | /// </summary> |
| 412 | public void Add(ServerServiceDefinition serviceDefinition) |
| 413 | { |
| 414 | server.AddServiceDefinitionInternal(serviceDefinition); |
| 415 | } |
| 416 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 417 | /// <summary> |
| 418 | /// Gets enumerator for this collection. |
| 419 | /// </summary> |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 420 | public IEnumerator<ServerServiceDefinition> GetEnumerator() |
| 421 | { |
| 422 | return server.serviceDefinitionsList.GetEnumerator(); |
| 423 | } |
| 424 | |
| 425 | IEnumerator IEnumerable.GetEnumerator() |
| 426 | { |
| 427 | return server.serviceDefinitionsList.GetEnumerator(); |
| 428 | } |
| 429 | } |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 430 | |
| 431 | /// <summary> |
| 432 | /// Collection of server ports. |
| 433 | /// </summary> |
| 434 | public class ServerPortCollection : IEnumerable<ServerPort> |
| 435 | { |
| 436 | readonly Server server; |
| 437 | |
| 438 | internal ServerPortCollection(Server server) |
| 439 | { |
| 440 | this.server = server; |
| 441 | } |
| 442 | |
| 443 | /// <summary> |
| 444 | /// Adds a new port on which server should listen. |
| 445 | /// Only call this before Start(). |
| 446 | /// <returns>The port on which server will be listening.</returns> |
| 447 | /// </summary> |
| 448 | public int Add(ServerPort serverPort) |
| 449 | { |
| 450 | return server.AddPortInternal(serverPort); |
| 451 | } |
| 452 | |
| 453 | /// <summary> |
| 454 | /// Adds a new port on which server should listen. |
| 455 | /// <returns>The port on which server will be listening.</returns> |
| 456 | /// </summary> |
| 457 | /// <param name="host">the host</param> |
| 458 | /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> |
| 459 | /// <param name="credentials">credentials to use to secure this port.</param> |
| 460 | public int Add(string host, int port, ServerCredentials credentials) |
| 461 | { |
| 462 | return Add(new ServerPort(host, port, credentials)); |
| 463 | } |
| 464 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 465 | /// <summary> |
| 466 | /// Gets enumerator for this collection. |
| 467 | /// </summary> |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 468 | public IEnumerator<ServerPort> GetEnumerator() |
| 469 | { |
| 470 | return server.serverPortList.GetEnumerator(); |
| 471 | } |
| 472 | |
| 473 | IEnumerator IEnumerable.GetEnumerator() |
| 474 | { |
| 475 | return server.serverPortList.GetEnumerator(); |
| 476 | } |
| 477 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 478 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 479 | } |