Server/NetworkManager.cs
author StephaneLenclud
Tue, 10 Feb 2015 15:41:34 +0100
changeset 122 300f3d3114a8
parent 117 9e48cc704a69
child 123 0df386e37e29
permissions -rw-r--r--
More consistent clock and clear support.
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
}