# HG changeset patch # User StephaneLenclud # Date 1423502714 -3600 # Node ID 9e48cc704a69fc107ef36b5bdf60a9151c6470b0 # Parent 5fc39c560179de6b26fdaa88c4356bf90a12d898 Adding basic support for networks. diff -r 5fc39c560179 -r 9e48cc704a69 Server/MainForm.cs --- a/Server/MainForm.cs Mon Feb 09 11:09:33 2015 +0100 +++ b/Server/MainForm.cs Mon Feb 09 18:25:14 2015 +0100 @@ -19,6 +19,8 @@ using NAudio.CoreAudioApi; using NAudio.CoreAudioApi.Interfaces; using System.Runtime.InteropServices; +//Network +using NETWORKLIST; // using SharpDisplayClient; using SharpDisplay; @@ -64,6 +66,8 @@ //NAudio private MMDeviceEnumerator iMultiMediaDeviceEnumerator; private MMDevice iMultiMediaDevice; + //Network + private NetworkManager iNetworkManager; /// @@ -132,10 +136,14 @@ //NAudio iMultiMediaDeviceEnumerator = new MMDeviceEnumerator(); - iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this); - + iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this); UpdateAudioDeviceAndMasterVolumeThreadSafe(); + //Network + iNetworkManager = new NetworkManager(); + iNetworkManager.OnConnectivityChanged += OnConnectivityChanged; + UpdateNetworkStatus(); + //Setup notification icon SetupTrayIcon(); @@ -182,8 +190,10 @@ //Initiate asynchronous request iDisplay.RequestFirmwareRevision(); - // + //Audio UpdateMasterVolumeThreadSafe(); + //Network + UpdateNetworkStatus(); #if DEBUG //Testing icon in debug, no arm done if icon not supported @@ -202,7 +212,25 @@ //Our display was just closed, update our UI consequently UpdateStatus(); } - + + public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity) + { + //Update network status + UpdateNetworkStatus(); + } + + /// + /// Update our Network Status + /// + private void UpdateNetworkStatus() + { + if (iDisplay.IsOpen()) + { + iDisplay.SetIconOnOff(Display.TMiniDisplayIconType.EMiniDisplayIconNetwork, iNetworkManager.NetworkListManager.IsConnectedToInternet); + } + } + + /// /// Receive volume change notification and reflect changes on our slider. /// @@ -1077,6 +1105,7 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { + iNetworkManager.Dispose(); CloseDisplayConnection(); StopServer(); e.Cancel = iClosing; diff -r 5fc39c560179 -r 9e48cc704a69 Server/NetworkManager.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/NetworkManager.cs Mon Feb 09 18:25:14 2015 +0100 @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; +using System.Diagnostics; +using NETWORKLIST; + +namespace SharpDisplayManager +{ + public class NetworkManager: INetworkListManagerEvents, IDisposable + { + public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity); + public event OnConnectivityChangedDelegate OnConnectivityChanged; + + private int iCookie = 0; + private IConnectionPoint iConnectionPoint; + private INetworkListManager iNetworkListManager; + + + public NetworkManager() + { + iNetworkListManager = new NetworkListManager(); + ConnectToNetworkListManagerEvents(); + } + + public void Dispose() + { + //Not sure why this is not working form here + //Possibly because something is doing automatically before we get there + //DisconnectFromNetworkListManagerEvents(); + } + + + public INetworkListManager NetworkListManager + { + get { return iNetworkListManager; } + } + + public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity) + { + //Fire our event + OnConnectivityChanged(this, newConnectivity); + } + + public void ConnectToNetworkListManagerEvents() + { + Debug.WriteLine("Subscribing to INetworkListManagerEvents"); + IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager; + //similar event subscription can be used for INetworkEvents and INetworkConnectionEvents + Guid tempGuid = typeof(INetworkListManagerEvents).GUID; + icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint); + iConnectionPoint.Advise(this, out iCookie); + + } + + public void DisconnectFromNetworkListManagerEvents() + { + Debug.WriteLine("Un-subscribing to INetworkListManagerEvents"); + iConnectionPoint.Unadvise(iCookie); + } + + /// + /// Indicates whether any network connection is available + /// Filter connections below a specified speed, as well as virtual network cards. + /// + /// + /// true if a network connection is available; otherwise, false. + /// + public static bool IsAvailable() + { + return IsAvailable(0); + } + + /// + /// Indicates whether any network connection is available. + /// Filter connections below a specified speed, as well as virtual network cards. + /// + /// The minimum speed required. Passing 0 will not filter connection using speed. + /// + /// true if a network connection is available; otherwise, false. + /// + public static bool IsAvailable(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; + + // this allow to filter modems, serial, etc. + // I use 10000000 as a minimum speed for most cases + if (ni.Speed < minimumSpeed) + 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; + } + + /// + /// + /// + /// + public static bool HasInternet() + { + return false; + //ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile(); + //bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess; + //return internet; + } + + } +} diff -r 5fc39c560179 -r 9e48cc704a69 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Mon Feb 09 11:09:33 2015 +0100 +++ b/Server/SharpDisplayManager.csproj Mon Feb 09 18:25:14 2015 +0100 @@ -127,6 +127,7 @@ + @@ -223,6 +224,17 @@ + + + {DCB00D01-570F-4A9B-8D69-199FDBA5723B} + 1 + 0 + 0 + tlbimp + False + True + +