From 2c3b89032961da4be7042d1a5bd2b3e7404bb4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 17 Sep 2025 15:39:47 +0800 Subject: [PATCH] feat #642 --- .../DefaultCacheFileSystem.cs | 10 +++++ .../internal/UnityDownloadFileOperation.cs | 40 +++++++++++++++++++ .../UnityDownloadLocalFileOperation.cs | 4 +- .../UnityDownloadNormalFileOperation.cs | 2 + .../UnityDownloadResumeFileOperation.cs | 2 + .../FileSystem/FileSystemParametersDefine.cs | 1 + 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index f7642969..6681b28a 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -96,6 +96,11 @@ namespace YooAsset /// public int DownloadMaxRequestPerFrame { private set; get; } = int.MaxValue; + /// + /// 自定义参数:下载任务的看门狗机制监控时间 + /// + public int DownloadWatchDogTime { private set; get; } = int.MaxValue; + /// /// 自定义参数:启用断点续传的最小尺寸 /// @@ -252,6 +257,11 @@ namespace YooAsset int convertValue = Convert.ToInt32(value); DownloadMaxRequestPerFrame = Mathf.Clamp(convertValue, 1, int.MaxValue); } + else if (name == FileSystemParametersDefine.DOWNLOAD_WATCH_DOG_TIME) + { + int convertValue = Convert.ToInt32(value); + DownloadWatchDogTime = Mathf.Clamp(convertValue, 1, int.MaxValue); + } else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_MINMUM_SIZE) { ResumeDownloadMinimumSize = Convert.ToInt64(value); diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadFileOperation.cs index 0f2279ad..80ad5a9e 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadFileOperation.cs @@ -17,6 +17,11 @@ namespace YooAsset protected readonly PackageBundle _bundle; protected readonly string _tempFilePath; + private bool _watchDogInit = false; + private bool _watchDogAborted = false; + private ulong _lastDownloadBytes; + private double _lastGetDataTime; + /// /// 引用计数 /// @@ -33,6 +38,41 @@ namespace YooAsset return $"RefCount : {RefCount}"; } + /// + /// 更新看门狗监测 + /// 说明:监控时间范围内,如果没有接收到任何下载数据,那么直接终止任务! + /// + protected void UpdateWatchDog() + { + if (_fileSystem.DownloadWatchDogTime == int.MaxValue) + return; + + if (_watchDogAborted) + return; + + if (_watchDogInit == false) + { + _watchDogInit = true; + _lastDownloadBytes = 0; + _lastGetDataTime = UnityEngine.Time.realtimeSinceStartupAsDouble; + } + + if (_webRequest.downloadedBytes != _lastDownloadBytes) + { + _lastDownloadBytes = _webRequest.downloadedBytes; + _lastGetDataTime = UnityEngine.Time.realtimeSinceStartupAsDouble; + } + else + { + double deltaTime = UnityEngine.Time.realtimeSinceStartupAsDouble - _lastGetDataTime; + if (deltaTime > _fileSystem.DownloadWatchDogTime) + { + _watchDogAborted = true; + InternalAbort(); //终止网络请求 + } + } + } + /// /// 减少引用计数 /// diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadLocalFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadLocalFileOperation.cs index 5600ad70..b3961ec7 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadLocalFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadLocalFileOperation.cs @@ -4,7 +4,7 @@ using UnityEngine.Networking; namespace YooAsset { - internal class UnityDownloadLocalFileOperation : UnityDownloadFileOperation + internal sealed class UnityDownloadLocalFileOperation : UnityDownloadFileOperation { private VerifyTempFileOperation _verifyOperation; private ESteps _steps = ESteps.None; @@ -42,6 +42,8 @@ namespace YooAsset DownloadProgress = _webRequest.downloadProgress; DownloadedBytes = (long)_webRequest.downloadedBytes; Progress = DownloadProgress; + + UpdateWatchDog(); if (_webRequest.isDone == false) return; diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadNormalFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadNormalFileOperation.cs index ce883107..d2ded35b 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadNormalFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadNormalFileOperation.cs @@ -39,6 +39,8 @@ namespace YooAsset DownloadProgress = _webRequest.downloadProgress; DownloadedBytes = (long)_webRequest.downloadedBytes; Progress = DownloadProgress; + + UpdateWatchDog(); if (_webRequest.isDone == false) return; diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadResumeFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadResumeFileOperation.cs index a7748172..7a0cf47f 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadResumeFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/UnityDownloadResumeFileOperation.cs @@ -56,6 +56,8 @@ namespace YooAsset DownloadProgress = _webRequest.downloadProgress; DownloadedBytes = _fileOriginLength + (long)_webRequest.downloadedBytes; Progress = DownloadProgress; + + UpdateWatchDog(); if (_webRequest.isDone == false) return; diff --git a/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs index 90ac40a2..0f4829ab 100644 --- a/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs +++ b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs @@ -15,6 +15,7 @@ namespace YooAsset public const string DISABLE_ONDEMAND_DOWNLOAD = "DISABLE_ONDEMAND_DOWNLOAD"; public const string DOWNLOAD_MAX_CONCURRENCY = "DOWNLOAD_MAX_CONCURRENCY"; public const string DOWNLOAD_MAX_REQUEST_PER_FRAME = "DOWNLOAD_MAX_REQUEST_PER_FRAME"; + public const string DOWNLOAD_WATCH_DOG_TIME = "DOWNLOAD_WATCH_DOG_TIME"; public const string RESUME_DOWNLOAD_MINMUM_SIZE = "RESUME_DOWNLOAD_MINMUM_SIZE"; public const string RESUME_DOWNLOAD_RESPONSE_CODES = "RESUME_DOWNLOAD_RESPONSE_CODES"; public const string VIRTUAL_WEBGL_MODE = "VIRTUAL_WEBGL_MODE";