Server/NetworkManager.cs
author StephaneLenclud
Tue, 10 Feb 2015 17:14:27 +0100
changeset 123 0df386e37e29
parent 118 606c22398045
child 144 8e4c7cca77b8
permissions -rw-r--r--
Liscense and Copyright fix.
     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.Collections.Generic;
    22 using System.Linq;
    23 using System.Text;
    24 using System.Threading.Tasks;
    25 using System.Net.NetworkInformation;
    26 using System.Runtime.InteropServices;
    27 using System.Runtime.InteropServices.ComTypes;
    28 using System.Diagnostics;
    29 using NETWORKLIST;
    30 
    31 namespace SharpDisplayManager
    32 {
    33 	public class NetworkManager: INetworkListManagerEvents, IDisposable
    34     {
    35 		public delegate void OnConnectivityChangedDelegate(NetworkManager aNetworkManager, NLM_CONNECTIVITY aConnectivity);
    36 		public event OnConnectivityChangedDelegate OnConnectivityChanged;
    37 		
    38 		private int iCookie = 0;
    39         private IConnectionPoint iConnectionPoint;
    40         private INetworkListManager iNetworkListManager;
    41 
    42 
    43 		public NetworkManager()
    44 		{
    45 			iNetworkListManager = new NetworkListManager();
    46 			ConnectToNetworkListManagerEvents();
    47 		}
    48 
    49 		public void Dispose()
    50 		{
    51 			//Not sure why this is not working form here
    52 			//Possibly because something is doing automatically before we get there
    53 			//DisconnectFromNetworkListManagerEvents();
    54 		}
    55 
    56 
    57 		public INetworkListManager NetworkListManager
    58 		{
    59 			get { return iNetworkListManager; }
    60 		}
    61 
    62 		public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
    63 		{
    64 			//Fire our event
    65 			OnConnectivityChanged(this, newConnectivity);
    66 		}
    67 
    68 		public void ConnectToNetworkListManagerEvents()
    69 		{
    70 			Debug.WriteLine("Subscribing to INetworkListManagerEvents");
    71 			IConnectionPointContainer icpc = (IConnectionPointContainer)iNetworkListManager;
    72 			//similar event subscription can be used for INetworkEvents and INetworkConnectionEvents
    73 			Guid tempGuid = typeof(INetworkListManagerEvents).GUID;
    74 			icpc.FindConnectionPoint(ref tempGuid, out iConnectionPoint);
    75 			iConnectionPoint.Advise(this, out iCookie);
    76 			
    77 		}
    78 
    79 		public void DisconnectFromNetworkListManagerEvents()
    80 		{
    81 			Debug.WriteLine("Un-subscribing to INetworkListManagerEvents");
    82 			iConnectionPoint.Unadvise(iCookie);
    83 		} 
    84 	}
    85 }