IPAddressCollection.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /// Ping的时间点
  79. /// </summary>
  80. private int? pingTicks;
  81. /// <summary>
  82. /// 地址
  83. /// </summary>
  84. public IPAddress Address { get; }
  85. /// <summary>
  86. /// Ping耗时
  87. /// </summary>
  88. public TimeSpan PingElapsed { get; private set; } = TimeSpan.MaxValue;
  89. /// <summary>
  90. /// IP地址项
  91. /// </summary>
  92. /// <param name="address"></param>
  93. public IPAddressItem(IPAddress address)
  94. {
  95. this.Address = address;
  96. }
  97. /// <summary>
  98. /// 发起ping请求
  99. /// </summary>
  100. /// <returns></returns>
  101. public async Task PingAsync()
  102. {
  103. if (this.NeedToPing() == false)
  104. {
  105. return;
  106. }
  107. try
  108. {
  109. using var ping = new Ping();
  110. var reply = await ping.SendPingAsync(this.Address);
  111. this.PingElapsed = reply.Status == IPStatus.Success
  112. ? TimeSpan.FromMilliseconds(reply.RoundtripTime)
  113. : TimeSpan.MaxValue;
  114. }
  115. catch (Exception)
  116. {
  117. this.PingElapsed = TimeSpan.MaxValue;
  118. }
  119. finally
  120. {
  121. this.pingTicks = Environment.TickCount;
  122. }
  123. }
  124. /// <summary>
  125. /// 是否需要ping
  126. /// 5分钟内只ping一次
  127. /// </summary>
  128. /// <returns></returns>
  129. private bool NeedToPing()
  130. {
  131. var ticks = this.pingTicks;
  132. if (ticks == null)
  133. {
  134. return true;
  135. }
  136. var pingTimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount - ticks.Value);
  137. return pingTimeSpan > TimeSpan.FromMinutes(5d);
  138. }
  139. public bool Equals(IPAddressItem? other)
  140. {
  141. return other != null && other.Address.Equals(this.Address);
  142. }
  143. public override bool Equals(object? obj)
  144. {
  145. return obj is IPAddressItem other && this.Equals(other);
  146. }
  147. public override int GetHashCode()
  148. {
  149. return this.Address.GetHashCode();
  150. }
  151. }
  152. }
  153. }