diff --git a/ylgamehall/Source/Resource/LobbyZipUpgrader.swift b/ylgamehall/Source/Resource/LobbyZipUpgrader.swift index cefb417..4a215eb 100644 --- a/ylgamehall/Source/Resource/LobbyZipUpgrader.swift +++ b/ylgamehall/Source/Resource/LobbyZipUpgrader.swift @@ -28,18 +28,17 @@ public actor LobbyZipUpgrader { case stagingMoveFailed(any Error) } - private let session: URLSession + public init() {} - public init(session: URLSession = LobbyZipUpgrader.defaultSession) { - self.session = session - } - - /// 默认 URLSession:下载用 60s 超时(zip 大约 11 MB,4G 网络约 10s)。 - nonisolated public static var defaultSession: URLSession { + /// URLSession 配置:60s 单请求 / 300s 总体(zip 约 11 MB,4G 约 10s)。 + /// 每次 upgradeIfNeeded 用此 config 临时建 session,绑 download delegate 接收 + /// didWriteData 进度回调;ad-hoc delegate(`session.download(from:delegate:)`) + /// 不会触发 URLSessionDownloadDelegate 的 download-specific 方法,必须走 session 级。 + nonisolated private static var defaultConfig: URLSessionConfiguration { let cfg = URLSessionConfiguration.default cfg.timeoutIntervalForRequest = 60 cfg.timeoutIntervalForResource = 300 - return URLSession(configuration: cfg) + return cfg } /// 比较远端 game_version 与本地 version.xml。需要升级则下载 + 解压 + 原子 rename。 @@ -61,15 +60,23 @@ public actor LobbyZipUpgrader { return .noop } - // 1. 下载到 tmp(URLSession 默认管理临时位置),delegate 上报 0…1 进度 + // 1. 下载到 tmp(URLSession 默认管理临时位置),delegate 上报 0…1 进度。 + // delegate 必须在 URLSession init 时绑定,否则 didWriteData 不会被调用 + // (async API `download(from:delegate:)` 的 ad-hoc delegate 只收 task-level 回调)。 let progressDelegate = DownloadProgressDelegate(onProgress: onProgress) + let opQueue = OperationQueue() + opQueue.maxConcurrentOperationCount = 1 + let session = URLSession( + configuration: Self.defaultConfig, + delegate: progressDelegate, + delegateQueue: opQueue + ) + defer { session.invalidateAndCancel() } + let downloadedURL: URL let response: URLResponse do { - (downloadedURL, response) = try await session.download( - from: zipURL, - delegate: progressDelegate - ) + (downloadedURL, response) = try await session.download(from: zipURL) } catch { throw UpgradeError.downloadFailed(error) } diff --git a/ylgamehall/Source/Resource/SubGameDownloader.swift b/ylgamehall/Source/Resource/SubGameDownloader.swift index 27311a2..68c662e 100644 --- a/ylgamehall/Source/Resource/SubGameDownloader.swift +++ b/ylgamehall/Source/Resource/SubGameDownloader.swift @@ -36,17 +36,17 @@ public actor SubGameDownloader { case stagingMoveFailed(any Error) } - private let session: URLSession + public init() {} - public init(session: URLSession = SubGameDownloader.defaultSession) { - self.session = session - } - - nonisolated public static var defaultSession: URLSession { + /// URLSession 配置:60s 单请求 / 300s 总体(zip 体量约 1-10 MB)。 + /// 每次 ensureReady 用此 config 临时建 session,绑 download delegate 接收 + /// didWriteData 进度回调;ad-hoc delegate(`session.download(from:delegate:)`) + /// 不会触发 URLSessionDownloadDelegate 的 download-specific 方法,必须走 session 级。 + nonisolated private static var defaultConfig: URLSessionConfiguration { let cfg = URLSessionConfiguration.default cfg.timeoutIntervalForRequest = 60 cfg.timeoutIntervalForResource = 300 - return URLSession(configuration: cfg) + return cfg } /// 确保 `{Caches}/{request.gameDir}/{request.gameStart}/index.html` 已就绪。 @@ -72,15 +72,23 @@ public actor SubGameDownloader { throw DownloadError.badDownloadURL(request.downloadURL) } - // 1. 下载到 tmp + // 1. 下载到 tmp。delegate 必须在 URLSession init 时绑定,否则 + // URLSessionDownloadDelegate.didWriteData 不会被调用(async API + // `download(from:delegate:)` 的 ad-hoc delegate 只收 task-level 回调)。 let progressDelegate = DownloadProgressDelegate(onProgress: onProgress) + let opQueue = OperationQueue() + opQueue.maxConcurrentOperationCount = 1 + let session = URLSession( + configuration: Self.defaultConfig, + delegate: progressDelegate, + delegateQueue: opQueue + ) + defer { session.invalidateAndCancel() } + let downloadedURL: URL let response: URLResponse do { - (downloadedURL, response) = try await session.download( - from: zipURL, - delegate: progressDelegate - ) + (downloadedURL, response) = try await session.download(from: zipURL) } catch { throw DownloadError.downloadFailed(error) }