diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-04 19:57:21 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-04 19:57:21 -0400 |
| commit | 25312d7d03af665818cfd2cee2edb549e0e940f2 (patch) | |
| tree | 2220b08d58fb36b0b581a60cdc167d72e628b916 /Emby.Common.Implementations | |
| parent | 67ad1db6b77b2c2cb6d81c22808d99564a5f3ebc (diff) | |
add udp error handling
Diffstat (limited to 'Emby.Common.Implementations')
| -rw-r--r-- | Emby.Common.Implementations/Net/UdpSocket.cs | 23 | ||||
| -rw-r--r-- | Emby.Common.Implementations/Networking/BaseNetworkManager.cs | 85 |
2 files changed, 47 insertions, 61 deletions
diff --git a/Emby.Common.Implementations/Net/UdpSocket.cs b/Emby.Common.Implementations/Net/UdpSocket.cs index 6afb071ae..d999d3fe8 100644 --- a/Emby.Common.Implementations/Net/UdpSocket.cs +++ b/Emby.Common.Implementations/Net/UdpSocket.cs @@ -102,29 +102,10 @@ namespace Emby.Common.Implementations.Net { taskSource.TrySetException(ex); } - catch (ObjectDisposedException ex) - { - taskSource.TrySetException(ex); - } - catch (InvalidOperationException ex) - { - taskSource.TrySetException(ex); - } - catch (SecurityException ex) - { - taskSource.TrySetException(ex); - } + }, null); } - catch (SocketException ex) - { - taskSource.TrySetException(ex); - } - catch (ObjectDisposedException ex) - { - taskSource.TrySetException(ex); - } - catch (SecurityException ex) + catch (Exception ex) { taskSource.TrySetException(ex); } diff --git a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs index d1c299dc9..10d0db968 100644 --- a/Emby.Common.Implementations/Networking/BaseNetworkManager.cs +++ b/Emby.Common.Implementations/Networking/BaseNetworkManager.cs @@ -22,7 +22,7 @@ namespace Emby.Common.Implementations.Networking Logger = logger; } - private List<IPAddress> _localIpAddresses; + private List<IPAddress> _localIpAddresses; private readonly object _localIpAddressSyncLock = new object(); /// <summary> @@ -51,24 +51,24 @@ namespace Emby.Common.Implementations.Networking return _localIpAddresses; } - private IEnumerable<IPAddress> GetLocalIpAddressesInternal() + private IEnumerable<IPAddress> GetLocalIpAddressesInternal() { var list = GetIPsDefault() .ToList(); if (list.Count == 0) { - list.AddRange(GetLocalIpAddressesFallback().Result); + list.AddRange(GetLocalIpAddressesFallback().Result); } - return list.Where(FilterIpAddress).DistinctBy(i => i.ToString()); + return list.Where(FilterIpAddress).DistinctBy(i => i.ToString()); } - private bool FilterIpAddress(IPAddress address) + private bool FilterIpAddress(IPAddress address) { - var addressString = address.ToString (); + var addressString = address.ToString(); - if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase)) + if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase)) { return false; } @@ -156,12 +156,12 @@ namespace Emby.Common.Implementations.Networking { var prefix = addressString.Substring(0, lengthMatch); - if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) + if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) { return true; } } - } + } else if (resolveHost) { Uri uri; @@ -200,45 +200,50 @@ namespace Emby.Common.Implementations.Networking return Dns.GetHostAddressesAsync(hostName); } - private List<IPAddress> GetIPsDefault() - { - NetworkInterface[] interfaces; + private List<IPAddress> GetIPsDefault() + { + NetworkInterface[] interfaces; - try - { - interfaces = NetworkInterface.GetAllNetworkInterfaces(); - } - catch (Exception ex) - { - Logger.ErrorException("Error in GetAllNetworkInterfaces", ex); - return new List<IPAddress>(); - } + try + { + var validStatuses = new[] { OperationalStatus.Up, OperationalStatus.Unknown }; - return interfaces.SelectMany(network => { + interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(i => validStatuses.Contains(i.OperationalStatus)) + .ToArray(); + } + catch (Exception ex) + { + Logger.ErrorException("Error in GetAllNetworkInterfaces", ex); + return new List<IPAddress>(); + } + + return interfaces.SelectMany(network => + { - try - { + try + { Logger.Debug("Querying interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus); - var properties = network.GetIPProperties(); + var properties = network.GetIPProperties(); - return properties.UnicastAddresses + return properties.UnicastAddresses .Where(i => i.IsDnsEligible) .Select(i => i.Address) .Where(i => i.AddressFamily == AddressFamily.InterNetwork) - .ToList(); - } - catch (Exception ex) - { - Logger.ErrorException("Error querying network interface", ex); - return new List<IPAddress>(); - } - - }).DistinctBy(i => i.ToString()) - .ToList(); - } - - private async Task<IEnumerable<IPAddress>> GetLocalIpAddressesFallback() + .ToList(); + } + catch (Exception ex) + { + Logger.ErrorException("Error querying network interface", ex); + return new List<IPAddress>(); + } + + }).DistinctBy(i => i.ToString()) + .ToList(); + } + + private async Task<IEnumerable<IPAddress>> GetLocalIpAddressesFallback() { var host = await Dns.GetHostEntryAsync(Dns.GetHostName()).ConfigureAwait(false); @@ -310,7 +315,7 @@ namespace Emby.Common.Implementations.Networking string[] values = endpointstring.Split(new char[] { ':' }); IPAddress ipaddy; int port = -1; - + //check if we have an IPv6 or ports if (values.Length <= 2) // ipv4 or hostname { |
