From f5e7e4c0d0730c07478ceb44358b6e76aad508b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Fri, 26 Nov 2021 17:42:26 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Socket=E7=8A=B6=E6=80=81?=
=?UTF-8?q?=E7=A0=81=E5=88=A4=E6=96=AD=E7=BD=91=E7=BB=9C=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.DomainResolve/IPAddressService.cs | 23 +++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/FastGithub.DomainResolve/IPAddressService.cs b/FastGithub.DomainResolve/IPAddressService.cs
index 25e7ce2..eb3a4e2 100644
--- a/FastGithub.DomainResolve/IPAddressService.cs
+++ b/FastGithub.DomainResolve/IPAddressService.cs
@@ -5,7 +5,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
-using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
@@ -110,12 +109,12 @@ namespace FastGithub.DomainResolve
addressElapsed = new AddressElapsed(endPoint.Address, stopWatch.Elapsed);
return this.addressElapsedCache.Set(endPoint, addressElapsed, this.normaleElapsedExpiration);
}
- catch (Exception)
+ catch (Exception ex)
{
cancellationToken.ThrowIfCancellationRequested();
addressElapsed = new AddressElapsed(endPoint.Address, TimeSpan.MaxValue);
- var expiration = NetworkInterface.GetIsNetworkAvailable() ? this.normaleElapsedExpiration : this.brokeElapsedExpiration;
+ var expiration = IsLocalNetworkProblem(ex) ? this.brokeElapsedExpiration : this.normaleElapsedExpiration;
return this.addressElapsedCache.Set(endPoint, addressElapsed, expiration);
}
finally
@@ -123,5 +122,23 @@ namespace FastGithub.DomainResolve
stopWatch.Stop();
}
}
+
+ ///
+ /// 是否为本机网络问题
+ ///
+ ///
+ ///
+ private static bool IsLocalNetworkProblem(Exception ex)
+ {
+ if (ex is not SocketException socketException)
+ {
+ return false;
+ }
+
+ var code = socketException.SocketErrorCode;
+ return code == SocketError.NetworkDown ||
+ code == SocketError.NetworkUnreachable ||
+ code == SocketError.HostUnreachable;
+ }
}
}