From 83f7f30c1c83256f64e9d4bc3913a01183fc85d1 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 4 Jun 2015 15:03:20 +0800 Subject: [PATCH] Fix issue that __JSDownloaderDelegator::onError get called twice --- .../jsb_cocos2dx_extension_manual.cpp | 6 +----- extensions/assets-manager/Downloader.cpp | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp b/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp index bb3cd6c8e4..597701d0ba 100644 --- a/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp +++ b/cocos/scripting/js-bindings/manual/extension/jsb_cocos2dx_extension_manual.cpp @@ -951,11 +951,7 @@ void __JSDownloaderDelegator::startDownload() cocos2d::extension::Downloader::HeaderInfo info = _downloader->getHeader(_url); long contentSize = info.contentSize; - if (contentSize == -1 || info.responseCode >= 400) { - cocos2d::extension::Downloader::Error err; - onError(err); - } - else { + if (contentSize > 0 && info.responseCode < 400) { _size = contentSize / sizeof(unsigned char); _buffer = (unsigned char*)malloc(contentSize); _downloader->downloadToBufferSync(_url, _buffer, _size); diff --git a/extensions/assets-manager/Downloader.cpp b/extensions/assets-manager/Downloader.cpp index afa483440d..c5c260f7c4 100644 --- a/extensions/assets-manager/Downloader.cpp +++ b/extensions/assets-manager/Downloader.cpp @@ -296,16 +296,24 @@ Downloader::HeaderInfo Downloader::prepareHeader(const std::string &srcUrl, void curl_easy_getinfo(header, CURLINFO_CONTENT_TYPE, &contentType); curl_easy_getinfo(header, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &info.contentSize); curl_easy_getinfo(header, CURLINFO_RESPONSE_CODE, &info.responseCode); - info.url = url; - info.contentType = contentType; - info.valid = true; - if (_onHeader) + if (contentType == nullptr || info.contentSize == -1 || info.responseCode >= 400) { - _onHeader(srcUrl, info); + info.valid = false; + } + else + { + info.url = url; + info.contentType = contentType; + info.valid = true; } } - else + + if (info.valid && _onHeader) + { + _onHeader(srcUrl, info); + } + else if (!info.valid) { info.contentSize = -1; std::string msg = StringUtils::format("Can not get content size of file (%s) : Request header failed", srcUrl.c_str());