Server/NetworkManager.cs
author StephaneLenclud
Mon, 09 Feb 2015 22:50:43 +0100
changeset 121 5551b64e1eeb
parent 117 9e48cc704a69
child 123 0df386e37e29
permissions -rw-r--r--
Making App tab the last one again.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Net.NetworkInformation;
     7 using System.Runtime.InteropServices;
     8 using System.Runtime.InteropServices.ComTypes;
     9 using System.Diagnostics;
    10 using NETWORKLIST;
    11 
    12 namespace SharpDisplayManager
    13 {
    14 	public class NetworkManager: INetworkListManagerEvents, IDisposable
    15     {
    16 		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
    17 		public event OnConnectivityChangedDelegate OnConnectivityChanged;
    18 		
    19 		private int iCookie = 0;
    20         private IConnectionPoint iConnectionPoint;
    21         private INetworkListManager iNetworkListManager;
    22 
    23 
    24 		public NetworkManager()
    25 		{
    26 			iNetworkListManager = new NetworkListManager();
    27 			ConnectToNetworkListManagerEvents();
    28 		}
    29 
    30 		public void Dispose()
    31 		{
    32 			//Not sure why this is not working form here
    33 			//Possibly because something is doing automatically before we get there
    34 			//DisconnectFromNetworkListManagerEvents();
    35 		}
    36 
    37 
    38 		public INetworkListManager NetworkListManager
    39 		{
    40 			get { return iNetworkListManager; }
    41 		}
    42 
    43 		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    44 		{
    45 			//Fire our event
    46 			OnConnectivityChanged(this, newConnectivity);
    47 		}
    48 
    49 		public void ConnectToNetworkListManagerEvents()
    50 		{
    51 			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
    52 			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
    53 			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
    54 			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
    55 			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
    56 			iConnectionPoint.Advise(this, out iCookie);
    57 			
    58 		}
    59 
    60 		public void DisconnectFromNetworkListManagerEvents()
    61 		{
    62 			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
    63 			iConnectionPoint.Unadvise(iCookie);
    64 		} 
    65 	}
    66 }