IPAddressCollection.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. lock (this.syncRoot)
  41. {
  42. return this.hashSet.OrderBy(item => item.PingElapsed).Select(item => item.Address).ToArray();
  43. }
  44. }
  45. /// <summary>
  46. /// Ping所有IP
  47. /// </summary>
  48. /// <returns></returns>
  49. public async Task PingAllAsync()
  50. {
  51. foreach (var item in this.ToItemArray())
  52. {
  53. await item.PingAsync();
  54. }
  55. }
  56. /// <summary>
  57. /// 转换为数组
  58. /// </summary>
  59. /// <returns></returns>
  60. private IPAddressItem[] ToItemArray()
  61. {
  62. lock (this.syncRoot)
  63. {
  64. return this.hashSet.ToArray();
  65. }
  66. }
  67. /// <summary>
  68. /// IP地址项
  69. /// </summary>
  70. [DebuggerDisplay("Address = {Address}, PingElapsed = {PingElapsed}")]
  71. private class IPAddressItem : IEquatable<IPAddressItem>
  72. {
  73. /// <summary>
  74. /// Ping的时间点
  75. /// </summary>
  76. private int? pingTicks;
  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. if (this.NeedToPing() == false)
  100. {
  101. return;
  102. }
  103. try
  104. {
  105. using var ping = new Ping();
  106. var reply = await ping.SendPingAsync(this.Address);
  107. this.PingElapsed = reply.Status == IPStatus.Success
  108. ? TimeSpan.FromMilliseconds(reply.RoundtripTime)
  109. : TimeSpan.MaxValue;
  110. }
  111. catch (Exception)
  112. {
  113. this.PingElapsed = TimeSpan.MaxValue;
  114. }
  115. finally
  116. {
  117. this.pingTicks = Environment.TickCount;
  118. }
  119. }
  120. /// <summary>
  121. /// 是否需要ping
  122. /// 5分钟内只ping一次
  123. /// </summary>
  124. /// <returns></returns>
  125. private bool NeedToPing()
  126. {
  127. var ticks = this.pingTicks;
  128. if (ticks == null)
  129. {
  130. return true;
  131. }
  132. var pingTimeSpan = TimeSpan.FromMilliseconds(Environment.TickCount - ticks.Value);
  133. return pingTimeSpan > TimeSpan.FromMinutes(5d);
  134. }
  135. public bool Equals(IPAddressItem? other)
  136. {
  137. return other != null && other.Address.Equals(this.Address);
  138. }
  139. public override bool Equals(object? obj)
  140. {
  141. return obj is IPAddressItem other && this.Equals(other);
  142. }
  143. public override int GetHashCode()
  144. {
  145. return this.Address.GetHashCode();
  146. }
  147. }
  148. }
  149. }