| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using Microsoft.Extensions.Logging; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Net; |
| | 7 | |
|
| | 8 | | namespace IceRpc.Internal; |
| | 9 | |
|
| | 10 | | /// <summary>Provides a decorator that adds logging to the <see cref="IProtocolConnection" />.</summary> |
| | 11 | | internal class LogProtocolConnectionDecorator : IProtocolConnection |
| | 12 | | { |
| 60 | 13 | | private bool IsServer => _remoteNetworkAddress is not null; |
| | 14 | |
|
| | 15 | | // _connectionInformation is not volatile because all correct callers of IProtocolConnection.ConnectAsync wait for |
| | 16 | | // the connection establishment to complete (successfully or not) before calling any other method on |
| | 17 | | // IProtocolConnection, including DisposeAsync. |
| | 18 | | private TransportConnectionInformation? _connectionInformation; |
| | 19 | |
|
| | 20 | | private readonly IProtocolConnection _decoratee; |
| | 21 | |
|
| | 22 | | private readonly ILogger _logger; |
| | 23 | |
|
| | 24 | | private readonly EndPoint? _remoteNetworkAddress; |
| | 25 | |
|
| | 26 | | private readonly ServerAddress _serverAddress; |
| | 27 | |
|
| | 28 | | public async Task<(TransportConnectionInformation ConnectionInformation, Task ShutdownRequested)> ConnectAsync( |
| | 29 | | CancellationToken cancellationToken) |
| 28 | 30 | | { |
| | 31 | | try |
| 28 | 32 | | { |
| 28 | 33 | | (_connectionInformation, Task shutdownRequested) = await _decoratee.ConnectAsync(cancellationToken) |
| 28 | 34 | | .ConfigureAwait(false); |
| | 35 | |
|
| 24 | 36 | | _logger.LogConnectionConnected( |
| 24 | 37 | | IsServer, |
| 24 | 38 | | _connectionInformation.LocalNetworkAddress, |
| 24 | 39 | | _connectionInformation.RemoteNetworkAddress); |
| | 40 | |
|
| 24 | 41 | | return (_connectionInformation, shutdownRequested); |
| | 42 | | } |
| 4 | 43 | | catch (Exception exception) |
| 4 | 44 | | { |
| 4 | 45 | | if (_remoteNetworkAddress is null) |
| 4 | 46 | | { |
| 4 | 47 | | _logger.LogConnectionConnectFailed(_serverAddress, exception); |
| 4 | 48 | | } |
| | 49 | | else |
| 0 | 50 | | { |
| 0 | 51 | | _logger.LogConnectionConnectFailed(_serverAddress, _remoteNetworkAddress, exception); |
| 0 | 52 | | } |
| 4 | 53 | | throw; |
| | 54 | | } |
| 24 | 55 | | } |
| | 56 | |
|
| | 57 | | public async ValueTask DisposeAsync() |
| 28 | 58 | | { |
| 28 | 59 | | await _decoratee.DisposeAsync().ConfigureAwait(false); |
| | 60 | |
|
| 28 | 61 | | if (_connectionInformation is not null) |
| 24 | 62 | | { |
| 24 | 63 | | _logger.LogConnectionDisposed( |
| 24 | 64 | | IsServer, |
| 24 | 65 | | _connectionInformation.LocalNetworkAddress, |
| 24 | 66 | | _connectionInformation.RemoteNetworkAddress); |
| 24 | 67 | | } |
| 28 | 68 | | } |
| | 69 | |
|
| | 70 | | public Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) => |
| 12 | 71 | | _decoratee.InvokeAsync(request, cancellationToken); |
| | 72 | |
|
| | 73 | | public async Task ShutdownAsync(CancellationToken cancellationToken) |
| 12 | 74 | | { |
| | 75 | | try |
| 12 | 76 | | { |
| 12 | 77 | | await _decoratee.ShutdownAsync(cancellationToken).ConfigureAwait(false); |
| | 78 | |
|
| 8 | 79 | | Debug.Assert(_connectionInformation is not null); |
| | 80 | |
|
| 8 | 81 | | _logger.LogConnectionShutdown( |
| 8 | 82 | | IsServer, |
| 8 | 83 | | _connectionInformation.LocalNetworkAddress, |
| 8 | 84 | | _connectionInformation.RemoteNetworkAddress); |
| 8 | 85 | | } |
| 4 | 86 | | catch (Exception exception) when (_connectionInformation is not null) |
| 4 | 87 | | { |
| 4 | 88 | | _logger.LogConnectionShutdownFailed( |
| 4 | 89 | | IsServer, |
| 4 | 90 | | _connectionInformation.LocalNetworkAddress, |
| 4 | 91 | | _connectionInformation.RemoteNetworkAddress, |
| 4 | 92 | | exception); |
| 4 | 93 | | throw; |
| | 94 | | } |
| 8 | 95 | | } |
| | 96 | |
|
| 28 | 97 | | internal LogProtocolConnectionDecorator( |
| 28 | 98 | | IProtocolConnection decoratee, |
| 28 | 99 | | ServerAddress serverAddress, |
| 28 | 100 | | EndPoint? remoteNetworkAddress, |
| 28 | 101 | | ILogger logger) |
| 28 | 102 | | { |
| 28 | 103 | | _decoratee = decoratee; |
| 28 | 104 | | _logger = logger; |
| 28 | 105 | | _remoteNetworkAddress = remoteNetworkAddress; |
| 28 | 106 | | _serverAddress = serverAddress; |
| 28 | 107 | | } |
| | 108 | | } |