Пример #1
0
        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);
            }
        }
Пример #2
0
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)));
            }
        }