IPAddressCollection.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using System.Threading.Tasks;
  8. namespace FastGithub.DomainResolve
  9. {
  10. /// <summary>
  11. /// IPAddress集合
  12. /// </summary>
  13. [DebuggerDisplay("Count = {Count}")]
  14. sealed class IPAddressCollection
  15. {
  16. private readonly object syncRoot = new();
  17. private readonly HashSet<IPAddressItem> hashSet = new();
  18. /// <summary>
  19. /// 获取元素数量
  20. /// </summary>
  21. public int Count => this.hashSet.Count;
  22. /// <summary>
  23. /// 添加元素
  24. /// </summary>
  25. /// <param name="address"></param>
  26. /// <returns></returns>
  27. public bool Add(IPAddress address)
  28. {
  29. lock (this.syncRoot)
  30. {
  31. return this.hashSet.Add(new IPAddressItem(address));
  32. }
  33. }
  34. /// <summary>
  35. /// 转后为数组
  36. /// </summary>
  37. /// <returns></returns>
  38. public IPAddress[] ToArray()
  39. {
  40. return this.ToItemArray().OrderBy(item => item.PingElapsed).Select(item => item.Address).ToArray();
  41. }
  42. /// <summary>
  43. /// Ping所有IP
  44. /// </summary>
  45. /// <returns></returns>
  46. public Task PingAllAsync()
  47. {
  48. var items = this.ToItemArray();
  49. if (items.Length == 0)
  50. {
  51. return Task.CompletedTask;
  52. }
  53. if (items.Length == 1)
  54. {
  55. return items[0].PingAsync();
  56. }
  57. var tasks = items.Select(item => item.PingAsync());
  58. return Task.WhenAll(tasks);
  59. }
  60. /// <summary>
  61. /// 转换为数组
  62. /// </summary>
  63. /// <returns></returns>
  64. private IPAddressItem[] ToItemArray()
  65. {
  66. lock (this.syncRoot)
  67. {
  68. return this.hashSet.ToArray();
  69. }
  70. }
  71. /// <summary>
  72. /// IP地址项
  73. /// </summary>
  74. [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")]
  75. private class IPAddressItem : IEquatable<IPAddressItem>
  76. {
  77. /// <summary>
  78. /// 地址
  79. /// </summary>
  80. public IPAddress Address { get; }
  81. /// <summary>
  82. /// Ping耗时
  83. /// </summary>
  84. public TimeSpan PingElapsed { get; private set; } = TimeSpan.MaxValue;
  85. /// <summary>
  86. /// IP地址项
  87. /// </summary>
  88. /// <param name="address"></param>
  89. public IPAddressItem(IPAddress address)
  90. {
  91. this.Address = address;
  92. }
  93. /// <summary>
  94. /// 发起ping请求
  95. /// </summary>
  96. /// <returns></returns>
  97. public async Task PingAsync()
  98. {
  99. try
  100. {
  101. using var ping = new Ping();
  102. var reply = await ping.SendPingAsync(this.Address);
  103. this.PingElapsed = reply.Status == IPStatus.Success
  104. ? TimeSpan.FromMilliseconds(reply.RoundtripTime)
  105. : TimeSpan.MaxValue;
  106. }
  107. catch (Exception)
  108. {
  109. this.PingElapsed = TimeSpan.MaxValue;
  110. }
  111. }
  112. public bool Equals(IPAddressItem? other)
  113. {
  114. return other != null && other.Address.Equals(this.Address);
  115. }
  116. public override bool Equals(object? obj)
  117. {
  118. return obj is IPAddressItem other && this.Equals(other);
  119. }
  120. public override int GetHashCode()
  121. {
  122. return this.Address.GetHashCode();
  123. }
  124. }
  125. }
  126. }