Server/NetworkManager.cs
author StephaneLenclud
Mon, 09 Feb 2015 18:25:14 +0100
changeset 117 9e48cc704a69
child 118 606c22398045
permissions -rw-r--r--
Adding basic support for networks.
StephaneLenclud@117
     1
using System;
StephaneLenclud@117
     2
using System.Collections.Generic;
StephaneLenclud@117
     3
using System.Linq;
StephaneLenclud@117
     4
using System.Text;
StephaneLenclud@117
     5
using System.Threading.Tasks;
StephaneLenclud@117
     6
using System.Net.NetworkInformation;
StephaneLenclud@117
     7
using System.Runtime.InteropServices;
StephaneLenclud@117
     8
using System.Runtime.InteropServices.ComTypes;
StephaneLenclud@117
     9
using System.Diagnostics;
StephaneLenclud@117
    10
using NETWORKLIST;
StephaneLenclud@117
    11
StephaneLenclud@117
    12
namespace SharpDisplayManager
StephaneLenclud@117
    13
{
StephaneLenclud@117
    14
	public class NetworkManager: INetworkListManagerEvents, IDisposable
StephaneLenclud@117
    15
    {
StephaneLenclud@117
    16
		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
StephaneLenclud@117
    17
		public event OnConnectivityChangedDelegate OnConnectivityChanged;
StephaneLenclud@117
    18
		
StephaneLenclud@117
    19
		private int iCookie = 0;
StephaneLenclud@117
    20
        private IConnectionPoint iConnectionPoint;
StephaneLenclud@117
    21
        private INetworkListManager iNetworkListManager;
StephaneLenclud@117
    22
StephaneLenclud@117
    23
StephaneLenclud@117
    24
		public NetworkManager()
StephaneLenclud@117
    25
		{
StephaneLenclud@117
    26
			iNetworkListManager = new NetworkListManager();
StephaneLenclud@117
    27
			ConnectToNetworkListManagerEvents();
StephaneLenclud@117
    28
		}
StephaneLenclud@117
    29
StephaneLenclud@117
    30
		public void Dispose()
StephaneLenclud@117
    31
		{
StephaneLenclud@117
    32
			//Not sure why this is not working form here
StephaneLenclud@117
    33
			//Possibly because something is doing automatically before we get there
StephaneLenclud@117
    34
			//DisconnectFromNetworkListManagerEvents();
StephaneLenclud@117
    35
		}
StephaneLenclud@117
    36
StephaneLenclud@117
    37
StephaneLenclud@117
    38
		public INetworkListManager NetworkListManager
StephaneLenclud@117
    39
		{
StephaneLenclud@117
    40
			get { return iNetworkListManager; }
StephaneLenclud@117
    41
		}
StephaneLenclud@117
    42
StephaneLenclud@117
    43
		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@117
    44
		{
StephaneLenclud@117
    45
			//Fire our event
StephaneLenclud@117
    46
			OnConnectivityChanged(this, newConnectivity);
StephaneLenclud@117
    47
		}
StephaneLenclud@117
    48
StephaneLenclud@117
    49
		public void ConnectToNetworkListManagerEvents()
StephaneLenclud@117
    50
		{
StephaneLenclud@117
    51
			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    52
			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
StephaneLenclud@117
    53
			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
StephaneLenclud@117
    54
			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
StephaneLenclud@117
    55
			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
StephaneLenclud@117
    56
			iConnectionPoint.Advise(this, out iCookie);
StephaneLenclud@117
    57
			
StephaneLenclud@117
    58
		}
StephaneLenclud@117
    59
StephaneLenclud@117
    60
		public void DisconnectFromNetworkListManagerEvents()
StephaneLenclud@117
    61
		{
StephaneLenclud@117
    62
			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    63
			iConnectionPoint.Unadvise(iCookie);
StephaneLenclud@117
    64
		} 
StephaneLenclud@117
    65
StephaneLenclud@117
    66
		/// <summary>
StephaneLenclud@117
    67
		/// Indicates whether any network connection is available
StephaneLenclud@117
    68
		/// Filter connections below a specified speed, as well as virtual network cards.
StephaneLenclud@117
    69
		/// </summary>
StephaneLenclud@117
    70
		/// <returns>
StephaneLenclud@117
    71
		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
StephaneLenclud@117
    72
		/// </returns>
StephaneLenclud@117
    73
		public static bool IsAvailable()
StephaneLenclud@117
    74
		{
StephaneLenclud@117
    75
			return IsAvailable(0);
StephaneLenclud@117
    76
		}
StephaneLenclud@117
    77
StephaneLenclud@117
    78
		/// <summary>
StephaneLenclud@117
    79
		/// Indicates whether any network connection is available.
StephaneLenclud@117
    80
		/// Filter connections below a specified speed, as well as virtual network cards.
StephaneLenclud@117
    81
		/// </summary>
StephaneLenclud@117
    82
		/// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
StephaneLenclud@117
    83
		/// <returns>
StephaneLenclud@117
    84
		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
StephaneLenclud@117
    85
		/// </returns>
StephaneLenclud@117
    86
		public static bool IsAvailable(long minimumSpeed)
StephaneLenclud@117
    87
		{
StephaneLenclud@117
    88
			if (!NetworkInterface.GetIsNetworkAvailable())
StephaneLenclud@117
    89
				return false;
StephaneLenclud@117
    90
StephaneLenclud@117
    91
			foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
StephaneLenclud@117
    92
			{
StephaneLenclud@117
    93
				// discard because of standard reasons
StephaneLenclud@117
    94
				if ((ni.OperationalStatus != OperationalStatus.Up) ||
StephaneLenclud@117
    95
					(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
StephaneLenclud@117
    96
					(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
StephaneLenclud@117
    97
					continue;
StephaneLenclud@117
    98
StephaneLenclud@117
    99
				// this allow to filter modems, serial, etc.
StephaneLenclud@117
   100
				// I use 10000000 as a minimum speed for most cases
StephaneLenclud@117
   101
				if (ni.Speed < minimumSpeed)
StephaneLenclud@117
   102
					continue;
StephaneLenclud@117
   103
StephaneLenclud@117
   104
				// discard virtual cards (virtual box, virtual pc, etc.)
StephaneLenclud@117
   105
				if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
StephaneLenclud@117
   106
					(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
StephaneLenclud@117
   107
					continue;
StephaneLenclud@117
   108
StephaneLenclud@117
   109
				// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
StephaneLenclud@117
   110
				if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
StephaneLenclud@117
   111
					continue;
StephaneLenclud@117
   112
StephaneLenclud@117
   113
				return true;
StephaneLenclud@117
   114
			}
StephaneLenclud@117
   115
			return false;
StephaneLenclud@117
   116
		}
StephaneLenclud@117
   117
StephaneLenclud@117
   118
		/// <summary>
StephaneLenclud@117
   119
		/// 
StephaneLenclud@117
   120
		/// </summary>
StephaneLenclud@117
   121
		/// <returns></returns>
StephaneLenclud@117
   122
		public static bool HasInternet()
StephaneLenclud@117
   123
		{
StephaneLenclud@117
   124
			return false;
StephaneLenclud@117
   125
			//ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
StephaneLenclud@117
   126
			//bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
StephaneLenclud@117
   127
			//return internet;
StephaneLenclud@117
   128
		}
StephaneLenclud@117
   129
StephaneLenclud@117
   130
	}
StephaneLenclud@117
   131
}