Skip to content

Commit

Permalink
fix(FindNetworkInterfaces): catch exceptions
Browse files Browse the repository at this point in the history
FindNetworkInterfaces is called by an event handler, so we do not want to bubble up exceptions.
  • Loading branch information
richardschneider committed Dec 13, 2018
1 parent 94e8e96 commit 8f7a766
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,48 +229,57 @@ public void Stop()

void FindNetworkInterfaces()
{
var currentNics = GetNetworkInterfaces().ToList();
log.Debug("Finding network interfaces");

var newNics = new List<NetworkInterface>();
var oldNics = new List<NetworkInterface>();

foreach (var nic in knownNics.Where(k => !currentNics.Any(n => k.Id == n.Id)))
try
{
oldNics.Add(nic);
var currentNics = GetNetworkInterfaces().ToList();

var newNics = new List<NetworkInterface>();
var oldNics = new List<NetworkInterface>();

if (log.IsDebugEnabled)
foreach (var nic in knownNics.Where(k => !currentNics.Any(n => k.Id == n.Id)))
{
log.Debug($"Removed nic '{nic.Name}'.");
}
}
oldNics.Add(nic);

foreach (var nic in currentNics.Where(nic => !knownNics.Any(k => k.Id == nic.Id)))
{
newNics.Add(nic);
if (log.IsDebugEnabled)
{
log.Debug($"Removed nic '{nic.Name}'.");
}
}

if (log.IsDebugEnabled)
foreach (var nic in currentNics.Where(nic => !knownNics.Any(k => k.Id == nic.Id)))
{
log.Debug($"Found nic '{nic.Name}'.");
newNics.Add(nic);

if (log.IsDebugEnabled)
{
log.Debug($"Found nic '{nic.Name}'.");
}
}
}

knownNics = currentNics;
knownNics = currentNics;

client?.Dispose();
client = new MulticastClient(MdnsEndpoint, networkInterfacesFilter?.Invoke(knownNics) ?? knownNics);
client.Receive(OnDnsMessage);
client?.Dispose();
client = new MulticastClient(MdnsEndpoint, networkInterfacesFilter?.Invoke(knownNics) ?? knownNics);
client.Receive(OnDnsMessage);

// Tell others.
if (newNics.Any())
{
NetworkInterfaceDiscovered?.Invoke(this, new NetworkInterfaceEventArgs
// Tell others.
if (newNics.Any())
{
NetworkInterfaces = newNics
});
}
NetworkInterfaceDiscovered?.Invoke(this, new NetworkInterfaceEventArgs
{
NetworkInterfaces = newNics
});
}

NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
}
catch (Exception e)
{
log.Error("FindNics failed", e);
}
}

/// <inheritdoc />
Expand Down

0 comments on commit 8f7a766

Please # to comment.