Skip to content

Commit

Permalink
ServiceBrowser: implement StopBrowse and set version to 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed May 7, 2015
1 parent 07806bf commit f8cc2d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.3.0.0")]
[assembly: AssemblyFileVersion("0.3.0.0")]
[assembly: AssemblyVersion("0.3.1.0")]
[assembly: AssemblyFileVersion("0.3.1.0")]
43 changes: 38 additions & 5 deletions Tmds/MDns/ServiceBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public void StartBrowse(IEnumerable<string> serviceTypes, SynchronizationContext
StartBrowsing(synchronizationContext);
}

public void StopBrowse()
{
if (!IsBrowsing)
{
return;
}
IsBrowsing = false;

NetworkChange.NetworkAddressChanged -= _networkAddressChangedEventHandler;
lock (_interfaceHandlers)
{
foreach (var interfaceHandler in _interfaceHandlers.Values)
{
interfaceHandler.Disable();
OnNetworkInterfaceRemoved(interfaceHandler.NetworkInterface);
}
_interfaceHandlers = null;
}
_serviceTypes.Clear();
SynchronizationContext = null;
}

public void StartBrowse(IEnumerable<string> serviceTypes, bool useSynchronizationContext = true)
{
if (useSynchronizationContext)
Expand Down Expand Up @@ -193,15 +215,25 @@ private void StartBrowsing(SynchronizationContext synchronizationContext)
IsBrowsing = true;
SynchronizationContext = synchronizationContext;

_interfaceHandlers = new Dictionary<int, NetworkInterfaceHandler>();
NetworkChange.NetworkAddressChanged += CheckNetworkInterfaceStatuses;
CheckNetworkInterfaceStatuses(null, null);
Dictionary<int, NetworkInterfaceHandler> interfaceHandlers = new Dictionary<int, NetworkInterfaceHandler>();
_interfaceHandlers = interfaceHandlers;
_networkAddressChangedEventHandler = (s, e) =>
{
CheckNetworkInterfaceStatuses(interfaceHandlers);
};
NetworkChange.NetworkAddressChanged += _networkAddressChangedEventHandler;
CheckNetworkInterfaceStatuses(interfaceHandlers);
}

private void CheckNetworkInterfaceStatuses(object sender, EventArgs ev)
private void CheckNetworkInterfaceStatuses(Dictionary<int, NetworkInterfaceHandler> interfaceHandlers)
{
lock(_interfaceHandlers)
lock (interfaceHandlers)
{
if (interfaceHandlers != _interfaceHandlers)
{
return;
}

HashSet<NetworkInterfaceHandler> handlers = new HashSet<NetworkInterfaceHandler>(_interfaceHandlers.Values);
NetworkInterfaceInformation[] interfaceInfos = NetworkInterfaceInformation.GetAllNetworkInterfaces();
foreach (NetworkInterfaceInformation interfaceInfo in interfaceInfos)
Expand Down Expand Up @@ -261,6 +293,7 @@ private void SynchronizationContextPost(SendOrPostCallback cb)
private readonly HashSet<ServiceAnnouncement> _services = new HashSet<ServiceAnnouncement>();
private readonly Dictionary<Tuple<string, Name>, ServiceAnnouncement> _serviceAnnouncements = new Dictionary<Tuple<string, Name>, ServiceAnnouncement>();
private Dictionary<int, NetworkInterfaceHandler> _interfaceHandlers;
NetworkAddressChangedEventHandler _networkAddressChangedEventHandler;
private List<string> _serviceTypes = new List<string>();
}
}

0 comments on commit f8cc2d8

Please sign in to comment.