Example #1
0
        /// <summary>
        /// Reads the file from the server url.
        /// In case of errors reading the file from the server, returned <see cref="T:Coding4Fun.VisualStudio.RemoteControl.GetFileResult" /> object's
        /// IsSuccessStatusCode value will be false.
        /// </summary>
        /// <returns>Information about the file obtained from the server</returns>
        async Task <GetFileResult> IRemoteControlHTTPRequestor.GetFileFromServerAsync()
        {
            GetFileResult result = new GetFileResult
            {
                Code = HttpStatusCode.Unused
            };

            try
            {
                int i = 0;
                while (true)
                {
                    if (i >= 2)
                    {
                        return(result);
                    }
                    if (i > 0)
                    {
                        int num = Math.Min(32, (int)Math.Pow(2.0, i));
                        try
                        {
                            await Task.Delay(num * 1000, cancellationTokenSource.Token).ConfigureAwait(false);
                        }
                        catch (OperationCanceledException)
                        {
                            return(result);
                        }
                        catch (ObjectDisposedException)
                        {
                            return(result);
                        }
                        result.Dispose();
                    }
                    result = await GetFile(url, httpRequestTimeoutMillis, ServerRevalidatePolicy).ConfigureAwait(false);

                    if (result.IsSuccessStatusCode)
                    {
                        break;
                    }
                    i++;
                }
                return(result);
            }
            finally
            {
                if (!result.IsSuccessStatusCode && Platform.IsWindows)
                {
                    WinINetHelper.WriteErrorResponseToCache(errorMarkerFileUrl, result.Code);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Reads the settings file based on the <paramref name="staleBehavior" /> specified. This is the Async version
        /// of ReadFile method.
        /// </summary>
        /// <param name="staleBehavior">See <see cref="T:Coding4Fun.VisualStudio.RemoteControl.BehaviorOnStale" /> for details about each possible setting.
        /// In most cases use the BehaviorOnStale.ReturnStale setting.</param>
        /// <returns>A Stream that can be used to read the setting file. !Callers must call Dispose on this stream
        /// object returned. Or Null is returned in case of failure to get the file (or if server returned
        /// NotFound).</returns>
        public async Task <Stream> ReadFileAsync(BehaviorOnStale staleBehavior)
        {
            if (isDisabled)
            {
                return(null);
            }
            if (isDisposed)
            {
                throw new ObjectDisposedException("RemoteControlClient");
            }
            if (Uri.IsLocalFile)
            {
                return((await ReadFileFromLocalAsync().ConfigureAwait(false)).RespStream);
            }
            switch (staleBehavior)
            {
            case BehaviorOnStale.ReturnStale:
                return((await GetFileAndInstrumentAsync().ConfigureAwait(false)).RespStream);

            case BehaviorOnStale.ReturnNull:
            {
                GetFileResult getFileResult = await GetFileAndInstrumentAsync().ConfigureAwait(false);

                if (IsStale(getFileResult))
                {
                    return(getFileResult.RespStream);
                }
                getFileResult.Dispose();
                return(null);
            }

            case BehaviorOnStale.ForceDownload:
                return((await GetFileAndInstrumentAsync(true).ConfigureAwait(false)).RespStream);

            default:
                return(null);
            }
        }