IPAddressElapsed.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace FastGithub.DomainResolve
  9. {
  10. /// <summary>
  11. /// IP延时记录
  12. /// 5分钟有效期
  13. /// 5秒连接超时
  14. /// </summary>
  15. [DebuggerDisplay("Adddress={Adddress} Elapsed={Elapsed}")]
  16. sealed class IPAddressElapsed : IEquatable<IPAddressElapsed>
  17. {
  18. private static readonly long maxLifeTime = 5 * 60 * 1000;
  19. private static readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d);
  20. private long lastTestTickCount = 0L;
  21. /// <summary>
  22. /// 获取IP地址
  23. /// </summary>
  24. public IPAddress Adddress { get; }
  25. /// <summary>
  26. /// 获取端口
  27. /// </summary>
  28. public int Port { get; }
  29. /// <summary>
  30. /// 获取延时
  31. /// </summary>
  32. public TimeSpan Elapsed { get; private set; } = TimeSpan.MaxValue;
  33. /// <summary>
  34. /// IP延时
  35. /// </summary>
  36. /// <param name="adddress"></param>
  37. /// <param name="port"></param>
  38. public IPAddressElapsed(IPAddress adddress, int port)
  39. {
  40. this.Adddress = adddress;
  41. this.Port = port;
  42. }
  43. /// <summary>
  44. /// 是否可以更新延时
  45. /// </summary>
  46. /// <returns></returns>
  47. public bool CanUpdateElapsed()
  48. {
  49. return Environment.TickCount64 - this.lastTestTickCount > maxLifeTime;
  50. }
  51. /// <summary>
  52. /// 更新连接耗时
  53. /// </summary>
  54. /// <param name="cancellationToken"></param>
  55. /// <returns></returns>
  56. public async Task UpdateElapsedAsync(CancellationToken cancellationToken)
  57. {
  58. var stopWatch = Stopwatch.StartNew();
  59. try
  60. {
  61. using var timeoutTokenSource = new CancellationTokenSource(connectTimeout);
  62. using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
  63. using var socket = new Socket(this.Adddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  64. await socket.ConnectAsync(this.Adddress, this.Port, linkedTokenSource.Token);
  65. this.Elapsed = stopWatch.Elapsed;
  66. }
  67. catch (Exception)
  68. {
  69. cancellationToken.ThrowIfCancellationRequested();
  70. this.Elapsed = TimeSpan.MaxValue;
  71. }
  72. finally
  73. {
  74. this.lastTestTickCount = Environment.TickCount64;
  75. stopWatch.Stop();
  76. }
  77. }
  78. public bool Equals(IPAddressElapsed? other)
  79. {
  80. return other != null && other.Adddress.Equals(this.Adddress);
  81. }
  82. public override bool Equals([NotNullWhen(true)] object? obj)
  83. {
  84. return obj is IPAddressElapsed other && this.Equals(other);
  85. }
  86. public override int GetHashCode()
  87. {
  88. return this.Adddress.GetHashCode();
  89. }
  90. public override string ToString()
  91. {
  92. return this.Adddress.ToString();
  93. }
  94. }
  95. }