InputQueueChannel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.ServiceModel.Diagnostics;
  9. abstract class InputQueueChannel<TDisposable> : ChannelBase
  10. where TDisposable : class, IDisposable
  11. {
  12. InputQueue<TDisposable> inputQueue;
  13. protected InputQueueChannel(ChannelManagerBase channelManager)
  14. : base(channelManager)
  15. {
  16. this.inputQueue = TraceUtility.CreateInputQueue<TDisposable>();
  17. }
  18. public int InternalPendingItems
  19. {
  20. get
  21. {
  22. return this.inputQueue.PendingCount;
  23. }
  24. }
  25. public int PendingItems
  26. {
  27. get
  28. {
  29. ThrowIfDisposedOrNotOpen();
  30. return InternalPendingItems;
  31. }
  32. }
  33. public void EnqueueAndDispatch(TDisposable item)
  34. {
  35. EnqueueAndDispatch(item, null);
  36. }
  37. public void EnqueueAndDispatch(TDisposable item, Action dequeuedCallback, bool canDispatchOnThisThread)
  38. {
  39. OnEnqueueItem(item);
  40. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  41. inputQueue.EnqueueAndDispatch(item, dequeuedCallback, canDispatchOnThisThread);
  42. }
  43. public void EnqueueAndDispatch(Exception exception, Action dequeuedCallback, bool canDispatchOnThisThread)
  44. {
  45. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  46. inputQueue.EnqueueAndDispatch(exception, dequeuedCallback, canDispatchOnThisThread);
  47. }
  48. public void EnqueueAndDispatch(TDisposable item, Action dequeuedCallback)
  49. {
  50. OnEnqueueItem(item);
  51. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  52. inputQueue.EnqueueAndDispatch(item, dequeuedCallback);
  53. }
  54. public bool EnqueueWithoutDispatch(Exception exception, Action dequeuedCallback)
  55. {
  56. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  57. return inputQueue.EnqueueWithoutDispatch(exception, dequeuedCallback);
  58. }
  59. public bool EnqueueWithoutDispatch(TDisposable item, Action dequeuedCallback)
  60. {
  61. OnEnqueueItem(item);
  62. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  63. return inputQueue.EnqueueWithoutDispatch(item, dequeuedCallback);
  64. }
  65. public void Dispatch()
  66. {
  67. // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
  68. inputQueue.Dispatch();
  69. }
  70. public void Shutdown()
  71. {
  72. inputQueue.Shutdown();
  73. }
  74. protected override void OnFaulted()
  75. {
  76. base.OnFaulted();
  77. inputQueue.Shutdown(() => this.GetPendingException());
  78. }
  79. protected virtual void OnEnqueueItem(TDisposable item)
  80. {
  81. }
  82. protected IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
  83. {
  84. this.ThrowIfNotOpened();
  85. return inputQueue.BeginDequeue(timeout, callback, state);
  86. }
  87. protected bool EndDequeue(IAsyncResult result, out TDisposable item)
  88. {
  89. bool dequeued = inputQueue.EndDequeue(result, out item);
  90. if (item == null)
  91. {
  92. this.ThrowIfFaulted();
  93. this.ThrowIfAborted();
  94. }
  95. return dequeued;
  96. }
  97. protected bool Dequeue(TimeSpan timeout, out TDisposable item)
  98. {
  99. this.ThrowIfNotOpened();
  100. bool dequeued = inputQueue.Dequeue(timeout, out item);
  101. if (item == null)
  102. {
  103. this.ThrowIfFaulted();
  104. this.ThrowIfAborted();
  105. }
  106. return dequeued;
  107. }
  108. protected bool WaitForItem(TimeSpan timeout)
  109. {
  110. this.ThrowIfNotOpened();
  111. bool dequeued = inputQueue.WaitForItem(timeout);
  112. this.ThrowIfFaulted();
  113. this.ThrowIfAborted();
  114. return dequeued;
  115. }
  116. protected IAsyncResult BeginWaitForItem(TimeSpan timeout, AsyncCallback callback, object state)
  117. {
  118. this.ThrowIfNotOpened();
  119. return inputQueue.BeginWaitForItem(timeout, callback, state);
  120. }
  121. protected bool EndWaitForItem(IAsyncResult result)
  122. {
  123. bool dequeued = inputQueue.EndWaitForItem(result);
  124. this.ThrowIfFaulted();
  125. this.ThrowIfAborted();
  126. return dequeued;
  127. }
  128. protected override void OnClosing()
  129. {
  130. base.OnClosing();
  131. inputQueue.Shutdown(() => this.GetPendingException());
  132. }
  133. protected override void OnAbort()
  134. {
  135. inputQueue.Close();
  136. }
  137. protected override void OnClose(TimeSpan timeout)
  138. {
  139. inputQueue.Close();
  140. }
  141. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  142. {
  143. inputQueue.Close();
  144. return new CompletedAsyncResult(callback, state);
  145. }
  146. protected override void OnEndClose(IAsyncResult result)
  147. {
  148. CompletedAsyncResult.End(result);
  149. }
  150. }
  151. }