Optimize file download

pull/3234/head
2dust 2 years ago
parent 7eb869ab1d
commit ec59249d79

@ -13,6 +13,62 @@ namespace v2rayN.Base
{
}
public async Task<string> DownloadStringAsync(IWebProxy webProxy, string url, string? userAgent, int timeout)
{
if (string.IsNullOrEmpty(url))
{
return null;
}
var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(timeout * 1000);
Uri uri = new Uri(url);
//Authorization Header
var headers = new WebHeaderCollection();
if (!Utils.IsNullOrEmpty(uri.UserInfo))
{
headers.Add(HttpRequestHeader.Authorization, "Basic " + Utils.Base64Encode(uri.UserInfo));
}
var downloadOpt = new DownloadConfiguration()
{
Timeout = timeout * 1000,
MaxTryAgainOnFailover = 2,
RequestConfiguration =
{
Headers = headers,
UserAgent = userAgent,
Timeout = timeout * 1000,
Proxy = webProxy
}
};
string text = string.Empty;
using (var downloader = new DownloadService(downloadOpt))
{
downloader.DownloadFileCompleted += (sender, value) =>
{
if (value.Error != null)
{
throw new Exception(string.Format("{0}", value.Error.Message));
}
};
using (var stream = await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token))
{
using (StreamReader reader = new StreamReader(stream))
{
text = reader.ReadToEnd();
}
}
}
downloadOpt = null;
return text;
}
public async Task DownloadDataAsync4Speed(IWebProxy webProxy, string url, IProgress<string> progress, int timeout)
{
if (string.IsNullOrEmpty(url))
@ -38,52 +94,45 @@ namespace v2rayN.Base
int totalSecond = 0;
var hasValue = false;
double maxSpeed = 0;
var downloader = new DownloadService(downloadOpt);
//downloader.DownloadStarted += (sender, value) =>
//{
// if (progress != null)
// {
// progress.Report("Start download data...");
// }
//};
downloader.DownloadProgressChanged += (sender, value) =>
using (var downloader = new DownloadService(downloadOpt))
{
TimeSpan ts = (DateTime.Now - totalDatetime);
if (progress != null && ts.Seconds > totalSecond)
//downloader.DownloadStarted += (sender, value) =>
//{
// if (progress != null)
// {
// progress.Report("Start download data...");
// }
//};
downloader.DownloadProgressChanged += (sender, value) =>
{
hasValue = true;
totalSecond = ts.Seconds;
if (value.BytesPerSecondSpeed > maxSpeed)
TimeSpan ts = (DateTime.Now - totalDatetime);
if (progress != null && ts.Seconds > totalSecond)
{
maxSpeed = value.BytesPerSecondSpeed;
var speed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(speed);
hasValue = true;
totalSecond = ts.Seconds;
if (value.BytesPerSecondSpeed > maxSpeed)
{
maxSpeed = value.BytesPerSecondSpeed;
var speed = (maxSpeed / 1000 / 1000).ToString("#0.0");
progress.Report(speed);
}
}
}
};
downloader.DownloadFileCompleted += (sender, value) =>
{
if (progress != null)
};
downloader.DownloadFileCompleted += (sender, value) =>
{
if (!hasValue && value.Error != null)
if (progress != null)
{
progress.Report(value.Error?.Message);
if (!hasValue && value.Error != null)
{
progress.Report(value.Error?.Message);
}
}
}
};
progress.Report("......");
await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token);
//var stream = await downloader.DownloadFileTaskAsync(url);
};
progress.Report("......");
//using (StreamReader reader = new StreamReader(stream))
//{
// string text = reader.ReadToEnd();
// stream.Dispose();
//}
await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token);
}
downloader.Dispose();
downloader = null;
downloadOpt = null;
}
@ -118,39 +167,39 @@ namespace v2rayN.Base
var progressPercentage = 0;
var hasValue = false;
var downloader = new DownloadService(downloadOpt);
downloader.DownloadStarted += (sender, value) =>
using (var downloader = new DownloadService(downloadOpt))
{
if (progress != null)
downloader.DownloadStarted += (sender, value) =>
{
progress.Report(0);
}
};
downloader.DownloadProgressChanged += (sender, value) =>
{
hasValue = true;
var percent = (int)value.ProgressPercentage;// Convert.ToInt32((totalRead * 1d) / (total * 1d) * 100);
if (progressPercentage != percent && percent % 10 == 0)
if (progress != null)
{
progress.Report(0);
}
};
downloader.DownloadProgressChanged += (sender, value) =>
{
progressPercentage = percent;
progress.Report(percent);
}
};
downloader.DownloadFileCompleted += (sender, value) =>
{
if (progress != null)
hasValue = true;
var percent = (int)value.ProgressPercentage;// Convert.ToInt32((totalRead * 1d) / (total * 1d) * 100);
if (progressPercentage != percent && percent % 10 == 0)
{
progressPercentage = percent;
progress.Report(percent);
}
};
downloader.DownloadFileCompleted += (sender, value) =>
{
if (hasValue && value.Error == null)
if (progress != null)
{
progress.Report(101);
if (hasValue && value.Error == null)
{
progress.Report(101);
}
}
}
};
};
await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token);
await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token);
}
downloader.Dispose();
downloader = null;
downloadOpt = null;
}
}

@ -2,7 +2,6 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using v2rayN.Base;
using v2rayN.Resx;
@ -80,7 +79,8 @@ namespace v2rayN.Handler
}
};
_ = DownloaderHelper.Instance.DownloadFileAsync(GetWebProxy(blProxy),
var webProxy = GetWebProxy(blProxy);
_ = DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url,
Utils.GetTempPath(Utils.GetDownloadFileName(url)),
progress,
@ -129,28 +129,9 @@ namespace v2rayN.Handler
try
{
Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13);
var client = new HttpClient(new SocketsHttpHandler()
{
Proxy = GetWebProxy(blProxy)
});
if (Utils.IsNullOrEmpty(userAgent))
{
userAgent = $"{Utils.GetVersion(false)}";
}
client.DefaultRequestHeaders.UserAgent.TryParseAdd(userAgent);
Uri uri = new Uri(url);
//Authorization Header
if (!Utils.IsNullOrEmpty(uri.UserInfo))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Utils.Base64Encode(uri.UserInfo));
}
var cts = new CancellationTokenSource();
cts.CancelAfter(1000 * 30);
var result = await HttpClientHelper.GetInstance().GetAsync(client, url, cts.Token);
var webProxy = GetWebProxy(blProxy);
var result = await DownloaderHelper.Instance.DownloadStringAsync(webProxy, url, userAgent, 30);
return result;
}
catch (Exception ex)

Loading…
Cancel
Save