Ejemplo n.º 1
0
        /// <summary>
        ///     Searches for updates, asynchronously.
        /// </summary>
        /// <seealso cref="SearchForUpdates" />
        /// <exception cref="SizeCalculationException" />
        /// <exception cref="OperationCanceledException" />
        public Task <bool> SearchForUpdatesAsync()
        {
            return(TaskEx.Run(async() =>
            {
                // It may be that this is not the first search call and previously saved data needs to be disposed.
                Cleanup();
                _searchCancellationTokenSource?.Dispose();
                _searchCancellationTokenSource = new CancellationTokenSource();

                if (!ConnectionManager.IsConnectionAvailable())
                {
                    return false;
                }

                _searchCancellationTokenSource.Token.ThrowIfCancellationRequested();
                // Check for SSL and ignore it
                ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
                var configuration =
                    await UpdateConfiguration.DownloadAsync(UpdateConfigurationFileUri, HttpAuthenticationCredentials,
                                                            Proxy, _searchCancellationTokenSource, SearchTimeout);

                _searchCancellationTokenSource.Token.ThrowIfCancellationRequested();
                var result = new UpdateResult(configuration, CurrentVersion,
                                              IncludeAlpha, IncludeBeta);
                if (!result.UpdatesFound)
                {
                    return false;
                }

                PackageConfigurations = result.NewestConfigurations;
                double updatePackageSize = 0;
                foreach (var updateConfiguration in PackageConfigurations)
                {
                    updateConfiguration.UpdatePackageUri = ConvertPackageUri(updateConfiguration.UpdatePackageUri);
                    updateConfiguration.UpdatePhpFileUri = ConvertStatisticsUri(updateConfiguration.UpdatePhpFileUri);

                    _searchCancellationTokenSource.Token.ThrowIfCancellationRequested();
                    var newPackageSize = GetUpdatePackageSize(updateConfiguration.UpdatePackageUri);
                    if (newPackageSize == null)
                    {
                        throw new SizeCalculationException(_lp.PackageSizeCalculationExceptionText);
                    }

                    updatePackageSize += newPackageSize.Value;
                    _packageOperations.Add(new UpdateVersion(updateConfiguration.LiteralVersion),
                                           updateConfiguration.Operations);
                }

                TotalSize = updatePackageSize;
                if (!_searchCancellationTokenSource.Token.IsCancellationRequested)
                {
                    return true;
                }
                throw new OperationCanceledException();
            }));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Searches for updates, asynchronously.
        /// </summary>
        /// <seealso cref="SearchForUpdates" />
        public void SearchForUpdatesAsync()
        {
            OnUpdateSearchStarted(this, EventArgs.Empty);

            Task.Factory.StartNew(() =>
            {
                // It may be that this is not the first search call and previously saved data needs to be disposed.
                Cleanup();

                // Reinitialize the cancellation tokens and wait handles
                _searchCancellationTokenSource?.Dispose();
                _searchCancellationTokenSource = new CancellationTokenSource();
                _searchManualResetEvent.Reset();

                if (!ConnectionManager.IsConnectionAvailable())
                {
                    return(false);
                }

                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = (SecurityProtocolType)3072;

                IEnumerable <UpdateConfiguration> configurations = null;
                Exception exception = null;
                UpdateConfiguration.DownloadAsync(UpdateConfigurationFileUri, HttpAuthenticationCredentials,
                                                  Proxy,
                                                  (c, e) =>
                {
                    configurations = c;
                    exception      = e;
                    _searchManualResetEvent.Set();
                }, _searchCancellationTokenSource, SearchTimeout);
                _searchManualResetEvent.WaitOne();

                // Check for cancellation before throwing any errors
                if (_searchCancellationTokenSource.IsCancellationRequested)
                {
                    return(false);
                }
                if (exception != null)
                {
                    throw exception;
                }

                var result = new UpdateResult(configurations, CurrentVersion,
                                              IncludeAlpha, IncludeBeta, Conditions);
                if (!result.UpdatesFound)
                {
                    return(false);
                }

                PackageConfigurations    = result.NewestConfigurations;
                double updatePackageSize = 0;
                foreach (var updateConfiguration in PackageConfigurations)
                {
                    updateConfiguration.UpdatePackageUri = ConvertPackageUri(updateConfiguration.UpdatePackageUri);
                    updateConfiguration.UpdatePhpFileUri = ConvertStatisticsUri(updateConfiguration.UpdatePhpFileUri);

                    if (_searchCancellationTokenSource.IsCancellationRequested)
                    {
                        return(false);
                    }

                    var newPackageSize = GetUpdatePackageSize(updateConfiguration.UpdatePackageUri);
                    if (newPackageSize == null)
                    {
                        throw new SizeCalculationException(_lp.PackageSizeCalculationExceptionText);
                    }

                    updatePackageSize += newPackageSize.Value;
                    if (updateConfiguration.Operations == null)
                    {
                        continue;
                    }
                    if (_packageOperations == null)
                    {
                        _packageOperations = new Dictionary <UpdateVersion, IEnumerable <Operation> >();
                    }
                    _packageOperations.Add(new UpdateVersion(updateConfiguration.LiteralVersion),
                                           updateConfiguration.Operations);
                }

                TotalSize = updatePackageSize;
                return(true);
            }).ContinueWith(SearchTaskCompleted);
        }