mirror of https://github.com/axmolengine/axmol.git
Add CCDownloader-apple for mac and ios.
This commit is contained in:
parent
3f160ddbb1
commit
b4f585732e
|
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
#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
|
||||
/// @}
|
|
@ -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 <Foundation/Foundation.h>
|
||||
#import <Foundation/NSData.h>
|
||||
|
||||
@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)
|
|
@ -24,13 +24,15 @@
|
|||
|
||||
#include "network/CCDownloader.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include <thread>
|
||||
|
||||
#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)
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue