internal static async Task <IEnumerable <Guid> > ScanNetworksAsync(Base.WlanNotificationClient client, TimeSpan timeout, CancellationToken cancellationToken) { if (timeout <= TimeSpan.Zero) { throw new ArgumentException(nameof(timeout)); } using (var container = new DisposableContainer <Base.WlanNotificationClient>(client)) { var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle); var interfaceIds = interfaceInfoList.Select(x => x.InterfaceGuid).ToArray(); var tcs = new TaskCompletionSource <bool>(); var counter = new ScanCounter(() => Task.Run(() => tcs.TrySetResult(true)), interfaceIds); container.Content.NotificationReceived += (sender, data) => { switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode) { case WLAN_NOTIFICATION_ACM.wlan_notification_acm_scan_complete: counter.SetSuccess(data.InterfaceGuid); break; case WLAN_NOTIFICATION_ACM.wlan_notification_acm_scan_fail: counter.SetFailure(data.InterfaceGuid); break; } }; foreach (var interfaceId in interfaceIds) { var result = Base.Scan(container.Content.Handle, interfaceId); if (!result) { counter.SetFailure(interfaceId); } } var scanTask = tcs.Task; await Task.WhenAny(scanTask, Task.Delay(timeout, cancellationToken)); return(counter.Results); } }
internal static async Task <bool> DisconnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, TimeSpan timeout, CancellationToken cancellationToken) { if (interfaceId == Guid.Empty) { throw new ArgumentException(nameof(interfaceId)); } if (timeout <= TimeSpan.Zero) { throw new ArgumentException(nameof(timeout)); } using (var container = new DisposableContainer <Base.WlanNotificationClient>(client)) { var tcs = new TaskCompletionSource <bool>(); container.Content.NotificationReceived += (sender, data) => { if (data.InterfaceGuid != interfaceId) { return; } switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode) { case WLAN_NOTIFICATION_ACM.wlan_notification_acm_disconnected: Task.Run(() => tcs.TrySetResult(true)); break; } }; var result = Base.Disconnect(container.Content.Handle, interfaceId); if (!result) { tcs.SetResult(false); } var disconnectTask = tcs.Task; var completedTask = await Task.WhenAny(disconnectTask, Task.Delay(timeout, cancellationToken)); return((completedTask == disconnectTask) && disconnectTask.Result); } }
internal static async Task <bool> ConnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, string profileName, BssType bssType, TimeSpan timeout, CancellationToken cancellationToken) { if (interfaceId == Guid.Empty) { throw new ArgumentException(nameof(interfaceId)); } if (string.IsNullOrWhiteSpace(profileName)) { throw new ArgumentNullException(nameof(profileName)); } if (timeout <= TimeSpan.Zero) { throw new ArgumentException(nameof(timeout)); } using (var container = new DisposableContainer <Base.WlanNotificationClient>(client)) { var tcs = new TaskCompletionSource <bool>(); container.Content.NotificationReceived += (sender, data) => { if (data.InterfaceGuid != interfaceId) { return; } switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode) { case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_complete: case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_attempt_fail: break; default: return; } var connectionNotificationData = Marshal.PtrToStructure <WLAN_CONNECTION_NOTIFICATION_DATA>(data.pData); if (connectionNotificationData.strProfileName != profileName) { return; } switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode) { case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_complete: Task.Run(() => tcs.TrySetResult(true)); break; case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_attempt_fail: // This notification will not always mean that a connection has failed. // A connection consists of one or more connection attempts and this notification // may be received zero or more times before the connection completes. Task.Run(() => tcs.TrySetResult(false)); break; } }; var result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)); if (!result) { tcs.SetResult(false); } var connectTask = tcs.Task; var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken)); return((completedTask == connectTask) && connectTask.Result); } }
/// <summary> /// Constructor /// </summary> public NativeWifiPlayer() { _client = new Base.WlanNotificationClient(); _client.NotificationReceived += OnNotificationReceived; }