NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Debug/SROptions.Download.cs

182 lines
6.4 KiB
C#
Raw Normal View History

2024-12-11 19:52:38 +08:00
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Gameplay;
using UnityEngine;
using UnityEngine.Networking;
public partial class SROptions
{
private readonly double timeOutTick = 10f; // 10秒没下完超时
/// <summary>
/// 最大间隔
/// </summary>
private const float MaxIntervals = 600f;
private float intervals = 600f;
private float timer = 0f;
/// <summary>
/// 减少最大间隔时间次数
/// </summary>
private int count = 0;
2024-12-12 12:23:25 +08:00
/// <summary>
/// 总下载次数
/// </summary>
private int total;
private StreamWriter writerUnityWebRequest;
private StreamWriter writerHttpClient;
2024-12-11 19:52:38 +08:00
private bool isDownload = false;
private string downLoadUrl = "http://1.14.62.163/Build/Dev/Android/0.1.0/BuildReport_DefaultPackage_638683185541237500.json";
[Category("下载测试"), DisplayName("开始下载")]
public async void BeginDownload()
{
2024-12-12 12:23:25 +08:00
#if UNITY_EDITOR
string fileUnityWebRequestPath = Path.Combine(Application.streamingAssetsPath, "UnityWebRequestTest.txt");
string fileHttpClientPath = Path.Combine(Application.streamingAssetsPath, "HttpClientTest.txt");
#else
string fileUnityWebRequestPath = Path.Combine(Application.persistentDataPath, "UnityWebRequestTest.txt");
string fileHttpClientPath = Path.Combine(Application.persistentDataPath, "HttpClientTest.txt");
#endif
writerUnityWebRequest = File.CreateText(fileUnityWebRequestPath);
writerHttpClient = File.CreateText(fileHttpClientPath);
2024-12-11 19:52:38 +08:00
timer = intervals;
2024-12-12 12:23:25 +08:00
total = 0;
2024-12-11 19:52:38 +08:00
isDownload = true;
while (isDownload)
{
if (timer >= intervals)
{
timer -= intervals;
2024-12-12 12:23:25 +08:00
++total;
2024-12-11 19:52:38 +08:00
DownloadByUnityWebRequest();
DownloadByUnityHttpClient();
}
await UniTask.WaitForSeconds(0.1f);
timer += 0.1f;
}
}
[Category("下载测试"), DisplayName("停止下载")]
public void StopDownload()
{
isDownload = false;
2024-12-12 12:23:25 +08:00
writerUnityWebRequest.Flush();
2024-12-11 20:02:54 +08:00
writerUnityWebRequest.Close();
2024-12-11 19:52:38 +08:00
2024-12-12 12:23:25 +08:00
writerHttpClient.Flush();
2024-12-11 20:02:54 +08:00
writerHttpClient.Close();
2024-12-11 19:52:38 +08:00
}
/// <summary>
/// 通过WebRequest下载
/// </summary>
private async void DownloadByUnityWebRequest()
{
string log = string.Empty;
DateTime startTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
DateTime endTime;
using UnityWebRequest request = UnityWebRequest.Get(downLoadUrl);
UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
while (!asyncOperation.isDone)
{
await UniTask.DelayFrame(1);
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
TimeSpan timeSpan = endTime - startTime;
if (timeSpan.TotalSeconds > timeOutTick)
{
request.Abort();
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(UnityWebRequest)}, {timeOutTick}s超时URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
2024-12-11 19:52:38 +08:00
intervals = 20f;
count = 3;
return;
}
}
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
if (request.result != UnityWebRequest.Result.Success)
{
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(UnityWebRequest)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false}URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
2024-12-11 19:52:38 +08:00
intervals = 20f;
count = 3;
return;
}
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(UnityWebRequest)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true}URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
2024-12-11 19:52:38 +08:00
intervals = MaxIntervals;
--count;
if (count <= 0)
intervals = MaxIntervals;
}
/// <summary>
/// 通过HttpClient下载
/// </summary>
private async void DownloadByUnityHttpClient()
{
string log = string.Empty;
DateTime startTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
DateTime endTime;
CancellationTokenSource cancellationTokenSource = new();
using HttpClient client = new();
Task<HttpResponseMessage> response = client.GetAsync(downLoadUrl, cancellationTokenSource.Token);
while (!response.IsCompleted)
{
await UniTask.DelayFrame(1);
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
TimeSpan timeSpan = endTime - startTime;
if (timeSpan.TotalSeconds > timeOutTick)
{
cancellationTokenSource.Cancel();
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(HttpClient)}, {timeOutTick}s超时URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
2024-12-11 19:52:38 +08:00
intervals = 20f;
count = 3;
return;
}
}
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
if (!response.IsCompletedSuccessfully)
{
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(HttpClient)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false}URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
2024-12-11 19:52:38 +08:00
intervals = 20f;
count = 3;
return;
}
2024-12-12 12:23:25 +08:00
log = $"{total},类型:{nameof(HttpClient)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true}URL:{downLoadUrl}";
2024-12-11 19:52:38 +08:00
Debug.Log(log);
2024-12-12 12:23:25 +08:00
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
2024-12-11 19:52:38 +08:00
--count;
if (count <= 0)
intervals = MaxIntervals;
}
}