From b4f585732e994a9b55ae530af4f9243565986d22 Mon Sep 17 00:00:00 2001 From: Vincent Yang Date: Thu, 20 Aug 2015 11:35:40 +0800 Subject: [PATCH] Add CCDownloader-apple for mac and ios. --- cocos/network/CCDownloader-apple.h | 83 ++++++++++++ cocos/network/CCDownloader-apple.mm | 191 ++++++++++++++++++++++++++++ cocos/network/CCDownloader.cpp | 8 +- cocos/network/CCDownloader.h | 4 +- cocos/network/CCDownloaderImpl.h | 6 +- cocos/network/CCIDownloaderImpl.h | 1 + 6 files changed, 285 insertions(+), 8 deletions(-) create mode 100644 cocos/network/CCDownloader-apple.h create mode 100644 cocos/network/CCDownloader-apple.mm diff --git a/cocos/network/CCDownloader-apple.h b/cocos/network/CCDownloader-apple.h new file mode 100644 index 0000000000..40f51d5c25 --- /dev/null +++ b/cocos/network/CCDownloader-apple.h @@ -0,0 +1,83 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#pragma once + +#include +#include + +#include "network/CCIDownloaderImpl.h" +#include "platform/CCPlatformMacros.h" + +/** + * @addtogroup network + * @{ + */ + +NS_CC_BEGIN + +namespace network +{ + class DownloaderImpl : public IDownloaderImpl + { + public: + DownloaderImpl(); + virtual ~DownloaderImpl(); + + bool init() override; + + // Overrides + int performDownload(DownloadUnit* unit, + const WriterCallback& writerCallback, + const ProgressCallback& progressCallback + ) override; + int performBatchDownload(const DownloadUnits& units, + const WriterCallback& writerCallback, + const ProgressCallback& progressCallback, + const ErrorCallback& errorCallback + ) override; + int getHeader(const std::string& url, HeaderInfo* headerInfo) override; + std::string getStrError() const override; + void setConnectionTimeout(int timeout) override; + bool supportsResume(const std::string& url) override; + + // + const WriterCallback& getWriterCallback() const { return _writerCallback; } + const ProgressCallback& getProgressCallback() const { return _progressCallback; } + + private: + + WriterCallback _writerCallback; + ProgressCallback _progressCallback; + + int _lastErrCode; + std::string _lastErrStr; + }; + +} // namespace network + +NS_CC_END + +// end group +/// @} diff --git a/cocos/network/CCDownloader-apple.mm b/cocos/network/CCDownloader-apple.mm new file mode 100644 index 0000000000..93273d8742 --- /dev/null +++ b/cocos/network/CCDownloader-apple.mm @@ -0,0 +1,191 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "platform/CCPlatformConfig.h" +#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) +#include "network/CCDownloader-apple.h" + +#import +#import + +@interface Conn : NSObject +{ + std::string name; +} +@property (retain) NSURLConnection *connection; + +@end + +@implementation Conn + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response +{ + NSLog(@"didReceiveResponse"); +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data +{ + NSLog(@"didReceiveData"); +} + +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error +{ + + NSLog(@"didFailWithError"); +} + +- (void)connectionDidFinishLoading:(NSURLConnection *)connection +{ + NSLog(@"Succeeded! Receive bytes of data(unsigned long)"); + +} + +@end +NS_CC_BEGIN + +using namespace network; + +DownloaderImpl::DownloaderImpl() +: IDownloaderImpl() +{ +} + +DownloaderImpl::~DownloaderImpl() +{ + +} + +bool DownloaderImpl::init() +{ + return true; +} + +int DownloaderImpl::performDownload(DownloadUnit* unit, + const WriterCallback& writerCallback, + const ProgressCallback& progressCallback + ) +{ + _lastErrCode = 0; + _lastErrStr = ""; + + NSString *urlStr = [NSString stringWithCString: unit->srcUrl.c_str() encoding:NSUTF8StringEncoding]; + NSURL *url = [NSURL URLWithString: urlStr]; + NSURLRequest *request = [NSURLRequest requestWithURL: url]; + + // due to this function is sync implement, so send a sync request by NSURLConnection + NSURLResponse *response = nil; + NSError *err = nil; + NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; + if (err) + { + _lastErrCode = (int)err.code; + NSString *errStr = [err localizedDescription]; + if (errStr) + { + _lastErrStr = [errStr UTF8String]; + } + } + else + { + int dataLen = 0; + if (data) + { + dataLen = (int)[data length]; + writerCallback((void*)[data bytes], 1, dataLen, unit); + } + progressCallback(unit, dataLen, dataLen); + } + return _lastErrCode; +} + +int DownloaderImpl::performBatchDownload(const DownloadUnits& units, + const WriterCallback& writerCallback, + const ProgressCallback& progressCallback, + const ErrorCallback& errorCallback + ) +{ + NSOperationQueue *queue = [[NSOperationQueue alloc] init]; + queue.maxConcurrentOperationCount = 4; + + NSMutableArray *operations = [NSMutableArray arrayWithCapacity: units.size()]; + + for (auto it = units.begin(), end = units.end(); it != end; it++) + { + DownloadUnit *unit = (DownloadUnit *)&(it->second); + NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ + NSString *urlStr = [NSString stringWithCString: unit->srcUrl.c_str() encoding:NSUTF8StringEncoding]; + NSURL *url = [NSURL URLWithString: urlStr]; + NSError *err = nil; + NSData *data = [NSData dataWithContentsOfURL: url options: NSDataReadingUncached error: &err]; + if (err) + { + NSString *errStr = [err localizedDescription]; + if (errStr) + { + const char * c_errStr = [errStr UTF8String]; + errorCallback(c_errStr, (int)err.code, c_errStr); + } + } + else + { + int dataLen = 0; + if (data) + { + dataLen = (int)[data length]; + writerCallback((void*)[data bytes], 1, dataLen, unit); + } + progressCallback(unit, dataLen, dataLen); + } + }]; + [operations addObject:operation]; + } + + [queue addOperations:operations waitUntilFinished:YES]; + NSLog(@"performBatchDownload end"); + return 0; +} + +int DownloaderImpl::getHeader(const std::string& url, HeaderInfo* headerInfo) +{ + return 0; +} + +std::string DownloaderImpl::getStrError() const +{ + return _lastErrStr; +} + +void DownloaderImpl::setConnectionTimeout(int timeout) +{ + +} + +bool DownloaderImpl::supportsResume(const std::string& url) +{ + return false; +} + +NS_CC_END + +#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) \ No newline at end of file diff --git a/cocos/network/CCDownloader.cpp b/cocos/network/CCDownloader.cpp index e47894c375..947a378b15 100644 --- a/cocos/network/CCDownloader.cpp +++ b/cocos/network/CCDownloader.cpp @@ -24,13 +24,15 @@ #include "network/CCDownloader.h" -#include -#include #include #include #include +#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) +#include "network/CCDownloader-apple.h" +#else #include "network/CCDownloaderImpl.h" +#endif #include "base/CCDirector.h" #include "base/CCScheduler.h" #include "deprecated/CCString.h" @@ -100,7 +102,7 @@ void Downloader::notifyError(ErrorCode code, const std::string& msg/* ="" */, co void Downloader::notifyError(const std::string& msg, int curlm_code, const std::string& customId/* = ""*/) { - notifyError(ErrorCode::CURL_MULTI_ERROR, msg, customId, CURLE_OK, curlm_code); + notifyError(ErrorCode::CURL_MULTI_ERROR, msg, customId, 0, curlm_code); } void Downloader::notifyError(const std::string& msg, const std::string& customId, int curle_code) diff --git a/cocos/network/CCDownloader.h b/cocos/network/CCDownloader.h index 01e531d1a7..d600464c08 100644 --- a/cocos/network/CCDownloader.h +++ b/cocos/network/CCDownloader.h @@ -30,7 +30,7 @@ #include #include -#include "network/CCDownloaderImpl.h" +#include "network/CCIDownloaderImpl.h" #include "platform/CCFileUtils.h" #include "extensions/ExtensionMacros.h" #include "extensions/ExtensionExport.h" @@ -151,7 +151,7 @@ private: int _connectionTimeout; FileUtils* _fileUtils; bool _supportResuming; - DownloaderImpl* _downloaderImpl; + IDownloaderImpl* _downloaderImpl; }; } // namespace cocos2d diff --git a/cocos/network/CCDownloaderImpl.h b/cocos/network/CCDownloaderImpl.h index 3707c3f40f..f4ae8e3353 100644 --- a/cocos/network/CCDownloaderImpl.h +++ b/cocos/network/CCDownloaderImpl.h @@ -55,17 +55,17 @@ namespace network int performBatchDownload(const DownloadUnits& units, const WriterCallback& writerCallback, const ProgressCallback& progressCallback, - const ErrorCallback& errorCallback + const ErrorCallback& errorCallback ) override; int getHeader(const std::string& url, HeaderInfo* headerInfo) override; std::string getStrError() const override; void setConnectionTimeout(int timeout) override; - bool supportsResume(const std::string& url); + bool supportsResume(const std::string& url) override; // const WriterCallback& getWriterCallback() const { return _writerCallback; } const ProgressCallback& getProgressCallback() const { return _progressCallback; } - + private: int _connectionTimeout; diff --git a/cocos/network/CCIDownloaderImpl.h b/cocos/network/CCIDownloaderImpl.h index 468be2db28..125380fad7 100644 --- a/cocos/network/CCIDownloaderImpl.h +++ b/cocos/network/CCIDownloaderImpl.h @@ -96,6 +96,7 @@ namespace network virtual int getHeader(const std::string& url, HeaderInfo* headerInfo) = 0; virtual std::string getStrError() const = 0; virtual void setConnectionTimeout(int timeout) = 0; + virtual bool supportsResume(const std::string& url) = 0; }; }