Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions native/cocos/network/DownloaderImpl-apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ of this software and associated engine source code (the "Software"), a limited,
#import <Foundation/Foundation.h>
#include "application/ApplicationManager.h"
#include "base/memory/Memory.h"
#include "base/std/container/queue.h"
#include "base/std/container/list.h"
#include "base/UTF8.h"
#include "network/Downloader.h"
#include "platform/FileUtils.h"
Expand All @@ -54,7 +54,8 @@ - (uint32_t)transferDataToBuffer:(void *)buffer lengthOfBuffer:(uint32_t)len;
@interface DownloaderAppleImpl : NSObject <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate> {
const cc::network::DownloaderApple *_outer;
cc::network::DownloaderHints _hints;
ccstd::queue<NSURLSessionTask *> _taskQueue;
ccstd::list<NSURLSessionTask *> _waitingList;
int _runningCount;
}
@property (nonatomic, strong) NSURLSession *downloadSession;
@property (nonatomic, strong) NSMutableDictionary *taskDict; // ocTask: DownloadTaskWrapper
Expand Down Expand Up @@ -203,6 +204,7 @@ - (id)init:(const cc::network::DownloaderApple *)o hints:(const cc::network::Dow
_hints = hints;
// create task dictionary
self.taskDict = [NSMutableDictionary dictionary];
_runningCount = 0;

#if CC_PLATFORM == CC_PLATFORM_IOS
// create backgroundSession for iOS to support background download
Expand Down Expand Up @@ -264,11 +266,11 @@ - (NSURLSessionDataTask *)createDataTask:(std::shared_ptr<const cc::network::Dow
[self.taskDict setObject:taskWrapper forKey:ocTask];
[taskWrapper release];

if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
if (_runningCount < _hints.countOfMaxProcessingTasks) {
[ocTask resume];
_taskQueue.push(nil);
_runningCount++;
} else {
_taskQueue.push(ocTask);
_waitingList.push_back(ocTask);
}
return ocTask;
};
Expand Down Expand Up @@ -315,11 +317,11 @@ - (NSURLSessionDownloadTask *)createDownloadTask:(std::shared_ptr<const cc::netw
[self.taskDict setObject:taskWrapper forKey:ocTask];
[taskWrapper release];

if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
if (_runningCount < _hints.countOfMaxProcessingTasks) {
[ocTask resume];
_taskQueue.push(nil);
_runningCount++;
} else {
_taskQueue.push(ocTask);
_waitingList.push_back(ocTask);
}
return ocTask;
};
Expand Down Expand Up @@ -390,8 +392,8 @@ - (void)doDestroy {
}
_outer = nullptr;

while (!_taskQueue.empty())
_taskQueue.pop();
_waitingList.clear();
_runningCount = 0;

[self.downloadSession invalidateAndCancel];
[self release];
Expand Down Expand Up @@ -518,12 +520,18 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
}
[self.taskDict removeObjectForKey:task];

while (!_taskQueue.empty() && _taskQueue.front() == nil) {
_taskQueue.pop();
auto it = std::find(_waitingList.begin(), _waitingList.end(), task);
if (it != _waitingList.end()) {
_waitingList.erase(it);
} else {
_runningCount--;
}
if (!_taskQueue.empty()) {
[_taskQueue.front() resume];
_taskQueue.pop();

if (_runningCount < _hints.countOfMaxProcessingTasks && !_waitingList.empty()) {
NSURLSessionTask *nextTask = _waitingList.front();
_waitingList.pop_front();
[nextTask resume];
_runningCount++;
}
}

Expand Down
Loading