| | | 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 | | using CoreWCF.Channels; |
| | | 7 | | using CoreWCF.Collections.Generic; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal class SynchronizedChannelCollection<TChannel> : SynchronizedCollection<TChannel> |
| | | 12 | | where TChannel : IChannel |
| | | 13 | | { |
| | | 14 | | private readonly EventHandler _onChannelClosed; |
| | | 15 | | private readonly EventHandler _onChannelFaulted; |
| | | 16 | | |
| | | 17 | | internal SynchronizedChannelCollection(object syncRoot) |
| | 663 | 18 | | : base(syncRoot) |
| | | 19 | | { |
| | 663 | 20 | | _onChannelClosed = new EventHandler(OnChannelClosed); |
| | 663 | 21 | | _onChannelFaulted = new EventHandler(OnChannelFaulted); |
| | 663 | 22 | | } |
| | | 23 | | |
| | | 24 | | private void AddingChannel(TChannel channel) |
| | | 25 | | { |
| | 0 | 26 | | channel.Faulted += _onChannelFaulted; |
| | 0 | 27 | | channel.Closed += _onChannelClosed; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | private void RemovingChannel(TChannel channel) |
| | | 31 | | { |
| | 0 | 32 | | channel.Faulted -= _onChannelFaulted; |
| | 0 | 33 | | channel.Closed -= _onChannelClosed; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | private void OnChannelClosed(object sender, EventArgs args) |
| | | 37 | | { |
| | 0 | 38 | | TChannel channel = (TChannel)sender; |
| | 0 | 39 | | Remove(channel); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | private void OnChannelFaulted(object sender, EventArgs args) |
| | | 43 | | { |
| | 0 | 44 | | TChannel channel = (TChannel)sender; |
| | 0 | 45 | | Remove(channel); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | protected override void ClearItems() |
| | | 49 | | { |
| | 0 | 50 | | List<TChannel> items = Items; |
| | | 51 | | |
| | 0 | 52 | | for (int i = 0; i < items.Count; i++) |
| | | 53 | | { |
| | 0 | 54 | | RemovingChannel(items[i]); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | base.ClearItems(); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | protected override void InsertItem(int index, TChannel item) |
| | | 61 | | { |
| | 0 | 62 | | AddingChannel(item); |
| | 0 | 63 | | base.InsertItem(index, item); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | protected override void RemoveItem(int index) |
| | | 67 | | { |
| | 0 | 68 | | TChannel oldItem = Items[index]; |
| | | 69 | | |
| | 0 | 70 | | base.RemoveItem(index); |
| | 0 | 71 | | RemovingChannel(oldItem); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | protected override void SetItem(int index, TChannel item) |
| | | 75 | | { |
| | 0 | 76 | | TChannel oldItem = Items[index]; |
| | | 77 | | |
| | 0 | 78 | | AddingChannel(item); |
| | 0 | 79 | | base.SetItem(index, item); |
| | 0 | 80 | | RemovingChannel(oldItem); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |