using System; using System.Diagnostics; using System.Net; using System.Net.NetworkInformation; using System.Threading.Tasks; namespace FastGithub.DomainResolve { /// /// IP地址项 /// [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")] sealed class IPAddressItem : IEquatable { private readonly Ping ping = new(); /// /// 地址 /// public IPAddress Address { get; } /// /// Ping耗时 /// public TimeSpan PingElapsed { get; private set; } = TimeSpan.MaxValue; /// /// IP地址项 /// /// public IPAddressItem(IPAddress address) { this.Address = address; } /// /// 发起ping请求 /// /// public async Task PingAsync() { try { var reply = await this.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(); } } }