ReliableInputConnection.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Runtime;
  7. sealed class ReliableInputConnection
  8. {
  9. bool isLastKnown = false;
  10. bool isSequenceClosed = false;
  11. Int64 last = 0;
  12. SequenceRangeCollection ranges = SequenceRangeCollection.Empty;
  13. ReliableMessagingVersion reliableMessagingVersion;
  14. InterruptibleWaitObject shutdownWaitObject = new InterruptibleWaitObject(false);
  15. bool terminated = false;
  16. InterruptibleWaitObject terminateWaitObject = new InterruptibleWaitObject(false, false);
  17. public ReliableInputConnection()
  18. {
  19. }
  20. public bool AllAdded
  21. {
  22. get
  23. {
  24. return (this.ranges.Count == 1
  25. && this.ranges[0].Lower == 1
  26. && this.ranges[0].Upper == this.last)
  27. || this.isLastKnown;
  28. }
  29. }
  30. public bool IsLastKnown
  31. {
  32. get
  33. {
  34. return this.last != 0 || this.isLastKnown;
  35. }
  36. }
  37. public bool IsSequenceClosed
  38. {
  39. get
  40. {
  41. return this.isSequenceClosed;
  42. }
  43. }
  44. public Int64 Last
  45. {
  46. get
  47. {
  48. return this.last;
  49. }
  50. }
  51. public SequenceRangeCollection Ranges
  52. {
  53. get
  54. {
  55. return this.ranges;
  56. }
  57. }
  58. public ReliableMessagingVersion ReliableMessagingVersion
  59. {
  60. set
  61. {
  62. this.reliableMessagingVersion = value;
  63. }
  64. }
  65. public void Abort(ChannelBase channel)
  66. {
  67. this.shutdownWaitObject.Abort(channel);
  68. this.terminateWaitObject.Abort(channel);
  69. }
  70. public bool CanMerge(Int64 sequenceNumber)
  71. {
  72. return ReliableInputConnection.CanMerge(sequenceNumber, this.ranges);
  73. }
  74. // Returns true if merging the number will not increase the number of ranges past MaxSequenceRanges.
  75. public static bool CanMerge(Int64 sequenceNumber, SequenceRangeCollection ranges)
  76. {
  77. if (ranges.Count < ReliableMessagingConstants.MaxSequenceRanges)
  78. {
  79. return true;
  80. }
  81. ranges = ranges.MergeWith(sequenceNumber);
  82. return ranges.Count <= ReliableMessagingConstants.MaxSequenceRanges;
  83. }
  84. public void Fault(ChannelBase channel)
  85. {
  86. this.shutdownWaitObject.Fault(channel);
  87. this.terminateWaitObject.Fault(channel);
  88. }
  89. public bool IsValid(Int64 sequenceNumber, bool isLast)
  90. {
  91. if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  92. {
  93. if (isLast)
  94. {
  95. if (this.last == 0)
  96. {
  97. if (this.ranges.Count > 0)
  98. {
  99. return sequenceNumber > this.ranges[this.ranges.Count - 1].Upper;
  100. }
  101. else
  102. {
  103. return true;
  104. }
  105. }
  106. else
  107. {
  108. return sequenceNumber == this.last;
  109. }
  110. }
  111. else if (this.last > 0)
  112. {
  113. return sequenceNumber < this.last;
  114. }
  115. }
  116. else
  117. {
  118. if (this.isLastKnown)
  119. {
  120. return this.ranges.Contains(sequenceNumber);
  121. }
  122. }
  123. return true;
  124. }
  125. public void Merge(Int64 sequenceNumber, bool isLast)
  126. {
  127. this.ranges = this.ranges.MergeWith(sequenceNumber);
  128. if (isLast)
  129. this.last = sequenceNumber;
  130. if (this.AllAdded)
  131. this.shutdownWaitObject.Set();
  132. }
  133. public bool SetCloseSequenceLast(Int64 last)
  134. {
  135. WsrmUtilities.AssertWsrm11(this.reliableMessagingVersion);
  136. bool validLast;
  137. if ((last < 1) || (this.ranges.Count == 0))
  138. {
  139. validLast = true;
  140. }
  141. else
  142. {
  143. validLast = last >= this.ranges[this.ranges.Count - 1].Upper;
  144. }
  145. if (validLast)
  146. {
  147. this.isSequenceClosed = true;
  148. this.SetLast(last);
  149. }
  150. return validLast;
  151. }
  152. void SetLast(Int64 last)
  153. {
  154. if (this.isLastKnown)
  155. {
  156. throw Fx.AssertAndThrow("Last can only be set once.");
  157. }
  158. this.last = last;
  159. this.isLastKnown = true;
  160. this.shutdownWaitObject.Set();
  161. }
  162. // Two error cases:
  163. // (1) The sequence contains holes.
  164. // (2) TerminateSequence.LastMsgNumber < last received message number.
  165. // In both cases the channel should be faulted. In case (2) the channel should send a fault.
  166. public bool SetTerminateSequenceLast(Int64 last, out bool isLastLargeEnough)
  167. {
  168. WsrmUtilities.AssertWsrm11(this.reliableMessagingVersion);
  169. isLastLargeEnough = true;
  170. // unspecified last
  171. if (last < 1)
  172. {
  173. return false;
  174. }
  175. int rangeCount = this.ranges.Count;
  176. Int64 lastReceived = (rangeCount > 0) ? this.ranges[rangeCount - 1].Upper : 0;
  177. // last is too small to be valid
  178. if (last < lastReceived)
  179. {
  180. isLastLargeEnough = false;
  181. return false;
  182. }
  183. // there is a hole in the sequence
  184. if ((rangeCount > 1) || (last > lastReceived))
  185. {
  186. return false;
  187. }
  188. this.SetLast(last);
  189. return true;
  190. }
  191. public bool Terminate()
  192. {
  193. if ((this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  194. || this.isSequenceClosed)
  195. {
  196. if (!this.terminated && this.AllAdded)
  197. {
  198. this.terminateWaitObject.Set();
  199. this.terminated = true;
  200. }
  201. return this.terminated;
  202. }
  203. return this.isLastKnown;
  204. }
  205. public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  206. {
  207. OperationWithTimeoutBeginCallback[] beginCallbacks
  208. = new OperationWithTimeoutBeginCallback[] { shutdownWaitObject.BeginWait, terminateWaitObject.BeginWait };
  209. OperationEndCallback[] endCallbacks
  210. = new OperationEndCallback[] { shutdownWaitObject.EndWait, terminateWaitObject.EndWait };
  211. return OperationWithTimeoutComposer.BeginComposeAsyncOperations(timeout, beginCallbacks, endCallbacks, callback, state);
  212. }
  213. public void Close(TimeSpan timeout)
  214. {
  215. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  216. this.shutdownWaitObject.Wait(timeoutHelper.RemainingTime());
  217. this.terminateWaitObject.Wait(timeoutHelper.RemainingTime());
  218. }
  219. public void EndClose(IAsyncResult result)
  220. {
  221. OperationWithTimeoutComposer.EndComposeAsyncOperations(result);
  222. }
  223. }
  224. }