ApplySecurityAndSendAsyncResult.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ApplySecurityAndSendAsyncResult<MessageSenderType> : AsyncResult
  10. where MessageSenderType : class
  11. {
  12. readonly MessageSenderType channel;
  13. readonly SecurityProtocol binding;
  14. volatile bool secureOutgoingMessageDone;
  15. static AsyncCallback sharedCallback = Fx.ThunkCallback(new AsyncCallback(SharedCallback));
  16. SecurityProtocolCorrelationState newCorrelationState;
  17. TimeoutHelper timeoutHelper;
  18. public ApplySecurityAndSendAsyncResult(SecurityProtocol binding, MessageSenderType channel, TimeSpan timeout,
  19. AsyncCallback callback, object state)
  20. : base(callback, state)
  21. {
  22. this.binding = binding;
  23. this.channel = channel;
  24. this.timeoutHelper = new TimeoutHelper(timeout);
  25. }
  26. protected SecurityProtocolCorrelationState CorrelationState
  27. {
  28. get { return newCorrelationState; }
  29. }
  30. protected SecurityProtocol SecurityProtocol
  31. {
  32. get { return this.binding; }
  33. }
  34. protected void Begin(Message message, SecurityProtocolCorrelationState correlationState)
  35. {
  36. IAsyncResult result = this.binding.BeginSecureOutgoingMessage(message, timeoutHelper.RemainingTime(), correlationState, sharedCallback, this);
  37. if (result.CompletedSynchronously)
  38. {
  39. this.binding.EndSecureOutgoingMessage(result, out message, out newCorrelationState);
  40. bool completedSynchronously = this.OnSecureOutgoingMessageComplete(message);
  41. if (completedSynchronously)
  42. {
  43. Complete(true);
  44. }
  45. }
  46. }
  47. protected static void OnEnd(ApplySecurityAndSendAsyncResult<MessageSenderType> self)
  48. {
  49. AsyncResult.End<ApplySecurityAndSendAsyncResult<MessageSenderType>>(self);
  50. }
  51. bool OnSecureOutgoingMessageComplete(Message message)
  52. {
  53. if (message == null)
  54. {
  55. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
  56. }
  57. this.secureOutgoingMessageDone = true;
  58. IAsyncResult result = BeginSendCore(this.channel, message, timeoutHelper.RemainingTime(), sharedCallback, this);
  59. if (!result.CompletedSynchronously)
  60. {
  61. return false;
  62. }
  63. EndSendCore(this.channel, result);
  64. return this.OnSendComplete();
  65. }
  66. protected abstract IAsyncResult BeginSendCore(MessageSenderType channel, Message message, TimeSpan timeout, AsyncCallback callback, object state);
  67. protected abstract void EndSendCore(MessageSenderType channel, IAsyncResult result);
  68. bool OnSendComplete()
  69. {
  70. OnSendCompleteCore(timeoutHelper.RemainingTime());
  71. return true;
  72. }
  73. protected abstract void OnSendCompleteCore(TimeSpan timeout);
  74. static void SharedCallback(IAsyncResult result)
  75. {
  76. if (result == null)
  77. {
  78. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("result"));
  79. }
  80. if (result.CompletedSynchronously)
  81. {
  82. return;
  83. }
  84. ApplySecurityAndSendAsyncResult<MessageSenderType> self = result.AsyncState as ApplySecurityAndSendAsyncResult<MessageSenderType>;
  85. if (self == null)
  86. {
  87. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "result"));
  88. }
  89. bool completeSelf = false;
  90. Exception completionException = null;
  91. try
  92. {
  93. if (!self.secureOutgoingMessageDone)
  94. {
  95. Message message;
  96. self.binding.EndSecureOutgoingMessage(result, out message, out self.newCorrelationState);
  97. completeSelf = self.OnSecureOutgoingMessageComplete(message);
  98. }
  99. else
  100. {
  101. self.EndSendCore(self.channel, result);
  102. completeSelf = self.OnSendComplete();
  103. }
  104. }
  105. #pragma warning suppress 56500 // covered by FxCOP
  106. catch (Exception e)
  107. {
  108. if (Fx.IsFatal(e))
  109. {
  110. throw;
  111. }
  112. completeSelf = true;
  113. completionException = e;
  114. }
  115. if (completeSelf)
  116. {
  117. self.Complete(false, completionException);
  118. }
  119. }
  120. }
  121. }