| | | 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.Collections.Generic; |
| | | 6 | | using CoreWCF.Channels; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Dispatcher |
| | | 9 | | { |
| | | 10 | | public class EndpointDispatcher |
| | | 11 | | { |
| | | 12 | | private MessageFilter _addressFilter; |
| | | 13 | | private MessageFilter _contractFilter; |
| | | 14 | | private MessageFilter _endpointFilter; |
| | | 15 | | private EndpointIdentity _identity; |
| | | 16 | | |
| | | 17 | | internal EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace, string id, b |
| | 641 | 18 | | : this(address, contractName, contractNamespace) |
| | | 19 | | { |
| | 641 | 20 | | Id = id; |
| | 641 | 21 | | IsSystemEndpoint = isSystemEndpoint; |
| | 641 | 22 | | } |
| | | 23 | | |
| | | 24 | | public EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace) |
| | 641 | 25 | | : this(address, contractName, contractNamespace, false) |
| | | 26 | | { |
| | 641 | 27 | | } |
| | | 28 | | |
| | 664 | 29 | | public EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace, bool isSystemE |
| | | 30 | | { |
| | 664 | 31 | | OriginalAddress = address; |
| | 664 | 32 | | ContractName = contractName; |
| | 664 | 33 | | ContractNamespace = contractNamespace; |
| | | 34 | | |
| | 664 | 35 | | if (address != null) |
| | | 36 | | { |
| | 664 | 37 | | _addressFilter = new EndpointAddressMessageFilter(address); |
| | | 38 | | } |
| | | 39 | | else |
| | | 40 | | { |
| | 0 | 41 | | _addressFilter = new MatchAllMessageFilter(); |
| | | 42 | | } |
| | | 43 | | |
| | 664 | 44 | | _contractFilter = new MatchAllMessageFilter(); |
| | 664 | 45 | | DispatchRuntime = new DispatchRuntime(this); |
| | 664 | 46 | | FilterPriority = 0; |
| | 664 | 47 | | IsSystemEndpoint = isSystemEndpoint; |
| | 664 | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | private EndpointDispatcher(EndpointDispatcher baseEndpoint, IEnumerable<AddressHeader> headers) |
| | | 51 | | { |
| | 0 | 52 | | EndpointAddressBuilder builder = new EndpointAddressBuilder(baseEndpoint.EndpointAddress); |
| | 0 | 53 | | foreach (AddressHeader h in headers) |
| | | 54 | | { |
| | 0 | 55 | | builder.Headers.Add(h); |
| | | 56 | | } |
| | 0 | 57 | | EndpointAddress address = builder.ToEndpointAddress(); |
| | | 58 | | |
| | 0 | 59 | | _addressFilter = new EndpointAddressMessageFilter(address); |
| | | 60 | | // channelDispatcher is Attached |
| | 0 | 61 | | _contractFilter = baseEndpoint.ContractFilter; |
| | 0 | 62 | | ContractName = baseEndpoint.ContractName; |
| | 0 | 63 | | ContractNamespace = baseEndpoint.ContractNamespace; |
| | 0 | 64 | | DispatchRuntime = baseEndpoint.DispatchRuntime; |
| | | 65 | | // endpointFilter is lazy |
| | 0 | 66 | | FilterPriority = baseEndpoint.FilterPriority + 1; |
| | 0 | 67 | | OriginalAddress = address; |
| | | 68 | | //if (PerformanceCounters.PerformanceCountersEnabled) |
| | | 69 | | //{ |
| | | 70 | | // this.perfCounterId = baseEndpoint.perfCounterId; |
| | | 71 | | // this.perfCounterBaseId = baseEndpoint.perfCounterBaseId; |
| | | 72 | | //} |
| | 0 | 73 | | Id = baseEndpoint.Id; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public MessageFilter AddressFilter |
| | | 77 | | { |
| | 0 | 78 | | get { return _addressFilter; } |
| | | 79 | | set |
| | | 80 | | { |
| | 701 | 81 | | ThrowIfDisposedOrImmutable(); |
| | 701 | 82 | | _addressFilter = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value) |
| | 701 | 83 | | AddressFilterSetExplicit = true; |
| | 701 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 1342 | 87 | | internal bool AddressFilterSetExplicit { get; private set; } |
| | | 88 | | |
| | 7389 | 89 | | public ChannelDispatcher ChannelDispatcher { get; private set; } |
| | | 90 | | |
| | | 91 | | public MessageFilter ContractFilter |
| | | 92 | | { |
| | 0 | 93 | | get { return _contractFilter; } |
| | | 94 | | set |
| | | 95 | | { |
| | 701 | 96 | | ThrowIfDisposedOrImmutable(); |
| | 701 | 97 | | _contractFilter = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value |
| | 701 | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | 538 | 101 | | public string ContractName { get; } |
| | | 102 | | |
| | 505 | 103 | | public string ContractNamespace { get; } |
| | | 104 | | |
| | 6165 | 105 | | internal ServiceChannel DatagramChannel { get; set; } |
| | | 106 | | |
| | 18289 | 107 | | public DispatchRuntime DispatchRuntime { get; } |
| | | 108 | | |
| | 0 | 109 | | internal Uri ListenUri { get; } |
| | | 110 | | |
| | 781 | 111 | | internal EndpointAddress OriginalAddress { get; } |
| | | 112 | | |
| | | 113 | | public EndpointAddress EndpointAddress |
| | | 114 | | { |
| | | 115 | | get |
| | | 116 | | { |
| | 35 | 117 | | if (ChannelDispatcher == null) |
| | | 118 | | { |
| | 0 | 119 | | return OriginalAddress; |
| | | 120 | | } |
| | | 121 | | |
| | 35 | 122 | | if ((OriginalAddress != null) && (OriginalAddress.Identity != null)) |
| | | 123 | | { |
| | 0 | 124 | | return OriginalAddress; |
| | | 125 | | } |
| | | 126 | | |
| | 35 | 127 | | if (OriginalAddress != null && _identity == null) |
| | | 128 | | { |
| | 35 | 129 | | return OriginalAddress; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | EndpointAddressBuilder builder; |
| | 0 | 133 | | if (OriginalAddress != null) |
| | | 134 | | { |
| | 0 | 135 | | builder = new EndpointAddressBuilder(OriginalAddress); |
| | 0 | 136 | | builder.Identity = _identity; |
| | 0 | 137 | | return builder.ToEndpointAddress(); |
| | | 138 | | } |
| | | 139 | | else |
| | | 140 | | { |
| | 0 | 141 | | return null; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public EndpointIdentity Identity |
| | | 147 | | { |
| | 0 | 148 | | get { return _identity; } |
| | | 149 | | set |
| | | 150 | | { |
| | 17 | 151 | | if (value != _identity && _identity != null) |
| | | 152 | | { |
| | 0 | 153 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.OnlySetIdentityOnce); |
| | | 154 | | } |
| | | 155 | | |
| | 17 | 156 | | if (value == null) |
| | | 157 | | { |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 159 | | } |
| | | 160 | | |
| | 17 | 161 | | _identity = value; |
| | 17 | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | 2570 | 165 | | public bool IsSystemEndpoint { get; } |
| | | 166 | | |
| | | 167 | | internal MessageFilter EndpointFilter |
| | | 168 | | { |
| | | 169 | | get |
| | | 170 | | { |
| | 3181 | 171 | | if (_endpointFilter == null) |
| | | 172 | | { |
| | 661 | 173 | | MessageFilter addressFilter = _addressFilter; |
| | 661 | 174 | | MessageFilter contractFilter = _contractFilter; |
| | | 175 | | |
| | | 176 | | // Can't optimize addressFilter similarly. |
| | | 177 | | // AndMessageFilter tracks when the address filter matched so the correct |
| | | 178 | | // fault can be sent back. |
| | 661 | 179 | | if (contractFilter is MatchAllMessageFilter) |
| | | 180 | | { |
| | 47 | 181 | | _endpointFilter = addressFilter; |
| | | 182 | | } |
| | | 183 | | else |
| | | 184 | | { |
| | 614 | 185 | | _endpointFilter = new AndMessageFilter(addressFilter, contractFilter); |
| | | 186 | | } |
| | | 187 | | } |
| | 3181 | 188 | | return _endpointFilter; |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 4509 | 192 | | public int FilterPriority { get; set; } |
| | | 193 | | |
| | 692 | 194 | | public string Id { get; set; } |
| | | 195 | | |
| | | 196 | | //internal string PerfCounterId |
| | | 197 | | //{ |
| | | 198 | | // get { return this.perfCounterId; } |
| | | 199 | | //} |
| | | 200 | | |
| | | 201 | | //internal string PerfCounterBaseId |
| | | 202 | | //{ |
| | | 203 | | // get { return this.perfCounterBaseId; } |
| | | 204 | | //} |
| | | 205 | | |
| | 0 | 206 | | internal int PerfCounterInstanceId { get; set; } |
| | | 207 | | |
| | | 208 | | internal static EndpointDispatcher AddEndpointDispatcher(EndpointDispatcher baseEndpoint, |
| | | 209 | | IEnumerable<AddressHeader> headers) |
| | | 210 | | { |
| | 0 | 211 | | EndpointDispatcher endpoint = new EndpointDispatcher(baseEndpoint, headers); |
| | 0 | 212 | | baseEndpoint.ChannelDispatcher.Endpoints.Add(endpoint); |
| | 0 | 213 | | return endpoint; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal void Attach(ChannelDispatcher channelDispatcher) |
| | | 217 | | { |
| | 664 | 218 | | if (ChannelDispatcher != null) |
| | | 219 | | { |
| | 0 | 220 | | Exception error = new InvalidOperationException(SR.SFxEndpointDispatcherMultipleChannelDispatcher0); |
| | 0 | 221 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 222 | | } |
| | | 223 | | |
| | 664 | 224 | | ChannelDispatcher = channelDispatcher ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(na |
| | | 225 | | // TODO: Plumb through the listening Uri |
| | | 226 | | //listenUri = channelDispatcher.Listener?.Uri; |
| | 664 | 227 | | } |
| | | 228 | | |
| | | 229 | | internal void Detach(ChannelDispatcher channelDispatcher) |
| | | 230 | | { |
| | 0 | 231 | | if (channelDispatcher == null) |
| | | 232 | | { |
| | 0 | 233 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(channelDispatcher)); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | if (ChannelDispatcher != channelDispatcher) |
| | | 237 | | { |
| | 0 | 238 | | Exception error = new InvalidOperationException(SR.SFxEndpointDispatcherDifferentChannelDispatcher0); |
| | 0 | 239 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | //this.ReleasePerformanceCounters(); |
| | 0 | 243 | | ChannelDispatcher = null; |
| | 0 | 244 | | } |
| | | 245 | | |
| | | 246 | | //internal void ReleasePerformanceCounters() |
| | | 247 | | //{ |
| | | 248 | | // if (PerformanceCounters.PerformanceCountersEnabled) |
| | | 249 | | // { |
| | | 250 | | // PerformanceCounters.ReleasePerformanceCountersForEndpoint(this.perfCounterId, this.perfCounterBaseId); |
| | | 251 | | // } |
| | | 252 | | //} |
| | | 253 | | |
| | | 254 | | //internal bool SetPerfCounterId() |
| | | 255 | | //{ |
| | | 256 | | // Uri keyUri = null; |
| | | 257 | | // if (null != this.ListenUri) |
| | | 258 | | // { |
| | | 259 | | // keyUri = this.ListenUri; |
| | | 260 | | // } |
| | | 261 | | // else |
| | | 262 | | // { |
| | | 263 | | // EndpointAddress endpointAddress = this.EndpointAddress; |
| | | 264 | | // if (null != endpointAddress) |
| | | 265 | | // { |
| | | 266 | | // keyUri = endpointAddress.Uri; |
| | | 267 | | // } |
| | | 268 | | // } |
| | | 269 | | |
| | | 270 | | // if (null != keyUri) |
| | | 271 | | // { |
| | | 272 | | // this.perfCounterBaseId = keyUri.AbsoluteUri.ToUpperInvariant(); |
| | | 273 | | // this.perfCounterId = this.perfCounterBaseId + "/" + contractName.ToUpperInvariant(); |
| | | 274 | | |
| | | 275 | | // return true; |
| | | 276 | | // } |
| | | 277 | | // else |
| | | 278 | | // { |
| | | 279 | | // return false; |
| | | 280 | | // } |
| | | 281 | | //} |
| | | 282 | | |
| | | 283 | | private void ThrowIfDisposedOrImmutable() |
| | | 284 | | { |
| | 1402 | 285 | | ChannelDispatcher channelDispatcher = ChannelDispatcher; |
| | 1402 | 286 | | if (channelDispatcher != null) |
| | | 287 | | { |
| | 715 | 288 | | channelDispatcher.ThrowIfDisposedOrImmutable(); |
| | | 289 | | } |
| | 1402 | 290 | | } |
| | | 291 | | } |
| | | 292 | | } |