Server/NetworkManager.cs
author StephaneLenclud
Sun, 21 Jun 2015 10:18:12 +0200
changeset 142 45afadb954ba
parent 118 606c22398045
child 144 8e4c7cca77b8
permissions -rw-r--r--
Fixing clients management.
StephaneLenclud@123
     1
//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
StephaneLenclud@117
    21
using System.Collections.Generic;
StephaneLenclud@117
    22
using System.Linq;
StephaneLenclud@117
    23
using System.Text;
StephaneLenclud@117
    24
using System.Threading.Tasks;
StephaneLenclud@117
    25
using System.Net.NetworkInformation;
StephaneLenclud@117
    26
using System.Runtime.InteropServices;
StephaneLenclud@117
    27
using System.Runtime.InteropServices.ComTypes;
StephaneLenclud@117
    28
using System.Diagnostics;
StephaneLenclud@117
    29
using NETWORKLIST;
StephaneLenclud@117
    30
StephaneLenclud@117
    31
namespace SharpDisplayManager
StephaneLenclud@117
    32
{
StephaneLenclud@117
    33
	public class NetworkManager: INetworkListManagerEvents, IDisposable
StephaneLenclud@117
    34
    {
StephaneLenclud@117
    35
		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
StephaneLenclud@117
    36
		public event OnConnectivityChangedDelegate OnConnectivityChanged;
StephaneLenclud@117
    37
		
StephaneLenclud@117
    38
		private int iCookie = 0;
StephaneLenclud@117
    39
        private IConnectionPoint iConnectionPoint;
StephaneLenclud@117
    40
        private INetworkListManager iNetworkListManager;
StephaneLenclud@117
    41
StephaneLenclud@117
    42
StephaneLenclud@117
    43
		public NetworkManager()
StephaneLenclud@117
    44
		{
StephaneLenclud@117
    45
			iNetworkListManager = new NetworkListManager();
StephaneLenclud@117
    46
			ConnectToNetworkListManagerEvents();
StephaneLenclud@117
    47
		}
StephaneLenclud@117
    48
StephaneLenclud@117
    49
		public void Dispose()
StephaneLenclud@117
    50
		{
StephaneLenclud@117
    51
			//Not sure why this is not working form here
StephaneLenclud@117
    52
			//Possibly because something is doing automatically before we get there
StephaneLenclud@117
    53
			//DisconnectFromNetworkListManagerEvents();
StephaneLenclud@117
    54
		}
StephaneLenclud@117
    55
StephaneLenclud@117
    56
StephaneLenclud@117
    57
		public INetworkListManager NetworkListManager
StephaneLenclud@117
    58
		{
StephaneLenclud@117
    59
			get { return iNetworkListManager; }
StephaneLenclud@117
    60
		}
StephaneLenclud@117
    61
StephaneLenclud@117
    62
		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@117
    63
		{
StephaneLenclud@117
    64
			//Fire our event
StephaneLenclud@117
    65
			OnConnectivityChanged(this, newConnectivity);
StephaneLenclud@117
    66
		}
StephaneLenclud@117
    67
StephaneLenclud@117
    68
		public void ConnectToNetworkListManagerEvents()
StephaneLenclud@117
    69
		{
StephaneLenclud@117
    70
			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    71
			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
StephaneLenclud@117
    72
			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
StephaneLenclud@117
    73
			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
StephaneLenclud@117
    74
			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
StephaneLenclud@117
    75
			iConnectionPoint.Advise(this, out iCookie);
StephaneLenclud@117
    76
			
StephaneLenclud@117
    77
		}
StephaneLenclud@117
    78
StephaneLenclud@117
    79
		public void DisconnectFromNetworkListManagerEvents()
StephaneLenclud@117
    80
		{
StephaneLenclud@117
    81
			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    82
			iConnectionPoint.Unadvise(iCookie);
StephaneLenclud@117
    83
		} 
StephaneLenclud@117
    84
	}
StephaneLenclud@117
    85
}