-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from OpenIPC/glowing-connect-ping
add pulsing connect button based on ping status
- Loading branch information
Showing
9 changed files
with
545 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Net.NetworkInformation; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Serilog; | ||
|
||
namespace OpenIPC_Config.Services; | ||
|
||
public class PingService | ||
{ | ||
private static PingService _instance; | ||
private static readonly object _lock = new object(); | ||
|
||
private readonly SemaphoreSlim _pingSemaphore = new SemaphoreSlim(1, 1); | ||
private readonly TimeSpan _defaultTimeout = TimeSpan.FromMilliseconds(500); | ||
private readonly ILogger _logger; | ||
|
||
// Private constructor for singleton pattern | ||
private PingService(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
// Singleton instance getter | ||
public static PingService Instance(ILogger logger) | ||
{ | ||
if (_instance == null) | ||
{ | ||
lock (_lock) | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new PingService(logger); | ||
} | ||
} | ||
} | ||
return _instance; | ||
} | ||
|
||
// Ping method with default timeout | ||
public async Task<PingReply> SendPingAsync(string ipAddress) | ||
{ | ||
return await SendPingAsync(ipAddress, (int)_defaultTimeout.TotalMilliseconds); | ||
} | ||
|
||
// Ping method with custom timeout | ||
public async Task<PingReply> SendPingAsync(string ipAddress, int timeout) | ||
{ | ||
// Log which IP is being pinged | ||
_logger.Debug($"Attempting to ping IP: {ipAddress}"); | ||
|
||
if (await _pingSemaphore.WaitAsync(timeout)) | ||
{ | ||
try | ||
{ | ||
using (var ping = new Ping()) | ||
{ | ||
return await ping.SendPingAsync(ipAddress, timeout); | ||
} | ||
} | ||
finally | ||
{ | ||
// Release the semaphore when done | ||
_pingSemaphore.Release(); | ||
} | ||
} | ||
else | ||
{ | ||
_logger.Warning("Timeout waiting to acquire ping semaphore for {IpAddress}", ipAddress); | ||
|
||
// Since we can't create a PingReply directly, throw a meaningful exception instead | ||
throw new TimeoutException($"Ping operation to {ipAddress} was delayed due to concurrent requests"); | ||
} | ||
} | ||
|
||
// Dispose method to clean up all resources | ||
public void Dispose() | ||
{ | ||
_pingSemaphore.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.