mirror of https://github.com/axmolengine/axmol.git
Merge pull request #14530 from cezheng/fix/downloader_bug_ios_android
Fix Downloader bug iOS & Android
This commit is contained in:
commit
e23432c6fa
|
@ -70,7 +70,7 @@ namespace cocos2d { namespace network {
|
|||
if (JniHelper::getStaticMethodInfo(methodInfo,
|
||||
JCLS_DOWNLOADER,
|
||||
"createDownloader",
|
||||
"(II" JARG_STR ")" JARG_DOWNLOADER))
|
||||
"(II" JARG_STR "I)" JARG_DOWNLOADER))
|
||||
{
|
||||
jobject jStr = methodInfo.env->NewStringUTF(hints.tempFileNameSuffix.c_str());
|
||||
jobject jObj = methodInfo.env->CallStaticObjectMethod(
|
||||
|
@ -78,7 +78,8 @@ namespace cocos2d { namespace network {
|
|||
methodInfo.methodID,
|
||||
_id,
|
||||
hints.timeoutInSeconds,
|
||||
jStr
|
||||
jStr,
|
||||
hints.countOfMaxProcessingTasks
|
||||
);
|
||||
_impl = methodInfo.env->NewGlobalRef(jObj);
|
||||
DLLOG("android downloader: jObj: %p, _impl: %p", jObj, _impl);
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "network/CCDownloader-apple.h"
|
||||
|
||||
#include "network/CCDownloader.h"
|
||||
#include <queue>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// OC Classes Declaration
|
||||
|
@ -51,6 +52,7 @@
|
|||
{
|
||||
const cocos2d::network::DownloaderApple *_outer;
|
||||
cocos2d::network::DownloaderHints _hints;
|
||||
std::queue<NSURLSessionTask*> _taskQueue;
|
||||
}
|
||||
@property (nonatomic, strong) NSURLSession *downloadSession;
|
||||
@property (nonatomic, strong) NSMutableDictionary *taskDict; // ocTask: DownloadTaskWrapper
|
||||
|
@ -225,14 +227,20 @@ namespace cocos2d { namespace network {
|
|||
}
|
||||
NSURLSessionDataTask *ocTask = [self.downloadSession dataTaskWithRequest:request];
|
||||
[self.taskDict setObject:[[DownloadTaskWrapper alloc] init:task] forKey:ocTask];
|
||||
[ocTask resume];
|
||||
|
||||
if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
|
||||
[ocTask resume];
|
||||
_taskQueue.push(nil);
|
||||
} else {
|
||||
_taskQueue.push(ocTask);
|
||||
}
|
||||
return ocTask;
|
||||
};
|
||||
|
||||
-(NSURLSessionDownloadTask *)createFileTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task
|
||||
{
|
||||
const char *urlStr = task->requestURL.c_str();
|
||||
DLLOG("DownloaderAppleImpl createDataTask: %s", urlStr);
|
||||
DLLOG("DownloaderAppleImpl createFileTask: %s", urlStr);
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:urlStr]];
|
||||
NSURLRequest *request = nil;
|
||||
if (_hints.timeoutInSeconds > 0)
|
||||
|
@ -255,7 +263,13 @@ namespace cocos2d { namespace network {
|
|||
ocTask = [self.downloadSession downloadTaskWithRequest:request];
|
||||
}
|
||||
[self.taskDict setObject:[[DownloadTaskWrapper alloc] init:task] forKey:ocTask];
|
||||
[ocTask resume];
|
||||
|
||||
if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
|
||||
[ocTask resume];
|
||||
_taskQueue.push(nil);
|
||||
} else {
|
||||
_taskQueue.push(ocTask);
|
||||
}
|
||||
return ocTask;
|
||||
};
|
||||
|
||||
|
@ -373,6 +387,14 @@ namespace cocos2d { namespace network {
|
|||
}
|
||||
[self.taskDict removeObjectForKey:task];
|
||||
[wrapper release];
|
||||
|
||||
while (!_taskQueue.empty() && _taskQueue.front() == nil) {
|
||||
_taskQueue.pop();
|
||||
}
|
||||
if (!_taskQueue.empty()) {
|
||||
[_taskQueue.front() resume];
|
||||
_taskQueue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - NSURLSessionDataDelegate methods
|
||||
|
|
|
@ -99,6 +99,10 @@ class FileTaskHandler extends FileAsyncHttpResponseHandler {
|
|||
@Override
|
||||
public void onFinish() {
|
||||
// onFinish called after onSuccess/onFailure
|
||||
Runnable taskRunnable = _downloader.dequeue();
|
||||
if (taskRunnable != null) {
|
||||
Cocos2dxHelper.getActivity().runOnUiThread(taskRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -166,7 +170,9 @@ public class Cocos2dxDownloader {
|
|||
private int _id;
|
||||
private AsyncHttpClient _httpClient = new AsyncHttpClient();
|
||||
private String _tempFileNameSufix;
|
||||
private int _countOfMaxProcessingTasks;
|
||||
private HashMap _taskMap = new HashMap();
|
||||
private Queue<Runnable> _taskQueue = new LinkedList<Runnable>();
|
||||
|
||||
void onProgress(final int id, final long downloadBytes, final long downloadNow, final long downloadTotal) {
|
||||
DownloadTask task = (DownloadTask)_taskMap.get(id);
|
||||
|
@ -202,7 +208,7 @@ public class Cocos2dxDownloader {
|
|||
});
|
||||
}
|
||||
|
||||
public static Cocos2dxDownloader createDownloader(int id, int timeoutInSeconds, String tempFileNameSufix) {
|
||||
public static Cocos2dxDownloader createDownloader(int id, int timeoutInSeconds, String tempFileNameSufix, int countOfMaxProcessingTasks) {
|
||||
Cocos2dxDownloader downloader = new Cocos2dxDownloader();
|
||||
downloader._id = id;
|
||||
|
||||
|
@ -214,6 +220,7 @@ public class Cocos2dxDownloader {
|
|||
downloader._httpClient.allowRetryExceptionClass(javax.net.ssl.SSLException.class);
|
||||
|
||||
downloader._tempFileNameSufix = tempFileNameSufix;
|
||||
downloader._countOfMaxProcessingTasks = countOfMaxProcessingTasks;
|
||||
return downloader;
|
||||
}
|
||||
|
||||
|
@ -222,7 +229,7 @@ public class Cocos2dxDownloader {
|
|||
final String url = url_;
|
||||
final String path = path_;
|
||||
|
||||
Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
|
||||
Runnable taskRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
DownloadTask task = new DownloadTask();
|
||||
|
@ -269,7 +276,13 @@ public class Cocos2dxDownloader {
|
|||
downloader._taskMap.put(id, task);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
if (downloader._taskQueue.size() < downloader._countOfMaxProcessingTasks) {
|
||||
Cocos2dxHelper.getActivity().runOnUiThread(taskRunnable);
|
||||
downloader._taskQueue.add(null);
|
||||
} else {
|
||||
downloader._taskQueue.add(taskRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
public static void cancelAllRequests(final Cocos2dxDownloader downloader) {
|
||||
|
@ -291,6 +304,16 @@ public class Cocos2dxDownloader {
|
|||
});
|
||||
}
|
||||
|
||||
public Runnable dequeue() {
|
||||
if (!_taskQueue.isEmpty() && _taskQueue.element() == null) {
|
||||
_taskQueue.remove();
|
||||
}
|
||||
if (!_taskQueue.isEmpty()) {
|
||||
return _taskQueue.remove();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
native void nativeOnProgress(int id, int taskId, long dl, long dlnow, long dltotal);
|
||||
native void nativeOnFinish(int id, int taskId, int errCode, String errStr, final byte[] data);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue