DeliveryStrategy.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime;
  9. using System.Threading;
  10. abstract class DeliveryStrategy<ItemType> : IDisposable
  11. where ItemType : class, IDisposable
  12. {
  13. InputQueueChannel<ItemType> channel;
  14. Action dequeueCallback;
  15. int quota;
  16. public DeliveryStrategy(InputQueueChannel<ItemType> channel, int quota)
  17. {
  18. if (quota <= 0)
  19. {
  20. throw Fx.AssertAndThrow("Argument quota must be positive.");
  21. }
  22. this.channel = channel;
  23. this.quota = quota;
  24. }
  25. protected InputQueueChannel<ItemType> Channel
  26. {
  27. get
  28. {
  29. return this.channel;
  30. }
  31. }
  32. public Action DequeueCallback
  33. {
  34. get
  35. {
  36. return this.dequeueCallback;
  37. }
  38. set
  39. {
  40. this.dequeueCallback = value;
  41. }
  42. }
  43. public abstract int EnqueuedCount
  44. {
  45. get;
  46. }
  47. protected int Quota
  48. {
  49. get
  50. {
  51. return this.quota;
  52. }
  53. }
  54. public abstract bool CanEnqueue(Int64 sequenceNumber);
  55. public virtual void Dispose()
  56. {
  57. }
  58. public abstract bool Enqueue(ItemType item, Int64 sequenceNumber);
  59. }
  60. class OrderedDeliveryStrategy<ItemType> : DeliveryStrategy<ItemType>
  61. where ItemType : class, IDisposable
  62. {
  63. bool isEnqueueInOrder;
  64. Dictionary<Int64, ItemType> items;
  65. Action<object> onDispatchCallback;
  66. Int64 windowStart;
  67. public OrderedDeliveryStrategy(
  68. InputQueueChannel<ItemType> channel,
  69. int quota,
  70. bool isEnqueueInOrder)
  71. : base(channel, quota)
  72. {
  73. this.isEnqueueInOrder = isEnqueueInOrder;
  74. this.items = new Dictionary<Int64, ItemType>();
  75. this.windowStart = 1;
  76. }
  77. public override int EnqueuedCount
  78. {
  79. get
  80. {
  81. return this.Channel.InternalPendingItems + this.items.Count;
  82. }
  83. }
  84. Action<object> OnDispatchCallback
  85. {
  86. get
  87. {
  88. if (this.onDispatchCallback == null)
  89. {
  90. this.onDispatchCallback = this.OnDispatch;
  91. }
  92. return this.onDispatchCallback;
  93. }
  94. }
  95. public override bool CanEnqueue(long sequenceNumber)
  96. {
  97. if (this.EnqueuedCount >= this.Quota)
  98. {
  99. return false;
  100. }
  101. if (this.isEnqueueInOrder && (sequenceNumber > this.windowStart))
  102. {
  103. return false;
  104. }
  105. return (this.Channel.InternalPendingItems + sequenceNumber - this.windowStart < this.Quota);
  106. }
  107. public override bool Enqueue(ItemType item, long sequenceNumber)
  108. {
  109. if (sequenceNumber > this.windowStart)
  110. {
  111. this.items.Add(sequenceNumber, item);
  112. return false;
  113. }
  114. this.windowStart++;
  115. while (this.items.ContainsKey(this.windowStart))
  116. {
  117. if (this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback))
  118. {
  119. ActionItem.Schedule(this.OnDispatchCallback, null);
  120. }
  121. item = this.items[this.windowStart];
  122. this.items.Remove(this.windowStart);
  123. this.windowStart++;
  124. }
  125. return this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback);
  126. }
  127. static void DisposeItems(Dictionary<Int64, ItemType>.Enumerator items)
  128. {
  129. if (items.MoveNext())
  130. {
  131. using (ItemType item = items.Current.Value)
  132. {
  133. DisposeItems(items);
  134. }
  135. }
  136. }
  137. public override void Dispose()
  138. {
  139. DisposeItems(this.items.GetEnumerator());
  140. this.items.Clear();
  141. base.Dispose();
  142. }
  143. void OnDispatch(object state)
  144. {
  145. this.Channel.Dispatch();
  146. }
  147. }
  148. class UnorderedDeliveryStrategy<ItemType> : DeliveryStrategy<ItemType>
  149. where ItemType : class, IDisposable
  150. {
  151. public UnorderedDeliveryStrategy(InputQueueChannel<ItemType> channel, int quota)
  152. : base(channel, quota)
  153. {
  154. }
  155. public override int EnqueuedCount
  156. {
  157. get
  158. {
  159. return this.Channel.InternalPendingItems;
  160. }
  161. }
  162. public override bool CanEnqueue(Int64 sequenceNumber)
  163. {
  164. return (this.EnqueuedCount < this.Quota);
  165. }
  166. public override bool Enqueue(ItemType item, long sequenceNumber)
  167. {
  168. return this.Channel.EnqueueWithoutDispatch(item, this.DequeueCallback);
  169. }
  170. }
  171. }