| | | 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.Diagnostics; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Security.Authentication.ExtendedProtection; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Configuration; |
| | | 11 | | using CoreWCF.Diagnostics; |
| | | 12 | | using CoreWCF.Dispatcher; |
| | | 13 | | using CoreWCF.Runtime; |
| | | 14 | | using CoreWCF.Security; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Channels |
| | | 17 | | { |
| | | 18 | | internal abstract class TransportDuplexSessionChannel : TransportOutputChannel, IDuplexSessionChannel |
| | | 19 | | { |
| | | 20 | | private bool _isInputSessionClosed; |
| | | 21 | | private bool _isOutputSessionClosed; |
| | | 22 | | private ChannelBinding _channelBindingToken; |
| | | 23 | | private TaskCompletionSource<object> _inputSessionClosedTcs; |
| | | 24 | | |
| | | 25 | | protected TransportDuplexSessionChannel( |
| | | 26 | | ITransportFactorySettings settings, |
| | | 27 | | EndpointAddress localAddress, |
| | | 28 | | Uri localVia, |
| | | 29 | | EndpointAddress remoteAddress, |
| | | 30 | | Uri via) |
| | 11 | 31 | | : base(settings, remoteAddress, via, settings.ManualAddressing, settings.MessageVersion) |
| | | 32 | | { |
| | 11 | 33 | | LocalAddress = localAddress; |
| | 11 | 34 | | LocalVia = localVia; |
| | 11 | 35 | | BufferManager = settings.BufferManager; |
| | 11 | 36 | | MessageEncoder = settings.MessageEncoderFactory.CreateSessionEncoder(); |
| | 11 | 37 | | Session = new ConnectionDuplexSession(this); |
| | 11 | 38 | | _inputSessionClosedTcs = new TaskCompletionSource<object>(); |
| | 11 | 39 | | } |
| | | 40 | | |
| | 12 | 41 | | public EndpointAddress LocalAddress { get; } |
| | | 42 | | |
| | 11 | 43 | | public SecurityMessageProperty RemoteSecurity { get; protected set; } |
| | | 44 | | |
| | 11 | 45 | | public IDuplexSession Session { get; protected set; } |
| | | 46 | | |
| | 63 | 47 | | public SemaphoreSlim SendLock { get; } = new SemaphoreSlim(1); |
| | | 48 | | |
| | | 49 | | protected ChannelBinding ChannelBinding |
| | | 50 | | { |
| | | 51 | | get |
| | | 52 | | { |
| | 0 | 53 | | return _channelBindingToken; |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 29 | 57 | | protected BufferManager BufferManager { get; } |
| | | 58 | | |
| | 15 | 59 | | protected Uri LocalVia { get; } |
| | | 60 | | |
| | 37 | 61 | | protected MessageEncoder MessageEncoder { get; set; } |
| | | 62 | | |
| | 37 | 63 | | protected SynchronizedMessageSource MessageSource { get; private set; } |
| | | 64 | | |
| | | 65 | | protected abstract bool IsStreamedOutput { get; } |
| | | 66 | | |
| | | 67 | | public async Task StartReceivingAsync() |
| | | 68 | | { |
| | 11 | 69 | | if (ChannelDispatcher == null) |
| | | 70 | | { |
| | | 71 | | // TODO: Cleanup exception message, find a SR to use and add Fx error handling |
| | 0 | 72 | | throw new InvalidOperationException("ChannelDispatcher isn't set"); |
| | | 73 | | } |
| | | 74 | | |
| | 15 | 75 | | while (true) |
| | | 76 | | { |
| | 26 | 77 | | (Message message, bool success) = await TryReceiveAsync(CancellationToken.None); |
| | 26 | 78 | | if (success) |
| | | 79 | | { |
| | 26 | 80 | | await ChannelDispatcher.DispatchAsync(message); |
| | | 81 | | } |
| | | 82 | | |
| | 26 | 83 | | if (message == null || this.DoneReceivingInCurrentState()) // NULL message means client sent FIN byte |
| | | 84 | | { |
| | 11 | 85 | | return; |
| | | 86 | | } |
| | 15 | 87 | | } |
| | 11 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task<Message> ReceiveAsync(CancellationToken token) |
| | | 91 | | { |
| | 26 | 92 | | Message message = null; |
| | 26 | 93 | | if (this.DoneReceivingInCurrentState()) |
| | | 94 | | { |
| | 0 | 95 | | return null; |
| | | 96 | | } |
| | | 97 | | |
| | 26 | 98 | | bool shouldFault = true; |
| | | 99 | | try |
| | | 100 | | { |
| | 26 | 101 | | message = await MessageSource.ReceiveAsync(token); |
| | 26 | 102 | | OnReceiveMessage(message); |
| | 26 | 103 | | shouldFault = false; |
| | 26 | 104 | | return message; |
| | | 105 | | } |
| | | 106 | | finally |
| | | 107 | | { |
| | 26 | 108 | | if (shouldFault) |
| | | 109 | | { |
| | 0 | 110 | | if (message != null) |
| | | 111 | | { |
| | 0 | 112 | | message.Close(); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | Fault(); |
| | | 116 | | } |
| | | 117 | | } |
| | 26 | 118 | | } |
| | | 119 | | |
| | | 120 | | public async Task<(Message message, bool success)> TryReceiveAsync(CancellationToken token) |
| | | 121 | | { |
| | | 122 | | try |
| | | 123 | | { |
| | 26 | 124 | | return (await ReceiveAsync(token), true); |
| | | 125 | | } |
| | | 126 | | catch (TimeoutException e) |
| | | 127 | | { |
| | | 128 | | //if (TD.ReceiveTimeoutIsEnabled()) |
| | | 129 | | //{ |
| | | 130 | | // TD.ReceiveTimeout(e.Message); |
| | | 131 | | //} |
| | | 132 | | |
| | 0 | 133 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | | 134 | | |
| | 0 | 135 | | return (null, false); |
| | | 136 | | } |
| | 26 | 137 | | } |
| | | 138 | | |
| | | 139 | | protected void SetChannelBinding(ChannelBinding channelBinding) |
| | | 140 | | { |
| | | 141 | | Fx.Assert(_channelBindingToken == null, "ChannelBinding token can only be set once."); |
| | 0 | 142 | | _channelBindingToken = channelBinding; |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | protected async Task CloseOutputSessionAsync(CancellationToken token) |
| | | 146 | | { |
| | 11 | 147 | | ThrowIfNotOpened(); |
| | 11 | 148 | | ThrowIfFaulted(); |
| | | 149 | | try |
| | | 150 | | { |
| | 11 | 151 | | await SendLock.WaitAsync(token); |
| | 11 | 152 | | } |
| | 0 | 153 | | catch (OperationCanceledException) |
| | | 154 | | { |
| | | 155 | | // TODO: Fix the timeout value reported |
| | 0 | 156 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException( |
| | 0 | 157 | | SR.Format(SR.CloseTimedOut, TimeSpan.Zero), |
| | 0 | 158 | | TimeoutHelper.CreateEnterTimedOutException(TimeSpan.Zero))); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | try |
| | | 162 | | { |
| | | 163 | | // check again in case the previous send faulted while we were waiting for the lock |
| | 11 | 164 | | ThrowIfFaulted(); |
| | | 165 | | |
| | | 166 | | // we're synchronized by sendLock here |
| | 11 | 167 | | if (_isOutputSessionClosed) |
| | | 168 | | { |
| | 0 | 169 | | return; |
| | | 170 | | } |
| | | 171 | | |
| | 11 | 172 | | _isOutputSessionClosed = true; |
| | 11 | 173 | | bool shouldFault = true; |
| | | 174 | | try |
| | | 175 | | { |
| | 11 | 176 | | await CloseOutputSessionCoreAsync(token); |
| | 11 | 177 | | OnOutputSessionClosed(token); |
| | 11 | 178 | | shouldFault = false; |
| | 11 | 179 | | } |
| | | 180 | | finally |
| | | 181 | | { |
| | 11 | 182 | | if (shouldFault) |
| | | 183 | | { |
| | 0 | 184 | | Fault(); |
| | | 185 | | } |
| | | 186 | | } |
| | 11 | 187 | | } |
| | | 188 | | finally |
| | | 189 | | { |
| | 11 | 190 | | SendLock.Release(); |
| | | 191 | | } |
| | 11 | 192 | | } |
| | | 193 | | |
| | | 194 | | protected void SetMessageSource(IMessageSource messageSource) |
| | | 195 | | { |
| | 11 | 196 | | MessageSource = new SynchronizedMessageSource(messageSource); |
| | 11 | 197 | | } |
| | | 198 | | |
| | | 199 | | protected abstract Task CloseOutputSessionCoreAsync(CancellationToken token); |
| | | 200 | | |
| | | 201 | | // used to return cached connection to the pool/reader pool |
| | | 202 | | protected abstract void ReturnConnectionIfNecessary(bool abort, CancellationToken token); |
| | | 203 | | |
| | | 204 | | protected override void OnAbort() |
| | | 205 | | { |
| | 0 | 206 | | ReturnConnectionIfNecessary(true, CancellationToken.None); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | protected override void OnFaulted() |
| | | 210 | | { |
| | 0 | 211 | | base.OnFaulted(); |
| | 0 | 212 | | ReturnConnectionIfNecessary(true, CancellationToken.None); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | protected override async Task OnCloseAsync(CancellationToken token) |
| | | 216 | | { |
| | 11 | 217 | | await CloseOutputSessionAsync(token); |
| | | 218 | | |
| | | 219 | | // close input session if necessary |
| | 11 | 220 | | if (!_isInputSessionClosed) |
| | | 221 | | { |
| | 0 | 222 | | await EnsureInputClosedAsync(token); |
| | 0 | 223 | | OnInputSessionClosed(); |
| | | 224 | | } |
| | | 225 | | |
| | 11 | 226 | | await CompleteCloseAsync(token); |
| | 11 | 227 | | } |
| | | 228 | | |
| | | 229 | | protected override void OnClosed() |
| | | 230 | | { |
| | 11 | 231 | | base.OnClosed(); |
| | | 232 | | |
| | | 233 | | // clean up the CBT after transitioning to the closed state |
| | 11 | 234 | | ChannelBindingUtility.Dispose(ref _channelBindingToken); |
| | 11 | 235 | | } |
| | | 236 | | |
| | | 237 | | protected virtual void OnReceiveMessage(Message message) |
| | | 238 | | { |
| | 26 | 239 | | if (message == null) |
| | | 240 | | { |
| | 11 | 241 | | OnInputSessionClosed(); |
| | | 242 | | } |
| | | 243 | | else |
| | | 244 | | { |
| | 15 | 245 | | PrepareMessage(message); |
| | | 246 | | } |
| | 15 | 247 | | } |
| | | 248 | | |
| | | 249 | | protected void ApplyChannelBinding(Message message) |
| | | 250 | | { |
| | 30 | 251 | | ChannelBindingUtility.TryAddToMessage(_channelBindingToken, message, false); |
| | 30 | 252 | | } |
| | | 253 | | |
| | | 254 | | protected virtual void PrepareMessage(Message message) |
| | | 255 | | { |
| | 15 | 256 | | message.Properties.Via = LocalVia; |
| | | 257 | | |
| | 15 | 258 | | ApplyChannelBinding(message); |
| | | 259 | | |
| | | 260 | | //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled) |
| | | 261 | | //{ |
| | | 262 | | // EventTraceActivity eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); |
| | | 263 | | // Guid relatedActivityId = EventTraceActivity.GetActivityIdFromThread(); |
| | | 264 | | // if (eventTraceActivity == null) |
| | | 265 | | // { |
| | | 266 | | // eventTraceActivity = EventTraceActivity.GetFromThreadOrCreate(); |
| | | 267 | | // EventTraceActivityHelper.TryAttachActivity(message, eventTraceActivity); |
| | | 268 | | // } |
| | | 269 | | |
| | | 270 | | // if (TD.MessageReceivedByTransportIsEnabled()) |
| | | 271 | | // { |
| | | 272 | | // TD.MessageReceivedByTransport( |
| | | 273 | | // eventTraceActivity, |
| | | 274 | | // this.LocalAddress != null && this.LocalAddress.Uri != null ? this.LocalAddress.Uri.AbsoluteUri |
| | | 275 | | // relatedActivityId); |
| | | 276 | | // } |
| | | 277 | | //} |
| | | 278 | | |
| | | 279 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 280 | | //{ |
| | | 281 | | // TraceUtility.TraceEvent( |
| | | 282 | | // TraceEventType.Information, |
| | | 283 | | // TraceCode.MessageReceived, |
| | | 284 | | // SR.GetString(SR.TraceCodeMessageReceived), |
| | | 285 | | // MessageTransmitTraceRecord.CreateReceiveTraceRecord(message, this.LocalAddress), |
| | | 286 | | // this, |
| | | 287 | | // null, |
| | | 288 | | // message); |
| | | 289 | | //} |
| | 15 | 290 | | } |
| | | 291 | | |
| | | 292 | | protected abstract Task CloseOutputAsync(CancellationToken token); |
| | | 293 | | |
| | | 294 | | protected abstract ArraySegment<byte> EncodeMessage(Message message); |
| | | 295 | | |
| | | 296 | | protected abstract Task OnSendCoreAsync(Message message, CancellationToken token); |
| | | 297 | | |
| | | 298 | | protected override async Task OnSendAsync(Message message, CancellationToken token) |
| | | 299 | | { |
| | 15 | 300 | | ThrowIfDisposedOrNotOpen(); |
| | | 301 | | |
| | | 302 | | try |
| | | 303 | | { |
| | 15 | 304 | | await SendLock.WaitAsync(token); |
| | 15 | 305 | | } |
| | 0 | 306 | | catch (OperationCanceledException) |
| | | 307 | | { |
| | | 308 | | // TODO: Fix the timeout value reported |
| | 0 | 309 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException( |
| | 0 | 310 | | SR.Format(SR.SendToViaTimedOut, Via, TimeSpan.Zero), |
| | 0 | 311 | | TimeoutHelper.CreateEnterTimedOutException(TimeSpan.Zero))); |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | try |
| | | 315 | | { |
| | | 316 | | // check again in case the previous send faulted while we were waiting for the lock |
| | 15 | 317 | | ThrowIfDisposedOrNotOpen(); |
| | 15 | 318 | | ThrowIfOutputSessionClosed(); |
| | | 319 | | |
| | 15 | 320 | | bool success = false; |
| | | 321 | | try |
| | | 322 | | { |
| | 15 | 323 | | ApplyChannelBinding(message); |
| | | 324 | | |
| | 15 | 325 | | await OnSendCoreAsync(message, token); |
| | 15 | 326 | | success = true; |
| | 15 | 327 | | } |
| | | 328 | | finally |
| | | 329 | | { |
| | 15 | 330 | | if (!success) |
| | | 331 | | { |
| | 0 | 332 | | Fault(); |
| | | 333 | | } |
| | | 334 | | } |
| | 15 | 335 | | } |
| | | 336 | | finally |
| | | 337 | | { |
| | 15 | 338 | | SendLock.Release(); |
| | | 339 | | } |
| | 15 | 340 | | } |
| | | 341 | | |
| | | 342 | | // cleanup after the framing handshake has completed |
| | | 343 | | protected abstract Task CompleteCloseAsync(CancellationToken token); |
| | | 344 | | |
| | | 345 | | private void ThrowIfOutputSessionClosed() |
| | | 346 | | { |
| | 15 | 347 | | if (_isOutputSessionClosed) |
| | | 348 | | { |
| | 0 | 349 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SendCannotBeC |
| | | 350 | | } |
| | 15 | 351 | | } |
| | | 352 | | |
| | | 353 | | private async Task EnsureInputClosedAsync(CancellationToken token) |
| | | 354 | | { |
| | 0 | 355 | | Message message = await MessageSource.ReceiveAsync(token); |
| | 0 | 356 | | if (message != null) |
| | | 357 | | { |
| | 0 | 358 | | using (message) |
| | | 359 | | { |
| | 0 | 360 | | ProtocolException error = ReceiveShutdownReturnedNonNull(message); |
| | 0 | 361 | | throw TraceUtility.ThrowHelperError(error, message); |
| | | 362 | | } |
| | | 363 | | } |
| | 0 | 364 | | } |
| | | 365 | | |
| | | 366 | | private void OnInputSessionClosed() |
| | | 367 | | { |
| | 11 | 368 | | lock (ThisLock) |
| | | 369 | | { |
| | 11 | 370 | | if (_isInputSessionClosed) |
| | | 371 | | { |
| | 0 | 372 | | return; |
| | | 373 | | } |
| | | 374 | | |
| | 11 | 375 | | _isInputSessionClosed = true; |
| | 11 | 376 | | _inputSessionClosedTcs.TrySetResult(null); |
| | 11 | 377 | | } |
| | 11 | 378 | | } |
| | | 379 | | |
| | | 380 | | private void OnOutputSessionClosed(CancellationToken token) |
| | | 381 | | { |
| | 11 | 382 | | bool releaseConnection = false; |
| | 11 | 383 | | lock (ThisLock) |
| | | 384 | | { |
| | 11 | 385 | | if (_isInputSessionClosed) |
| | | 386 | | { |
| | | 387 | | // we're all done, release the connection |
| | 11 | 388 | | releaseConnection = true; |
| | | 389 | | } |
| | 11 | 390 | | } |
| | | 391 | | |
| | 11 | 392 | | if (releaseConnection) |
| | | 393 | | { |
| | 11 | 394 | | ReturnConnectionIfNecessary(false, token); |
| | | 395 | | } |
| | 11 | 396 | | } |
| | | 397 | | |
| | | 398 | | internal void ThrowIfFaulted() |
| | | 399 | | { |
| | 22 | 400 | | ThrowPending(); |
| | | 401 | | |
| | 22 | 402 | | switch (State) |
| | | 403 | | { |
| | | 404 | | case CommunicationState.Created: |
| | | 405 | | break; |
| | | 406 | | |
| | | 407 | | case CommunicationState.Opening: |
| | | 408 | | break; |
| | | 409 | | |
| | | 410 | | case CommunicationState.Opened: |
| | | 411 | | break; |
| | | 412 | | |
| | | 413 | | case CommunicationState.Closing: |
| | | 414 | | break; |
| | | 415 | | |
| | | 416 | | case CommunicationState.Closed: |
| | | 417 | | break; |
| | | 418 | | |
| | | 419 | | case CommunicationState.Faulted: |
| | 0 | 420 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 421 | | |
| | | 422 | | default: |
| | 0 | 423 | | throw Fx.AssertAndThrow("ThrowIfFaulted: Unknown CommunicationObject.state"); |
| | | 424 | | } |
| | 22 | 425 | | } |
| | | 426 | | |
| | | 427 | | internal Exception CreateFaultedException() |
| | | 428 | | { |
| | 0 | 429 | | string message = SR.Format(SR.CommunicationObjectFaulted1, GetCommunicationObjectType().ToString()); |
| | 0 | 430 | | return new CommunicationObjectFaultedException(message); |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | internal ProtocolException ReceiveShutdownReturnedNonNull(Message message) |
| | | 434 | | { |
| | 0 | 435 | | if (message.IsFault) |
| | | 436 | | { |
| | | 437 | | try |
| | | 438 | | { |
| | 0 | 439 | | MessageFault fault = MessageFault.CreateFault(message, 64 * 1024); |
| | 0 | 440 | | FaultReasonText reason = fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture); |
| | 0 | 441 | | string text = SR.Format(SR.ReceiveShutdownReturnedFault, reason.Text); |
| | 0 | 442 | | return new ProtocolException(text); |
| | | 443 | | } |
| | 0 | 444 | | catch (QuotaExceededException) |
| | | 445 | | { |
| | 0 | 446 | | string text = SR.Format(SR.ReceiveShutdownReturnedLargeFault, message.Headers.Action); |
| | 0 | 447 | | return new ProtocolException(text); |
| | | 448 | | } |
| | | 449 | | } |
| | | 450 | | else |
| | | 451 | | { |
| | 0 | 452 | | string text = SR.Format(SR.ReceiveShutdownReturnedMessage, message.Headers.Action); |
| | 0 | 453 | | return new ProtocolException(text); |
| | | 454 | | } |
| | 0 | 455 | | } |
| | | 456 | | |
| | | 457 | | internal class ConnectionDuplexSession : IDuplexSession |
| | | 458 | | { |
| | | 459 | | private static UriGenerator s_uriGenerator; |
| | | 460 | | private string _id; |
| | | 461 | | |
| | | 462 | | public ConnectionDuplexSession(TransportDuplexSessionChannel channel) |
| | 11 | 463 | | : base() |
| | | 464 | | { |
| | 11 | 465 | | Channel = channel; |
| | 11 | 466 | | } |
| | | 467 | | |
| | | 468 | | public string Id |
| | | 469 | | { |
| | | 470 | | get |
| | | 471 | | { |
| | 0 | 472 | | if (_id == null) |
| | | 473 | | { |
| | 0 | 474 | | lock (Channel) |
| | | 475 | | { |
| | 0 | 476 | | if (_id == null) |
| | | 477 | | { |
| | 0 | 478 | | _id = UriGenerator.Next(); |
| | | 479 | | } |
| | 0 | 480 | | } |
| | | 481 | | } |
| | | 482 | | |
| | 0 | 483 | | return _id; |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | public TransportDuplexSessionChannel Channel { get; } |
| | | 488 | | |
| | | 489 | | private static UriGenerator UriGenerator |
| | | 490 | | { |
| | | 491 | | get |
| | | 492 | | { |
| | 0 | 493 | | if (s_uriGenerator == null) |
| | | 494 | | { |
| | 0 | 495 | | s_uriGenerator = new UriGenerator(); |
| | | 496 | | } |
| | | 497 | | |
| | 0 | 498 | | return s_uriGenerator; |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | public Task CloseOutputSessionAsync() |
| | | 503 | | { |
| | 0 | 504 | | var timeoutHelper = new TimeoutHelper(Channel.DefaultCloseTimeout); |
| | 0 | 505 | | return CloseOutputSessionAsync(timeoutHelper.GetCancellationToken()); |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | public Task CloseOutputSessionAsync(CancellationToken token) |
| | | 509 | | { |
| | 0 | 510 | | return Channel.CloseOutputSessionAsync(token); |
| | | 511 | | } |
| | | 512 | | } |
| | | 513 | | } |
| | | 514 | | } |