using System.Collections.Concurrent;
using Vinno.IUS.Common.Log;
namespace Vinno.IUS.Common.Network.Channels
{
///
/// ChannelCollection is used for storing the channel of one leaf.
/// This can avoid GC to release the channel.
///
internal class ChannelCollection
{
private readonly ConcurrentDictionary _channels = new ConcurrentDictionary();
///
/// Gets the channels count.
///
public int Count => _channels.Count;
///
/// Add channel into collection.
///
///
public void AddChannel(IChannel channel)
{
_channels.TryAdd(channel.Id, channel);
}
///
/// Remove channel from collection.
///
///
public void RemoveChannel(IChannel channel)
{
_channels.TryRemove(channel.Id, out var existChannel);
if (existChannel == null)
{
Logger.WriteLineWarn($"Channel:{channel.Id} not found in collection");
}
}
///
/// Close all channels.
///
public void Close()
{
foreach (var channel in _channels.Values)
{
channel.Close();
}
_channels.Clear();
}
}
}