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 | e6d1de6 | 2016-05-25 19:32:15 -0400 | [diff] [blame] | 303 | handle.RequestCall((success, ctx) => HandleNewServerRpc(success, ctx, cq), cq); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 304 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 307 | /// <summary> |
| 308 | /// Checks that all ports have been bound successfully. |
| 309 | /// </summary> |
| 310 | private void CheckPortsBoundSuccessfully() |
| 311 | { |
| 312 | lock (myLock) |
| 313 | { |
Jan Tattermusch | c409646 | 2017-06-01 21:56:24 +0200 | [diff] [blame] | 314 | var unboundPort = ports.FirstOrDefault(port => port.BoundPort == 0); |
| 315 | if (unboundPort != null) |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 316 | { |
Jan Tattermusch | c409646 | 2017-06-01 21:56:24 +0200 | [diff] [blame] | 317 | throw new IOException( |
| 318 | string.Format("Failed to bind port \"{0}:{1}\"", unboundPort.Host, unboundPort.Port)); |
Jan Tattermusch | 9d56717 | 2017-05-26 14:19:23 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 323 | private void DisposeHandle() |
| 324 | { |
| 325 | var activeCallCount = activeCallCounter.Count; |
| 326 | if (activeCallCount > 0) |
| 327 | { |
| 328 | Logger.Warning("Server shutdown has finished but there are still {0} active calls for that server.", activeCallCount); |
| 329 | } |
| 330 | handle.Dispose(); |
| 331 | } |
| 332 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 333 | /// <summary> |
| 334 | /// Selects corresponding handler for given call and handles the call. |
| 335 | /// </summary> |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 336 | private async Task HandleCallAsync(ServerRpcNew newRpc, CompletionQueueSafeHandle cq, Action continuation) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 337 | { |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 338 | try |
| 339 | { |
| 340 | IServerCallHandler callHandler; |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 341 | if (!callHandlers.TryGetValue(newRpc.Method, out callHandler)) |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 342 | { |
Jan Tattermusch | f72ead3 | 2016-10-18 19:02:46 +0200 | [diff] [blame] | 343 | callHandler = UnimplementedMethodCallHandler.Instance; |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 344 | } |
Jan Tattermusch | b089320 | 2016-05-25 19:20:39 -0400 | [diff] [blame] | 345 | await callHandler.HandleCall(newRpc, cq).ConfigureAwait(false); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 346 | } |
| 347 | catch (Exception e) |
| 348 | { |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 349 | Logger.Warning(e, "Exception while handling RPC."); |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 350 | } |
Jan Tattermusch | ba09884 | 2016-10-24 11:19:41 +0200 | [diff] [blame] | 351 | finally |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 352 | { |
| 353 | continuation(); |
| 354 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 357 | /// <summary> |
| 358 | /// Handles the native callback. |
| 359 | /// </summary> |
Jan Tattermusch | 6e90171 | 2016-10-21 00:42:37 +0200 | [diff] [blame] | 360 | private void HandleNewServerRpc(bool success, RequestCallContextSafeHandle ctx, CompletionQueueSafeHandle cq) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 361 | { |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 362 | bool nextRpcRequested = false; |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 363 | if (success) |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 364 | { |
Jan Tattermusch | 6e90171 | 2016-10-21 00:42:37 +0200 | [diff] [blame] | 365 | var newRpc = ctx.GetServerRpcNew(this); |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 366 | |
| 367 | // after server shutdown, the callback returns with null call |
| 368 | if (!newRpc.Call.IsInvalid) |
| 369 | { |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 370 | nextRpcRequested = true; |
| 371 | |
| 372 | // Start asynchronous handler for the call. |
| 373 | // Don't await, the continuations will run on gRPC thread pool once triggered |
| 374 | // by cq.Next(). |
| 375 | #pragma warning disable 4014 |
| 376 | HandleCallAsync(newRpc, cq, () => AllowOneRpc(cq)); |
| 377 | #pragma warning restore 4014 |
Jan Tattermusch | 5bbd818 | 2015-07-20 20:48:40 -0700 | [diff] [blame] | 378 | } |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 379 | } |
Jan Tattermusch | a00698f | 2016-10-17 14:53:48 +0200 | [diff] [blame] | 380 | |
| 381 | if (!nextRpcRequested) |
| 382 | { |
| 383 | AllowOneRpc(cq); |
| 384 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Jan Tattermusch | 97e294a | 2015-04-23 14:30:59 -0700 | [diff] [blame] | 387 | /// <summary> |
| 388 | /// Handles native callback. |
| 389 | /// </summary> |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 390 | private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx) |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 391 | { |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 392 | shutdownTcs.SetResult(null); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 393 | } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 394 | |
| 395 | /// <summary> |
| 396 | /// Collection of service definitions. |
| 397 | /// </summary> |
| 398 | public class ServiceDefinitionCollection : IEnumerable<ServerServiceDefinition> |
| 399 | { |
| 400 | readonly Server server; |
| 401 | |
| 402 | internal ServiceDefinitionCollection(Server server) |
| 403 | { |
| 404 | this.server = server; |
| 405 | } |
| 406 | |
| 407 | /// <summary> |
| 408 | /// Adds a service definition to the server. This is how you register |
| 409 | /// handlers for a service with the server. Only call this before Start(). |
| 410 | /// </summary> |
| 411 | public void Add(ServerServiceDefinition serviceDefinition) |
| 412 | { |
| 413 | server.AddServiceDefinitionInternal(serviceDefinition); |
| 414 | } |
| 415 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 416 | /// <summary> |
| 417 | /// Gets enumerator for this collection. |
| 418 | /// </summary> |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 419 | public IEnumerator<ServerServiceDefinition> GetEnumerator() |
| 420 | { |
| 421 | return server.serviceDefinitionsList.GetEnumerator(); |
| 422 | } |
| 423 | |
| 424 | IEnumerator IEnumerable.GetEnumerator() |
| 425 | { |
| 426 | return server.serviceDefinitionsList.GetEnumerator(); |
| 427 | } |
| 428 | } |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 429 | |
| 430 | /// <summary> |
| 431 | /// Collection of server ports. |
| 432 | /// </summary> |
| 433 | public class ServerPortCollection : IEnumerable<ServerPort> |
| 434 | { |
| 435 | readonly Server server; |
| 436 | |
| 437 | internal ServerPortCollection(Server server) |
| 438 | { |
| 439 | this.server = server; |
| 440 | } |
| 441 | |
| 442 | /// <summary> |
| 443 | /// Adds a new port on which server should listen. |
| 444 | /// Only call this before Start(). |
| 445 | /// <returns>The port on which server will be listening.</returns> |
| 446 | /// </summary> |
| 447 | public int Add(ServerPort serverPort) |
| 448 | { |
| 449 | return server.AddPortInternal(serverPort); |
| 450 | } |
| 451 | |
| 452 | /// <summary> |
| 453 | /// Adds a new port on which server should listen. |
| 454 | /// <returns>The port on which server will be listening.</returns> |
| 455 | /// </summary> |
| 456 | /// <param name="host">the host</param> |
| 457 | /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> |
| 458 | /// <param name="credentials">credentials to use to secure this port.</param> |
| 459 | public int Add(string host, int port, ServerCredentials credentials) |
| 460 | { |
| 461 | return Add(new ServerPort(host, port, credentials)); |
| 462 | } |
| 463 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 464 | /// <summary> |
| 465 | /// Gets enumerator for this collection. |
| 466 | /// </summary> |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 467 | public IEnumerator<ServerPort> GetEnumerator() |
| 468 | { |
| 469 | return server.serverPortList.GetEnumerator(); |
| 470 | } |
| 471 | |
| 472 | IEnumerator IEnumerable.GetEnumerator() |
| 473 | { |
| 474 | return server.serverPortList.GetEnumerator(); |
| 475 | } |
| 476 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 477 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 478 | } |