using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FastGithub.DomainResolve
{
///
/// IPAddressItem集合
///
sealed class IPAddressItemHashSet
{
private readonly object syncRoot = new();
private readonly HashSet hashSet = new();
///
/// 获取元素数量
///
public int Count => this.hashSet.Count;
///
/// 添加元素
///
///
///
public bool Add(IPAddressItem item)
{
lock (this.syncRoot)
{
return this.hashSet.Add(item);
}
}
///
/// 转换为数组
///
///
public IPAddressItem[] ToArray()
{
lock (this.syncRoot)
{
return this.hashSet.ToArray();
}
}
///
/// Ping所有IP
///
///
public Task PingAllAsync()
{
var items = this.ToArray();
if (items.Length == 0)
{
return Task.CompletedTask;
}
if (items.Length == 1)
{
return items[0].PingAsync();
}
var tasks = items.Select(item => item.PingAsync());
return Task.WhenAll(tasks);
}
}
}