-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be97cd0
commit 821e788
Showing
3 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
namespace WoL.Services | ||
{ | ||
public interface IPingService | ||
{ | ||
Task<bool> IsReachable(IPAddress ip, int timeout); | ||
Task<PingResult> IsReachable(string hostname, int timeout); | ||
|
||
public enum PingResult | ||
{ | ||
Unreachable, | ||
HostNotFound, | ||
Success | ||
} | ||
} | ||
} |
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,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using System.Threading.Tasks; | ||
using static WoL.Services.IPingService; | ||
|
||
namespace WoL.Services | ||
{ | ||
public class PingService : IPingService | ||
{ | ||
private IAddressLookupService addressLookupService; | ||
|
||
public PingService(IAddressLookupService addressLookupService) | ||
{ | ||
this.addressLookupService = addressLookupService; | ||
} | ||
|
||
public async Task<PingResult> IsReachable(string hostname, int timeout) | ||
{ | ||
await Task.Delay(1000); | ||
IPAddress ip; | ||
try | ||
{ | ||
(ip, _) = await addressLookupService.GetIpAndName(hostname).ConfigureAwait(false); | ||
} | ||
catch | ||
{ | ||
return PingResult.HostNotFound; | ||
} | ||
return await IsReachable(ip, timeout).ConfigureAwait(false) ? PingResult.Success : PingResult.Unreachable; | ||
} | ||
|
||
public async Task<bool> IsReachable(IPAddress ip, int timeout) | ||
{ | ||
using var ping = new Ping(); | ||
var reply = await ping.SendPingAsync(ip, timeout).ConfigureAwait(false); | ||
return reply.Status == IPStatus.Success; | ||
} | ||
} | ||
} |
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