Skip to content

Commit

Permalink
Improve network interface selection and error handling
Browse files Browse the repository at this point in the history
- Return single active network interface directly if only one is found.
- Enhance error handling to log exceptions and fallback to a valid interface.
- Remove redundant code for obtaining IPv4 statistics.
- Simplify code by removing conditional block for download speed display.
  • Loading branch information
BoiHanny committed Nov 30, 2024
1 parent 5114b0c commit 0d0805c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions vrcosc-magicchatbox/Classes/Modules/NetworkStatisticsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ private NetworkInterface GetActiveNetworkInterface()
if (networkInterfaces.Count == 0)
return null;

// If there's only one active network interface, return it directly
if (networkInterfaces.Count == 1)
{
return networkInterfaces.First();
}

// Measure initial bytes sent/received
var interfaceStats = new List<InterfaceStats>();
foreach (var ni in networkInterfaces)
Expand Down Expand Up @@ -164,14 +170,20 @@ private NetworkInterface GetActiveNetworkInterface()
}
else
{
// If no activity detected, return the first interface
// If no activity detected or measurement fails, fallback to selecting the first interface
return networkInterfaces.FirstOrDefault();
}
}
catch (Exception ex)
{
// Log the exception and fallback to the first available network interface
Logging.WriteException(ex, MSGBox: false);
return null;
return NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(ni =>
ni.OperationalStatus == OperationalStatus.Up &&
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&
ni.GetIPProperties().UnicastAddresses.Count > 0);
}
}

Expand Down Expand Up @@ -223,8 +235,6 @@ private void OnTimedEvent(object state)
MaxUploadSpeedMbps = 0;
}



var stats = _activeNetworkInterface.GetIPv4Statistics();

// Calculate the differences since the last check
Expand Down Expand Up @@ -296,8 +306,6 @@ public string GenerateDescription()
// List to store individual network stats descriptions
var networkStatsDescriptions = new List<string>();



if (ViewModel.Instance.NetworkStats_ShowCurrentDown)
networkStatsDescriptions.Add($"{ConvertToSuperScriptIfNeeded("Down: ")} {FormatSpeed(CurrentDownloadSpeedMbps)}");

Expand Down

0 comments on commit 0d0805c

Please sign in to comment.