ServiceChannelManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Collections.Generic;
  7. using System.Runtime;
  8. using System.ServiceModel.Channels;
  9. using System.Threading;
  10. delegate void InstanceContextEmptyCallback(InstanceContext instanceContext);
  11. class ServiceChannelManager : LifetimeManager
  12. {
  13. int activityCount;
  14. ICommunicationWaiter activityWaiter;
  15. int activityWaiterCount;
  16. InstanceContextEmptyCallback emptyCallback;
  17. IChannel firstIncomingChannel;
  18. ChannelCollection incomingChannels;
  19. ChannelCollection outgoingChannels;
  20. InstanceContext instanceContext;
  21. public ServiceChannelManager(InstanceContext instanceContext)
  22. : this(instanceContext, null)
  23. {
  24. }
  25. public ServiceChannelManager(InstanceContext instanceContext, InstanceContextEmptyCallback emptyCallback)
  26. : base(instanceContext.ThisLock)
  27. {
  28. this.instanceContext = instanceContext;
  29. this.emptyCallback = emptyCallback;
  30. }
  31. public int ActivityCount
  32. {
  33. get { return this.activityCount; }
  34. }
  35. public ICollection<IChannel> IncomingChannels
  36. {
  37. get
  38. {
  39. this.EnsureIncomingChannelCollection();
  40. return (ICollection<IChannel>)this.incomingChannels;
  41. }
  42. }
  43. public ICollection<IChannel> OutgoingChannels
  44. {
  45. get
  46. {
  47. if (this.outgoingChannels == null)
  48. {
  49. lock (this.ThisLock)
  50. {
  51. if (this.outgoingChannels == null)
  52. this.outgoingChannels = new ChannelCollection(this, this.ThisLock);
  53. }
  54. }
  55. return this.outgoingChannels;
  56. }
  57. }
  58. public bool IsBusy
  59. {
  60. get
  61. {
  62. if (this.ActivityCount > 0)
  63. return true;
  64. if (base.BusyCount > 0)
  65. return true;
  66. ICollection<IChannel> outgoing = this.outgoingChannels;
  67. if ((outgoing != null) && (outgoing.Count > 0))
  68. return true;
  69. return false;
  70. }
  71. }
  72. public void AddIncomingChannel(IChannel channel)
  73. {
  74. bool added = false;
  75. lock (this.ThisLock)
  76. {
  77. if (this.State == LifetimeState.Opened)
  78. {
  79. if (this.firstIncomingChannel == null)
  80. {
  81. if (this.incomingChannels == null)
  82. {
  83. this.firstIncomingChannel = channel;
  84. this.ChannelAdded(channel);
  85. }
  86. else
  87. {
  88. if (this.incomingChannels.Contains(channel))
  89. return;
  90. this.incomingChannels.Add(channel);
  91. }
  92. }
  93. else
  94. {
  95. this.EnsureIncomingChannelCollection();
  96. if (this.incomingChannels.Contains(channel))
  97. return;
  98. this.incomingChannels.Add(channel);
  99. }
  100. added = true;
  101. }
  102. }
  103. if (!added)
  104. {
  105. channel.Abort();
  106. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
  107. }
  108. }
  109. public IAsyncResult BeginCloseInput(TimeSpan timeout, AsyncCallback callback, object state)
  110. {
  111. CloseCommunicationAsyncResult closeResult = null;
  112. lock (this.ThisLock)
  113. {
  114. if (this.activityCount > 0)
  115. {
  116. closeResult = new CloseCommunicationAsyncResult(timeout, callback, state, this.ThisLock);
  117. if (!(this.activityWaiter == null))
  118. {
  119. Fx.Assert("ServiceChannelManager.BeginCloseInput: (this.activityWaiter == null)");
  120. }
  121. this.activityWaiter = closeResult;
  122. Interlocked.Increment(ref this.activityWaiterCount);
  123. }
  124. }
  125. if (closeResult != null)
  126. return closeResult;
  127. else
  128. return new CompletedAsyncResult(callback, state);
  129. }
  130. void ChannelAdded(IChannel channel)
  131. {
  132. base.IncrementBusyCount();
  133. channel.Closed += this.OnChannelClosed;
  134. }
  135. void ChannelRemoved(IChannel channel)
  136. {
  137. channel.Closed -= this.OnChannelClosed;
  138. base.DecrementBusyCount();
  139. }
  140. public void CloseInput(TimeSpan timeout)
  141. {
  142. SyncCommunicationWaiter activityWaiter = null;
  143. lock (this.ThisLock)
  144. {
  145. if (this.activityCount > 0)
  146. {
  147. activityWaiter = new SyncCommunicationWaiter(this.ThisLock);
  148. if (!(this.activityWaiter == null))
  149. {
  150. Fx.Assert("ServiceChannelManager.CloseInput: (this.activityWaiter == null)");
  151. }
  152. this.activityWaiter = activityWaiter;
  153. Interlocked.Increment(ref this.activityWaiterCount);
  154. }
  155. }
  156. if (activityWaiter != null)
  157. {
  158. CommunicationWaitResult result = activityWaiter.Wait(timeout, false);
  159. if (Interlocked.Decrement(ref this.activityWaiterCount) == 0)
  160. {
  161. activityWaiter.Dispose();
  162. this.activityWaiter = null;
  163. }
  164. switch (result)
  165. {
  166. case CommunicationWaitResult.Expired:
  167. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.GetString(SR.SfxCloseTimedOutWaitingForDispatchToComplete)));
  168. case CommunicationWaitResult.Aborted:
  169. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
  170. }
  171. }
  172. }
  173. public void DecrementActivityCount()
  174. {
  175. ICommunicationWaiter activityWaiter = null;
  176. bool empty = false;
  177. lock (this.ThisLock)
  178. {
  179. if (!(this.activityCount > 0))
  180. {
  181. Fx.Assert("ServiceChannelManager.DecrementActivityCount: (this.activityCount > 0)");
  182. }
  183. if (--this.activityCount == 0)
  184. {
  185. if (this.activityWaiter != null)
  186. {
  187. activityWaiter = this.activityWaiter;
  188. Interlocked.Increment(ref this.activityWaiterCount);
  189. }
  190. if (this.BusyCount == 0)
  191. empty = true;
  192. }
  193. }
  194. if (activityWaiter != null)
  195. {
  196. activityWaiter.Signal();
  197. if (Interlocked.Decrement(ref this.activityWaiterCount) == 0)
  198. {
  199. activityWaiter.Dispose();
  200. this.activityWaiter = null;
  201. }
  202. }
  203. if (empty && this.State == LifetimeState.Opened)
  204. OnEmpty();
  205. }
  206. public void EndCloseInput(IAsyncResult result)
  207. {
  208. if (result is CloseCommunicationAsyncResult)
  209. {
  210. CloseCommunicationAsyncResult.End(result);
  211. if (Interlocked.Decrement(ref this.activityWaiterCount) == 0)
  212. {
  213. this.activityWaiter.Dispose();
  214. this.activityWaiter = null;
  215. }
  216. }
  217. else
  218. CompletedAsyncResult.End(result);
  219. }
  220. void EnsureIncomingChannelCollection()
  221. {
  222. lock (this.ThisLock)
  223. {
  224. if (this.incomingChannels == null)
  225. {
  226. this.incomingChannels = new ChannelCollection(this, this.ThisLock);
  227. if (this.firstIncomingChannel != null)
  228. {
  229. this.incomingChannels.Add(this.firstIncomingChannel);
  230. this.ChannelRemoved(this.firstIncomingChannel); // Adding to collection called ChannelAdded, so call ChannelRemoved to balance
  231. this.firstIncomingChannel = null;
  232. }
  233. }
  234. }
  235. }
  236. public void IncrementActivityCount()
  237. {
  238. lock (this.ThisLock)
  239. {
  240. if (this.State == LifetimeState.Closed)
  241. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
  242. this.activityCount++;
  243. }
  244. }
  245. protected override void IncrementBusyCount()
  246. {
  247. base.IncrementBusyCount();
  248. }
  249. protected override void OnAbort()
  250. {
  251. IChannel[] channels = this.SnapshotChannels();
  252. for (int index = 0; index < channels.Length; index++)
  253. channels[index].Abort();
  254. ICommunicationWaiter activityWaiter = null;
  255. lock (this.ThisLock)
  256. {
  257. if (this.activityWaiter != null)
  258. {
  259. activityWaiter = this.activityWaiter;
  260. Interlocked.Increment(ref this.activityWaiterCount);
  261. }
  262. }
  263. if (activityWaiter != null)
  264. {
  265. activityWaiter.Signal();
  266. if (Interlocked.Decrement(ref this.activityWaiterCount) == 0)
  267. {
  268. activityWaiter.Dispose();
  269. this.activityWaiter = null;
  270. }
  271. }
  272. base.OnAbort();
  273. }
  274. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  275. {
  276. return new ChainedAsyncResult(timeout, callback, state, BeginCloseInput, EndCloseInput, OnBeginCloseContinue, OnEndCloseContinue);
  277. }
  278. IAsyncResult OnBeginCloseContinue(TimeSpan timeout, AsyncCallback callback, object state)
  279. {
  280. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  281. return base.OnBeginClose(timeoutHelper.RemainingTime(), callback, state);
  282. }
  283. protected override void OnClose(TimeSpan timeout)
  284. {
  285. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  286. this.CloseInput(timeoutHelper.RemainingTime());
  287. base.OnClose(timeoutHelper.RemainingTime());
  288. }
  289. protected override void OnEndClose(IAsyncResult result)
  290. {
  291. ChainedAsyncResult.End(result);
  292. }
  293. void OnEndCloseContinue(IAsyncResult result)
  294. {
  295. base.OnEndClose(result);
  296. }
  297. protected override void OnEmpty()
  298. {
  299. if (this.emptyCallback != null)
  300. this.emptyCallback(this.instanceContext);
  301. }
  302. void OnChannelClosed(object sender, EventArgs args)
  303. {
  304. this.RemoveChannel((IChannel)sender);
  305. }
  306. public bool RemoveChannel(IChannel channel)
  307. {
  308. lock (this.ThisLock)
  309. {
  310. if (this.firstIncomingChannel == channel)
  311. {
  312. this.firstIncomingChannel = null;
  313. this.ChannelRemoved(channel);
  314. return true;
  315. }
  316. else if (this.incomingChannels != null && this.incomingChannels.Contains(channel))
  317. {
  318. this.incomingChannels.Remove(channel);
  319. return true;
  320. }
  321. else if (this.outgoingChannels != null && this.outgoingChannels.Contains(channel))
  322. {
  323. this.outgoingChannels.Remove(channel);
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. public IChannel[] SnapshotChannels()
  330. {
  331. lock (this.ThisLock)
  332. {
  333. int outgoingCount = (this.outgoingChannels != null ? this.outgoingChannels.Count : 0);
  334. if (this.firstIncomingChannel != null)
  335. {
  336. IChannel[] channels = new IChannel[1 + outgoingCount];
  337. channels[0] = this.firstIncomingChannel;
  338. if (outgoingCount > 0)
  339. this.outgoingChannels.CopyTo(channels, 1);
  340. return channels;
  341. }
  342. if (this.incomingChannels != null)
  343. {
  344. IChannel[] channels = new IChannel[this.incomingChannels.Count + outgoingCount];
  345. this.incomingChannels.CopyTo(channels, 0);
  346. if (outgoingCount > 0)
  347. this.outgoingChannels.CopyTo(channels, this.incomingChannels.Count);
  348. return channels;
  349. }
  350. if (outgoingCount > 0)
  351. {
  352. IChannel[] channels = new IChannel[outgoingCount];
  353. this.outgoingChannels.CopyTo(channels, 0);
  354. return channels;
  355. }
  356. }
  357. return EmptyArray<IChannel>.Allocate(0);
  358. }
  359. class ChannelCollection : ICollection<IChannel>
  360. {
  361. ServiceChannelManager channelManager;
  362. object syncRoot;
  363. HashSet<IChannel> hashSet = new HashSet<IChannel>();
  364. public bool IsReadOnly
  365. {
  366. get { return false; }
  367. }
  368. public int Count
  369. {
  370. get
  371. {
  372. lock (this.syncRoot)
  373. {
  374. return this.hashSet.Count;
  375. }
  376. }
  377. }
  378. public ChannelCollection(ServiceChannelManager channelManager, object syncRoot)
  379. {
  380. if (syncRoot == null)
  381. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("syncRoot"));
  382. this.channelManager = channelManager;
  383. this.syncRoot = syncRoot;
  384. }
  385. public void Add(IChannel channel)
  386. {
  387. lock (this.syncRoot)
  388. {
  389. if (this.hashSet.Add(channel))
  390. {
  391. this.channelManager.ChannelAdded(channel);
  392. }
  393. }
  394. }
  395. public void Clear()
  396. {
  397. lock (this.syncRoot)
  398. {
  399. foreach (IChannel channel in this.hashSet)
  400. this.channelManager.ChannelRemoved(channel);
  401. this.hashSet.Clear();
  402. }
  403. }
  404. public bool Contains(IChannel channel)
  405. {
  406. lock (this.syncRoot)
  407. {
  408. if (channel != null)
  409. {
  410. return this.hashSet.Contains(channel);
  411. }
  412. return false;
  413. }
  414. }
  415. public void CopyTo(IChannel[] array, int arrayIndex)
  416. {
  417. lock (this.syncRoot)
  418. {
  419. this.hashSet.CopyTo(array, arrayIndex);
  420. }
  421. }
  422. public bool Remove(IChannel channel)
  423. {
  424. lock (this.syncRoot)
  425. {
  426. bool ret = false;
  427. if (channel != null)
  428. {
  429. ret = this.hashSet.Remove(channel);
  430. if (ret)
  431. {
  432. this.channelManager.ChannelRemoved(channel);
  433. }
  434. }
  435. return ret;
  436. }
  437. }
  438. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  439. {
  440. lock (this.syncRoot)
  441. {
  442. return this.hashSet.GetEnumerator();
  443. }
  444. }
  445. IEnumerator<IChannel> IEnumerable<IChannel>.GetEnumerator()
  446. {
  447. lock (this.syncRoot)
  448. {
  449. return this.hashSet.GetEnumerator();
  450. }
  451. }
  452. }
  453. }
  454. }