From a9ace2505e1c7b616426fe7811c4420a2b01b888 Mon Sep 17 00:00:00 2001 From: Hojin Choi Date: Fri, 26 Apr 2013 12:42:50 +0900 Subject: [PATCH 1/2] Introduce HttpResponse callback selector type. to resolve #2365 --- extensions/network/HttpClient.cpp | 4 ++-- extensions/network/HttpRequest.h | 39 +++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/extensions/network/HttpClient.cpp b/extensions/network/HttpClient.cpp index 47cb9429d1..b194271dd9 100644 --- a/extensions/network/HttpClient.cpp +++ b/extensions/network/HttpClient.cpp @@ -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(); diff --git a/extensions/network/HttpRequest.h b/extensions/network/HttpRequest.h index 3addad2a6e..28dbf6b762 100644 --- a/extensions/network/HttpRequest.h +++ b/extensions/network/HttpRequest.h @@ -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,12 +181,26 @@ public: { return _pTarget; } - /** Get the selector function pointer, mainly used by CCHttpClient */ - inline SEL_CallFuncND getSelector() - { - return _pSelector; - } + /* This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse, + someday this way will be removed */ + class _prxy + { + 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 **/ inline void setHeaders(std::vector pHeaders) { @@ -196,7 +221,7 @@ protected: std::vector _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 _headers; /// custom http headers }; From 8b9644150d290dce791b9c0e683a991ed2d119d7 Mon Sep 17 00:00:00 2001 From: Hojin Choi Date: Sun, 28 Apr 2013 23:42:31 +0900 Subject: [PATCH 2/2] Changes samples' function prototypes to apply newly introduced callback type. --- .../NetworkTest/HttpClientTest.cpp | 16 +++++++--------- .../ExtensionsTest/NetworkTest/HttpClientTest.h | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp index 251439c2fa..8c9850b45f 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp @@ -61,7 +61,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender) CCHttpRequest* request = new CCHttpRequest(); request->setUrl("http://just-make-this-request-failed.com"); request->setRequestType(CCHttpRequest::kHttpGet); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setTag("GET test1"); CCHttpClient::getInstance()->send(request); request->release(); @@ -73,7 +73,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender) // required fields request->setUrl("http://www.httpbin.org/ip"); request->setRequestType(CCHttpRequest::kHttpGet); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); // optional fields request->setTag("GET test2"); @@ -88,7 +88,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender) CCHttpRequest* request = new CCHttpRequest(); request->setUrl("http://www.httpbin.org/get"); request->setRequestType(CCHttpRequest::kHttpGet); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setTag("GET test3"); CCHttpClient::getInstance()->send(request); request->release(); @@ -106,7 +106,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::CCObject *sender) CCHttpRequest* request = new CCHttpRequest(); request->setUrl("http://www.httpbin.org/post"); request->setRequestType(CCHttpRequest::kHttpPost); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -125,7 +125,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::CCObject *sender) std::vector headers; headers.push_back("Content-Type: application/json; charset=utf-8"); request->setHeaders(headers); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -145,7 +145,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::CCObject *sender) CCHttpRequest* request = new CCHttpRequest(); request->setUrl("http://www.httpbin.org/post"); request->setRequestType(CCHttpRequest::kHttpPost); - request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); // write the post data char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response @@ -159,10 +159,8 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::CCObject *sender) m_labelStatusCode->setString("waiting..."); } -void HttpClientTest::onHttpRequestCompleted(cocos2d::CCNode *sender, void *data) +void HttpClientTest::onHttpRequestCompleted(CCHttpClient *sender, CCHttpResponse *response) { - CCHttpResponse *response = (CCHttpResponse*)data; - if (!response) { return; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.h index 41516661d9..418efda3e7 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/HttpClientTest.h @@ -17,7 +17,7 @@ public: void onMenuPostBinaryTestClicked(cocos2d::CCObject *sender); //Http Response Callback - void onHttpRequestCompleted(cocos2d::CCNode *sender, void *data); + void onHttpRequestCompleted(cocos2d::extension::CCHttpClient *sender, cocos2d::extension::CCHttpResponse *response); private: cocos2d::CCLabelTTF* m_labelStatusCode;