Beispiel #1
0
        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 interfaceIds = Base.GetInterfaceInfoList(container.Content.Handle)
                                   .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);
                    }
                }

                using (cancellationToken.Register(() => tcs.TrySetCanceled()))
                {
                    var scanTask = tcs.Task;
                    await Task.WhenAny(scanTask, Task.Delay(timeout, cancellationToken));

                    return(counter.Results);
                }
            }
        }
Beispiel #2
0
        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);
                }

                using (cancellationToken.Register(() => tcs.TrySetCanceled()))
                {
                    var disconnectTask = tcs.Task;
                    var completedTask  = await Task.WhenAny(disconnectTask, Task.Delay(timeout, cancellationToken));

                    return((completedTask == disconnectTask) && disconnectTask.IsCompleted && disconnectTask.Result);
                }
            }
        }
        internal static async Task <bool> ConnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, string profileName, BssType bssType, PhysicalAddress bssid, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }
            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "The timeout duration must be positive.");
            }

            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;
                }
            };

            bool result;

            if (bssid is not null)
            {
                var dot11MacAddress = new DOT11_MAC_ADDRESS {
                    ucDot11MacAddress = bssid.GetAddressBytes()
                };
                result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType), dot11MacAddress);
            }
            else
            {
                result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType));
            }
            if (!result)
            {
                tcs.SetResult(false);
            }

            using (cancellationToken.Register(() => tcs.TrySetCanceled()))
            {
                var connectTask   = tcs.Task;
                var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken));

                return((completedTask == connectTask) && connectTask.IsCompleted && connectTask.Result);
            }
        }
 /// <summary>
 /// Constructor
 /// </summary>
 public NativeWifiPlayer()
 {
     _client = new Base.WlanNotificationClient();
     _client.NotificationReceived += OnNotificationReceived;
 }