| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Internal; |
| | | 4 | | using IceRpc.Transports; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Net; |
| | | 9 | | using System.Net.Security; |
| | | 10 | | using System.Security.Authentication; |
| | | 11 | | |
| | | 12 | | namespace IceRpc; |
| | | 13 | | |
| | | 14 | | /// <summary>A server accepts connections from clients and dispatches the requests it receives over these connections. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class Server : IAsyncDisposable |
| | | 17 | | { |
| | 142 | 18 | | private readonly LinkedList<IProtocolConnection> _connections = new(); |
| | | 19 | | |
| | | 20 | | private readonly TimeSpan _connectTimeout; |
| | | 21 | | |
| | | 22 | | // A detached connection is a protocol connection that we've decided to connect, or that is connecting, shutting |
| | | 23 | | // down or being disposed. It counts towards _maxConnections and both Server.ShutdownAsync and DisposeAsync wait for |
| | | 24 | | // detached connections to reach 0 using _detachedConnectionsTcs. Such a connection is "detached" because it's not |
| | | 25 | | // in _connections. |
| | | 26 | | private int _detachedConnectionCount; |
| | | 27 | | |
| | 142 | 28 | | private readonly TaskCompletionSource _detachedConnectionsTcs = new(); |
| | | 29 | | |
| | | 30 | | // A cancellation token source that is canceled by DisposeAsync. |
| | 142 | 31 | | private readonly CancellationTokenSource _disposedCts = new(); |
| | | 32 | | |
| | | 33 | | private Task? _disposeTask; |
| | | 34 | | |
| | | 35 | | private readonly Func<IConnectorListener> _listenerFactory; |
| | | 36 | | |
| | | 37 | | private Task? _listenTask; |
| | | 38 | | |
| | | 39 | | private readonly int _maxConnections; |
| | | 40 | | |
| | | 41 | | private readonly int _maxPendingConnections; |
| | | 42 | | |
| | 142 | 43 | | private readonly object _mutex = new(); |
| | | 44 | | |
| | | 45 | | private readonly ServerAddress _serverAddress; |
| | | 46 | | |
| | | 47 | | // A cancellation token source canceled by ShutdownAsync and DisposeAsync. |
| | | 48 | | private readonly CancellationTokenSource _shutdownCts; |
| | | 49 | | |
| | | 50 | | private Task? _shutdownTask; |
| | | 51 | | |
| | | 52 | | private readonly TimeSpan _shutdownTimeout; |
| | | 53 | | |
| | | 54 | | /// <summary>Constructs a server.</summary> |
| | | 55 | | /// <param name="options">The server options.</param> |
| | | 56 | | /// <param name="duplexServerTransport">The transport used to create ice protocol connections. The <see |
| | | 57 | | /// langword="null" /> value is equivalent to <see cref="IDuplexServerTransport.Default" />.</param> |
| | | 58 | | /// <param name="multiplexedServerTransport">The transport used to create icerpc protocol connections. The <see |
| | | 59 | | /// langword="null" /> value is equivalent to <see cref="IMultiplexedServerTransport.Default" />.</param> |
| | | 60 | | /// <param name="logger">The logger. <see langword="null" /> is equivalent to <see cref="NullLogger.Instance" |
| | | 61 | | /// />.</param> |
| | 142 | 62 | | public Server( |
| | 142 | 63 | | ServerOptions options, |
| | 142 | 64 | | IDuplexServerTransport? duplexServerTransport = null, |
| | 142 | 65 | | IMultiplexedServerTransport? multiplexedServerTransport = null, |
| | 142 | 66 | | ILogger? logger = null) |
| | 142 | 67 | | { |
| | 142 | 68 | | if (options.ConnectionOptions.Dispatcher is null) |
| | 0 | 69 | | { |
| | 0 | 70 | | throw new ArgumentException($"{nameof(ServerOptions.ConnectionOptions.Dispatcher)} cannot be null"); |
| | | 71 | | } |
| | | 72 | | |
| | 142 | 73 | | logger ??= NullLogger.Instance; |
| | | 74 | | |
| | 142 | 75 | | _shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(_disposedCts.Token); |
| | | 76 | | |
| | 142 | 77 | | duplexServerTransport ??= IDuplexServerTransport.Default; |
| | 142 | 78 | | multiplexedServerTransport ??= IMultiplexedServerTransport.Default; |
| | 142 | 79 | | _maxConnections = options.MaxConnections; |
| | 142 | 80 | | _maxPendingConnections = options.MaxPendingConnections; |
| | | 81 | | |
| | 142 | 82 | | _connectTimeout = options.ConnectTimeout; |
| | 142 | 83 | | _shutdownTimeout = options.ShutdownTimeout; |
| | | 84 | | |
| | 142 | 85 | | _serverAddress = options.ServerAddress; |
| | 142 | 86 | | if (_serverAddress.Transport is null) |
| | 132 | 87 | | { |
| | 132 | 88 | | _serverAddress = _serverAddress with |
| | 132 | 89 | | { |
| | 132 | 90 | | Transport = _serverAddress.Protocol == Protocol.Ice ? |
| | 132 | 91 | | duplexServerTransport.Name : multiplexedServerTransport.Name |
| | 132 | 92 | | }; |
| | 132 | 93 | | } |
| | | 94 | | |
| | 142 | 95 | | _listenerFactory = () => |
| | 140 | 96 | | { |
| | 142 | 97 | | IConnectorListener listener; |
| | 140 | 98 | | if (_serverAddress.Protocol == Protocol.Ice) |
| | 37 | 99 | | { |
| | 37 | 100 | | IListener<IDuplexConnection> transportListener = duplexServerTransport.Listen( |
| | 37 | 101 | | _serverAddress, |
| | 37 | 102 | | new DuplexConnectionOptions |
| | 37 | 103 | | { |
| | 37 | 104 | | MinSegmentSize = options.ConnectionOptions.MinSegmentSize, |
| | 37 | 105 | | Pool = options.ConnectionOptions.Pool, |
| | 37 | 106 | | }, |
| | 37 | 107 | | options.ServerAuthenticationOptions); |
| | 142 | 108 | | |
| | 37 | 109 | | listener = new IceConnectorListener( |
| | 37 | 110 | | transportListener, |
| | 37 | 111 | | options.ConnectionOptions); |
| | 37 | 112 | | } |
| | 142 | 113 | | else |
| | 103 | 114 | | { |
| | 103 | 115 | | IListener<IMultiplexedConnection> transportListener = multiplexedServerTransport.Listen( |
| | 103 | 116 | | _serverAddress, |
| | 103 | 117 | | new MultiplexedConnectionOptions |
| | 103 | 118 | | { |
| | 103 | 119 | | MaxBidirectionalStreams = options.ConnectionOptions.MaxIceRpcBidirectionalStreams, |
| | 103 | 120 | | // Add an additional stream for the icerpc protocol control stream. |
| | 103 | 121 | | MaxUnidirectionalStreams = options.ConnectionOptions.MaxIceRpcUnidirectionalStreams + 1, |
| | 103 | 122 | | MinSegmentSize = options.ConnectionOptions.MinSegmentSize, |
| | 103 | 123 | | Pool = options.ConnectionOptions.Pool |
| | 103 | 124 | | }, |
| | 103 | 125 | | options.ServerAuthenticationOptions); |
| | 142 | 126 | | |
| | 103 | 127 | | listener = new IceRpcConnectorListener( |
| | 103 | 128 | | transportListener, |
| | 103 | 129 | | options.ConnectionOptions, |
| | 103 | 130 | | logger == NullLogger.Instance ? null : new LogTaskExceptionObserver(logger)); |
| | 103 | 131 | | } |
| | 142 | 132 | | |
| | 140 | 133 | | listener = new MetricsConnectorListenerDecorator(listener); |
| | 140 | 134 | | if (logger != NullLogger.Instance) |
| | 20 | 135 | | { |
| | 20 | 136 | | listener = new LogConnectorListenerDecorator(listener, logger); |
| | 20 | 137 | | } |
| | 140 | 138 | | return listener; |
| | 282 | 139 | | }; |
| | 142 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary>Constructs a server with the specified dispatcher and authentication options. All other properties |
| | | 143 | | /// use the <see cref="ServerOptions" /> defaults.</summary> |
| | | 144 | | /// <param name="dispatcher">The dispatcher of the server.</param> |
| | | 145 | | /// <param name="serverAuthenticationOptions">The SSL server authentication options. When not <see langword="null" |
| | | 146 | | /// />, the server will accept only secure connections.</param> |
| | | 147 | | /// <param name="duplexServerTransport">The transport used to create ice protocol connections. <see langword="null" |
| | | 148 | | /// /> is equivalent to <see cref="IDuplexServerTransport.Default" />.</param> |
| | | 149 | | /// <param name="multiplexedServerTransport">The transport used to create icerpc protocol connections. <see |
| | | 150 | | /// langword="null" /> is equivalent to <see cref="IMultiplexedServerTransport.Default" />.</param> |
| | | 151 | | /// <param name="logger">The logger. <see langword="null" /> is equivalent to <see cref="NullLogger.Instance" |
| | | 152 | | /// />.</param> |
| | | 153 | | public Server( |
| | | 154 | | IDispatcher dispatcher, |
| | | 155 | | SslServerAuthenticationOptions? serverAuthenticationOptions = null, |
| | | 156 | | IDuplexServerTransport? duplexServerTransport = null, |
| | | 157 | | IMultiplexedServerTransport? multiplexedServerTransport = null, |
| | | 158 | | ILogger? logger = null) |
| | 2 | 159 | | : this( |
| | 2 | 160 | | new ServerOptions |
| | 2 | 161 | | { |
| | 2 | 162 | | ServerAuthenticationOptions = serverAuthenticationOptions, |
| | 2 | 163 | | ConnectionOptions = new() |
| | 2 | 164 | | { |
| | 2 | 165 | | Dispatcher = dispatcher, |
| | 2 | 166 | | } |
| | 2 | 167 | | }, |
| | 2 | 168 | | duplexServerTransport, |
| | 2 | 169 | | multiplexedServerTransport, |
| | 2 | 170 | | logger) |
| | 2 | 171 | | { |
| | 2 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary>Constructs a server with the specified dispatcher, server address and authentication options. All |
| | | 175 | | /// other properties use the <see cref="ServerOptions" /> defaults.</summary> |
| | | 176 | | /// <param name="dispatcher">The dispatcher of the server.</param> |
| | | 177 | | /// <param name="serverAddress">The server address of the server.</param> |
| | | 178 | | /// <param name="serverAuthenticationOptions">The SSL server authentication options. When not <see langword="null" |
| | | 179 | | /// />, the server will accept only secure connections.</param> |
| | | 180 | | /// <param name="duplexServerTransport">The transport used to create ice protocol connections. <see langword="null" |
| | | 181 | | /// /> is equivalent to <see cref="IDuplexServerTransport.Default" />.</param> |
| | | 182 | | /// <param name="multiplexedServerTransport">The transport used to create icerpc protocol connections. <see |
| | | 183 | | /// langword="null" /> is equivalent to <see cref="IMultiplexedServerTransport.Default" />.</param> |
| | | 184 | | /// <param name="logger">The logger. <see langword="null" /> is equivalent to <see cref="NullLogger.Instance" |
| | | 185 | | /// />.</param> |
| | | 186 | | public Server( |
| | | 187 | | IDispatcher dispatcher, |
| | | 188 | | ServerAddress serverAddress, |
| | | 189 | | SslServerAuthenticationOptions? serverAuthenticationOptions = null, |
| | | 190 | | IDuplexServerTransport? duplexServerTransport = null, |
| | | 191 | | IMultiplexedServerTransport? multiplexedServerTransport = null, |
| | | 192 | | ILogger? logger = null) |
| | 50 | 193 | | : this( |
| | 50 | 194 | | new ServerOptions |
| | 50 | 195 | | { |
| | 50 | 196 | | ServerAuthenticationOptions = serverAuthenticationOptions, |
| | 50 | 197 | | ConnectionOptions = new() |
| | 50 | 198 | | { |
| | 50 | 199 | | Dispatcher = dispatcher, |
| | 50 | 200 | | }, |
| | 50 | 201 | | ServerAddress = serverAddress |
| | 50 | 202 | | }, |
| | 50 | 203 | | duplexServerTransport, |
| | 50 | 204 | | multiplexedServerTransport, |
| | 50 | 205 | | logger) |
| | 50 | 206 | | { |
| | 50 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary>Constructs a server with the specified dispatcher, server address URI and authentication options. All |
| | | 210 | | /// other properties use the <see cref="ServerOptions" /> defaults.</summary> |
| | | 211 | | /// <param name="dispatcher">The dispatcher of the server.</param> |
| | | 212 | | /// <param name="serverAddressUri">A URI that represents the server address of the server.</param> |
| | | 213 | | /// <param name="serverAuthenticationOptions">The SSL server authentication options. When not <see langword="null" |
| | | 214 | | /// />, the server will accept only secure connections.</param> |
| | | 215 | | /// <param name="duplexServerTransport">The transport used to create ice protocol connections. <see langword="null" |
| | | 216 | | /// /> is equivalent to <see cref="IDuplexServerTransport.Default" />.</param> |
| | | 217 | | /// <param name="multiplexedServerTransport">The transport used to create icerpc protocol connections. <see |
| | | 218 | | /// langword="null" /> is equivalent to <see cref="IMultiplexedServerTransport.Default" />.</param> |
| | | 219 | | /// <param name="logger">The logger. <see langword="null" /> is equivalent to <see cref="NullLogger.Instance" |
| | | 220 | | /// />.</param> |
| | | 221 | | public Server( |
| | | 222 | | IDispatcher dispatcher, |
| | | 223 | | Uri serverAddressUri, |
| | | 224 | | SslServerAuthenticationOptions? serverAuthenticationOptions = null, |
| | | 225 | | IDuplexServerTransport? duplexServerTransport = null, |
| | | 226 | | IMultiplexedServerTransport? multiplexedServerTransport = null, |
| | | 227 | | ILogger? logger = null) |
| | 10 | 228 | | : this( |
| | 10 | 229 | | dispatcher, |
| | 10 | 230 | | new ServerAddress(serverAddressUri), |
| | 10 | 231 | | serverAuthenticationOptions, |
| | 10 | 232 | | duplexServerTransport, |
| | 10 | 233 | | multiplexedServerTransport, |
| | 10 | 234 | | logger) |
| | 10 | 235 | | { |
| | 10 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary>Releases all resources allocated by this server. The server stops listening for new connections and |
| | | 239 | | /// disposes the connections it accepted from clients.</summary> |
| | | 240 | | /// <returns>A value task that completes when the disposal of all connections accepted by the server has completed. |
| | | 241 | | /// This includes connections that were active when this method is called and connections whose disposal was |
| | | 242 | | /// initiated prior to this call.</returns> |
| | | 243 | | /// <remarks>The disposal of an underlying connection of the server aborts invocations, cancels dispatches and |
| | | 244 | | /// disposes the underlying transport connection without waiting for the peer. To wait for invocations and |
| | | 245 | | /// dispatches to complete, call <see cref="ShutdownAsync" /> first. If the configured dispatcher does not complete |
| | | 246 | | /// promptly when its cancellation token is canceled, the disposal can hang.</remarks> |
| | | 247 | | public ValueTask DisposeAsync() |
| | 144 | 248 | | { |
| | 144 | 249 | | lock (_mutex) |
| | 144 | 250 | | { |
| | 144 | 251 | | if (_disposeTask is null) |
| | 142 | 252 | | { |
| | 142 | 253 | | _shutdownTask ??= Task.CompletedTask; |
| | 142 | 254 | | if (_detachedConnectionCount == 0) |
| | 128 | 255 | | { |
| | 128 | 256 | | _ = _detachedConnectionsTcs.TrySetResult(); |
| | 128 | 257 | | } |
| | | 258 | | |
| | 142 | 259 | | _disposeTask = PerformDisposeAsync(); |
| | 142 | 260 | | } |
| | 144 | 261 | | return new(_disposeTask); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | async Task PerformDisposeAsync() |
| | 142 | 265 | | { |
| | 142 | 266 | | await Task.Yield(); // exit mutex lock |
| | | 267 | | |
| | 142 | 268 | | _disposedCts.Cancel(); |
| | | 269 | | |
| | | 270 | | // _listenTask etc are immutable when _disposeTask is not null. |
| | | 271 | | |
| | 142 | 272 | | if (_listenTask is not null) |
| | 140 | 273 | | { |
| | | 274 | | // Wait for shutdown before disposing connections. |
| | | 275 | | try |
| | 140 | 276 | | { |
| | 140 | 277 | | await Task.WhenAll(_listenTask, _shutdownTask).ConfigureAwait(false); |
| | 140 | 278 | | } |
| | 0 | 279 | | catch |
| | 0 | 280 | | { |
| | | 281 | | // Ignore exceptions. |
| | 0 | 282 | | } |
| | | 283 | | |
| | 140 | 284 | | await Task.WhenAll( |
| | 140 | 285 | | _connections |
| | 80 | 286 | | .Select(connection => connection.DisposeAsync().AsTask()) |
| | 140 | 287 | | .Append(_detachedConnectionsTcs.Task)).ConfigureAwait(false); |
| | 140 | 288 | | } |
| | | 289 | | |
| | 142 | 290 | | _disposedCts.Dispose(); |
| | 142 | 291 | | _shutdownCts.Dispose(); |
| | 142 | 292 | | } |
| | 144 | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <summary>Starts accepting connections on the configured server address. Requests received over these connections |
| | | 296 | | /// are then dispatched by the configured dispatcher.</summary> |
| | | 297 | | /// <returns>The server address this server is listening on and that a client would connect to. This address is the |
| | | 298 | | /// same as the <see cref="ServerOptions.ServerAddress" /> of <see cref="ServerOptions" /> except its |
| | | 299 | | /// <see cref="ServerAddress.Transport" /> property is always non-null and its port number is never 0 when the host |
| | | 300 | | /// is an IP address.</returns> |
| | | 301 | | /// <exception cref="IceRpcException">Thrown when the server transport fails to listen on the configured <see |
| | | 302 | | /// cref="ServerOptions.ServerAddress" />.</exception> |
| | | 303 | | /// <exception cref="InvalidOperationException">Thrown when the server is already listening, shut down or shutting |
| | | 304 | | /// down.</exception> |
| | | 305 | | /// <exception cref="ObjectDisposedException">Throw when the server is disposed.</exception> |
| | | 306 | | /// <remarks><see cref="Listen" /> can also throw exceptions from the transport; for example, the transport can |
| | | 307 | | /// reject the server address.</remarks> |
| | | 308 | | public ServerAddress Listen() |
| | 144 | 309 | | { |
| | 144 | 310 | | lock (_mutex) |
| | 144 | 311 | | { |
| | 144 | 312 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 313 | | |
| | 142 | 314 | | if (_shutdownTask is not null) |
| | 0 | 315 | | { |
| | 0 | 316 | | throw new InvalidOperationException($"Server '{this}' is shut down or shutting down."); |
| | | 317 | | } |
| | 142 | 318 | | if (_listenTask is not null) |
| | 2 | 319 | | { |
| | 2 | 320 | | throw new InvalidOperationException($"Server '{this}' is already listening."); |
| | | 321 | | } |
| | | 322 | | |
| | 140 | 323 | | IConnectorListener listener = _listenerFactory(); |
| | 140 | 324 | | _listenTask = ListenAsync(listener); // _listenTask owns listener and must dispose it |
| | 140 | 325 | | return listener.ServerAddress; |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | async Task ListenAsync(IConnectorListener listener) |
| | 140 | 329 | | { |
| | 140 | 330 | | await Task.Yield(); // exit mutex lock |
| | | 331 | | |
| | | 332 | | try |
| | 140 | 333 | | { |
| | 140 | 334 | | using var pendingConnectionSemaphore = new SemaphoreSlim( |
| | 140 | 335 | | _maxPendingConnections, |
| | 140 | 336 | | _maxPendingConnections); |
| | | 337 | | |
| | 279 | 338 | | while (!_shutdownCts.IsCancellationRequested) |
| | 279 | 339 | | { |
| | 279 | 340 | | await pendingConnectionSemaphore.WaitAsync(_shutdownCts.Token).ConfigureAwait(false); |
| | | 341 | | |
| | 277 | 342 | | IConnector? connector = null; |
| | | 343 | | do |
| | 407 | 344 | | { |
| | | 345 | | try |
| | 407 | 346 | | { |
| | 407 | 347 | | (connector, _) = await listener.AcceptAsync(_shutdownCts.Token).ConfigureAwait(false); |
| | 139 | 348 | | } |
| | 268 | 349 | | catch (Exception exception) when (IsRetryableAcceptException(exception)) |
| | 130 | 350 | | { |
| | | 351 | | // continue |
| | 130 | 352 | | } |
| | 269 | 353 | | } |
| | 269 | 354 | | while (connector is null); |
| | | 355 | | |
| | | 356 | | // We don't wait for the connection to be activated or shutdown. This could take a while for some |
| | | 357 | | // transports such as TLS based transports where the handshake requires few round trips between the |
| | | 358 | | // client and server. Waiting could also cause a security issue if the client doesn't respond to the |
| | | 359 | | // connection initialization as we wouldn't be able to accept new connections in the meantime. The |
| | | 360 | | // call will eventually timeout if the ConnectTimeout expires. |
| | 139 | 361 | | CancellationToken cancellationToken = _disposedCts.Token; |
| | 139 | 362 | | _ = Task.Run( |
| | 139 | 363 | | async () => |
| | 139 | 364 | | { |
| | 139 | 365 | | try |
| | 139 | 366 | | { |
| | 139 | 367 | | await ConnectAsync(connector, cancellationToken).ConfigureAwait(false); |
| | 125 | 368 | | } |
| | 14 | 369 | | catch |
| | 14 | 370 | | { |
| | 139 | 371 | | // Ignore connection establishment failure. This failures are logged by the |
| | 139 | 372 | | // LogConnectorDecorator |
| | 14 | 373 | | } |
| | 139 | 374 | | finally |
| | 139 | 375 | | { |
| | 139 | 376 | | // The connection dispose will dispose the transport connection if it has not been |
| | 139 | 377 | | // adopted by the protocol connection. |
| | 139 | 378 | | await connector.DisposeAsync().ConfigureAwait(false); |
| | 139 | 379 | | |
| | 139 | 380 | | // The pending connection semaphore is disposed by the listen task completion once |
| | 139 | 381 | | // shutdown / dispose is initiated. |
| | 139 | 382 | | lock (_mutex) |
| | 139 | 383 | | { |
| | 139 | 384 | | if (_shutdownTask is null) |
| | 129 | 385 | | { |
| | 129 | 386 | | pendingConnectionSemaphore.Release(); |
| | 129 | 387 | | } |
| | 139 | 388 | | } |
| | 139 | 389 | | } |
| | 139 | 390 | | }, |
| | 139 | 391 | | CancellationToken.None); // the task must run to dispose the connector. |
| | 139 | 392 | | } |
| | 0 | 393 | | } |
| | 140 | 394 | | catch |
| | 140 | 395 | | { |
| | | 396 | | // Ignore. Exceptions thrown by listener.AcceptAsync are logged by the log decorator when appropriate. |
| | 140 | 397 | | } |
| | | 398 | | finally |
| | 140 | 399 | | { |
| | 140 | 400 | | await listener.DisposeAsync().ConfigureAwait(false); |
| | 140 | 401 | | } |
| | | 402 | | |
| | | 403 | | async Task ConnectAsync(IConnector connector, CancellationToken cancellationToken) |
| | 139 | 404 | | { |
| | 139 | 405 | | using var connectCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 139 | 406 | | connectCts.CancelAfter(_connectTimeout); |
| | | 407 | | |
| | | 408 | | // Connect the transport connection first. This connection establishment can be interrupted by the |
| | | 409 | | // connect timeout or the server ShutdownAsync/DisposeAsync. |
| | 139 | 410 | | TransportConnectionInformation transportConnectionInformation = |
| | 139 | 411 | | await connector.ConnectTransportConnectionAsync(connectCts.Token).ConfigureAwait(false); |
| | | 412 | | |
| | 129 | 413 | | IProtocolConnection? protocolConnection = null; |
| | 129 | 414 | | bool serverBusy = false; |
| | | 415 | | |
| | 129 | 416 | | lock (_mutex) |
| | 129 | 417 | | { |
| | 129 | 418 | | Debug.Assert( |
| | 129 | 419 | | _maxConnections == 0 || _connections.Count + _detachedConnectionCount <= _maxConnections); |
| | | 420 | | |
| | 129 | 421 | | if (_shutdownTask is null) |
| | 129 | 422 | | { |
| | 129 | 423 | | if (_maxConnections > 0 && (_connections.Count + _detachedConnectionCount) == _maxConnections) |
| | 14 | 424 | | { |
| | 14 | 425 | | serverBusy = true; |
| | 14 | 426 | | } |
| | | 427 | | else |
| | 115 | 428 | | { |
| | | 429 | | // The protocol connection adopts the transport connection from the connector and it's |
| | | 430 | | // now responsible for disposing of it. |
| | 115 | 431 | | protocolConnection = connector.CreateProtocolConnection(transportConnectionInformation); |
| | 115 | 432 | | _detachedConnectionCount++; |
| | 115 | 433 | | } |
| | 129 | 434 | | } |
| | 129 | 435 | | } |
| | | 436 | | |
| | 129 | 437 | | if (protocolConnection is null) |
| | 14 | 438 | | { |
| | | 439 | | try |
| | 14 | 440 | | { |
| | 14 | 441 | | await connector.RefuseTransportConnectionAsync(serverBusy, connectCts.Token) |
| | 14 | 442 | | .ConfigureAwait(false); |
| | 10 | 443 | | } |
| | 4 | 444 | | catch |
| | 4 | 445 | | { |
| | | 446 | | // ignore and continue |
| | 4 | 447 | | } |
| | | 448 | | // The transport connection is disposed by the disposal of the connector. |
| | 14 | 449 | | } |
| | | 450 | | else |
| | 115 | 451 | | { |
| | | 452 | | Task shutdownRequested; |
| | | 453 | | try |
| | 115 | 454 | | { |
| | 115 | 455 | | (_, shutdownRequested) = await protocolConnection.ConnectAsync(connectCts.Token) |
| | 115 | 456 | | .ConfigureAwait(false); |
| | 111 | 457 | | } |
| | 4 | 458 | | catch |
| | 4 | 459 | | { |
| | 4 | 460 | | await DisposeDetachedConnectionAsync(protocolConnection, withShutdown: false) |
| | 4 | 461 | | .ConfigureAwait(false); |
| | 4 | 462 | | throw; |
| | | 463 | | } |
| | | 464 | | |
| | 111 | 465 | | LinkedListNode<IProtocolConnection>? listNode = null; |
| | | 466 | | |
| | 111 | 467 | | lock (_mutex) |
| | | 468 | | { |
| | 111 | 469 | | if (_shutdownTask is null) |
| | | 470 | | { |
| | 111 | 471 | | listNode = _connections.AddLast(protocolConnection); |
| | | 472 | | |
| | | 473 | | // protocolConnection is no longer a detached connection since it's now "attached" in |
| | | 474 | | // _connections. |
| | 111 | 475 | | _detachedConnectionCount--; |
| | | 476 | | } |
| | 111 | 477 | | } |
| | | 478 | | |
| | 111 | 479 | | if (listNode is null) |
| | | 480 | | { |
| | 0 | 481 | | await DisposeDetachedConnectionAsync(protocolConnection, withShutdown: true) |
| | 0 | 482 | | .ConfigureAwait(false); |
| | | 483 | | } |
| | | 484 | | else |
| | 111 | 485 | | { |
| | | 486 | | // Schedule removal after successful ConnectAsync. |
| | 111 | 487 | | _ = ShutdownWhenRequestedAsync(protocolConnection, shutdownRequested, listNode); |
| | | 488 | | } |
| | 111 | 489 | | } |
| | | 490 | | } |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | async Task DisposeDetachedConnectionAsync(IProtocolConnection connection, bool withShutdown) |
| | 35 | 494 | | { |
| | 35 | 495 | | if (withShutdown) |
| | 31 | 496 | | { |
| | | 497 | | // _disposedCts is not disposed since we own a _backgroundConnectionDisposeCount. |
| | 31 | 498 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource(_disposedCts.Token); |
| | 31 | 499 | | cts.CancelAfter(_shutdownTimeout); |
| | | 500 | | |
| | | 501 | | try |
| | 31 | 502 | | { |
| | | 503 | | // Can be canceled by DisposeAsync or the shutdown timeout. |
| | 31 | 504 | | await connection.ShutdownAsync(cts.Token).ConfigureAwait(false); |
| | 16 | 505 | | } |
| | 15 | 506 | | catch |
| | 15 | 507 | | { |
| | | 508 | | // Ignore connection shutdown failures. connection.ShutdownAsync makes sure it's an "expected" |
| | | 509 | | // exception. |
| | 15 | 510 | | } |
| | 31 | 511 | | } |
| | | 512 | | |
| | 35 | 513 | | await connection.DisposeAsync().ConfigureAwait(false); |
| | 35 | 514 | | lock (_mutex) |
| | 35 | 515 | | { |
| | 35 | 516 | | if (--_detachedConnectionCount == 0 && _shutdownTask is not null) |
| | 16 | 517 | | { |
| | 16 | 518 | | _detachedConnectionsTcs.SetResult(); |
| | 16 | 519 | | } |
| | 35 | 520 | | } |
| | 35 | 521 | | } |
| | | 522 | | |
| | | 523 | | // Remove the connection from _connections after a successful ConnectAsync. |
| | | 524 | | async Task ShutdownWhenRequestedAsync( |
| | | 525 | | IProtocolConnection connection, |
| | | 526 | | Task shutdownRequested, |
| | | 527 | | LinkedListNode<IProtocolConnection> listNode) |
| | 111 | 528 | | { |
| | 111 | 529 | | await shutdownRequested.ConfigureAwait(false); |
| | | 530 | | |
| | 89 | 531 | | lock (_mutex) |
| | 89 | 532 | | { |
| | 89 | 533 | | if (_shutdownTask is null) |
| | 31 | 534 | | { |
| | 31 | 535 | | _connections.Remove(listNode); |
| | 31 | 536 | | _detachedConnectionCount++; |
| | 31 | 537 | | } |
| | | 538 | | else |
| | 58 | 539 | | { |
| | | 540 | | // _connections is immutable and ShutdownAsync/DisposeAsync is responsible to shutdown/dispose |
| | | 541 | | // this connection. |
| | 58 | 542 | | return; |
| | | 543 | | } |
| | 31 | 544 | | } |
| | | 545 | | |
| | 31 | 546 | | await DisposeDetachedConnectionAsync(connection, withShutdown: true).ConfigureAwait(false); |
| | 89 | 547 | | } |
| | 405 | 548 | | } |
| | | 549 | | |
| | | 550 | | /// <summary>Gracefully shuts down this server: the server stops accepting new connections and shuts down gracefully |
| | | 551 | | /// all its connections.</summary> |
| | | 552 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 553 | | /// <returns>A task that completes successfully once the shutdown of all connections accepted by the server has |
| | | 554 | | /// completed. This includes connections that were active when this method is called and connections whose shutdown |
| | | 555 | | /// was initiated prior to this call. This task can also complete with one of the following exceptions: |
| | | 556 | | /// <list type="bullet"> |
| | | 557 | | /// <item><description><see cref="IceRpcException" /> with error <see cref="IceRpcError.OperationAborted" /> if the |
| | | 558 | | /// server is disposed while being shut down.</description></item> |
| | | 559 | | /// <item><description><see cref="OperationCanceledException" /> if cancellation was requested through the |
| | | 560 | | /// cancellation token.</description></item> |
| | | 561 | | /// <item><description><see cref="TimeoutException" /> if the shutdown timed out.</description></item> |
| | | 562 | | /// </list> |
| | | 563 | | /// </returns> |
| | | 564 | | /// <exception cref="InvalidOperationException">Thrown if this method is called more than once.</exception> |
| | | 565 | | /// <exception cref="ObjectDisposedException">Thrown if the server is disposed.</exception> |
| | | 566 | | public Task ShutdownAsync(CancellationToken cancellationToken = default) |
| | 36 | 567 | | { |
| | 36 | 568 | | lock (_mutex) |
| | 36 | 569 | | { |
| | 36 | 570 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 571 | | |
| | 36 | 572 | | if (_shutdownTask is not null) |
| | 0 | 573 | | { |
| | 0 | 574 | | throw new InvalidOperationException($"Server '{this}' is shut down or shutting down."); |
| | | 575 | | } |
| | | 576 | | |
| | 36 | 577 | | if (_detachedConnectionCount == 0) |
| | 34 | 578 | | { |
| | 34 | 579 | | _detachedConnectionsTcs.SetResult(); |
| | 34 | 580 | | } |
| | | 581 | | |
| | 36 | 582 | | _shutdownTask = PerformShutdownAsync(); |
| | 36 | 583 | | } |
| | 36 | 584 | | return _shutdownTask; |
| | | 585 | | |
| | | 586 | | async Task PerformShutdownAsync() |
| | 36 | 587 | | { |
| | 36 | 588 | | await Task.Yield(); // exit mutex lock |
| | | 589 | | |
| | 36 | 590 | | _shutdownCts.Cancel(); |
| | | 591 | | |
| | | 592 | | // _listenTask is immutable once _shutdownTask is not null. |
| | 36 | 593 | | if (_listenTask is not null) |
| | 36 | 594 | | { |
| | | 595 | | try |
| | 36 | 596 | | { |
| | 36 | 597 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource( |
| | 36 | 598 | | cancellationToken, |
| | 36 | 599 | | _disposedCts.Token); |
| | | 600 | | |
| | 36 | 601 | | cts.CancelAfter(_shutdownTimeout); |
| | | 602 | | |
| | | 603 | | try |
| | 36 | 604 | | { |
| | 36 | 605 | | await Task.WhenAll( |
| | 36 | 606 | | _connections |
| | 24 | 607 | | .Select(connection => connection.ShutdownAsync(cts.Token)) |
| | 36 | 608 | | .Append(_listenTask.WaitAsync(cts.Token)) |
| | 36 | 609 | | .Append(_detachedConnectionsTcs.Task.WaitAsync(cts.Token))) |
| | 36 | 610 | | .ConfigureAwait(false); |
| | 36 | 611 | | } |
| | 0 | 612 | | catch (OperationCanceledException) |
| | 0 | 613 | | { |
| | 0 | 614 | | throw; |
| | | 615 | | } |
| | 0 | 616 | | catch |
| | 0 | 617 | | { |
| | | 618 | | // Ignore _listenTask and connection shutdown exceptions |
| | | 619 | | |
| | | 620 | | // Throw OperationCanceledException if this WhenAll exception is hiding an OCE. |
| | 0 | 621 | | cts.Token.ThrowIfCancellationRequested(); |
| | 0 | 622 | | } |
| | 36 | 623 | | } |
| | 0 | 624 | | catch (OperationCanceledException) |
| | 0 | 625 | | { |
| | 0 | 626 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 627 | | |
| | 0 | 628 | | if (_disposedCts.IsCancellationRequested) |
| | 0 | 629 | | { |
| | 0 | 630 | | throw new IceRpcException( |
| | 0 | 631 | | IceRpcError.OperationAborted, |
| | 0 | 632 | | "The shutdown was aborted because the server was disposed."); |
| | | 633 | | } |
| | | 634 | | else |
| | 0 | 635 | | { |
| | 0 | 636 | | throw new TimeoutException( |
| | 0 | 637 | | $"The server shut down timed out after {_shutdownTimeout.TotalSeconds} s."); |
| | | 638 | | } |
| | | 639 | | } |
| | 36 | 640 | | } |
| | 36 | 641 | | } |
| | 36 | 642 | | } |
| | | 643 | | |
| | | 644 | | /// <summary>Returns a string that represents this server.</summary> |
| | | 645 | | /// <returns>A string that represents this server.</returns> |
| | 2 | 646 | | public override string ToString() => _serverAddress.ToString(); |
| | | 647 | | |
| | | 648 | | /// <summary>Returns true if the <see cref="IConnectorListener.AcceptAsync" /> failure can be retried.</summary> |
| | | 649 | | private static bool IsRetryableAcceptException(Exception exception) => |
| | | 650 | | // Transports such as Quic do the SSL handshake when the connection is accepted, this can throw |
| | | 651 | | // AuthenticationException if it fails. |
| | 268 | 652 | | exception is IceRpcException or AuthenticationException; |
| | | 653 | | |
| | | 654 | | /// <summary>Provides a decorator that adds logging to a <see cref="IConnectorListener" />.</summary> |
| | | 655 | | private class LogConnectorListenerDecorator : IConnectorListener |
| | | 656 | | { |
| | 92 | 657 | | public ServerAddress ServerAddress => _decoratee.ServerAddress; |
| | | 658 | | |
| | | 659 | | private readonly IConnectorListener _decoratee; |
| | | 660 | | private readonly ILogger _logger; |
| | | 661 | | |
| | | 662 | | public async Task<(IConnector, EndPoint)> AcceptAsync(CancellationToken cancellationToken) |
| | 36 | 663 | | { |
| | | 664 | | try |
| | 36 | 665 | | { |
| | 36 | 666 | | (IConnector connector, EndPoint remoteNetworkAddress) = |
| | 36 | 667 | | await _decoratee.AcceptAsync(cancellationToken).ConfigureAwait(false); |
| | | 668 | | |
| | 16 | 669 | | _logger.LogConnectionAccepted(ServerAddress, remoteNetworkAddress); |
| | 16 | 670 | | return ( |
| | 16 | 671 | | new LogConnectorDecorator(connector, ServerAddress, remoteNetworkAddress, _logger), |
| | 16 | 672 | | remoteNetworkAddress); |
| | | 673 | | } |
| | 20 | 674 | | catch (OperationCanceledException exception) when (exception.CancellationToken == cancellationToken) |
| | 20 | 675 | | { |
| | | 676 | | // Do not log this exception. The AcceptAsync call can fail with OperationCanceledException during |
| | | 677 | | // shutdown once the shutdown cancellation token is canceled. |
| | 20 | 678 | | throw; |
| | | 679 | | } |
| | 0 | 680 | | catch (ObjectDisposedException) |
| | 0 | 681 | | { |
| | | 682 | | // Do not log this exception. The AcceptAsync call can fail with ObjectDisposedException during |
| | | 683 | | // shutdown once the listener is disposed or if it is accepting a connection while the listener is |
| | | 684 | | // disposed. |
| | 0 | 685 | | throw; |
| | | 686 | | } |
| | 0 | 687 | | catch (Exception exception) when (IsRetryableAcceptException(exception)) |
| | 0 | 688 | | { |
| | 0 | 689 | | _logger.LogConnectionAcceptFailedWithRetryableException(ServerAddress, exception); |
| | 0 | 690 | | throw; |
| | | 691 | | } |
| | 0 | 692 | | catch (Exception exception) |
| | 0 | 693 | | { |
| | 0 | 694 | | _logger.LogConnectionAcceptFailed(ServerAddress, exception); |
| | 0 | 695 | | throw; |
| | | 696 | | } |
| | 16 | 697 | | } |
| | | 698 | | |
| | | 699 | | public ValueTask DisposeAsync() |
| | 20 | 700 | | { |
| | 20 | 701 | | _logger.LogStopAcceptingConnections(ServerAddress); |
| | 20 | 702 | | return _decoratee.DisposeAsync(); |
| | 20 | 703 | | } |
| | | 704 | | |
| | 20 | 705 | | internal LogConnectorListenerDecorator(IConnectorListener decoratee, ILogger logger) |
| | 20 | 706 | | { |
| | 20 | 707 | | _decoratee = decoratee; |
| | 20 | 708 | | _logger = logger; |
| | 20 | 709 | | _logger.LogStartAcceptingConnections(ServerAddress); |
| | 20 | 710 | | } |
| | | 711 | | } |
| | | 712 | | |
| | | 713 | | private class LogConnectorDecorator : IConnector |
| | | 714 | | { |
| | | 715 | | private readonly IConnector _decoratee; |
| | | 716 | | private readonly ILogger _logger; |
| | | 717 | | private readonly EndPoint _remoteNetworkAddress; |
| | | 718 | | private readonly ServerAddress _serverAddress; |
| | | 719 | | |
| | | 720 | | public async Task<TransportConnectionInformation> ConnectTransportConnectionAsync( |
| | | 721 | | CancellationToken cancellationToken) |
| | 16 | 722 | | { |
| | | 723 | | try |
| | 16 | 724 | | { |
| | 16 | 725 | | return await _decoratee.ConnectTransportConnectionAsync(cancellationToken).ConfigureAwait(false); |
| | | 726 | | } |
| | 4 | 727 | | catch (Exception exception) |
| | 4 | 728 | | { |
| | 4 | 729 | | _logger.LogConnectionConnectFailed(_serverAddress, _remoteNetworkAddress, exception); |
| | 4 | 730 | | throw; |
| | | 731 | | } |
| | 12 | 732 | | } |
| | | 733 | | |
| | | 734 | | public IProtocolConnection CreateProtocolConnection( |
| | | 735 | | TransportConnectionInformation transportConnectionInformation) => |
| | 12 | 736 | | new LogProtocolConnectionDecorator( |
| | 12 | 737 | | _decoratee.CreateProtocolConnection(transportConnectionInformation), |
| | 12 | 738 | | _serverAddress, |
| | 12 | 739 | | _remoteNetworkAddress, |
| | 12 | 740 | | _logger); |
| | | 741 | | |
| | 16 | 742 | | public ValueTask DisposeAsync() => _decoratee.DisposeAsync(); |
| | | 743 | | |
| | | 744 | | public Task RefuseTransportConnectionAsync(bool serverBusy, CancellationToken cancel) => |
| | 0 | 745 | | _decoratee.RefuseTransportConnectionAsync(serverBusy, cancel); |
| | | 746 | | |
| | 16 | 747 | | internal LogConnectorDecorator( |
| | 16 | 748 | | IConnector decoratee, |
| | 16 | 749 | | ServerAddress serverAddress, |
| | 16 | 750 | | EndPoint remoteNetworkAddress, |
| | 16 | 751 | | ILogger logger) |
| | 16 | 752 | | { |
| | 16 | 753 | | _decoratee = decoratee; |
| | 16 | 754 | | _logger = logger; |
| | 16 | 755 | | _serverAddress = serverAddress; |
| | 16 | 756 | | _remoteNetworkAddress = remoteNetworkAddress; |
| | 16 | 757 | | } |
| | | 758 | | } |
| | | 759 | | |
| | | 760 | | /// <summary>Provides a decorator that adds metrics to a <see cref="IConnectorListener" />.</summary> |
| | | 761 | | private class MetricsConnectorListenerDecorator : IConnectorListener |
| | | 762 | | { |
| | 212 | 763 | | public ServerAddress ServerAddress => _decoratee.ServerAddress; |
| | | 764 | | |
| | | 765 | | private readonly IConnectorListener _decoratee; |
| | | 766 | | |
| | | 767 | | public async Task<(IConnector, EndPoint)> AcceptAsync( |
| | | 768 | | CancellationToken cancellationToken) |
| | 407 | 769 | | { |
| | 407 | 770 | | (IConnector connector, EndPoint remoteNetworkAddress) = |
| | 407 | 771 | | await _decoratee.AcceptAsync(cancellationToken).ConfigureAwait(false); |
| | 139 | 772 | | return (new MetricsConnectorDecorator(connector), remoteNetworkAddress); |
| | 139 | 773 | | } |
| | | 774 | | |
| | 140 | 775 | | public ValueTask DisposeAsync() => _decoratee.DisposeAsync(); |
| | | 776 | | |
| | 140 | 777 | | internal MetricsConnectorListenerDecorator(IConnectorListener decoratee) => |
| | 140 | 778 | | _decoratee = decoratee; |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | private class MetricsConnectorDecorator : IConnector |
| | | 782 | | { |
| | | 783 | | private readonly IConnector _decoratee; |
| | | 784 | | |
| | | 785 | | public async Task<TransportConnectionInformation> ConnectTransportConnectionAsync( |
| | | 786 | | CancellationToken cancellationToken) |
| | 139 | 787 | | { |
| | 139 | 788 | | Metrics.ServerMetrics.ConnectStart(); |
| | | 789 | | try |
| | 139 | 790 | | { |
| | 139 | 791 | | return await _decoratee.ConnectTransportConnectionAsync(cancellationToken).ConfigureAwait(false); |
| | | 792 | | } |
| | 10 | 793 | | catch |
| | 10 | 794 | | { |
| | 10 | 795 | | Metrics.ServerMetrics.ConnectStop(); |
| | 10 | 796 | | Metrics.ServerMetrics.ConnectionFailure(); |
| | 10 | 797 | | throw; |
| | | 798 | | } |
| | 129 | 799 | | } |
| | | 800 | | |
| | | 801 | | public IProtocolConnection CreateProtocolConnection( |
| | | 802 | | TransportConnectionInformation transportConnectionInformation) => |
| | 115 | 803 | | new MetricsProtocolConnectionDecorator( |
| | 115 | 804 | | _decoratee.CreateProtocolConnection(transportConnectionInformation), |
| | 115 | 805 | | Metrics.ServerMetrics, |
| | 115 | 806 | | connectStarted: true); |
| | | 807 | | |
| | 139 | 808 | | public ValueTask DisposeAsync() => _decoratee.DisposeAsync(); |
| | | 809 | | |
| | | 810 | | public async Task RefuseTransportConnectionAsync(bool serverBusy, CancellationToken cancel) |
| | 14 | 811 | | { |
| | | 812 | | try |
| | 14 | 813 | | { |
| | 14 | 814 | | await _decoratee.RefuseTransportConnectionAsync(serverBusy, cancel).ConfigureAwait(false); |
| | 10 | 815 | | } |
| | | 816 | | finally |
| | 14 | 817 | | { |
| | 14 | 818 | | Metrics.ServerMetrics.ConnectionFailure(); |
| | 14 | 819 | | Metrics.ServerMetrics.ConnectStop(); |
| | 14 | 820 | | } |
| | 10 | 821 | | } |
| | | 822 | | |
| | 278 | 823 | | internal MetricsConnectorDecorator(IConnector decoratee) => _decoratee = decoratee; |
| | | 824 | | } |
| | | 825 | | |
| | | 826 | | /// <summary>A connector listener accepts a transport connection and returns a <see cref="IConnector" />. The |
| | | 827 | | /// connector is used to refuse the transport connection or obtain a protocol connection once the transport |
| | | 828 | | /// connection is connected.</summary> |
| | | 829 | | private interface IConnectorListener : IAsyncDisposable |
| | | 830 | | { |
| | | 831 | | ServerAddress ServerAddress { get; } |
| | | 832 | | |
| | | 833 | | Task<(IConnector, EndPoint)> AcceptAsync(CancellationToken cancel); |
| | | 834 | | } |
| | | 835 | | |
| | | 836 | | /// <summary>A connector is returned by <see cref="IConnectorListener" />. The connector allows to connect the |
| | | 837 | | /// transport connection. If successful, the transport connection can either be refused or accepted by creating the |
| | | 838 | | /// protocol connection out of it.</summary> |
| | | 839 | | private interface IConnector : IAsyncDisposable |
| | | 840 | | { |
| | | 841 | | Task<TransportConnectionInformation> ConnectTransportConnectionAsync(CancellationToken cancellationToken); |
| | | 842 | | |
| | | 843 | | IProtocolConnection CreateProtocolConnection(TransportConnectionInformation transportConnectionInformation); |
| | | 844 | | |
| | | 845 | | Task RefuseTransportConnectionAsync(bool serverBusy, CancellationToken cancel); |
| | | 846 | | } |
| | | 847 | | |
| | | 848 | | private class IceConnectorListener : IConnectorListener |
| | | 849 | | { |
| | 73 | 850 | | public ServerAddress ServerAddress => _listener.ServerAddress; |
| | | 851 | | |
| | | 852 | | private readonly IListener<IDuplexConnection> _listener; |
| | | 853 | | private readonly ConnectionOptions _options; |
| | | 854 | | |
| | 37 | 855 | | public ValueTask DisposeAsync() => _listener.DisposeAsync(); |
| | | 856 | | |
| | | 857 | | public async Task<(IConnector, EndPoint)> AcceptAsync(CancellationToken cancel) |
| | 76 | 858 | | { |
| | 76 | 859 | | (IDuplexConnection transportConnection, EndPoint remoteNetworkAddress) = await _listener.AcceptAsync( |
| | 76 | 860 | | cancel).ConfigureAwait(false); |
| | 39 | 861 | | return (new IceConnector(transportConnection, _options), remoteNetworkAddress); |
| | 39 | 862 | | } |
| | | 863 | | |
| | 37 | 864 | | internal IceConnectorListener(IListener<IDuplexConnection> listener, ConnectionOptions options) |
| | 37 | 865 | | { |
| | 37 | 866 | | _listener = listener; |
| | 37 | 867 | | _options = options; |
| | 37 | 868 | | } |
| | | 869 | | } |
| | | 870 | | |
| | | 871 | | private class IceConnector : IConnector |
| | | 872 | | { |
| | | 873 | | private readonly ConnectionOptions _options; |
| | | 874 | | private IDuplexConnection? _transportConnection; |
| | | 875 | | |
| | | 876 | | public Task<TransportConnectionInformation> ConnectTransportConnectionAsync( |
| | | 877 | | CancellationToken cancellationToken) => |
| | 39 | 878 | | _transportConnection!.ConnectAsync(cancellationToken); |
| | | 879 | | |
| | | 880 | | public IProtocolConnection CreateProtocolConnection( |
| | | 881 | | TransportConnectionInformation transportConnectionInformation) |
| | 33 | 882 | | { |
| | | 883 | | // The protocol connection takes ownership of the transport connection. |
| | 33 | 884 | | var protocolConnection = new IceProtocolConnection( |
| | 33 | 885 | | _transportConnection!, |
| | 33 | 886 | | transportConnectionInformation, |
| | 33 | 887 | | _options); |
| | 33 | 888 | | _transportConnection = null; |
| | 33 | 889 | | return protocolConnection; |
| | 33 | 890 | | } |
| | | 891 | | |
| | | 892 | | public ValueTask DisposeAsync() |
| | 39 | 893 | | { |
| | 39 | 894 | | _transportConnection?.Dispose(); |
| | 39 | 895 | | return new(); |
| | 39 | 896 | | } |
| | | 897 | | |
| | | 898 | | public Task RefuseTransportConnectionAsync(bool serverBusy, CancellationToken cancellationToken) |
| | 4 | 899 | | { |
| | 4 | 900 | | _transportConnection!.Dispose(); |
| | 4 | 901 | | return Task.CompletedTask; |
| | 4 | 902 | | } |
| | | 903 | | |
| | 39 | 904 | | internal IceConnector(IDuplexConnection transportConnection, ConnectionOptions options) |
| | 39 | 905 | | { |
| | 39 | 906 | | _transportConnection = transportConnection; |
| | 39 | 907 | | _options = options; |
| | 39 | 908 | | } |
| | | 909 | | } |
| | | 910 | | |
| | | 911 | | private class IceRpcConnectorListener : IConnectorListener |
| | | 912 | | { |
| | 139 | 913 | | public ServerAddress ServerAddress => _listener.ServerAddress; |
| | | 914 | | |
| | | 915 | | private readonly IListener<IMultiplexedConnection> _listener; |
| | | 916 | | private readonly ConnectionOptions _options; |
| | | 917 | | private readonly ITaskExceptionObserver? _taskExceptionObserver; |
| | | 918 | | |
| | | 919 | | public async Task<(IConnector, EndPoint)> AcceptAsync(CancellationToken cancel) |
| | 331 | 920 | | { |
| | 331 | 921 | | (IMultiplexedConnection transportConnection, EndPoint remoteNetworkAddress) = await _listener.AcceptAsync( |
| | 331 | 922 | | cancel).ConfigureAwait(false); |
| | 100 | 923 | | return (new IceRpcConnector(transportConnection, _options, _taskExceptionObserver), remoteNetworkAddress); |
| | 100 | 924 | | } |
| | | 925 | | |
| | 103 | 926 | | public ValueTask DisposeAsync() => _listener.DisposeAsync(); |
| | | 927 | | |
| | 103 | 928 | | internal IceRpcConnectorListener( |
| | 103 | 929 | | IListener<IMultiplexedConnection> listener, |
| | 103 | 930 | | ConnectionOptions options, |
| | 103 | 931 | | ITaskExceptionObserver? taskExceptionObserver) |
| | 103 | 932 | | { |
| | 103 | 933 | | _listener = listener; |
| | 103 | 934 | | _options = options; |
| | 103 | 935 | | _taskExceptionObserver = taskExceptionObserver; |
| | 103 | 936 | | } |
| | | 937 | | } |
| | | 938 | | |
| | | 939 | | private class IceRpcConnector : IConnector |
| | | 940 | | { |
| | | 941 | | private readonly ConnectionOptions _options; |
| | | 942 | | private readonly ITaskExceptionObserver? _taskExceptionObserver; |
| | | 943 | | private IMultiplexedConnection? _transportConnection; |
| | | 944 | | |
| | | 945 | | public Task<TransportConnectionInformation> ConnectTransportConnectionAsync( |
| | | 946 | | CancellationToken cancellationToken) => |
| | 100 | 947 | | _transportConnection!.ConnectAsync(cancellationToken); |
| | | 948 | | |
| | | 949 | | public IProtocolConnection CreateProtocolConnection( |
| | | 950 | | TransportConnectionInformation transportConnectionInformation) |
| | 82 | 951 | | { |
| | | 952 | | // The protocol connection takes ownership of the transport connection. |
| | 82 | 953 | | var protocolConnection = new IceRpcProtocolConnection( |
| | 82 | 954 | | _transportConnection!, |
| | 82 | 955 | | transportConnectionInformation, |
| | 82 | 956 | | _options, |
| | 82 | 957 | | _taskExceptionObserver); |
| | 82 | 958 | | _transportConnection = null; |
| | 82 | 959 | | return protocolConnection; |
| | 82 | 960 | | } |
| | | 961 | | |
| | 100 | 962 | | public ValueTask DisposeAsync() => _transportConnection?.DisposeAsync() ?? new(); |
| | | 963 | | |
| | | 964 | | public Task RefuseTransportConnectionAsync(bool serverBusy, CancellationToken cancellationToken) => |
| | 10 | 965 | | _transportConnection!.CloseAsync( |
| | 10 | 966 | | serverBusy ? MultiplexedConnectionCloseError.ServerBusy : MultiplexedConnectionCloseError.Refused, |
| | 10 | 967 | | cancellationToken); |
| | | 968 | | |
| | 100 | 969 | | internal IceRpcConnector( |
| | 100 | 970 | | IMultiplexedConnection transportConnection, |
| | 100 | 971 | | ConnectionOptions options, |
| | 100 | 972 | | ITaskExceptionObserver? taskExceptionObserver) |
| | 100 | 973 | | { |
| | 100 | 974 | | _transportConnection = transportConnection; |
| | 100 | 975 | | _options = options; |
| | 100 | 976 | | _taskExceptionObserver = taskExceptionObserver; |
| | 100 | 977 | | } |
| | | 978 | | } |
| | | 979 | | } |