InstanceContextManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.Threading;
  11. using System.ServiceModel.Diagnostics.Application;
  12. interface IInstanceContextManager
  13. {
  14. void Abort();
  15. void Add(InstanceContext instanceContext);
  16. IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state);
  17. IAsyncResult BeginCloseInput(TimeSpan timeout, AsyncCallback callback, object state);
  18. void Close(TimeSpan timeout);
  19. void CloseInput(TimeSpan timeout);
  20. void EndClose(IAsyncResult result);
  21. void EndCloseInput(IAsyncResult result);
  22. bool Remove(InstanceContext instanceContext);
  23. InstanceContext[] ToArray();
  24. }
  25. class InstanceContextManager : LifetimeManager, IInstanceContextManager
  26. {
  27. int firstFreeIndex;
  28. Item[] items;
  29. public InstanceContextManager(object mutex)
  30. : base(mutex)
  31. {
  32. }
  33. public void Add(InstanceContext instanceContext)
  34. {
  35. bool added = false;
  36. lock (this.ThisLock)
  37. {
  38. if (this.State == LifetimeState.Opened)
  39. {
  40. if (instanceContext.InstanceContextManagerIndex != 0)
  41. return;
  42. if (this.firstFreeIndex == 0)
  43. this.GrowItems();
  44. this.AddItem(instanceContext);
  45. base.IncrementBusyCountWithoutLock();
  46. added = true;
  47. }
  48. }
  49. if (!added)
  50. {
  51. instanceContext.Abort();
  52. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
  53. }
  54. }
  55. void AddItem(InstanceContext instanceContext)
  56. {
  57. int index = this.firstFreeIndex;
  58. this.firstFreeIndex = this.items[index].nextFreeIndex;
  59. this.items[index].instanceContext = instanceContext;
  60. instanceContext.InstanceContextManagerIndex = index;
  61. }
  62. public IAsyncResult BeginCloseInput(TimeSpan timeout, AsyncCallback callback, object state)
  63. {
  64. return new CloseInputAsyncResult(timeout, callback, state, this.ToArray());
  65. }
  66. void CloseInitiate(TimeSpan timeout)
  67. {
  68. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  69. InstanceContext[] instances = this.ToArray();
  70. for (int index = 0; index < instances.Length; index++)
  71. {
  72. InstanceContext instance = instances[index];
  73. try
  74. {
  75. if (instance.State == CommunicationState.Opened)
  76. {
  77. IAsyncResult result = instance.BeginClose(timeoutHelper.RemainingTime(), Fx.ThunkCallback(new AsyncCallback(CloseInstanceContextCallback)), instance);
  78. if (!result.CompletedSynchronously)
  79. continue;
  80. instance.EndClose(result);
  81. }
  82. else
  83. {
  84. instance.Abort();
  85. }
  86. }
  87. catch (ObjectDisposedException e)
  88. {
  89. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  90. }
  91. catch (InvalidOperationException e)
  92. {
  93. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  94. }
  95. catch (CommunicationException e)
  96. {
  97. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  98. }
  99. catch (TimeoutException e)
  100. {
  101. if (TD.CloseTimeoutIsEnabled())
  102. {
  103. TD.CloseTimeout(e.Message);
  104. }
  105. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  106. }
  107. }
  108. }
  109. public void CloseInput(TimeSpan timeout)
  110. {
  111. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  112. InstanceContext[] instances = this.ToArray();
  113. for (int index = 0; index < instances.Length; index++)
  114. instances[index].CloseInput(timeoutHelper.RemainingTime());
  115. }
  116. static void CloseInstanceContextCallback(IAsyncResult result)
  117. {
  118. if (result.CompletedSynchronously)
  119. return;
  120. InstanceContext instanceContext = (InstanceContext)result.AsyncState;
  121. try
  122. {
  123. instanceContext.EndClose(result);
  124. }
  125. catch (ObjectDisposedException e)
  126. {
  127. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  128. }
  129. catch (InvalidOperationException e)
  130. {
  131. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  132. }
  133. catch (CommunicationException e)
  134. {
  135. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  136. }
  137. catch (TimeoutException e)
  138. {
  139. if (TD.CloseTimeoutIsEnabled())
  140. {
  141. TD.CloseTimeout(e.Message);
  142. }
  143. DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
  144. }
  145. }
  146. public void EndCloseInput(IAsyncResult result)
  147. {
  148. CloseInputAsyncResult.End(result);
  149. }
  150. void GrowItems()
  151. {
  152. Item[] existingItems = this.items;
  153. if (existingItems != null)
  154. {
  155. this.InitItems(existingItems.Length * 2);
  156. for (int i = 1; i < existingItems.Length; i++)
  157. this.AddItem(existingItems[i].instanceContext);
  158. }
  159. else
  160. {
  161. this.InitItems(4);
  162. }
  163. }
  164. void InitItems(int count)
  165. {
  166. this.items = new Item[count];
  167. for (int i = count - 2; i > 0; i--)
  168. {
  169. this.items[i].nextFreeIndex = i + 1;
  170. }
  171. this.firstFreeIndex = 1;
  172. }
  173. protected override void OnAbort()
  174. {
  175. InstanceContext[] instances = this.ToArray();
  176. for (int index = 0; index < instances.Length; index++)
  177. {
  178. instances[index].Abort();
  179. }
  180. base.OnAbort();
  181. }
  182. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  183. {
  184. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  185. CloseInitiate(timeoutHelper.RemainingTime());
  186. return base.OnBeginClose(timeoutHelper.RemainingTime(), callback, state);
  187. }
  188. protected override void OnClose(TimeSpan timeout)
  189. {
  190. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  191. CloseInitiate(timeoutHelper.RemainingTime());
  192. base.OnClose(timeoutHelper.RemainingTime());
  193. }
  194. protected override void OnEndClose(IAsyncResult result)
  195. {
  196. base.OnEndClose(result);
  197. }
  198. public bool Remove(InstanceContext instanceContext)
  199. {
  200. if (instanceContext == null)
  201. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("instanceContext"));
  202. lock (this.ThisLock)
  203. {
  204. int index = instanceContext.InstanceContextManagerIndex;
  205. if (index == 0)
  206. return false;
  207. instanceContext.InstanceContextManagerIndex = 0;
  208. this.items[index].nextFreeIndex = this.firstFreeIndex;
  209. this.items[index].instanceContext = null;
  210. this.firstFreeIndex = index;
  211. }
  212. base.DecrementBusyCount();
  213. return true;
  214. }
  215. public InstanceContext[] ToArray()
  216. {
  217. if (this.items == null)
  218. {
  219. return EmptyArray<InstanceContext>.Instance;
  220. }
  221. lock (this.ThisLock)
  222. {
  223. int count = 0;
  224. for (int i = 1; i < this.items.Length; i++)
  225. if (this.items[i].instanceContext != null)
  226. count++;
  227. if (count == 0)
  228. return EmptyArray<InstanceContext>.Instance;
  229. InstanceContext[] array = new InstanceContext[count];
  230. count = 0;
  231. for (int i = 1; i < this.items.Length; i++)
  232. {
  233. InstanceContext instanceContext = this.items[i].instanceContext;
  234. if (instanceContext != null)
  235. {
  236. array[count++] = instanceContext;
  237. }
  238. }
  239. return array;
  240. }
  241. }
  242. struct Item
  243. {
  244. public int nextFreeIndex;
  245. public InstanceContext instanceContext;
  246. }
  247. }
  248. class CloseInputAsyncResult : AsyncResult
  249. {
  250. bool completedSynchronously;
  251. Exception exception;
  252. static AsyncCallback nestedCallback = Fx.ThunkCallback(new AsyncCallback(Callback));
  253. int count;
  254. TimeoutHelper timeoutHelper;
  255. public CloseInputAsyncResult(TimeSpan timeout, AsyncCallback otherCallback, object state, InstanceContext[] instances)
  256. : base(otherCallback, state)
  257. {
  258. this.timeoutHelper = new TimeoutHelper(timeout);
  259. completedSynchronously = true;
  260. count = instances.Length;
  261. if (count == 0)
  262. {
  263. Complete(true);
  264. return;
  265. }
  266. for (int index = 0; index < instances.Length; index++)
  267. {
  268. CallbackState callbackState = new CallbackState(this, instances[index]);
  269. IAsyncResult result;
  270. try
  271. {
  272. result = instances[index].BeginCloseInput(this.timeoutHelper.RemainingTime(), nestedCallback, callbackState);
  273. }
  274. #pragma warning suppress 56500 // covered by FxCOP
  275. catch (Exception e)
  276. {
  277. if (Fx.IsFatal(e))
  278. {
  279. throw;
  280. }
  281. Decrement(true, e);
  282. continue;
  283. }
  284. if (result.CompletedSynchronously)
  285. {
  286. instances[index].EndCloseInput(result);
  287. Decrement(true);
  288. }
  289. }
  290. }
  291. static void Callback(IAsyncResult result)
  292. {
  293. if (result.CompletedSynchronously)
  294. return;
  295. CallbackState callbackState = (CallbackState)result.AsyncState;
  296. try
  297. {
  298. callbackState.Instance.EndCloseInput(result);
  299. callbackState.Result.Decrement(false);
  300. }
  301. #pragma warning suppress 56500 // covered by FxCOP
  302. catch (Exception e)
  303. {
  304. if (Fx.IsFatal(e))
  305. {
  306. throw;
  307. }
  308. callbackState.Result.Decrement(false, e);
  309. }
  310. }
  311. void Decrement(bool completedSynchronously)
  312. {
  313. if (completedSynchronously == false)
  314. this.completedSynchronously = false;
  315. if (Interlocked.Decrement(ref count) == 0)
  316. {
  317. if (this.exception != null)
  318. Complete(this.completedSynchronously, this.exception);
  319. else
  320. Complete(this.completedSynchronously);
  321. }
  322. }
  323. void Decrement(bool completedSynchronously, Exception exception)
  324. {
  325. this.exception = exception;
  326. this.Decrement(completedSynchronously);
  327. }
  328. public static void End(IAsyncResult result)
  329. {
  330. AsyncResult.End<CloseInputAsyncResult>(result);
  331. }
  332. class CallbackState
  333. {
  334. InstanceContext instance;
  335. CloseInputAsyncResult result;
  336. public CallbackState(CloseInputAsyncResult result, InstanceContext instance)
  337. {
  338. this.result = result;
  339. this.instance = instance;
  340. }
  341. public InstanceContext Instance
  342. {
  343. get { return instance; }
  344. }
  345. public CloseInputAsyncResult Result
  346. {
  347. get { return result; }
  348. }
  349. }
  350. }
  351. }