Introduce HttpResponse callback selector type. to resolve #2365

This commit is contained in:
Hojin Choi 2013-04-26 12:42:50 +09:00
parent 38e96cbe24
commit a9ace2505e
2 changed files with 34 additions and 9 deletions

View File

@ -479,11 +479,11 @@ void CCHttpClient::dispatchResponseCallbacks(float delta)
CCHttpRequest *request = response->getHttpRequest();
CCObject *pTarget = request->getTarget();
SEL_CallFuncND pSelector = request->getSelector();
SEL_HttpResponse pSelector = request->getSelector();
if (pTarget && pSelector)
{
(pTarget->*pSelector)((CCNode *)this, response);
(pTarget->*pSelector)(this, response);
}
response->release();

View File

@ -30,11 +30,17 @@
NS_CC_EXT_BEGIN
class CCHttpClient;
class CCHttpResponse;
typedef void (CCObject::*SEL_HttpResponse)(CCHttpClient* client, CCHttpResponse* response);
#define httpresponse_selector(_SELECTOR) (SEL_HttpResponse)(&_SELECTOR)
/**
@brief defines the object which users must packed for CCHttpClient::send(HttpRequest*) method.
Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
@since v2.0.2
*/
class CCHttpRequest : public CCObject
{
public:
@ -155,7 +161,12 @@ public:
/** Required field. You should set the callback selector function at ack the http request completed
*/
inline void setResponseCallback(CCObject* pTarget, SEL_CallFuncND pSelector)
inline void setResponseCallback(CCObject* pTarget, SEL_CallFuncND pSelector) CC_DEPRECATED_ATTRIBUTE
{
setResponseCallback(pTarget, (SEL_HttpResponse) pSelector);
}
inline void setResponseCallback(CCObject* pTarget, SEL_HttpResponse pSelector)
{
_pTarget = pTarget;
_pSelector = pSelector;
@ -170,10 +181,24 @@ public:
{
return _pTarget;
}
/** Get the selector function pointer, mainly used by CCHttpClient */
inline SEL_CallFuncND getSelector()
/* This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse,
someday this way will be removed */
class _prxy
{
return _pSelector;
public:
_prxy( SEL_HttpResponse cb ) :_cb(cb) {}
~_prxy(){};
operator SEL_HttpResponse() const { return _cb; }
operator SEL_CallFuncND() const CC_DEPRECATED_ATTRIBUTE { return (SEL_CallFuncND) _cb; }
protected:
SEL_HttpResponse _cb;
};
/** Get the selector function pointer, mainly used by CCHttpClient */
inline _prxy getSelector()
{
return _prxy(_pSelector);
}
/** Set any custom headers **/
@ -196,7 +221,7 @@ protected:
std::vector<char> _requestData; /// used for POST
std::string _tag; /// user defined tag, to identify different requests in response callback
CCObject* _pTarget; /// callback target of pSelector function
SEL_CallFuncND _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(CCObject *sender, void *data)
SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(CCHttpClient *sender, CCHttpResponse * response)
void* _pUserData; /// You can add your customed data here
std::vector<std::string> _headers; /// custom http headers
};