| | | 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.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Diagnostics; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | // TODO: Go through and verify all the internal methods to see if they are needed/used |
| | | 14 | | public abstract class CommunicationObject : ICommunicationObject |
| | | 15 | | { |
| | | 16 | | private bool _closeCalled; |
| | | 17 | | private ExceptionQueue _exceptionQueue; |
| | | 18 | | private bool _onClosingCalled; |
| | | 19 | | private bool _onClosedCalled; |
| | | 20 | | private bool _onOpeningCalled; |
| | | 21 | | private bool _onOpenedCalled; |
| | | 22 | | private bool _raisedClosed; |
| | | 23 | | private bool _raisedClosing; |
| | | 24 | | private bool _raisedFaulted; |
| | | 25 | | |
| | 13726 | 26 | | protected CommunicationObject() : this(new object()) { } |
| | | 27 | | |
| | 6863 | 28 | | protected CommunicationObject(object mutex) |
| | | 29 | | { |
| | 6863 | 30 | | ThisLock = mutex; |
| | 6863 | 31 | | EventSender = this; |
| | 6863 | 32 | | State = CommunicationState.Created; |
| | 6863 | 33 | | } |
| | | 34 | | |
| | 7393 | 35 | | internal bool Aborted { get; private set; } |
| | | 36 | | |
| | 7598 | 37 | | internal object EventSender { get; set; } |
| | | 38 | | |
| | | 39 | | protected bool IsDisposed |
| | | 40 | | { |
| | 0 | 41 | | get { return State == CommunicationState.Closed; } |
| | | 42 | | } |
| | | 43 | | |
| | 86490 | 44 | | public CommunicationState State { get; private set; } |
| | | 45 | | |
| | 51779 | 46 | | protected object ThisLock { get; } |
| | | 47 | | |
| | | 48 | | protected abstract TimeSpan DefaultCloseTimeout { get; } |
| | | 49 | | protected abstract TimeSpan DefaultOpenTimeout { get; } |
| | | 50 | | |
| | | 51 | | internal TimeSpan InternalCloseTimeout |
| | | 52 | | { |
| | 0 | 53 | | get { return DefaultCloseTimeout; } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | internal TimeSpan InternalOpenTimeout |
| | | 57 | | { |
| | 516 | 58 | | get { return DefaultOpenTimeout; } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public event EventHandler Closed; |
| | | 62 | | public event EventHandler Closing; |
| | | 63 | | public event EventHandler Faulted; |
| | | 64 | | public event EventHandler Opened; |
| | | 65 | | public event EventHandler Opening; |
| | | 66 | | |
| | | 67 | | public void Abort() |
| | | 68 | | { |
| | 585 | 69 | | lock (ThisLock) |
| | | 70 | | { |
| | 585 | 71 | | if (Aborted || State == CommunicationState.Closed) |
| | | 72 | | { |
| | 1 | 73 | | return; |
| | | 74 | | } |
| | | 75 | | |
| | 584 | 76 | | Aborted = true; |
| | 584 | 77 | | State = CommunicationState.Closing; |
| | 584 | 78 | | } |
| | | 79 | | |
| | | 80 | | //if (DiagnosticUtility.ShouldTraceInformation) |
| | | 81 | | //{ |
| | | 82 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectAborted, SR.Format(SR |
| | | 83 | | //} |
| | | 84 | | |
| | | 85 | | //bool throwing = true; |
| | | 86 | | |
| | | 87 | | //try |
| | | 88 | | //{ |
| | 584 | 89 | | OnClosing(); |
| | 584 | 90 | | if (!_onClosingCalled) |
| | | 91 | | { |
| | 0 | 92 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, th |
| | | 93 | | } |
| | | 94 | | |
| | 584 | 95 | | OnAbort(); |
| | | 96 | | |
| | 584 | 97 | | OnClosed(); |
| | 584 | 98 | | if (!_onClosedCalled) |
| | | 99 | | { |
| | 0 | 100 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, thi |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | //throwing = false; |
| | | 104 | | //} |
| | | 105 | | //finally |
| | | 106 | | //{ |
| | | 107 | | // if (throwing) |
| | | 108 | | // { |
| | | 109 | | // if (DiagnosticUtility.ShouldTraceWarning) |
| | | 110 | | // TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectAbortFailed, SR.F |
| | | 111 | | // } |
| | | 112 | | //} |
| | 585 | 113 | | } |
| | | 114 | | |
| | | 115 | | public Task CloseAsync() |
| | | 116 | | { |
| | 2934 | 117 | | var helper = new TimeoutHelper(DefaultCloseTimeout); |
| | 2934 | 118 | | return CloseAsync(helper.GetCancellationToken()); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public async Task CloseAsync(CancellationToken token) |
| | | 122 | | { |
| | 3091 | 123 | | token.ThrowIfCancellationRequested(); |
| | | 124 | | |
| | | 125 | | //using (DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? this.CreateCloseActivity() : null) |
| | | 126 | | //{ |
| | | 127 | | |
| | | 128 | | CommunicationState originalState; |
| | 3091 | 129 | | lock (ThisLock) |
| | | 130 | | { |
| | 3091 | 131 | | originalState = State; |
| | 3091 | 132 | | if (originalState != CommunicationState.Closed) |
| | | 133 | | { |
| | 3021 | 134 | | State = CommunicationState.Closing; |
| | | 135 | | } |
| | | 136 | | |
| | 3091 | 137 | | _closeCalled = true; |
| | 3091 | 138 | | } |
| | | 139 | | |
| | | 140 | | switch (originalState) |
| | | 141 | | { |
| | | 142 | | case CommunicationState.Created: |
| | | 143 | | case CommunicationState.Opening: |
| | | 144 | | case CommunicationState.Faulted: |
| | 518 | 145 | | Abort(); |
| | 518 | 146 | | if (originalState == CommunicationState.Faulted) |
| | | 147 | | { |
| | 0 | 148 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 149 | | } |
| | | 150 | | break; |
| | | 151 | | |
| | | 152 | | case CommunicationState.Opened: |
| | | 153 | | { |
| | 2499 | 154 | | bool throwing = true; |
| | | 155 | | try |
| | | 156 | | { |
| | | 157 | | //TimeoutHelper actualTimeout = new TimeoutHelper(timeout); |
| | | 158 | | |
| | 2499 | 159 | | OnClosing(); |
| | 2499 | 160 | | if (!_onClosingCalled) |
| | | 161 | | { |
| | 0 | 162 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnClosing") |
| | | 163 | | } |
| | | 164 | | |
| | 2499 | 165 | | await OnCloseAsync(token); |
| | | 166 | | |
| | 2494 | 167 | | OnClosed(); |
| | 2494 | 168 | | if (!_onClosedCalled) |
| | | 169 | | { |
| | 0 | 170 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnClosed"), |
| | | 171 | | } |
| | | 172 | | |
| | 2494 | 173 | | throwing = false; |
| | 2494 | 174 | | } |
| | | 175 | | finally |
| | | 176 | | { |
| | 2499 | 177 | | if (throwing) |
| | | 178 | | { |
| | | 179 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 180 | | //{ |
| | | 181 | | // TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectClose |
| | | 182 | | //} |
| | | 183 | | |
| | 5 | 184 | | Abort(); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | break; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | case CommunicationState.Closing: |
| | | 191 | | case CommunicationState.Closed: |
| | | 192 | | break; |
| | | 193 | | |
| | | 194 | | default: |
| | 0 | 195 | | throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState"); |
| | | 196 | | } |
| | | 197 | | //} |
| | | 198 | | |
| | 3086 | 199 | | } |
| | | 200 | | |
| | | 201 | | public System.Threading.Tasks.Task OpenAsync() |
| | | 202 | | { |
| | 3316 | 203 | | CancellationToken token = new TimeoutHelper(DefaultOpenTimeout).GetCancellationToken(); |
| | 3316 | 204 | | return OpenAsync(token); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public async Task OpenAsync(CancellationToken token) |
| | | 208 | | { |
| | 6224 | 209 | | token.ThrowIfCancellationRequested(); |
| | | 210 | | |
| | | 211 | | //using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? Ser |
| | | 212 | | //{ |
| | | 213 | | //if (DiagnosticUtility.ShouldUseActivity) |
| | | 214 | | //{ |
| | | 215 | | // ServiceModelActivity.Start(activity, this.OpenActivityName, this.OpenActivityType); |
| | | 216 | | //} |
| | 6224 | 217 | | lock (ThisLock) |
| | | 218 | | { |
| | 6224 | 219 | | ThrowIfDisposedOrImmutable(); |
| | 6224 | 220 | | State = CommunicationState.Opening; |
| | 6224 | 221 | | } |
| | | 222 | | |
| | 6224 | 223 | | bool throwing = true; |
| | | 224 | | try |
| | | 225 | | { |
| | 6224 | 226 | | OnOpening(); |
| | 6224 | 227 | | if (!_onOpeningCalled) |
| | | 228 | | { |
| | 0 | 229 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnOpening"), Guid.Empty |
| | | 230 | | } |
| | | 231 | | |
| | 6224 | 232 | | await OnOpenAsync(token); |
| | | 233 | | |
| | 6224 | 234 | | OnOpened(); |
| | 6214 | 235 | | if (!_onOpenedCalled) |
| | | 236 | | { |
| | 0 | 237 | | throw TraceUtility.ThrowHelperError(CreateBaseClassMethodNotCalledException("OnOpened"), Guid.Empty, |
| | | 238 | | } |
| | | 239 | | |
| | 6214 | 240 | | throwing = false; |
| | 6214 | 241 | | } |
| | | 242 | | finally |
| | | 243 | | { |
| | 6224 | 244 | | if (throwing) |
| | | 245 | | { |
| | | 246 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 247 | | //{ |
| | | 248 | | // TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed, SR.Fo |
| | | 249 | | //} |
| | | 250 | | |
| | 10 | 251 | | Fault(); |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | //} |
| | 6214 | 255 | | } |
| | | 256 | | |
| | | 257 | | // TODO: Make internal again |
| | | 258 | | public void Fault(Exception exception) |
| | | 259 | | { |
| | 0 | 260 | | lock (ThisLock) |
| | | 261 | | { |
| | 0 | 262 | | if (_exceptionQueue == null) |
| | | 263 | | { |
| | 0 | 264 | | _exceptionQueue = new ExceptionQueue(ThisLock); |
| | | 265 | | } |
| | 0 | 266 | | } |
| | | 267 | | |
| | | 268 | | //if (exception != null && DiagnosticUtility.ShouldTraceInformation) |
| | | 269 | | //{ |
| | | 270 | | // TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectFaultReason, |
| | | 271 | | // SR.TraceCodeCommunicationObjectFaultReason, exception, null); |
| | | 272 | | //} |
| | | 273 | | |
| | 0 | 274 | | _exceptionQueue.AddException(exception); |
| | 0 | 275 | | Fault(); |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | internal void AddPendingException(Exception exception) |
| | | 279 | | { |
| | 0 | 280 | | lock (ThisLock) |
| | | 281 | | { |
| | 0 | 282 | | if (_exceptionQueue == null) |
| | | 283 | | { |
| | 0 | 284 | | _exceptionQueue = new ExceptionQueue(ThisLock); |
| | | 285 | | } |
| | 0 | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | _exceptionQueue.AddException(exception); |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | internal Exception GetPendingException() |
| | | 292 | | { |
| | 0 | 293 | | CommunicationState currentState = State; |
| | | 294 | | |
| | | 295 | | Fx.Assert(currentState == CommunicationState.Closing || currentState == CommunicationState.Closed || current |
| | | 296 | | "CommunicationObject.GetPendingException(currentState == CommunicationState.Closing || currentState == C |
| | | 297 | | |
| | 0 | 298 | | ExceptionQueue queue = _exceptionQueue; |
| | 0 | 299 | | if (queue != null) |
| | | 300 | | { |
| | 0 | 301 | | return queue.GetException(); |
| | | 302 | | } |
| | | 303 | | else |
| | | 304 | | { |
| | 0 | 305 | | return null; |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | protected void Fault() |
| | | 310 | | { |
| | 16 | 311 | | lock (ThisLock) |
| | | 312 | | { |
| | 16 | 313 | | if (State == CommunicationState.Closed || State == CommunicationState.Closing) |
| | | 314 | | { |
| | 5 | 315 | | return; |
| | | 316 | | } |
| | | 317 | | |
| | 11 | 318 | | if (State == CommunicationState.Faulted) |
| | | 319 | | { |
| | 0 | 320 | | return; |
| | | 321 | | } |
| | | 322 | | |
| | 11 | 323 | | State = CommunicationState.Faulted; |
| | 11 | 324 | | } |
| | | 325 | | |
| | 11 | 326 | | OnFaulted(); |
| | 16 | 327 | | } |
| | | 328 | | |
| | | 329 | | internal void ThrowIfClosed() |
| | | 330 | | { |
| | 10224 | 331 | | ThrowPending(); |
| | | 332 | | |
| | 10224 | 333 | | switch (State) |
| | | 334 | | { |
| | | 335 | | case CommunicationState.Created: |
| | | 336 | | break; |
| | | 337 | | |
| | | 338 | | case CommunicationState.Opening: |
| | | 339 | | break; |
| | | 340 | | |
| | | 341 | | case CommunicationState.Opened: |
| | | 342 | | break; |
| | | 343 | | |
| | | 344 | | case CommunicationState.Closing: |
| | | 345 | | break; |
| | | 346 | | |
| | | 347 | | case CommunicationState.Closed: |
| | 0 | 348 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 349 | | |
| | | 350 | | case CommunicationState.Faulted: |
| | 0 | 351 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 352 | | |
| | | 353 | | default: |
| | 0 | 354 | | throw Fx.AssertAndThrow("ThrowIfClosed: Unknown CommunicationObject.state"); |
| | | 355 | | } |
| | 10224 | 356 | | } |
| | | 357 | | |
| | | 358 | | protected virtual Type GetCommunicationObjectType() |
| | | 359 | | { |
| | 0 | 360 | | return GetType(); |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | protected abstract void OnAbort(); |
| | | 364 | | |
| | | 365 | | protected abstract Task OnCloseAsync(CancellationToken token); |
| | | 366 | | |
| | | 367 | | protected abstract Task OnOpenAsync(CancellationToken token); |
| | | 368 | | |
| | | 369 | | protected virtual void OnClosed() |
| | | 370 | | { |
| | 3078 | 371 | | _onClosedCalled = true; |
| | | 372 | | |
| | 3078 | 373 | | lock (ThisLock) |
| | | 374 | | { |
| | 3078 | 375 | | if (_raisedClosed) |
| | | 376 | | { |
| | 0 | 377 | | return; |
| | | 378 | | } |
| | | 379 | | |
| | 3078 | 380 | | _raisedClosed = true; |
| | 3078 | 381 | | State = CommunicationState.Closed; |
| | 3078 | 382 | | } |
| | | 383 | | |
| | | 384 | | //if (DiagnosticUtility.ShouldTraceVerbose) |
| | | 385 | | //{ |
| | | 386 | | // TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectClosed, SR.Format(SR.Trac |
| | | 387 | | //} |
| | | 388 | | |
| | 3078 | 389 | | EventHandler handler = Closed; |
| | 3078 | 390 | | if (handler != null) |
| | | 391 | | { |
| | | 392 | | try |
| | | 393 | | { |
| | 79 | 394 | | handler(EventSender, EventArgs.Empty); |
| | 79 | 395 | | } |
| | 0 | 396 | | catch (Exception exception) |
| | | 397 | | { |
| | 0 | 398 | | if (Fx.IsFatal(exception)) |
| | | 399 | | { |
| | 0 | 400 | | throw; |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception); |
| | | 404 | | } |
| | | 405 | | } |
| | 3078 | 406 | | } |
| | | 407 | | |
| | | 408 | | protected virtual void OnClosing() |
| | | 409 | | { |
| | 3083 | 410 | | _onClosingCalled = true; |
| | | 411 | | |
| | 3083 | 412 | | lock (ThisLock) |
| | | 413 | | { |
| | 3083 | 414 | | if (_raisedClosing) |
| | | 415 | | { |
| | 5 | 416 | | return; |
| | | 417 | | } |
| | | 418 | | |
| | 3078 | 419 | | _raisedClosing = true; |
| | 3078 | 420 | | } |
| | | 421 | | |
| | | 422 | | //if (DiagnosticUtility.ShouldTraceVerbose) |
| | | 423 | | //{ |
| | | 424 | | // TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectClosing, SR.Format(SR.Tra |
| | | 425 | | //} |
| | 3078 | 426 | | EventHandler handler = Closing; |
| | 3078 | 427 | | if (handler != null) |
| | | 428 | | { |
| | | 429 | | try |
| | | 430 | | { |
| | 0 | 431 | | handler(EventSender, EventArgs.Empty); |
| | 0 | 432 | | } |
| | 0 | 433 | | catch (Exception exception) |
| | | 434 | | { |
| | 0 | 435 | | if (Fx.IsFatal(exception)) |
| | | 436 | | { |
| | 0 | 437 | | throw; |
| | | 438 | | } |
| | | 439 | | |
| | 0 | 440 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception); |
| | | 441 | | } |
| | | 442 | | } |
| | 3083 | 443 | | } |
| | | 444 | | |
| | | 445 | | protected virtual void OnFaulted() |
| | | 446 | | { |
| | 11 | 447 | | lock (ThisLock) |
| | | 448 | | { |
| | 11 | 449 | | if (_raisedFaulted) |
| | | 450 | | { |
| | 0 | 451 | | return; |
| | | 452 | | } |
| | | 453 | | |
| | 11 | 454 | | _raisedFaulted = true; |
| | 11 | 455 | | } |
| | | 456 | | |
| | | 457 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 458 | | //{ |
| | | 459 | | // TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectFaulted, SR.Format(SR.Tra |
| | | 460 | | //} |
| | | 461 | | |
| | 11 | 462 | | EventHandler handler = Faulted; |
| | 11 | 463 | | if (handler != null) |
| | | 464 | | { |
| | | 465 | | try |
| | | 466 | | { |
| | 1 | 467 | | handler(EventSender, EventArgs.Empty); |
| | 1 | 468 | | } |
| | 0 | 469 | | catch (Exception exception) |
| | | 470 | | { |
| | 0 | 471 | | if (Fx.IsFatal(exception)) |
| | | 472 | | { |
| | 0 | 473 | | throw; |
| | | 474 | | } |
| | | 475 | | |
| | 0 | 476 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception); |
| | | 477 | | } |
| | | 478 | | } |
| | 11 | 479 | | } |
| | | 480 | | |
| | | 481 | | protected virtual void OnOpened() |
| | | 482 | | { |
| | 6224 | 483 | | _onOpenedCalled = true; |
| | | 484 | | |
| | 6224 | 485 | | lock (ThisLock) |
| | | 486 | | { |
| | 6224 | 487 | | if (Aborted || State != CommunicationState.Opening) |
| | | 488 | | { |
| | 0 | 489 | | return; |
| | | 490 | | } |
| | | 491 | | |
| | 6224 | 492 | | State = CommunicationState.Opened; |
| | 6224 | 493 | | } |
| | | 494 | | |
| | | 495 | | //if (DiagnosticUtility.ShouldTraceVerbose) |
| | | 496 | | // TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectOpened, SR.Format(SR.Trac |
| | | 497 | | |
| | 6224 | 498 | | EventHandler handler = Opened; |
| | 6224 | 499 | | if (handler != null) |
| | | 500 | | { |
| | | 501 | | try |
| | | 502 | | { |
| | 571 | 503 | | handler(EventSender, EventArgs.Empty); |
| | 561 | 504 | | } |
| | 10 | 505 | | catch (Exception exception) |
| | | 506 | | { |
| | 10 | 507 | | if (Fx.IsFatal(exception)) |
| | | 508 | | { |
| | 0 | 509 | | throw; |
| | | 510 | | } |
| | | 511 | | |
| | 10 | 512 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception); |
| | | 513 | | } |
| | | 514 | | } |
| | 6214 | 515 | | } |
| | | 516 | | |
| | | 517 | | protected virtual void OnOpening() |
| | | 518 | | { |
| | 6224 | 519 | | _onOpeningCalled = true; |
| | | 520 | | |
| | | 521 | | //if (DiagnosticUtility.ShouldTraceVerbose) |
| | | 522 | | //{ |
| | | 523 | | // TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectOpening, SR.Format(SR.Tra |
| | | 524 | | //} |
| | | 525 | | |
| | 6224 | 526 | | EventHandler handler = Opening; |
| | 6224 | 527 | | if (handler != null) |
| | | 528 | | { |
| | | 529 | | try |
| | | 530 | | { |
| | 83 | 531 | | handler(EventSender, EventArgs.Empty); |
| | 83 | 532 | | } |
| | 0 | 533 | | catch (Exception exception) |
| | | 534 | | { |
| | 0 | 535 | | if (Fx.IsFatal(exception)) |
| | | 536 | | { |
| | 0 | 537 | | throw; |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception); |
| | | 541 | | } |
| | | 542 | | } |
| | 6224 | 543 | | } |
| | | 544 | | internal void ThrowIfFaulted() |
| | | 545 | | { |
| | 44 | 546 | | ThrowPending(); |
| | | 547 | | |
| | 44 | 548 | | switch (State) |
| | | 549 | | { |
| | | 550 | | case CommunicationState.Created: |
| | | 551 | | break; |
| | | 552 | | |
| | | 553 | | case CommunicationState.Opening: |
| | | 554 | | break; |
| | | 555 | | |
| | | 556 | | case CommunicationState.Opened: |
| | | 557 | | break; |
| | | 558 | | |
| | | 559 | | case CommunicationState.Closing: |
| | | 560 | | break; |
| | | 561 | | |
| | | 562 | | case CommunicationState.Closed: |
| | | 563 | | break; |
| | | 564 | | |
| | | 565 | | case CommunicationState.Faulted: |
| | 0 | 566 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 567 | | |
| | | 568 | | default: |
| | 0 | 569 | | throw Fx.AssertAndThrow("ThrowIfFaulted: Unknown CommunicationObject.state"); |
| | | 570 | | } |
| | 44 | 571 | | } |
| | | 572 | | |
| | | 573 | | internal void ThrowIfAborted() |
| | | 574 | | { |
| | 0 | 575 | | if (Aborted && !_closeCalled) |
| | | 576 | | { |
| | 0 | 577 | | throw TraceUtility.ThrowHelperError(CreateAbortedException(), Guid.Empty, this); |
| | | 578 | | } |
| | 0 | 579 | | } |
| | | 580 | | |
| | | 581 | | private Exception CreateNotOpenException() |
| | | 582 | | { |
| | 0 | 583 | | return new InvalidOperationException(SR.Format(SRCommon.CommunicationObjectCannotBeUsed, GetCommunicationObj |
| | | 584 | | } |
| | | 585 | | |
| | | 586 | | private Exception CreateBaseClassMethodNotCalledException(string method) |
| | | 587 | | { |
| | 0 | 588 | | return new InvalidOperationException(SR.Format(SR.CommunicationObjectBaseClassMethodNotCalled, GetCommunicat |
| | | 589 | | } |
| | | 590 | | |
| | | 591 | | private Exception CreateImmutableException() |
| | | 592 | | { |
| | 0 | 593 | | return new InvalidOperationException(SR.Format(SR.CommunicationObjectCannotBeModifiedInState, GetCommunicati |
| | | 594 | | } |
| | | 595 | | |
| | | 596 | | internal Exception CreateClosedException() |
| | | 597 | | { |
| | 0 | 598 | | if (!_closeCalled) |
| | | 599 | | { |
| | 0 | 600 | | return CreateAbortedException(); |
| | | 601 | | } |
| | | 602 | | else |
| | | 603 | | { |
| | 0 | 604 | | return new ObjectDisposedException(GetCommunicationObjectType().ToString()); |
| | | 605 | | } |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | internal Exception CreateFaultedException() |
| | | 609 | | { |
| | 0 | 610 | | string message = SR.Format(SR.CommunicationObjectFaulted1, GetCommunicationObjectType().ToString()); |
| | 0 | 611 | | return new CommunicationObjectFaultedException(message); |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | internal Exception CreateAbortedException() |
| | | 615 | | { |
| | 0 | 616 | | return new CommunicationObjectAbortedException(SR.Format(SR.CommunicationObjectAborted1, GetCommunicationObj |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | protected internal void ThrowIfDisposed() |
| | | 620 | | { |
| | 2481 | 621 | | ThrowPending(); |
| | | 622 | | |
| | 2481 | 623 | | switch (State) |
| | | 624 | | { |
| | | 625 | | case CommunicationState.Created: |
| | | 626 | | break; |
| | | 627 | | |
| | | 628 | | case CommunicationState.Opening: |
| | | 629 | | break; |
| | | 630 | | |
| | | 631 | | case CommunicationState.Opened: |
| | | 632 | | break; |
| | | 633 | | |
| | | 634 | | case CommunicationState.Closing: |
| | 0 | 635 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 636 | | |
| | | 637 | | case CommunicationState.Closed: |
| | 0 | 638 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 639 | | |
| | | 640 | | case CommunicationState.Faulted: |
| | 0 | 641 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 642 | | |
| | | 643 | | default: |
| | 0 | 644 | | throw Fx.AssertAndThrow("ThrowIfDisposed: Unknown CommunicationObject.state"); |
| | | 645 | | } |
| | 2481 | 646 | | } |
| | | 647 | | |
| | | 648 | | internal void ThrowIfClosedOrOpened() |
| | | 649 | | { |
| | 640 | 650 | | ThrowPending(); |
| | | 651 | | |
| | 640 | 652 | | switch (State) |
| | | 653 | | { |
| | | 654 | | case CommunicationState.Created: |
| | | 655 | | break; |
| | | 656 | | |
| | | 657 | | case CommunicationState.Opening: |
| | | 658 | | break; |
| | | 659 | | |
| | | 660 | | case CommunicationState.Opened: |
| | 0 | 661 | | throw TraceUtility.ThrowHelperError(CreateImmutableException(), Guid.Empty, this); |
| | | 662 | | |
| | | 663 | | case CommunicationState.Closing: |
| | 0 | 664 | | throw TraceUtility.ThrowHelperError(CreateImmutableException(), Guid.Empty, this); |
| | | 665 | | |
| | | 666 | | case CommunicationState.Closed: |
| | 0 | 667 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 668 | | |
| | | 669 | | case CommunicationState.Faulted: |
| | 0 | 670 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 671 | | |
| | | 672 | | default: |
| | 0 | 673 | | throw Fx.AssertAndThrow("ThrowIfClosedOrOpened: Unknown CommunicationObject.state"); |
| | | 674 | | } |
| | 640 | 675 | | } |
| | | 676 | | |
| | | 677 | | protected internal void ThrowIfDisposedOrImmutable() |
| | | 678 | | { |
| | 12263 | 679 | | ThrowPending(); |
| | | 680 | | |
| | 12263 | 681 | | switch (State) |
| | | 682 | | { |
| | | 683 | | case CommunicationState.Created: |
| | | 684 | | break; |
| | | 685 | | |
| | | 686 | | case CommunicationState.Opening: |
| | 0 | 687 | | throw TraceUtility.ThrowHelperError(CreateImmutableException(), Guid.Empty, this); |
| | | 688 | | |
| | | 689 | | case CommunicationState.Opened: |
| | 0 | 690 | | throw TraceUtility.ThrowHelperError(CreateImmutableException(), Guid.Empty, this); |
| | | 691 | | |
| | | 692 | | case CommunicationState.Closing: |
| | 0 | 693 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 694 | | |
| | | 695 | | case CommunicationState.Closed: |
| | 0 | 696 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 697 | | |
| | | 698 | | case CommunicationState.Faulted: |
| | 0 | 699 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 700 | | |
| | | 701 | | default: |
| | 0 | 702 | | throw Fx.AssertAndThrow("ThrowIfDisposedOrImmutable: Unknown CommunicationObject.state"); |
| | | 703 | | } |
| | 12263 | 704 | | } |
| | | 705 | | |
| | | 706 | | protected internal void ThrowIfDisposedOrNotOpen() |
| | | 707 | | { |
| | 1382 | 708 | | ThrowPending(); |
| | | 709 | | |
| | 1382 | 710 | | switch (State) |
| | | 711 | | { |
| | | 712 | | case CommunicationState.Created: |
| | 0 | 713 | | throw TraceUtility.ThrowHelperError(CreateNotOpenException(), Guid.Empty, this); |
| | | 714 | | |
| | | 715 | | case CommunicationState.Opening: |
| | 0 | 716 | | throw TraceUtility.ThrowHelperError(CreateNotOpenException(), Guid.Empty, this); |
| | | 717 | | |
| | | 718 | | case CommunicationState.Opened: |
| | | 719 | | break; |
| | | 720 | | |
| | | 721 | | case CommunicationState.Closing: |
| | 0 | 722 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 723 | | |
| | | 724 | | case CommunicationState.Closed: |
| | 0 | 725 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 726 | | |
| | | 727 | | case CommunicationState.Faulted: |
| | 0 | 728 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 729 | | |
| | | 730 | | default: |
| | 0 | 731 | | throw Fx.AssertAndThrow("ThrowIfDisposedOrNotOpen: Unknown CommunicationObject.state"); |
| | | 732 | | } |
| | 1382 | 733 | | } |
| | | 734 | | |
| | | 735 | | public void ThrowIfNotOpened() |
| | | 736 | | { |
| | 109 | 737 | | if (State == CommunicationState.Created || State == CommunicationState.Opening) |
| | | 738 | | { |
| | 0 | 739 | | throw TraceUtility.ThrowHelperError(CreateNotOpenException(), Guid.Empty, this); |
| | | 740 | | } |
| | 109 | 741 | | } |
| | | 742 | | |
| | | 743 | | internal void ThrowIfClosedOrNotOpen() |
| | | 744 | | { |
| | 2748 | 745 | | ThrowPending(); |
| | | 746 | | |
| | 2748 | 747 | | switch (State) |
| | | 748 | | { |
| | | 749 | | case CommunicationState.Created: |
| | 0 | 750 | | throw TraceUtility.ThrowHelperError(CreateNotOpenException(), Guid.Empty, this); |
| | | 751 | | |
| | | 752 | | case CommunicationState.Opening: |
| | 0 | 753 | | throw TraceUtility.ThrowHelperError(CreateNotOpenException(), Guid.Empty, this); |
| | | 754 | | |
| | | 755 | | case CommunicationState.Opened: |
| | | 756 | | break; |
| | | 757 | | |
| | | 758 | | case CommunicationState.Closing: |
| | | 759 | | break; |
| | | 760 | | |
| | | 761 | | case CommunicationState.Closed: |
| | 0 | 762 | | throw TraceUtility.ThrowHelperError(CreateClosedException(), Guid.Empty, this); |
| | | 763 | | |
| | | 764 | | case CommunicationState.Faulted: |
| | 0 | 765 | | throw TraceUtility.ThrowHelperError(CreateFaultedException(), Guid.Empty, this); |
| | | 766 | | |
| | | 767 | | default: |
| | 0 | 768 | | throw Fx.AssertAndThrow("ThrowIfClosedOrNotOpen: Unknown CommunicationObject.state"); |
| | | 769 | | } |
| | 2748 | 770 | | } |
| | | 771 | | |
| | | 772 | | //TODO: Make internal again |
| | | 773 | | public void ThrowPending() |
| | | 774 | | { |
| | 30188 | 775 | | ExceptionQueue queue = _exceptionQueue; |
| | | 776 | | |
| | 30188 | 777 | | if (queue != null) |
| | | 778 | | { |
| | 0 | 779 | | Exception exception = queue.GetException(); |
| | | 780 | | |
| | 0 | 781 | | if (exception != null) |
| | | 782 | | { |
| | 0 | 783 | | throw TraceUtility.ThrowHelperError(exception, Guid.Empty, this); |
| | | 784 | | } |
| | | 785 | | } |
| | 30188 | 786 | | } |
| | | 787 | | |
| | | 788 | | private class ExceptionQueue |
| | | 789 | | { |
| | 0 | 790 | | private readonly Queue<Exception> _exceptions = new Queue<Exception>(); |
| | | 791 | | |
| | 0 | 792 | | internal ExceptionQueue(object thisLock) |
| | | 793 | | { |
| | 0 | 794 | | ThisLock = thisLock; |
| | 0 | 795 | | } |
| | | 796 | | |
| | 0 | 797 | | private object ThisLock { get; } |
| | | 798 | | |
| | | 799 | | public void AddException(Exception exception) |
| | | 800 | | { |
| | 0 | 801 | | if (exception == null) |
| | | 802 | | { |
| | 0 | 803 | | return; |
| | | 804 | | } |
| | | 805 | | |
| | 0 | 806 | | lock (ThisLock) |
| | | 807 | | { |
| | 0 | 808 | | _exceptions.Enqueue(exception); |
| | 0 | 809 | | } |
| | 0 | 810 | | } |
| | | 811 | | |
| | | 812 | | public Exception GetException() |
| | | 813 | | { |
| | 0 | 814 | | lock (ThisLock) |
| | | 815 | | { |
| | 0 | 816 | | if (_exceptions.Count > 0) |
| | | 817 | | { |
| | 0 | 818 | | return _exceptions.Dequeue(); |
| | | 819 | | } |
| | 0 | 820 | | } |
| | | 821 | | |
| | 0 | 822 | | return null; |
| | 0 | 823 | | } |
| | | 824 | | } |
| | | 825 | | } |
| | | 826 | | } |