Merge pull request #2490 from coolengineer/pull-request-httpclient

fixed #2119: Introduce HttpResponse callback selector type to resolve #2365, fixed version ;-)
This commit is contained in:
James Chen 2013-05-06 23:44:20 -07:00
commit a9aafdafac
4 changed files with 42 additions and 19 deletions

View File

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

View File

@ -30,11 +30,17 @@
NS_CC_EXT_BEGIN 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. @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 Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
@since v2.0.2 @since v2.0.2
*/ */
class CCHttpRequest : public CCObject class CCHttpRequest : public CCObject
{ {
public: public:
@ -155,7 +161,12 @@ public:
/** Required field. You should set the callback selector function at ack the http request completed /** 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; _pTarget = pTarget;
_pSelector = pSelector; _pSelector = pSelector;
@ -170,10 +181,24 @@ public:
{ {
return _pTarget; 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 **/ /** Set any custom headers **/
@ -196,7 +221,7 @@ protected:
std::vector<char> _requestData; /// used for POST std::vector<char> _requestData; /// used for POST
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
CCObject* _pTarget; /// callback target of pSelector function 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 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

@ -61,7 +61,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
CCHttpRequest* request = new CCHttpRequest(); CCHttpRequest* request = new CCHttpRequest();
request->setUrl("http://just-make-this-request-failed.com"); request->setUrl("http://just-make-this-request-failed.com");
request->setRequestType(CCHttpRequest::kHttpGet); request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
request->setTag("GET test1"); request->setTag("GET test1");
CCHttpClient::getInstance()->send(request); CCHttpClient::getInstance()->send(request);
request->release(); request->release();
@ -73,7 +73,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
// required fields // required fields
request->setUrl("http://www.httpbin.org/ip"); request->setUrl("http://www.httpbin.org/ip");
request->setRequestType(CCHttpRequest::kHttpGet); request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
// optional fields // optional fields
request->setTag("GET test2"); request->setTag("GET test2");
@ -88,7 +88,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
CCHttpRequest* request = new CCHttpRequest(); CCHttpRequest* request = new CCHttpRequest();
request->setUrl("http://www.httpbin.org/get"); request->setUrl("http://www.httpbin.org/get");
request->setRequestType(CCHttpRequest::kHttpGet); request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
request->setTag("GET test3"); request->setTag("GET test3");
CCHttpClient::getInstance()->send(request); CCHttpClient::getInstance()->send(request);
request->release(); request->release();
@ -106,7 +106,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::CCObject *sender)
CCHttpRequest* request = new CCHttpRequest(); CCHttpRequest* request = new CCHttpRequest();
request->setUrl("http://www.httpbin.org/post"); request->setUrl("http://www.httpbin.org/post");
request->setRequestType(CCHttpRequest::kHttpPost); request->setRequestType(CCHttpRequest::kHttpPost);
request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
// 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";
@ -125,7 +125,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::CCObject *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, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
// 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";
@ -145,7 +145,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::CCObject *sender)
CCHttpRequest* request = new CCHttpRequest(); CCHttpRequest* request = new CCHttpRequest();
request->setUrl("http://www.httpbin.org/post"); request->setUrl("http://www.httpbin.org/post");
request->setRequestType(CCHttpRequest::kHttpPost); request->setRequestType(CCHttpRequest::kHttpPost);
request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
// 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
@ -159,10 +159,8 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::CCObject *sender)
m_labelStatusCode->setString("waiting..."); 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) if (!response)
{ {
return; return;

View File

@ -17,7 +17,7 @@ public:
void onMenuPostBinaryTestClicked(cocos2d::CCObject *sender); void onMenuPostBinaryTestClicked(cocos2d::CCObject *sender);
//Http Response Callback //Http Response Callback
void onHttpRequestCompleted(cocos2d::CCNode *sender, void *data); void onHttpRequestCompleted(cocos2d::extension::CCHttpClient *sender, cocos2d::extension::CCHttpResponse *response);
private: private:
cocos2d::CCLabelTTF* m_labelStatusCode; cocos2d::CCLabelTTF* m_labelStatusCode;