ReceiveMessageAndVerifySecurityAsyncResultBase.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //----------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Runtime;
  7. using System.Runtime.InteropServices;
  8. using System.ServiceModel.Channels;
  9. abstract class ReceiveMessageAndVerifySecurityAsyncResultBase : AsyncResult
  10. {
  11. static AsyncCallback innerTryReceiveCompletedCallback = Fx.ThunkCallback(new AsyncCallback(InnerTryReceiveCompletedCallback));
  12. Message message;
  13. bool receiveCompleted;
  14. TimeoutHelper timeoutHelper;
  15. IInputChannel innerChannel;
  16. protected ReceiveMessageAndVerifySecurityAsyncResultBase(IInputChannel innerChannel, TimeSpan timeout, AsyncCallback callback, object state)
  17. : base(callback, state)
  18. {
  19. this.timeoutHelper = new TimeoutHelper(timeout);
  20. this.innerChannel = innerChannel;
  21. }
  22. public void Start()
  23. {
  24. IAsyncResult asyncResult = innerChannel.BeginTryReceive(this.timeoutHelper.RemainingTime(), innerTryReceiveCompletedCallback, this);
  25. if (!asyncResult.CompletedSynchronously)
  26. {
  27. return;
  28. }
  29. bool innerReceiveCompleted = innerChannel.EndTryReceive(asyncResult, out this.message);
  30. if (!innerReceiveCompleted)
  31. {
  32. receiveCompleted = false;
  33. }
  34. else
  35. {
  36. receiveCompleted = true;
  37. bool completedSynchronously = this.OnInnerReceiveDone(ref this.message, this.timeoutHelper.RemainingTime());
  38. if (!completedSynchronously)
  39. {
  40. return;
  41. }
  42. }
  43. Complete(true);
  44. }
  45. static void InnerTryReceiveCompletedCallback(IAsyncResult result)
  46. {
  47. if (result.CompletedSynchronously)
  48. {
  49. return;
  50. }
  51. ReceiveMessageAndVerifySecurityAsyncResultBase thisResult = (ReceiveMessageAndVerifySecurityAsyncResultBase)result.AsyncState;
  52. Exception completionException = null;
  53. bool completeSelf = false;
  54. try
  55. {
  56. bool innerReceiveCompleted = thisResult.innerChannel.EndTryReceive(result, out thisResult.message);
  57. if (!innerReceiveCompleted)
  58. {
  59. thisResult.receiveCompleted = false;
  60. completeSelf = true;
  61. }
  62. else
  63. {
  64. thisResult.receiveCompleted = true;
  65. completeSelf = thisResult.OnInnerReceiveDone(ref thisResult.message, thisResult.timeoutHelper.RemainingTime());
  66. }
  67. }
  68. #pragma warning suppress 56500 // covered by FxCOP
  69. catch (Exception e)
  70. {
  71. if (Fx.IsFatal(e))
  72. throw;
  73. completeSelf = true;
  74. completionException = e;
  75. }
  76. if (completeSelf)
  77. {
  78. thisResult.Complete(false, completionException);
  79. }
  80. }
  81. protected abstract bool OnInnerReceiveDone(ref Message message, TimeSpan timeout);
  82. public static bool End(IAsyncResult result, out Message message)
  83. {
  84. ReceiveMessageAndVerifySecurityAsyncResultBase thisResult = AsyncResult.End<ReceiveMessageAndVerifySecurityAsyncResultBase>(result);
  85. message = thisResult.message;
  86. return thisResult.receiveCompleted;
  87. }
  88. }
  89. }