88 lines
3.0 KiB
C#
Executable File
88 lines
3.0 KiB
C#
Executable File
using System.Net.NetworkInformation;
|
|
|
|
namespace MarketData.Integration
|
|
{
|
|
public class NetworkStatus
|
|
{
|
|
private NetworkStatus()
|
|
{
|
|
}
|
|
/// <summary>
|
|
/// Indicates whether any network connection is available
|
|
/// Filter connections below a specified speed, as well as virtual network cards.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
|
|
/// </returns>
|
|
public static bool IsNetworkAvailable()
|
|
{
|
|
return IsNetworkAvailable(0);
|
|
}
|
|
|
|
// On Linux platforms we must be priveleged to send custom payloads so we are using the send option that does not require a payload
|
|
// This differs from the windows implementation
|
|
public static bool IsInternetConnected()
|
|
{
|
|
try
|
|
{
|
|
Ping myPing = new Ping();
|
|
String host = "google.com";
|
|
int timeout = 5000;
|
|
PingReply reply = myPing.Send(host, timeout);
|
|
|
|
if (reply.Status == IPStatus.Success)
|
|
{
|
|
return true;
|
|
}
|
|
else if (reply.Status == IPStatus.TimedOut)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
MDTrace.WriteLine(LogLevel.DEBUG,$"[Ping] {exception.ToString()}");
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Indicates whether any network connection is available.
|
|
/// Filter connections below a specified speed, as well as virtual network cards.
|
|
/// </summary>
|
|
/// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
|
|
/// <returns>
|
|
/// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
|
|
/// </returns>
|
|
public static bool IsNetworkAvailable(long minimumSpeed)
|
|
{
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
return false;
|
|
|
|
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
// discard because of standard reasons
|
|
if ((ni.OperationalStatus != OperationalStatus.Up) ||
|
|
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
|
|
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
|
|
continue;
|
|
|
|
// discard virtual cards (virtual box, virtual pc, etc.)
|
|
if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
|
|
(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
|
|
continue;
|
|
|
|
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
|
|
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|