Server/NetworkManager.cs
author StephaneLenclud
Mon, 29 Aug 2016 17:36:02 +0200
changeset 256 51b86efdc448
parent 123 0df386e37e29
permissions -rw-r--r--
Published V.1.0.5.
Providing Harmony Hub reconnect.
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.Runtime.InteropServices.ComTypes;
StephaneLenclud@117
    22
using System.Diagnostics;
StephaneLenclud@117
    23
using NETWORKLIST;
StephaneLenclud@117
    24
StephaneLenclud@117
    25
namespace SharpDisplayManager
StephaneLenclud@117
    26
{
StephaneLenclud@117
    27
	public class NetworkManager: INetworkListManagerEvents, IDisposable
StephaneLenclud@117
    28
    {
StephaneLenclud@117
    29
		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
StephaneLenclud@117
    30
		public event OnConnectivityChangedDelegate OnConnectivityChanged;
StephaneLenclud@117
    31
		
StephaneLenclud@117
    32
		private int iCookie = 0;
StephaneLenclud@117
    33
        private IConnectionPoint iConnectionPoint;
StephaneLenclud@117
    34
        private INetworkListManager iNetworkListManager;
StephaneLenclud@117
    35
StephaneLenclud@117
    36
StephaneLenclud@117
    37
		public NetworkManager()
StephaneLenclud@117
    38
		{
StephaneLenclud@117
    39
			iNetworkListManager = new NetworkListManager();
StephaneLenclud@117
    40
			ConnectToNetworkListManagerEvents();
StephaneLenclud@117
    41
		}
StephaneLenclud@117
    42
StephaneLenclud@117
    43
		public void Dispose()
StephaneLenclud@117
    44
		{
StephaneLenclud@117
    45
			//Not sure why this is not working form here
StephaneLenclud@117
    46
			//Possibly because something is doing automatically before we get there
StephaneLenclud@117
    47
			//DisconnectFromNetworkListManagerEvents();
StephaneLenclud@117
    48
		}
StephaneLenclud@117
    49
StephaneLenclud@117
    50
StephaneLenclud@117
    51
		public INetworkListManager NetworkListManager
StephaneLenclud@117
    52
		{
StephaneLenclud@117
    53
			get { return iNetworkListManager; }
StephaneLenclud@117
    54
		}
StephaneLenclud@117
    55
StephaneLenclud@117
    56
		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@117
    57
		{
StephaneLenclud@117
    58
			//Fire our event
StephaneLenclud@117
    59
			OnConnectivityChanged(this, newConnectivity);
StephaneLenclud@117
    60
		}
StephaneLenclud@117
    61
StephaneLenclud@117
    62
		public void ConnectToNetworkListManagerEvents()
StephaneLenclud@117
    63
		{
StephaneLenclud@117
    64
			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    65
			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
StephaneLenclud@117
    66
			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
StephaneLenclud@117
    67
			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
StephaneLenclud@117
    68
			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
StephaneLenclud@117
    69
			iConnectionPoint.Advise(this, out iCookie);
StephaneLenclud@117
    70
			
StephaneLenclud@117
    71
		}
StephaneLenclud@117
    72
StephaneLenclud@117
    73
		public void DisconnectFromNetworkListManagerEvents()
StephaneLenclud@117
    74
		{
StephaneLenclud@117
    75
			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
StephaneLenclud@117
    76
			iConnectionPoint.Unadvise(iCookie);
StephaneLenclud@117
    77
		} 
StephaneLenclud@117
    78
	}
StephaneLenclud@117
    79
}