Adding basic support for networks.
authorStephaneLenclud
Mon, 09 Feb 2015 18:25:14 +0100
changeset 1179e48cc704a69
parent 116 5fc39c560179
child 118 606c22398045
Adding basic support for networks.
Server/MainForm.cs
Server/NetworkManager.cs
Server/SharpDisplayManager.csproj
     1.1 --- a/Server/MainForm.cs	Mon Feb 09 11:09:33 2015 +0100
     1.2 +++ b/Server/MainForm.cs	Mon Feb 09 18:25:14 2015 +0100
     1.3 @@ -19,6 +19,8 @@
     1.4  using NAudio.CoreAudioApi;
     1.5  using NAudio.CoreAudioApi.Interfaces;
     1.6  using System.Runtime.InteropServices;
     1.7 +//Network
     1.8 +using NETWORKLIST;
     1.9  //
    1.10  using SharpDisplayClient;
    1.11  using SharpDisplay;
    1.12 @@ -64,6 +66,8 @@
    1.13  		//NAudio
    1.14  		private MMDeviceEnumerator iMultiMediaDeviceEnumerator;
    1.15  		private MMDevice iMultiMediaDevice;
    1.16 +		//Network
    1.17 +		private NetworkManager iNetworkManager;
    1.18  		
    1.19  
    1.20  		/// <summary>
    1.21 @@ -132,10 +136,14 @@
    1.22  
    1.23  			//NAudio
    1.24  			iMultiMediaDeviceEnumerator = new MMDeviceEnumerator();
    1.25 -			iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);
    1.26 -			
    1.27 +			iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);			
    1.28  			UpdateAudioDeviceAndMasterVolumeThreadSafe();
    1.29  
    1.30 +			//Network
    1.31 +			iNetworkManager = new NetworkManager();
    1.32 +			iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;
    1.33 +			UpdateNetworkStatus();
    1.34 +
    1.35  			//Setup notification icon
    1.36  			SetupTrayIcon();
    1.37  
    1.38 @@ -182,8 +190,10 @@
    1.39  			//Initiate asynchronous request
    1.40  			iDisplay.RequestFirmwareRevision();
    1.41  
    1.42 -			//
    1.43 +			//Audio
    1.44  			UpdateMasterVolumeThreadSafe();
    1.45 +			//Network
    1.46 +			UpdateNetworkStatus();
    1.47  
    1.48  #if DEBUG
    1.49  			//Testing icon in debug, no arm done if icon not supported
    1.50 @@ -202,7 +212,25 @@
    1.51  			//Our display was just closed, update our UI consequently
    1.52  			UpdateStatus();
    1.53  		}
    1.54 -		
    1.55 +
    1.56 +		public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
    1.57 +		{
    1.58 +			//Update network status
    1.59 +			UpdateNetworkStatus();			
    1.60 +		}
    1.61 +
    1.62 +		/// <summary>
    1.63 +		/// Update our Network Status
    1.64 +		/// </summary>
    1.65 +		private void UpdateNetworkStatus()
    1.66 +		{
    1.67 +			if (iDisplay.IsOpen())
    1.68 +			{
    1.69 +				iDisplay.SetIconOnOff(Display.TMiniDisplayIconType.EMiniDisplayIconNetwork, iNetworkManager.NetworkListManager.IsConnectedToInternet);
    1.70 +			}
    1.71 +		}
    1.72 +
    1.73 +
    1.74          /// <summary>
    1.75          /// Receive volume change notification and reflect changes on our slider.
    1.76          /// </summary>
    1.77 @@ -1077,6 +1105,7 @@
    1.78  
    1.79          private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    1.80          {
    1.81 +			iNetworkManager.Dispose();
    1.82  			CloseDisplayConnection();
    1.83              StopServer();
    1.84              e.Cancel = iClosing;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Server/NetworkManager.cs	Mon Feb 09 18:25:14 2015 +0100
     2.3 @@ -0,0 +1,131 @@
     2.4 +using System;
     2.5 +using System.Collections.Generic;
     2.6 +using System.Linq;
     2.7 +using System.Text;
     2.8 +using System.Threading.Tasks;
     2.9 +using System.Net.NetworkInformation;
    2.10 +using System.Runtime.InteropServices;
    2.11 +using System.Runtime.InteropServices.ComTypes;
    2.12 +using System.Diagnostics;
    2.13 +using NETWORKLIST;
    2.14 +
    2.15 +namespace SharpDisplayManager
    2.16 +{
    2.17 +	public class NetworkManager: INetworkListManagerEvents, IDisposable
    2.18 +    {
    2.19 +		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
    2.20 +		public event OnConnectivityChangedDelegate OnConnectivityChanged;
    2.21 +		
    2.22 +		private int iCookie = 0;
    2.23 +        private IConnectionPoint iConnectionPoint;
    2.24 +        private INetworkListManager iNetworkListManager;
    2.25 +
    2.26 +
    2.27 +		public NetworkManager()
    2.28 +		{
    2.29 +			iNetworkListManager = new NetworkListManager();
    2.30 +			ConnectToNetworkListManagerEvents();
    2.31 +		}
    2.32 +
    2.33 +		public void Dispose()
    2.34 +		{
    2.35 +			//Not sure why this is not working form here
    2.36 +			//Possibly because something is doing automatically before we get there
    2.37 +			//DisconnectFromNetworkListManagerEvents();
    2.38 +		}
    2.39 +
    2.40 +
    2.41 +		public INetworkListManager NetworkListManager
    2.42 +		{
    2.43 +			get { return iNetworkListManager; }
    2.44 +		}
    2.45 +
    2.46 +		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    2.47 +		{
    2.48 +			//Fire our event
    2.49 +			OnConnectivityChanged(this, newConnectivity);
    2.50 +		}
    2.51 +
    2.52 +		public void ConnectToNetworkListManagerEvents()
    2.53 +		{
    2.54 +			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
    2.55 +			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
    2.56 +			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
    2.57 +			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
    2.58 +			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
    2.59 +			iConnectionPoint.Advise(this, out iCookie);
    2.60 +			
    2.61 +		}
    2.62 +
    2.63 +		public void DisconnectFromNetworkListManagerEvents()
    2.64 +		{
    2.65 +			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
    2.66 +			iConnectionPoint.Unadvise(iCookie);
    2.67 +		} 
    2.68 +
    2.69 +		/// <summary>
    2.70 +		/// Indicates whether any network connection is available
    2.71 +		/// Filter connections below a specified speed, as well as virtual network cards.
    2.72 +		/// </summary>
    2.73 +		/// <returns>
    2.74 +		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
    2.75 +		/// </returns>
    2.76 +		public static bool IsAvailable()
    2.77 +		{
    2.78 +			return IsAvailable(0);
    2.79 +		}
    2.80 +
    2.81 +		/// <summary>
    2.82 +		/// Indicates whether any network connection is available.
    2.83 +		/// Filter connections below a specified speed, as well as virtual network cards.
    2.84 +		/// </summary>
    2.85 +		/// <param name="minimumSpeed">The minimum speed required. Passing 0 will not filter connection using speed.</param>
    2.86 +		/// <returns>
    2.87 +		///     <c>true</c> if a network connection is available; otherwise, <c>false</c>.
    2.88 +		/// </returns>
    2.89 +		public static bool IsAvailable(long minimumSpeed)
    2.90 +		{
    2.91 +			if (!NetworkInterface.GetIsNetworkAvailable())
    2.92 +				return false;
    2.93 +
    2.94 +			foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
    2.95 +			{
    2.96 +				// discard because of standard reasons
    2.97 +				if ((ni.OperationalStatus != OperationalStatus.Up) ||
    2.98 +					(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
    2.99 +					(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
   2.100 +					continue;
   2.101 +
   2.102 +				// this allow to filter modems, serial, etc.
   2.103 +				// I use 10000000 as a minimum speed for most cases
   2.104 +				if (ni.Speed < minimumSpeed)
   2.105 +					continue;
   2.106 +
   2.107 +				// discard virtual cards (virtual box, virtual pc, etc.)
   2.108 +				if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
   2.109 +					(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
   2.110 +					continue;
   2.111 +
   2.112 +				// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
   2.113 +				if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
   2.114 +					continue;
   2.115 +
   2.116 +				return true;
   2.117 +			}
   2.118 +			return false;
   2.119 +		}
   2.120 +
   2.121 +		/// <summary>
   2.122 +		/// 
   2.123 +		/// </summary>
   2.124 +		/// <returns></returns>
   2.125 +		public static bool HasInternet()
   2.126 +		{
   2.127 +			return false;
   2.128 +			//ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
   2.129 +			//bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
   2.130 +			//return internet;
   2.131 +		}
   2.132 +
   2.133 +	}
   2.134 +}
     3.1 --- a/Server/SharpDisplayManager.csproj	Mon Feb 09 11:09:33 2015 +0100
     3.2 +++ b/Server/SharpDisplayManager.csproj	Mon Feb 09 18:25:14 2015 +0100
     3.3 @@ -127,6 +127,7 @@
     3.4    </ItemGroup>
     3.5    <ItemGroup>
     3.6      <Compile Include="CbtHook.cs" />
     3.7 +    <Compile Include="NetworkManager.cs" />
     3.8      <Compile Include="DialogBox.cs" />
     3.9      <Compile Include="Display.cs" />
    3.10      <Compile Include="DisplaySettings.cs" />
    3.11 @@ -223,6 +224,17 @@
    3.12      <Content Include="MiniDisplay.dll" />
    3.13      <EmbeddedResource Include="Resources\vfd.ico" />
    3.14    </ItemGroup>
    3.15 +  <ItemGroup>
    3.16 +    <COMReference Include="NETWORKLIST">
    3.17 +      <Guid>{DCB00D01-570F-4A9B-8D69-199FDBA5723B}</Guid>
    3.18 +      <VersionMajor>1</VersionMajor>
    3.19 +      <VersionMinor>0</VersionMinor>
    3.20 +      <Lcid>0</Lcid>
    3.21 +      <WrapperTool>tlbimp</WrapperTool>
    3.22 +      <Isolated>False</Isolated>
    3.23 +      <EmbedInteropTypes>True</EmbedInteropTypes>
    3.24 +    </COMReference>
    3.25 +  </ItemGroup>
    3.26    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    3.27    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
    3.28         Other similar extension points exist, see Microsoft.Common.targets.