| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | internal class CommunicationObjectManager<TItemType> : LifetimeManager where TItemType : class, ICommunicationObject |
| | | 10 | | { |
| | | 11 | | private bool _inputClosed; |
| | | 12 | | private readonly ISet<TItemType> _itemsSet; |
| | | 13 | | |
| | | 14 | | public CommunicationObjectManager(object mutex) |
| | 663 | 15 | | : base(mutex) |
| | | 16 | | { |
| | 663 | 17 | | _itemsSet = new HashSet<TItemType>(); |
| | 663 | 18 | | } |
| | | 19 | | |
| | | 20 | | public void Add(TItemType item) |
| | | 21 | | { |
| | 13 | 22 | | bool added = false; |
| | | 23 | | |
| | 13 | 24 | | lock (ThisLock) |
| | | 25 | | { |
| | 13 | 26 | | if (State == LifetimeState.Opened && !_inputClosed) |
| | | 27 | | { |
| | 13 | 28 | | if (_itemsSet.Contains(item)) |
| | | 29 | | { |
| | 0 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | |
| | 13 | 33 | | _itemsSet.Add(item); |
| | 13 | 34 | | IncrementBusyCountWithoutLock(); |
| | 13 | 35 | | item.Closed += OnItemClosed; |
| | 13 | 36 | | added = true; |
| | | 37 | | } |
| | 13 | 38 | | } |
| | | 39 | | |
| | 13 | 40 | | if (!added) |
| | | 41 | | { |
| | 0 | 42 | | item.Abort(); |
| | 0 | 43 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToString |
| | | 44 | | } |
| | 13 | 45 | | } |
| | | 46 | | |
| | | 47 | | public void CloseInput() |
| | | 48 | | { |
| | | 49 | | //Abort can reenter this call as a result of |
| | | 50 | | //close timeout, Closing input twice is not a |
| | | 51 | | //FailFast case. |
| | 0 | 52 | | _inputClosed = true; |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public void DecrementActivityCount() |
| | | 56 | | { |
| | 128 | 57 | | DecrementBusyCount(); |
| | 128 | 58 | | } |
| | | 59 | | |
| | | 60 | | public void IncrementActivityCount() |
| | | 61 | | { |
| | 2318 | 62 | | IncrementBusyCount(); |
| | 2318 | 63 | | } |
| | | 64 | | |
| | | 65 | | private void OnItemClosed(object sender, EventArgs args) |
| | | 66 | | { |
| | 0 | 67 | | Remove((TItemType)sender); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public void Remove(TItemType item) |
| | | 71 | | { |
| | 0 | 72 | | lock (ThisLock) |
| | | 73 | | { |
| | 0 | 74 | | if (!_itemsSet.Contains(item)) |
| | | 75 | | { |
| | 0 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | _itemsSet.Remove(item); |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | item.Closed -= OnItemClosed; |
| | 0 | 83 | | DecrementBusyCount(); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public TItemType[] ToArray() |
| | | 87 | | { |
| | 0 | 88 | | lock (ThisLock) |
| | | 89 | | { |
| | 0 | 90 | | int index = 0; |
| | 0 | 91 | | TItemType[] items = new TItemType[_itemsSet.Count]; |
| | 0 | 92 | | foreach (TItemType item in _itemsSet) |
| | | 93 | | { |
| | 0 | 94 | | items[index++] = item; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | return items; |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |