Last active
May 14, 2021 13:05
-
-
Save glebov21/5dd07b77165986fe9a4f22eb48642edc to your computer and use it in GitHub Desktop.
WebClient with progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AutogrammaLauncher | |
{ | |
using System; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public static class Server | |
{ | |
public delegate void ProgressDelegate(int percents, long bytesReceived, long totalBytesToReceive); | |
public static async Task<Response<string>> GetHashTableAsync() | |
{ | |
return await DownloadStringAsync(Config.ServerHashFileName); | |
} | |
public static async Task<Response<string>> GetVersionAsync() | |
{ | |
return await DownloadStringAsync(Config.ServerVersionFileName); | |
} | |
public static async Task<Response<string>> GetVersionLauncherAsync() | |
{ | |
return await DownloadStringAsync(Config.ServerLauncherVersionFileName); | |
} | |
public static async Task<Response<string>> DownloadFileAsync(string relPath, string saveFilePath, ProgressDelegate onProgress = null) | |
{ | |
return await DownloadAsync(async (webClient) => { | |
await webClient.DownloadFileTaskAsync(new Uri(Config.ServiceUrl + relPath), saveFilePath); | |
return new Response<string>(true, saveFilePath, null); | |
}, onProgress); | |
} | |
public static async Task<Response<byte[]>> DownloadDataAsync(string relPath, ProgressDelegate onProgress = null) | |
{ | |
return await DownloadAsync(async (webClient) => { | |
var output = await webClient.DownloadDataTaskAsync(new Uri(Config.ServiceUrl + relPath)); | |
return new Response<byte[]>(true, output, null); | |
}, onProgress); | |
} | |
public static async Task<Response<string>> DownloadStringAsync(string relPath, ProgressDelegate onProgress = null) | |
{ | |
return await DownloadAsync(async (webClient) => { | |
var output = await webClient.DownloadStringTaskAsync(new Uri(Config.ServiceUrl + relPath)); | |
return new Response<string>(true, output, null); | |
}, onProgress); | |
} | |
private static async Task<Response<T>> DownloadAsync<T>(Func<WebClient, Task<Response<T>>> downloadAction, ProgressDelegate onProgress = null) | |
{ | |
using (var webClient = new WebClient()) | |
{ | |
if (onProgress != null) | |
{ | |
webClient.DownloadProgressChanged += (sender, e) => | |
{ | |
SynchronizationContext.Current.Post((state) => { | |
onProgress.Invoke(e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive); | |
}, null); | |
}; | |
} | |
webClient.Encoding = Encoding.UTF8; | |
try | |
{ | |
return await downloadAction(webClient); | |
} | |
catch (Exception e) | |
{ | |
var ae = e as AggregateException; | |
if (ae != null && ae.InnerExceptions.Count > 0) | |
return new Response<T>(false, default(T), ae.InnerExceptions[0]); | |
return new Response<T>(false, default(T), e); | |
} | |
} | |
} | |
} | |
public struct Response<T> | |
{ | |
public Response(bool isDone, T result, Exception error) | |
{ | |
IsDone = isDone; | |
Result = result; | |
Error = error; | |
} | |
public bool IsDone { get; set; } | |
public T Result { get; set; } | |
public Exception Error { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment