123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- namespace FastGithub.DomainResolve
- {
- /// <summary>
- /// IPAddress集合
- /// </summary>
- [DebuggerDisplay("Count = {Count}")]
- sealed class IPAddressCollection
- {
- private readonly object syncRoot = new();
- private readonly HashSet<IPAddressItem> hashSet = new();
- /// <summary>
- /// 获取元素数量
- /// </summary>
- public int Count => this.hashSet.Count;
- /// <summary>
- /// 添加元素
- /// </summary>
- /// <param name="address"></param>
- /// <returns></returns>
- public bool Add(IPAddress address)
- {
- lock (this.syncRoot)
- {
- return this.hashSet.Add(new IPAddressItem(address));
- }
- }
- /// <summary>
- /// 转后为数组
- /// </summary>
- /// <returns></returns>
- public IPAddress[] ToArray()
- {
- return this.ToItemArray().OrderBy(item => item.PingElapsed).Select(item => item.Address).ToArray();
- }
- /// <summary>
- /// Ping所有IP
- /// </summary>
- /// <returns></returns>
- public Task PingAllAsync()
- {
- var items = this.ToItemArray();
- 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);
- }
- /// <summary>
- /// 转换为数组
- /// </summary>
- /// <returns></returns>
- private IPAddressItem[] ToItemArray()
- {
- lock (this.syncRoot)
- {
- return this.hashSet.ToArray();
- }
- }
- /// <summary>
- /// IP地址项
- /// </summary>
- [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")]
- private class IPAddressItem : IEquatable<IPAddressItem>
- {
- /// <summary>
- /// 地址
- /// </summary>
- public IPAddress Address { get; }
- /// <summary>
- /// Ping耗时
- /// </summary>
- public TimeSpan PingElapsed { get; private set; } = TimeSpan.MaxValue;
- /// <summary>
- /// IP地址项
- /// </summary>
- /// <param name="address"></param>
- public IPAddressItem(IPAddress address)
- {
- this.Address = address;
- }
- /// <summary>
- /// 发起ping请求
- /// </summary>
- /// <returns></returns>
- public async Task PingAsync()
- {
- try
- {
- using var ping = new Ping();
- var reply = await ping.SendPingAsync(this.Address);
- this.PingElapsed = reply.Status == IPStatus.Success
- ? TimeSpan.FromMilliseconds(reply.RoundtripTime)
- : TimeSpan.MaxValue;
- }
- catch (Exception)
- {
- this.PingElapsed = TimeSpan.MaxValue;
- }
- }
- public bool Equals(IPAddressItem? other)
- {
- return other != null && other.Address.Equals(this.Address);
- }
- public override bool Equals(object? obj)
- {
- return obj is IPAddressItem other && this.Equals(other);
- }
- public override int GetHashCode()
- {
- return this.Address.GetHashCode();
- }
- }
- }
- }
|