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