From 40e6288ac78925860778c3b3882300e653ffaaf0 Mon Sep 17 00:00:00 2001 From: Mazyad Alabduljaleel Date: Sun, 13 Apr 2014 01:06:04 +0400 Subject: [PATCH] [NEW]: update HTTPClient to use C++11 callbacks [NEW]: updated tests to use the new API [DEP]: deprecated old target/selector API --- cocos/network/HttpClient.cpp | 7 +++++- cocos/network/HttpRequest.h | 24 +++++++++++++++---- .../NetworkTest/HttpClientTest.cpp | 20 ++++++++-------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/cocos/network/HttpClient.cpp b/cocos/network/HttpClient.cpp index e9df1a0ac4..0327cdee17 100644 --- a/cocos/network/HttpClient.cpp +++ b/cocos/network/HttpClient.cpp @@ -502,10 +502,15 @@ void HttpClient::dispatchResponseCallbacks() if (response) { HttpRequest *request = response->getHttpRequest(); + const ccHttpRequestCallback& callback = request->getCallback(); Ref* pTarget = request->getTarget(); SEL_HttpResponse pSelector = request->getSelector(); - if (pTarget && pSelector) + if (callback != nullptr) + { + callback(this, response); + } + else if (pTarget && pSelector) { (pTarget->*pSelector)(this, response); } diff --git a/cocos/network/HttpRequest.h b/cocos/network/HttpRequest.h index a21ce6c385..af48e0af54 100644 --- a/cocos/network/HttpRequest.h +++ b/cocos/network/HttpRequest.h @@ -38,6 +38,7 @@ namespace network { class HttpClient; class HttpResponse; +typedef std::function ccHttpRequestCallback; typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response); #define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR) @@ -78,9 +79,10 @@ public: _url.clear(); _requestData.clear(); _tag.clear(); - _pTarget = NULL; - _pSelector = NULL; - _pUserData = NULL; + _pTarget = nullptr; + _pSelector = nullptr; + _pCallback = nullptr; + _pUserData = nullptr; }; /** Destructor */ @@ -183,7 +185,7 @@ public: setResponseCallback(pTarget, (SEL_HttpResponse) pSelector); } - inline void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector) + CC_DEPRECATED_ATTRIBUTE inline void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector) { _pTarget = pTarget; _pSelector = pSelector; @@ -192,7 +194,13 @@ public: { _pTarget->retain(); } - } + } + + inline void setResponseCallback(const ccHttpRequestCallback& callback) + { + _pCallback = callback; + } + /** Get the target of callback selector funtion, mainly used by HttpClient */ inline Ref* getTarget() { @@ -218,6 +226,11 @@ public: return _prxy(_pSelector); } + inline const ccHttpRequestCallback& getCallback() + { + return _pCallback; + } + /** Set any custom headers **/ inline void setHeaders(std::vector pHeaders) { @@ -238,6 +251,7 @@ protected: std::string _tag; /// user defined tag, to identify different requests in response callback Ref* _pTarget; /// callback target of pSelector function SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(HttpClient *sender, HttpResponse * response) + ccHttpRequestCallback _pCallback; /// C++11 style callbacks void* _pUserData; /// You can add your customed data here std::vector _headers; /// custom http headers }; diff --git a/tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp b/tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp index 41f7cdcbb5..206836343c 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp @@ -77,7 +77,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://just-make-this-request-failed.com"); request->setRequestType(HttpRequest::Type::GET); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); request->setTag("GET test1"); HttpClient::getInstance()->send(request); request->release(); @@ -89,7 +89,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender) // required fields request->setUrl("http://httpbin.org/ip"); request->setRequestType(HttpRequest::Type::GET); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // optional fields request->setTag("GET test2"); @@ -104,7 +104,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/get"); request->setRequestType(HttpRequest::Type::GET); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); request->setTag("GET test3"); HttpClient::getInstance()->send(request); request->release(); @@ -122,7 +122,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/post"); request->setRequestType(HttpRequest::Type::POST); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -141,7 +141,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender) std::vector headers; headers.push_back("Content-Type: application/json; charset=utf-8"); request->setHeaders(headers); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -161,7 +161,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/post"); request->setRequestType(HttpRequest::Type::POST); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // write the post data char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response @@ -184,7 +184,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/put"); request->setRequestType(HttpRequest::Type::PUT); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -203,7 +203,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender) std::vector headers; headers.push_back("Content-Type: application/json; charset=utf-8"); request->setHeaders(headers); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); // write the post data const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; @@ -225,7 +225,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://just-make-this-request-failed.com"); request->setRequestType(HttpRequest::Type::DELETE); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); request->setTag("DELETE test1"); HttpClient::getInstance()->send(request); request->release(); @@ -236,7 +236,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender) HttpRequest* request = new HttpRequest(); request->setUrl("http://httpbin.org/delete"); request->setRequestType(HttpRequest::Type::DELETE); - request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); + request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this)); request->setTag("DELETE test2"); HttpClient::getInstance()->send(request); request->release();