Merge pull request #14542 from pandemosth/issue_14539

Fix for Issue 14539
This commit is contained in:
pandamicro 2015-12-03 18:47:17 +08:00
commit 91909081da
1 changed files with 62 additions and 32 deletions

View File

@ -61,7 +61,7 @@
-(const cocos2d::network::DownloaderHints&)getHints;
-(NSURLSessionDataTask *)createDataTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task;
-(NSURLSessionDownloadTask *)createFileTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task;
-(void)doDestory;
-(void)doDestroy;
@end
@ -99,7 +99,7 @@ namespace cocos2d { namespace network {
DownloaderApple::~DownloaderApple()
{
DeclareDownloaderImplVar;
[impl doDestory];
[impl doDestroy];
DLLOG("Destruct DownloaderApple %p", this);
}
IDownloadTask *DownloaderApple::createCoTask(std::shared_ptr<const DownloadTask>& task)
@ -273,14 +273,24 @@ namespace cocos2d { namespace network {
return ocTask;
};
-(void)doDestory
-(void)doDestroy
{
// cancel all download task
NSEnumerator * enumeratorKey = [self.taskDict keyEnumerator];
for (NSURLSessionDownloadTask *task in enumeratorKey)
{
DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];
NSString *tempFilePath = [NSString stringWithFormat:@"%s%s", [wrapper get]->storagePath.c_str(), _hints.tempFileNameSuffix.c_str()];
// no resume support for a data task
std::string storagePath = [wrapper get]->storagePath;
if(storagePath.length() == 0) {
[task cancel];
}
else {
[task cancelByProducingResumeData:^(NSData *resumeData) {
if (resumeData)
{
NSString *tempFilePath = [NSString stringWithFormat:@"%s%s", storagePath.c_str(), _hints.tempFileNameSuffix.c_str()];
NSString *tempFileDir = [tempFilePath stringByDeletingLastPathComponent];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = false;
@ -289,7 +299,8 @@ namespace cocos2d { namespace network {
if (NO == isDir)
{
// TODO: the directory is a file, not a directory, how to echo to developer?
continue;
DLLOG("DownloaderAppleImpl temp dir is a file!");
return;
}
}
else
@ -298,16 +309,16 @@ namespace cocos2d { namespace network {
if (NO == [fileManager createDirectoryAtURL:tempFileURL withIntermediateDirectories:YES attributes:nil error:nil])
{
// create directory failed
continue;
DLLOG("DownloaderAppleImpl create temp dir failed");
return;
}
}
[task cancelByProducingResumeData:^(NSData *resumeData) {
if (resumeData)
{
[resumeData writeToFile:tempFilePath atomically:YES];
}
}];
}
}
_outer = nullptr;
[self.downloadSession invalidateAndCancel];
@ -375,9 +386,9 @@ namespace cocos2d { namespace network {
// clean wrapper C++ object
DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];
// if no error, callback has been called in finish task
if (_outer && error)
if(_outer)
{
if(error) {
std::vector<unsigned char> buf; // just a placeholder
_outer->onTaskFinish(*[wrapper get],
cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
@ -385,6 +396,25 @@ namespace cocos2d { namespace network {
[error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding],
buf);
}
else if(![wrapper get]->storagePath.length()) {
// call onTaskFinish for a data task
// (for a file download task, callback is called in didFinishDownloadingToURL)
std::string errorString;
const int64_t buflen = [wrapper totalBytesReceived];
char buf[buflen];
[wrapper transferDataToBuffer:buf lengthOfBuffer:buflen];
std::vector<unsigned char> data(buf, buf + buflen);
_outer->onTaskFinish(*[wrapper get],
cocos2d::network::DownloadTask::ERROR_NO_ERROR,
0,
errorString,
data);
}
}
[self.taskDict removeObjectForKey:task];
[wrapper release];