Merge pull request #6263 from Mazyod/develop-cpp11-http-client

closed #4702: Update HTTPClient to use C++11 callbacks
This commit is contained in:
James Chen 2014-04-25 11:06:06 +08:00
commit 328408d792
3 changed files with 35 additions and 16 deletions

View File

@ -502,10 +502,15 @@ void HttpClient::dispatchResponseCallbacks()
if (response) if (response)
{ {
HttpRequest *request = response->getHttpRequest(); HttpRequest *request = response->getHttpRequest();
const ccHttpRequestCallback& callback = request->getCallback();
Ref* pTarget = request->getTarget(); Ref* pTarget = request->getTarget();
SEL_HttpResponse pSelector = request->getSelector(); SEL_HttpResponse pSelector = request->getSelector();
if (pTarget && pSelector) if (callback != nullptr)
{
callback(this, response);
}
else if (pTarget && pSelector)
{ {
(pTarget->*pSelector)(this, response); (pTarget->*pSelector)(this, response);
} }

View File

@ -38,6 +38,7 @@ namespace network {
class HttpClient; class HttpClient;
class HttpResponse; class HttpResponse;
typedef std::function<void(HttpClient* client, HttpResponse* response)> ccHttpRequestCallback;
typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response); typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response);
#define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR) #define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR)
@ -78,9 +79,10 @@ public:
_url.clear(); _url.clear();
_requestData.clear(); _requestData.clear();
_tag.clear(); _tag.clear();
_pTarget = NULL; _pTarget = nullptr;
_pSelector = NULL; _pSelector = nullptr;
_pUserData = NULL; _pCallback = nullptr;
_pUserData = nullptr;
}; };
/** Destructor */ /** Destructor */
@ -183,7 +185,7 @@ public:
setResponseCallback(pTarget, (SEL_HttpResponse) pSelector); 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; _pTarget = pTarget;
_pSelector = pSelector; _pSelector = pSelector;
@ -193,6 +195,12 @@ public:
_pTarget->retain(); _pTarget->retain();
} }
} }
inline void setResponseCallback(const ccHttpRequestCallback& callback)
{
_pCallback = callback;
}
/** Get the target of callback selector funtion, mainly used by HttpClient */ /** Get the target of callback selector funtion, mainly used by HttpClient */
inline Ref* getTarget() inline Ref* getTarget()
{ {
@ -218,6 +226,11 @@ public:
return _prxy(_pSelector); return _prxy(_pSelector);
} }
inline const ccHttpRequestCallback& getCallback()
{
return _pCallback;
}
/** Set any custom headers **/ /** Set any custom headers **/
inline void setHeaders(std::vector<std::string> pHeaders) inline void setHeaders(std::vector<std::string> pHeaders)
{ {
@ -238,6 +251,7 @@ protected:
std::string _tag; /// user defined tag, to identify different requests in response callback std::string _tag; /// user defined tag, to identify different requests in response callback
Ref* _pTarget; /// callback target of pSelector function Ref* _pTarget; /// callback target of pSelector function
SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(HttpClient *sender, HttpResponse * response) 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 void* _pUserData; /// You can add your customed data here
std::vector<std::string> _headers; /// custom http headers std::vector<std::string> _headers; /// custom http headers
}; };

View File

@ -77,7 +77,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("http://just-make-this-request-failed.com"); request->setUrl("http://just-make-this-request-failed.com");
request->setRequestType(HttpRequest::Type::GET); request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
request->setTag("GET test1"); request->setTag("GET test1");
HttpClient::getInstance()->send(request); HttpClient::getInstance()->send(request);
request->release(); request->release();
@ -89,7 +89,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
// required fields // required fields
request->setUrl("http://httpbin.org/ip"); request->setUrl("http://httpbin.org/ip");
request->setRequestType(HttpRequest::Type::GET); request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
// optional fields // optional fields
request->setTag("GET test2"); request->setTag("GET test2");
@ -104,7 +104,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("https://httpbin.org/get"); request->setUrl("https://httpbin.org/get");
request->setRequestType(HttpRequest::Type::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"); request->setTag("GET test3");
HttpClient::getInstance()->send(request); HttpClient::getInstance()->send(request);
request->release(); request->release();
@ -122,7 +122,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("http://httpbin.org/post"); request->setUrl("http://httpbin.org/post");
request->setRequestType(HttpRequest::Type::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 // write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
@ -141,7 +141,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
std::vector<std::string> headers; std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8"); headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers); request->setHeaders(headers);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
// write the post data // write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
@ -161,7 +161,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("http://httpbin.org/post"); request->setUrl("http://httpbin.org/post");
request->setRequestType(HttpRequest::Type::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 // write the post data
char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response 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(); HttpRequest* request = new HttpRequest();
request->setUrl("http://httpbin.org/put"); request->setUrl("http://httpbin.org/put");
request->setRequestType(HttpRequest::Type::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 // write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
@ -203,7 +203,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
std::vector<std::string> headers; std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8"); headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers); request->setHeaders(headers);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
// write the post data // write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest"; const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
@ -225,7 +225,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("http://just-make-this-request-failed.com"); request->setUrl("http://just-make-this-request-failed.com");
request->setRequestType(HttpRequest::Type::DELETE); request->setRequestType(HttpRequest::Type::DELETE);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
request->setTag("DELETE test1"); request->setTag("DELETE test1");
HttpClient::getInstance()->send(request); HttpClient::getInstance()->send(request);
request->release(); request->release();
@ -236,7 +236,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
HttpRequest* request = new HttpRequest(); HttpRequest* request = new HttpRequest();
request->setUrl("http://httpbin.org/delete"); request->setUrl("http://httpbin.org/delete");
request->setRequestType(HttpRequest::Type::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"); request->setTag("DELETE test2");
HttpClient::getInstance()->send(request); HttpClient::getInstance()->send(request);
request->release(); request->release();