From e31129dd9fc328a70244c5c523a99abc285d579c Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Wed, 14 Feb 2024 22:10:47 +0100 Subject: [PATCH] feat: Add and set default SyncLockTimeoutInMs to 30sec --- src/Atc.Network/Helpers/DnsLookupHelper.cs | 4 +++- src/Atc.Network/Helpers/InternetWorldTimeHelper.cs | 4 ++-- .../Helpers/MacAddressVendorLookupHelper.cs | 4 ++-- src/Atc.Network/Internet/IPPortScan.cs | 7 ++++--- src/Atc.Network/Internet/IPScanner.cs | 13 +++++++------ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Atc.Network/Helpers/DnsLookupHelper.cs b/src/Atc.Network/Helpers/DnsLookupHelper.cs index 4423b10..e9eafbb 100644 --- a/src/Atc.Network/Helpers/DnsLookupHelper.cs +++ b/src/Atc.Network/Helpers/DnsLookupHelper.cs @@ -5,6 +5,8 @@ namespace Atc.Network.Helpers; /// public static class DnsLookupHelper { + private const int SyncLockTimeoutInMs = 30_000; + private static readonly SemaphoreSlim SyncLock = new(1, 1); private static string? hostname; private static IPAddress[]? hostAddresses; @@ -34,7 +36,7 @@ public static class DnsLookupHelper try { - await SyncLock.WaitAsync(cancellationToken); + await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); if (ipAddress.IsPrivate()) { diff --git a/src/Atc.Network/Helpers/InternetWorldTimeHelper.cs b/src/Atc.Network/Helpers/InternetWorldTimeHelper.cs index 06081c5..2425420 100644 --- a/src/Atc.Network/Helpers/InternetWorldTimeHelper.cs +++ b/src/Atc.Network/Helpers/InternetWorldTimeHelper.cs @@ -6,7 +6,7 @@ namespace Atc.Network.Helpers; public static class InternetWorldTimeHelper { private const string WorldTimeBaseApi = "https://worldtimeapi.org/api/timezone/"; - private const int SyncLockTimeout = 3_000; + private const int SyncLockTimeoutInMs = 30_000; private static readonly SemaphoreSlim SyncLock = new(1, 1); /// @@ -40,7 +40,7 @@ public static class InternetWorldTimeHelper try { - await SyncLock.WaitAsync(SyncLockTimeout, cancellationToken); + await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); if (!NetworkInformationHelper.HasConnection()) { diff --git a/src/Atc.Network/Helpers/MacAddressVendorLookupHelper.cs b/src/Atc.Network/Helpers/MacAddressVendorLookupHelper.cs index b31bd07..f374ea8 100644 --- a/src/Atc.Network/Helpers/MacAddressVendorLookupHelper.cs +++ b/src/Atc.Network/Helpers/MacAddressVendorLookupHelper.cs @@ -9,7 +9,7 @@ public static class MacAddressVendorLookupHelper private const string AtcCacheFolder = "AtcCache"; private const string AtcCacheFile = "macvendors.txt"; private const int MinimumCallDelay = 1_200; - private const int SyncLockTimeout = 3_000; + private const int SyncLockTimeoutInMs = 30_000; private static readonly SemaphoreSlim SyncLock = new(1, 1); private static readonly Uri MacVendorsApiUrl = new("http://api.macvendors.com/"); @@ -24,7 +24,7 @@ public static class MacAddressVendorLookupHelper try { - await SyncLock.WaitAsync(SyncLockTimeout, cancellationToken); + await SyncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); macAddress = macAddress.ToUpper(GlobalizationConstants.EnglishCultureInfo); var cacheVendorName = GetVendorFromCacheFileLines(macAddress); diff --git a/src/Atc.Network/Internet/IPPortScan.cs b/src/Atc.Network/Internet/IPPortScan.cs index 5b6da30..bac5028 100644 --- a/src/Atc.Network/Internet/IPPortScan.cs +++ b/src/Atc.Network/Internet/IPPortScan.cs @@ -9,8 +9,9 @@ namespace Atc.Network.Internet; public class IPPortScan : IIPPortScan, IDisposable { private const int InternalDelayInMs = 5; - private readonly SemaphoreSlim syncLock = new(1, 1); + private const int SyncLockTimeoutInMs = 30_000; + private readonly SemaphoreSlim syncLock = new(1, 1); private IPAddress? ipAddress; private int timeoutInMs = 100; @@ -69,7 +70,7 @@ public async Task CanConnectWithTcp( try { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); await Task.Delay(InternalDelayInMs, cancellationToken); var cancellationCompletionSource = new TaskCompletionSource(); @@ -151,7 +152,7 @@ public async Task CanConnectWithHttpOrHttps( try { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); await Task.Delay(InternalDelayInMs, cancellationToken); var cancellationCompletionSource = new TaskCompletionSource(); diff --git a/src/Atc.Network/Internet/IPScanner.cs b/src/Atc.Network/Internet/IPScanner.cs index e5bf945..3e0324f 100644 --- a/src/Atc.Network/Internet/IPScanner.cs +++ b/src/Atc.Network/Internet/IPScanner.cs @@ -8,6 +8,8 @@ namespace Atc.Network.Internet; [SuppressMessage("Minor Code Smell", "S2486:Generic exceptions should not be ignored", Justification = "OK.")] public partial class IPScanner : IIPScanner, IDisposable { + private const int SyncLockTimeoutInMs = 30_000; + private readonly SemaphoreSlim syncLock = new(1, 1); private readonly ConcurrentBag processedScanResults = new(); private readonly IPScannerProgressReport progressReporting = new(); @@ -85,7 +87,7 @@ public async Task ScanRange( } var ipAddresses = IPv4AddressHelper.GetAddressesInRange(startIpAddress, endIpAddress); - if (!ipAddresses.Any()) + if (ipAddresses.Count == 0) { scanResults.ErrorMessage = "Nothing to process"; scanResults.End = DateTime.Now; @@ -94,7 +96,7 @@ public async Task ScanRange( try { - await syncLock.WaitAsync(cancellationToken); + await syncLock.WaitAsync(SyncLockTimeoutInMs, cancellationToken); if (Configuration.ResolveMacAddress) { @@ -191,13 +193,12 @@ private async Task DoScan( HandleResolveMacAddress(ipScanResult, ipAddress); } - if (Configuration.ResolveMacAddress && - Configuration.ResolveVendorFromMacAddress) + if (Configuration is { ResolveMacAddress: true, ResolveVendorFromMacAddress: true }) { await HandleResolveVendorFromMacAddress(ipScanResult, cancellationToken); } - if (Configuration.PortNumbers.Any()) + if (Configuration.PortNumbers.Count != 0) { foreach (var portNumber in Configuration.PortNumbers) { @@ -238,7 +239,7 @@ private void HandleResolveMacAddress( IPAddress ipAddress) { arpEntities ??= ArpHelper.GetArpResult(); - if (arpEntities.Any()) + if (arpEntities.Length != 0) { var arpEntity = arpEntities.FirstOrDefault(x => x.IPAddress.Equals(ipAddress)); if (arpEntity is not null)