SEBWIN-788: Improved network adapter implementation.

This commit is contained in:
Damian Büchel 2024-06-10 19:39:58 +02:00
parent 04dce13d86
commit 471e69d460

View file

@ -255,7 +255,7 @@ namespace SafeExamBrowser.SystemComponents.Network
private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{ {
logger.Debug($"Network availability changed ({(e.IsAvailable ? "available" : "unavailable")}."); logger.Debug($"Network availability changed ({(e.IsAvailable ? "available" : "unavailable")}).");
Update(); Update();
} }
@ -317,41 +317,45 @@ namespace SafeExamBrowser.SystemComponents.Network
{ {
try try
{ {
lock (@lock) var currentNetwork = default(WirelessNetwork);
var hasConnection = nativeMethods.HasInternetConnection();
var isConnecting = Status == ConnectionStatus.Connecting;
var networks = new List<WirelessNetwork>();
var previousStatus = Status;
if (HasWirelessAdapter)
{ {
var hasConnection = nativeMethods.HasInternetConnection(); hasConnection &= TryGetCurrentWirelessNetwork(out var current);
var isConnecting = Status == ConnectionStatus.Connecting;
var previousStatus = Status;
wirelessNetworks.Clear(); foreach (var network in adapter.NetworkReport.AvailableNetworks.FilterAndOrder())
if (HasWirelessAdapter)
{ {
hasConnection &= TryGetCurrentWirelessNetwork(out var currentNetwork); var wirelessNetwork = network.ToWirelessNetwork();
foreach (var network in adapter.NetworkReport.AvailableNetworks.FilterAndOrder()) if (network.Ssid == current)
{ {
var wirelessNetwork = network.ToWirelessNetwork(); currentNetwork = wirelessNetwork;
wirelessNetwork.Status = ConnectionStatus.Connected;
if (network.Ssid == currentNetwork)
{
wirelessNetwork.Status = ConnectionStatus.Connected;
}
wirelessNetworks.Add(wirelessNetwork);
} }
if (rescan) networks.Add(wirelessNetwork);
{
_ = adapter.ScanAsync();
}
} }
Type = HasWirelessAdapter ? ConnectionType.Wireless : (hasConnection ? ConnectionType.Wired : ConnectionType.Undefined); if (rescan)
Status = hasConnection ? ConnectionStatus.Connected : (isConnecting ? ConnectionStatus.Connecting : ConnectionStatus.Disconnected); {
_ = adapter.ScanAsync();
LogNetworkChanges(previousStatus, wirelessNetworks.FirstOrDefault(n => n.Status == ConnectionStatus.Connected)); }
} }
lock (@lock)
{
wirelessNetworks.Clear();
wirelessNetworks.AddRange(networks);
}
Type = HasWirelessAdapter ? ConnectionType.Wireless : (hasConnection ? ConnectionType.Wired : ConnectionType.Undefined);
Status = hasConnection ? ConnectionStatus.Connected : (isConnecting ? ConnectionStatus.Connecting : ConnectionStatus.Disconnected);
LogNetworkChanges(previousStatus, currentNetwork);
} }
catch (Exception e) catch (Exception e)
{ {