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秒没下完超时
///
/// 最大间隔
///
private const float MaxIntervals = 600f;
private float intervals = 600f;
private float timer = 0f;
///
/// 减少最大间隔时间次数
///
private int count = 0;
///
/// 总下载次数
///
private int total;
private StreamWriter writerUnityWebRequest;
private StreamWriter writerHttpClient;
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()
{
#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);
timer = intervals;
total = 0;
isDownload = true;
while (isDownload)
{
if (timer >= intervals)
{
timer -= intervals;
++total;
DownloadByUnityWebRequest();
DownloadByUnityHttpClient();
}
await UniTask.WaitForSeconds(0.1f);
timer += 0.1f;
}
}
[Category("下载测试"), DisplayName("停止下载")]
public void StopDownload()
{
isDownload = false;
writerUnityWebRequest.Flush();
writerUnityWebRequest.Close();
writerHttpClient.Flush();
writerHttpClient.Close();
}
///
/// 通过WebRequest下载
///
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();
log = $"{total},类型:{nameof(UnityWebRequest)}, {timeOutTick}s超时,URL:{downLoadUrl}";
Debug.Log(log);
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
intervals = 20f;
count = 3;
return;
}
}
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
if (request.result != UnityWebRequest.Result.Success)
{
log = $"{total},类型:{nameof(UnityWebRequest)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false},URL:{downLoadUrl}";
Debug.Log(log);
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
intervals = 20f;
count = 3;
return;
}
log = $"{total},类型:{nameof(UnityWebRequest)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true},URL:{downLoadUrl}";
Debug.Log(log);
writerUnityWebRequest.WriteLine(log);
writerUnityWebRequest.Flush();
intervals = MaxIntervals;
--count;
if (count <= 0)
intervals = MaxIntervals;
}
///
/// 通过HttpClient下载
///
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 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();
log = $"{total},类型:{nameof(HttpClient)}, {timeOutTick}s超时,URL:{downLoadUrl}";
Debug.Log(log);
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
intervals = 20f;
count = 3;
return;
}
}
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
if (!response.IsCompletedSuccessfully)
{
log = $"{total},类型:{nameof(HttpClient)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{false},URL:{downLoadUrl}";
Debug.Log(log);
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
intervals = 20f;
count = 3;
return;
}
log = $"{total},类型:{nameof(HttpClient)}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,是否成功:{true},URL:{downLoadUrl}";
Debug.Log(log);
writerHttpClient.WriteLine(log);
writerHttpClient.Flush();
--count;
if (count <= 0)
intervals = MaxIntervals;
}
}