| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | public sealed class RemoteEndpointMessageProperty |
| | | 10 | | { |
| | | 11 | | private string _address; |
| | | 12 | | private int _port; |
| | | 13 | | private IPEndPoint _remoteEndPoint; |
| | | 14 | | private IRemoteEndpointProvider _remoteEndpointProvider; |
| | | 15 | | private InitializationState _state; |
| | | 16 | | |
| | 11 | 17 | | public RemoteEndpointMessageProperty(string address, int port) |
| | | 18 | | { |
| | 11 | 19 | | if (string.IsNullOrEmpty(address)) |
| | | 20 | | { |
| | 0 | 21 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(address)); |
| | | 22 | | } |
| | | 23 | | |
| | 11 | 24 | | if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) |
| | | 25 | | { |
| | 0 | 26 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(port), |
| | 0 | 27 | | SR.Format(SR.ValueMustBeInRange, IPEndPoint.MinPort, IPEndPoint.MaxPort)); |
| | | 28 | | } |
| | | 29 | | |
| | 11 | 30 | | _port = port; |
| | 11 | 31 | | _address = address; |
| | 11 | 32 | | _state = InitializationState.All; |
| | 11 | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | internal RemoteEndpointMessageProperty(IRemoteEndpointProvider remoteEndpointProvider) |
| | | 36 | | { |
| | 0 | 37 | | _remoteEndpointProvider = remoteEndpointProvider; |
| | 0 | 38 | | } |
| | | 39 | | |
| | 739 | 40 | | public RemoteEndpointMessageProperty(IPEndPoint remoteEndPoint) |
| | | 41 | | { |
| | 739 | 42 | | _remoteEndPoint = remoteEndPoint; |
| | 739 | 43 | | } |
| | | 44 | | |
| | | 45 | | public const string Name = "CoreWCF.Channels.RemoteEndpointMessageProperty"; |
| | | 46 | | |
| | | 47 | | public string Address |
| | | 48 | | { |
| | | 49 | | get |
| | | 50 | | { |
| | 37 | 51 | | if ((_state & InitializationState.Address) != InitializationState.Address) |
| | | 52 | | { |
| | 35 | 53 | | lock (ThisLock) |
| | | 54 | | { |
| | 35 | 55 | | if ((_state & InitializationState.Address) != InitializationState.Address) |
| | | 56 | | { |
| | 35 | 57 | | Initialize(false); |
| | | 58 | | } |
| | 35 | 59 | | } |
| | | 60 | | } |
| | 37 | 61 | | return _address; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public int Port |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 34 | 69 | | if ((_state & InitializationState.Port) != InitializationState.Port) |
| | | 70 | | { |
| | 0 | 71 | | lock (ThisLock) |
| | | 72 | | { |
| | 0 | 73 | | if ((_state & InitializationState.Port) != InitializationState.Port) |
| | | 74 | | { |
| | 0 | 75 | | Initialize(true); |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | } |
| | 34 | 79 | | return _port; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 785 | 83 | | private object ThisLock { get; } = new object(); |
| | | 84 | | |
| | | 85 | | private void Initialize(bool getHostedPort) |
| | | 86 | | { |
| | 35 | 87 | | if (_remoteEndPoint != null) |
| | | 88 | | { |
| | 35 | 89 | | _address = _remoteEndPoint.Address.ToString(); |
| | 35 | 90 | | _port = _remoteEndPoint.Port; |
| | 35 | 91 | | _state = InitializationState.All; |
| | 35 | 92 | | _remoteEndPoint = null; |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 0 | 96 | | if ((_state & InitializationState.Address) != InitializationState.Address) |
| | | 97 | | { |
| | 0 | 98 | | _address = _remoteEndpointProvider.GetAddress(); |
| | 0 | 99 | | _state |= InitializationState.Address; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | if (getHostedPort) |
| | | 103 | | { |
| | 0 | 104 | | _port = _remoteEndpointProvider.GetPort(); |
| | 0 | 105 | | _state |= InitializationState.Port; |
| | 0 | 106 | | _remoteEndpointProvider = null; |
| | | 107 | | } |
| | | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal interface IRemoteEndpointProvider |
| | | 112 | | { |
| | | 113 | | string GetAddress(); |
| | | 114 | | int GetPort(); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | [Flags] |
| | | 118 | | private enum InitializationState |
| | | 119 | | { |
| | | 120 | | None = 0, |
| | | 121 | | Address = 1, |
| | | 122 | | Port = 2, |
| | | 123 | | All = 3 |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |