| | | 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 System.Runtime.CompilerServices; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Xml; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Configuration; |
| | | 12 | | using CoreWCF.Runtime; |
| | | 13 | | using CoreWCF.Security; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Dispatcher |
| | | 16 | | { |
| | | 17 | | internal class DuplexChannelBinder : IChannelBinder |
| | | 18 | | { |
| | | 19 | | private IDuplexChannel _channel; |
| | | 20 | | private IRequestReplyCorrelator _correlator; |
| | | 21 | | private IdentityVerifier _identityVerifier; |
| | | 22 | | private int _pending; |
| | | 23 | | private List<IDuplexRequest> _requests; |
| | | 24 | | private List<ICorrelatorKey> _timedOutRequests; |
| | | 25 | | private ChannelHandler _channelHandler; |
| | | 26 | | private bool _requestAborted; |
| | | 27 | | private bool _initialized = false; |
| | | 28 | | private IDefaultCommunicationTimeouts _timeouts; |
| | | 29 | | private IServiceChannelDispatcher _next; |
| | | 30 | | |
| | 150 | 31 | | public DuplexChannelBinder() { } |
| | | 32 | | |
| | | 33 | | internal void Init(IDuplexSessionChannel channel, IRequestReplyCorrelator correlator, Uri listenUri) |
| | | 34 | | { |
| | 75 | 35 | | Init((IDuplexChannel)channel, correlator, listenUri); |
| | 75 | 36 | | HasSession = true; |
| | 75 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal void Init(IDuplexChannel channel, IRequestReplyCorrelator correlator, Uri listenUri) |
| | | 40 | | { |
| | 75 | 41 | | if (_initialized) |
| | | 42 | | { |
| | | 43 | | Fx.Assert(_channel == channel, "Wrong channel when calling Init"); |
| | | 44 | | Fx.Assert(_correlator == correlator, "Wrong channel when calling Init"); |
| | | 45 | | Fx.Assert(ListenUri == listenUri, "Wrong listenUri when calling Init"); |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | Fx.Assert(channel != null, "caller must verify"); |
| | | 50 | | Fx.Assert(correlator != null, "caller must verify"); |
| | | 51 | | |
| | 75 | 52 | | _channel = channel; |
| | 75 | 53 | | ListenUri = listenUri; |
| | 75 | 54 | | _correlator = correlator; |
| | 75 | 55 | | _channel.Faulted += new EventHandler(OnFaulted); |
| | 75 | 56 | | _initialized = true; |
| | 75 | 57 | | } |
| | | 58 | | |
| | 89 | 59 | | public TimeSpan DefaultSendTimeout => _timeouts.SendTimeout; |
| | 89 | 60 | | public TimeSpan DefaultCloseTimeout => _timeouts.CloseTimeout; |
| | | 61 | | |
| | | 62 | | public IChannel Channel |
| | | 63 | | { |
| | 604 | 64 | | get { return _channel; } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | internal ChannelHandler ChannelHandler |
| | | 68 | | { |
| | | 69 | | get |
| | | 70 | | { |
| | 0 | 71 | | if (!(_channelHandler != null)) |
| | | 72 | | { |
| | | 73 | | Fx.Assert("DuplexChannelBinder.ChannelHandler: (channelHandler != null)"); |
| | | 74 | | } |
| | 0 | 75 | | return _channelHandler; |
| | | 76 | | } |
| | | 77 | | set |
| | | 78 | | { |
| | 0 | 79 | | if (!(_channelHandler == null)) |
| | | 80 | | { |
| | | 81 | | Fx.Assert("DuplexChannelBinder.ChannelHandler: (channelHandler == null)"); |
| | | 82 | | } |
| | 0 | 83 | | _channelHandler = value; |
| | 0 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 368 | 87 | | public bool HasSession { get; private set; } |
| | | 88 | | |
| | | 89 | | internal IdentityVerifier IdentityVerifier |
| | | 90 | | { |
| | | 91 | | get |
| | | 92 | | { |
| | 0 | 93 | | if (_identityVerifier == null) |
| | | 94 | | { |
| | 0 | 95 | | _identityVerifier = IdentityVerifier.CreateDefault(); |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return _identityVerifier; |
| | | 99 | | } |
| | | 100 | | set |
| | | 101 | | { |
| | 0 | 102 | | _identityVerifier = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(val |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 75 | 106 | | public Uri ListenUri { get; private set; } |
| | | 107 | | |
| | | 108 | | public EndpointAddress LocalAddress |
| | | 109 | | { |
| | 0 | 110 | | get { return _channel.LocalAddress; } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public EndpointAddress RemoteAddress |
| | | 114 | | { |
| | 0 | 115 | | get { return _channel.RemoteAddress; } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private List<IDuplexRequest> Requests |
| | | 119 | | { |
| | | 120 | | get |
| | | 121 | | { |
| | 1 | 122 | | lock (ThisLock) |
| | | 123 | | { |
| | 1 | 124 | | if (_requests == null) |
| | | 125 | | { |
| | 1 | 126 | | _requests = new List<IDuplexRequest>(); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | return _requests; |
| | | 130 | | } |
| | 1 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private List<ICorrelatorKey> TimedOutRequests |
| | | 135 | | { |
| | | 136 | | get |
| | | 137 | | { |
| | 0 | 138 | | lock (ThisLock) |
| | | 139 | | { |
| | 0 | 140 | | if (_timedOutRequests == null) |
| | | 141 | | { |
| | 0 | 142 | | _timedOutRequests = new List<ICorrelatorKey>(); |
| | | 143 | | } |
| | 0 | 144 | | return _timedOutRequests; |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | private object ThisLock |
| | | 150 | | { |
| | 74 | 151 | | get { return this; } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private void OnFaulted(object sender, EventArgs e) |
| | | 155 | | { |
| | | 156 | | //Some unhandled exception happened on the channel. |
| | | 157 | | //So close all pending requests so the callbacks (in case of async) |
| | | 158 | | //on the requests are called. |
| | 1 | 159 | | AbortRequests(); |
| | 1 | 160 | | } |
| | | 161 | | |
| | | 162 | | public void Abort() |
| | | 163 | | { |
| | 1 | 164 | | _channel.Abort(); |
| | 1 | 165 | | AbortRequests(); |
| | 1 | 166 | | } |
| | | 167 | | |
| | | 168 | | public void CloseAfterFault(TimeSpan timeout) |
| | | 169 | | { |
| | 0 | 170 | | var helper = new TimeoutHelper(timeout); |
| | 0 | 171 | | _channel.CloseAsync(helper.GetCancellationToken()); |
| | 0 | 172 | | AbortRequests(); |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | private void AbortRequests() |
| | | 176 | | { |
| | 71 | 177 | | IDuplexRequest[] array = null; |
| | 71 | 178 | | lock (ThisLock) |
| | | 179 | | { |
| | 71 | 180 | | if (_requests != null) |
| | | 181 | | { |
| | 0 | 182 | | array = _requests.ToArray(); |
| | | 183 | | |
| | 0 | 184 | | foreach (IDuplexRequest request in array) |
| | | 185 | | { |
| | 0 | 186 | | request.Abort(); |
| | | 187 | | } |
| | | 188 | | } |
| | 71 | 189 | | _requests = null; |
| | 71 | 190 | | _requestAborted = true; |
| | 71 | 191 | | } |
| | | 192 | | |
| | | 193 | | // Remove requests from the correlator since the channel might be either faulting or aborting, |
| | | 194 | | // We are not going to get a reply for these requests. If they are not removed from the correlator, this wil |
| | | 195 | | // This operation does not have to be under the lock |
| | 71 | 196 | | if (array != null && array.Length > 0) |
| | | 197 | | { |
| | 0 | 198 | | if (_correlator is RequestReplyCorrelator requestReplyCorrelator) |
| | | 199 | | { |
| | 0 | 200 | | foreach (IDuplexRequest request in array) |
| | | 201 | | { |
| | 0 | 202 | | if (request is ICorrelatorKey keyedRequest) |
| | | 203 | | { |
| | 0 | 204 | | requestReplyCorrelator.RemoveRequest(keyedRequest); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | //if there are any timed out requests, delete it from the correlator table |
| | 71 | 211 | | DeleteTimedoutRequestsFromCorrelator(); |
| | 71 | 212 | | } |
| | | 213 | | |
| | | 214 | | private TimeoutException GetReceiveTimeoutException(TimeSpan timeout) |
| | | 215 | | { |
| | 0 | 216 | | EndpointAddress address = _channel.RemoteAddress ?? _channel.LocalAddress; |
| | 0 | 217 | | if (address != null) |
| | | 218 | | { |
| | 0 | 219 | | return new TimeoutException(SR.Format(SR.SFxRequestTimedOut2, address, timeout)); |
| | | 220 | | } |
| | | 221 | | else |
| | | 222 | | { |
| | 0 | 223 | | return new TimeoutException(SR.Format(SR.SFxRequestTimedOut1, timeout)); |
| | | 224 | | } |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | internal bool HandleRequestAsReply(Message message) |
| | | 228 | | { |
| | 89 | 229 | | UniqueId relatesTo = null; |
| | | 230 | | try |
| | | 231 | | { |
| | 89 | 232 | | relatesTo = message.Headers.RelatesTo; |
| | 89 | 233 | | } |
| | 0 | 234 | | catch (MessageHeaderException) |
| | | 235 | | { |
| | | 236 | | // ignore it |
| | 0 | 237 | | } |
| | 89 | 238 | | if (relatesTo == null) |
| | | 239 | | { |
| | 88 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | else |
| | | 243 | | { |
| | 1 | 244 | | return HandleRequestAsReplyCore(message); |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private bool HandleRequestAsReplyCore(Message message) |
| | | 249 | | { |
| | 1 | 250 | | IDuplexRequest request = _correlator.Find<IDuplexRequest>(message, true); |
| | 1 | 251 | | if (request != null) |
| | | 252 | | { |
| | 1 | 253 | | request.GotReply(message); |
| | 1 | 254 | | return true; |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public RequestContext CreateRequestContext(Message message) |
| | | 261 | | { |
| | 89 | 262 | | return new DuplexRequestContext(_channel, message, this); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | public Task SendAsync(Message message, CancellationToken token) |
| | | 266 | | { |
| | 0 | 267 | | return _channel.SendAsync(message, token); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | public async Task<Message> RequestAsync(Message message, CancellationToken token) |
| | | 271 | | { |
| | 1 | 272 | | RequestReplyCorrelator.PrepareRequest(message); |
| | 1 | 273 | | AsyncDuplexRequest duplexRequest = new AsyncDuplexRequest(this); |
| | | 274 | | |
| | 1 | 275 | | lock (ThisLock) |
| | | 276 | | { |
| | 1 | 277 | | RequestStarting(message, duplexRequest); |
| | 1 | 278 | | } |
| | | 279 | | |
| | 1 | 280 | | await _channel.SendAsync(message, token); |
| | 1 | 281 | | return await duplexRequest.WaitForReplyAsync(token); |
| | 1 | 282 | | } |
| | | 283 | | |
| | | 284 | | // ASSUMPTION: caller holds lock (this.mutex) |
| | | 285 | | private void RequestStarting(Message message, IDuplexRequest request) |
| | | 286 | | { |
| | 1 | 287 | | if (request != null) |
| | | 288 | | { |
| | 1 | 289 | | Requests.Add(request); |
| | 1 | 290 | | if (!_requestAborted) |
| | | 291 | | { |
| | 1 | 292 | | _correlator.Add<IDuplexRequest>(message, request); |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | 1 | 296 | | _pending++; |
| | 1 | 297 | | } |
| | | 298 | | |
| | | 299 | | // ASSUMPTION: (mmaruch) caller holds lock (this.mutex) |
| | | 300 | | private void RequestCompleting(IDuplexRequest request) |
| | | 301 | | { |
| | 1 | 302 | | _pending--; |
| | 1 | 303 | | if (_pending == 0) |
| | | 304 | | { |
| | 1 | 305 | | _requests = null; |
| | | 306 | | } |
| | 0 | 307 | | else if ((request != null) && (_requests != null)) |
| | | 308 | | { |
| | 0 | 309 | | _requests.Remove(request); |
| | | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | // ASSUMPTION: caller holds ThisLock |
| | | 314 | | private void AddToTimedOutRequestList(ICorrelatorKey request) |
| | | 315 | | { |
| | | 316 | | Fx.Assert(request != null, "request cannot be null"); |
| | 0 | 317 | | TimedOutRequests.Add(request); |
| | 0 | 318 | | } |
| | | 319 | | |
| | | 320 | | // ASSUMPTION: caller holds ThisLock |
| | | 321 | | private void RemoveFromTimedOutRequestList(ICorrelatorKey request) |
| | | 322 | | { |
| | | 323 | | Fx.Assert(request != null, "request cannot be null"); |
| | 0 | 324 | | if (_timedOutRequests != null) |
| | | 325 | | { |
| | 0 | 326 | | _timedOutRequests.Remove(request); |
| | | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | private void DeleteTimedoutRequestsFromCorrelator() |
| | | 331 | | { |
| | 71 | 332 | | ICorrelatorKey[] array = null; |
| | 71 | 333 | | if (_timedOutRequests != null && _timedOutRequests.Count > 0) |
| | | 334 | | { |
| | 0 | 335 | | lock (ThisLock) |
| | | 336 | | { |
| | 0 | 337 | | if (_timedOutRequests != null && _timedOutRequests.Count > 0) |
| | | 338 | | { |
| | 0 | 339 | | array = _timedOutRequests.ToArray(); |
| | 0 | 340 | | _timedOutRequests = null; |
| | | 341 | | } |
| | 0 | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | // Remove requests from the correlator since the channel might be either faulting, aborting or closing |
| | | 346 | | // We are not going to get a reply for these timed out requests. If they are not removed from the correlator |
| | | 347 | | // This operation does not have to be under the lock |
| | 71 | 348 | | if (array != null && array.Length > 0) |
| | | 349 | | { |
| | 0 | 350 | | if (_correlator is RequestReplyCorrelator requestReplyCorrelator) |
| | | 351 | | { |
| | 0 | 352 | | foreach (ICorrelatorKey request in array) |
| | | 353 | | { |
| | 0 | 354 | | requestReplyCorrelator.RemoveRequest(request); |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | } |
| | 71 | 358 | | } |
| | | 359 | | |
| | | 360 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 361 | | private void EnsureIncomingIdentity(SecurityMessageProperty property, EndpointAddress address, Message reply) |
| | | 362 | | { |
| | 0 | 363 | | IdentityVerifier.EnsureIncomingIdentity(address, property.ServiceSecurityContext.AuthorizationContext); |
| | 0 | 364 | | } |
| | | 365 | | |
| | | 366 | | private void ThrowIfInvalidReplyIdentity(Message reply) |
| | | 367 | | { |
| | 1 | 368 | | if (!HasSession) |
| | | 369 | | { |
| | 0 | 370 | | SecurityMessageProperty property = reply.Properties.Security; |
| | 0 | 371 | | EndpointAddress address = _channel.RemoteAddress; |
| | | 372 | | |
| | 0 | 373 | | if ((property != null) && (address != null)) |
| | | 374 | | { |
| | 0 | 375 | | EnsureIncomingIdentity(property, address, reply); |
| | | 376 | | } |
| | | 377 | | } |
| | 1 | 378 | | } |
| | | 379 | | |
| | | 380 | | public void SetNextDispatcher(IServiceChannelDispatcher dispatcher) |
| | | 381 | | { |
| | | 382 | | Fx.Assert(dispatcher is IDefaultCommunicationTimeouts, "Next Dispatcher must implement IDefaultCommunication |
| | 75 | 383 | | _timeouts = dispatcher as IDefaultCommunicationTimeouts; |
| | 75 | 384 | | _next = dispatcher; |
| | 75 | 385 | | } |
| | | 386 | | |
| | | 387 | | public Task DispatchAsync(RequestContext context) |
| | | 388 | | { |
| | 0 | 389 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | public Task DispatchAsync(Message message) |
| | | 393 | | { |
| | | 394 | | Fx.Assert(_next != null, "SetNextDispatcher wasn't called"); |
| | | 395 | | Fx.Assert(_channel.State != CommunicationState.Closed, "Expected dispatcher state to be Opened or Faulted, i |
| | 157 | 396 | | if (_channel.State == CommunicationState.Faulted || message == null) |
| | | 397 | | { |
| | 69 | 398 | | AbortRequests(); |
| | 69 | 399 | | return _next.DispatchAsync((RequestContext)null); |
| | | 400 | | } |
| | | 401 | | |
| | 88 | 402 | | return _next.DispatchAsync(CreateRequestContext(message)); |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | private class DuplexRequestContext : RequestContextBase |
| | | 406 | | { |
| | | 407 | | private readonly DuplexChannelBinder _binder; |
| | | 408 | | private readonly IDuplexChannel _channel; |
| | | 409 | | |
| | | 410 | | internal DuplexRequestContext(IDuplexChannel channel, Message request, DuplexChannelBinder binder) |
| | 89 | 411 | | : base(request, binder.DefaultCloseTimeout, binder.DefaultSendTimeout) |
| | | 412 | | { |
| | 89 | 413 | | _channel = channel; |
| | 89 | 414 | | _binder = binder; |
| | 89 | 415 | | } |
| | | 416 | | |
| | | 417 | | protected override void OnAbort() |
| | | 418 | | { |
| | 0 | 419 | | } |
| | | 420 | | |
| | | 421 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 422 | | { |
| | 88 | 423 | | return Task.CompletedTask; |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | protected override Task OnReplyAsync(Message message, CancellationToken token) |
| | | 427 | | { |
| | 88 | 428 | | if (message != null) |
| | | 429 | | { |
| | 87 | 430 | | return _channel.SendAsync(message, token); |
| | | 431 | | } |
| | | 432 | | |
| | 1 | 433 | | return Task.CompletedTask; |
| | | 434 | | } |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | private interface IDuplexRequest |
| | | 438 | | { |
| | | 439 | | void Abort(); |
| | | 440 | | void GotReply(Message reply); |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | private class AsyncDuplexRequest : IDuplexRequest, ICorrelatorKey |
| | | 444 | | { |
| | | 445 | | private Message _reply; |
| | | 446 | | private readonly DuplexChannelBinder _parent; |
| | 1 | 447 | | private readonly AsyncManualResetEvent _wait = new AsyncManualResetEvent(); |
| | | 448 | | private int _waitCount = 0; |
| | | 449 | | private RequestReplyCorrelator.Key _requestCorrelatorKey; |
| | | 450 | | |
| | 1 | 451 | | internal AsyncDuplexRequest(DuplexChannelBinder parent) |
| | | 452 | | { |
| | 1 | 453 | | _parent = parent; |
| | 1 | 454 | | } |
| | | 455 | | |
| | | 456 | | RequestReplyCorrelator.Key ICorrelatorKey.RequestCorrelatorKey |
| | | 457 | | { |
| | | 458 | | get |
| | | 459 | | { |
| | 0 | 460 | | return _requestCorrelatorKey; |
| | | 461 | | } |
| | | 462 | | set |
| | | 463 | | { |
| | | 464 | | Fx.Assert(_requestCorrelatorKey == null, "RequestCorrelatorKey is already set for this request"); |
| | 1 | 465 | | _requestCorrelatorKey = value; |
| | 1 | 466 | | } |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | public void Abort() |
| | | 470 | | { |
| | 0 | 471 | | _wait.Set(); |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | internal async Task<Message> WaitForReplyAsync(CancellationToken token) |
| | | 475 | | { |
| | | 476 | | try |
| | | 477 | | { |
| | 1 | 478 | | if (!await _wait.WaitAsync(token)) |
| | | 479 | | { |
| | 0 | 480 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_parent.GetReceiveTimeoutException(Tim |
| | | 481 | | } |
| | 1 | 482 | | } |
| | | 483 | | finally |
| | | 484 | | { |
| | 1 | 485 | | CloseWaitHandle(); |
| | | 486 | | } |
| | | 487 | | |
| | 1 | 488 | | _parent.ThrowIfInvalidReplyIdentity(_reply); |
| | 1 | 489 | | return _reply; |
| | 1 | 490 | | } |
| | | 491 | | |
| | | 492 | | public void GotReply(Message reply) |
| | | 493 | | { |
| | 1 | 494 | | lock (_parent.ThisLock) |
| | | 495 | | { |
| | 1 | 496 | | _parent.RequestCompleting(this); |
| | 1 | 497 | | } |
| | 1 | 498 | | _reply = reply; |
| | 1 | 499 | | _wait.Set(); |
| | 1 | 500 | | CloseWaitHandle(); |
| | 1 | 501 | | } |
| | | 502 | | |
| | | 503 | | private void CloseWaitHandle() |
| | | 504 | | { |
| | 2 | 505 | | if (Interlocked.Increment(ref _waitCount) == 2) |
| | | 506 | | { |
| | 1 | 507 | | _wait.Dispose(); |
| | | 508 | | } |
| | 2 | 509 | | } |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | } |