Server/NetworkManager.cs
changeset 117 9e48cc704a69
child 118 606c22398045
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Server/NetworkManager.cs	Mon Feb 09 18:25:14 2015 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Linq;
     1.7 +using System.Text;
     1.8 +using System.Threading.Tasks;
     1.9 +using System.Net.NetworkInformation;
    1.10 +using System.Runtime.InteropServices;
    1.11 +using System.Runtime.InteropServices.ComTypes;
    1.12 +using System.Diagnostics;
    1.13 +using NETWORKLIST;
    1.14 +
    1.15 +namespace SharpDisplayManager
    1.16 +{
    1.17 +	public class NetworkManager: INetworkListManagerEvents, IDisposable
    1.18 +    {
    1.19 +		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
    1.20 +		public event OnConnectivityChangedDelegate OnConnectivityChanged;
    1.21 +		
    1.22 +		private int iCookie = 0;
    1.23 +        private IConnectionPoint iConnectionPoint;
    1.24 +        private INetworkListManager iNetworkListManager;
    1.25 +
    1.26 +
    1.27 +		public NetworkManager()
    1.28 +		{
    1.29 +			iNetworkListManager = new NetworkListManager();
    1.30 +			ConnectToNetworkListManagerEvents();
    1.31 +		}
    1.32 +
    1.33 +		public void Dispose()
    1.34 +		{
    1.35 +			//Not sure why this is not working form here
    1.36 +			//Possibly because something is doing automatically before we get there
    1.37 +			//DisconnectFromNetworkListManagerEvents();
    1.38 +		}
    1.39 +
    1.40 +
    1.41 +		public INetworkListManager NetworkListManager
    1.42 +		{
    1.43 +			get { return iNetworkListManager; }
    1.44 +		}
    1.45 +
    1.46 +		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    1.47 +		{
    1.48 +			//Fire our event
    1.49 +			OnConnectivityChanged(this, newConnectivity);
    1.50 +		}
    1.51 +
    1.52 +		public void ConnectToNetworkListManagerEvents()
    1.53 +		{
    1.54 +			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
    1.55 +			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
    1.56 +			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
    1.57 +			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
    1.58 +			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
    1.59 +			iConnectionPoint.Advise(this, out iCookie);
    1.60 +			
    1.61 +		}
    1.62 +
    1.63 +		public void DisconnectFromNetworkListManagerEvents()
    1.64 +		{
    1.65 +			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
    1.66 +			iConnectionPoint.Unadvise(iCookie);
    1.67 +		} 
    1.68 +
    1.69 +		/// <summary>
    1.70 +		/// Indicates whether any network connection is available
    1.71 +		/// Filter connections below a specified speed, as well as virtual network cards.
    1.72 +		/// </summary>
    1.73 +		/// <returns>
    1.74 +		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
    1.75 +		/// </returns>
    1.76 +		public static bool IsAvailable()
    1.77 +		{
    1.78 +			return IsAvailable(0);
    1.79 +		}
    1.80 +
    1.81 +		/// <summary>
    1.82 +		/// Indicates whether any network connection is available.
    1.83 +		/// Filter connections below a specified speed, as well as virtual network cards.
    1.84 +		/// </summary>
    1.85 +		/// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
    1.86 +		/// <returns>
    1.87 +		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
    1.88 +		/// </returns>
    1.89 +		public static bool IsAvailable(long minimumSpeed)
    1.90 +		{
    1.91 +			if (!NetworkInterface.GetIsNetworkAvailable())
    1.92 +				return false;
    1.93 +
    1.94 +			foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
    1.95 +			{
    1.96 +				// discard because of standard reasons
    1.97 +				if ((ni.OperationalStatus != OperationalStatus.Up) ||
    1.98 +					(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
    1.99 +					(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
   1.100 +					continue;
   1.101 +
   1.102 +				// this allow to filter modems, serial, etc.
   1.103 +				// I use 10000000 as a minimum speed for most cases
   1.104 +				if (ni.Speed < minimumSpeed)
   1.105 +					continue;
   1.106 +
   1.107 +				// discard virtual cards (virtual box, virtual pc, etc.)
   1.108 +				if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
   1.109 +					(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
   1.110 +					continue;
   1.111 +
   1.112 +				// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
   1.113 +				if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
   1.114 +					continue;
   1.115 +
   1.116 +				return true;
   1.117 +			}
   1.118 +			return false;
   1.119 +		}
   1.120 +
   1.121 +		/// <summary>
   1.122 +		/// 
   1.123 +		/// </summary>
   1.124 +		/// <returns></returns>
   1.125 +		public static bool HasInternet()
   1.126 +		{
   1.127 +			return false;
   1.128 +			//ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
   1.129 +			//bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
   1.130 +			//return internet;
   1.131 +		}
   1.132 +
   1.133 +	}
   1.134 +}