From 38b462100b0facfe4d2ecd9b125c0350d1ada5ae Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Mon, 20 Oct 2014 16:05:13 +0800 Subject: [PATCH 1/3] Update LuaMinXmlHttpRequest.h/.cpp --- .../manual/network/lua_xml_http_request.cpp | 90 +++++++++++++++++-- .../manual/network/lua_xml_http_request.h | 12 ++- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp index 35e0626334..6a078e9115 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp @@ -33,12 +33,25 @@ using namespace cocos2d; using namespace std; -LuaMinXmlHttpRequest::LuaMinXmlHttpRequest():_isNetwork(true) +LuaMinXmlHttpRequest::LuaMinXmlHttpRequest() +:_isNetwork(true), +_url(""), +_meth(""), +_type(""), +_dataSize(0), +_readyState(UNSENT), +_status(0), +_statusText(""), +_responseType(ResponseType::STRING), +_timeout(0), +_isAsync(false), +_withCredentialsValue(true), +_errorFlag(false), +_isAborted(false) { _httpHeader.clear(); _requestHeader.clear(); - _withCredentialsValue = true; - _httpRequest = new network::HttpRequest(); + _httpRequest = new (std::nothrow)cocos2d::network::HttpRequest(); } LuaMinXmlHttpRequest::~LuaMinXmlHttpRequest() @@ -182,6 +195,9 @@ void LuaMinXmlHttpRequest::_setHttpRequestHeader() void LuaMinXmlHttpRequest::_sendRequest() { _httpRequest->setResponseCallback([this](cocos2d::network::HttpClient* sender, cocos2d::network::HttpResponse* response){ + if (_isAborted) + return ; + if (0 != strlen(response->getHttpRequest()->getTag())) { CCLOG("%s completed", response->getHttpRequest()->getTag()); @@ -193,8 +209,13 @@ void LuaMinXmlHttpRequest::_sendRequest() if (!response->isSucceed()) { - CCLOG("response failed"); - CCLOG("error buffer: %s", response->getErrorBuffer()); + CCLOG("Response failed, error buffer: %s", response->getErrorBuffer()); + if(statusCode == 0) + { + _errorFlag = true; + _status = 0; + _statusText.clear(); + } return; } @@ -244,7 +265,7 @@ void LuaMinXmlHttpRequest::_sendRequest() } release(); }); - network::HttpClient::getInstance()->send(_httpRequest); + network::HttpClient::getInstance()->sendImmediate(_httpRequest); _httpRequest->release(); retain(); } @@ -641,11 +662,17 @@ static int lua_get_XMLHttpRequest_response(lua_State* L) if (self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::JSON) { + if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag()) + return 0; + lua_pushstring(L, self->getDataStr().c_str()); return 1; } else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER) { + if (self->getReadyState() != LuaMinXmlHttpRequest::DONE || self->getErrorFlag()) + return 0; + LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); if (NULL == pStack) { return 0; @@ -747,10 +774,22 @@ static int lua_cocos2dx_XMLHttpRequest_open(lua_State* L) { self->getHttpRequest()->setRequestType(network::HttpRequest::Type::POST); } - else + else if(method.compare("get") == 0 || method.compare("GET") == 0) { self->getHttpRequest()->setRequestType(network::HttpRequest::Type::GET); } + else if(method.compare("put") == 0 || method.compare("PUT") == 0) + { + self->getHttpRequest()->setRequestType(network::HttpRequest::Type::PUT); + } + else if(method.compare("delete") == 0 || method.compare("DELETE") == 0) + { + self->getHttpRequest()->setRequestType(network::HttpRequest::Type::DELETE); + } + else + { + self->getHttpRequest()->setRequestType(network::HttpRequest::Type::UNKNOWN); + } self->getHttpRequest()->setUrl(url.c_str()); @@ -758,6 +797,8 @@ static int lua_cocos2dx_XMLHttpRequest_open(lua_State* L) self->setIsNetWork(true); self->setReadyState(LuaMinXmlHttpRequest::OPENED); + self->setStatus(0); + self->setAborted(false); return 0; } @@ -793,6 +834,8 @@ static int lua_cocos2dx_XMLHttpRequest_send(lua_State* L) return 0; } #endif + self->getHttpHeader().clear(); + self->setErrorFlag(false); argc = lua_gettop(L) - 1; @@ -807,7 +850,8 @@ static int lua_cocos2dx_XMLHttpRequest_send(lua_State* L) } if (size > 0 && - (self->getMethod().compare("post") == 0 || self->getMethod().compare("POST") == 0) && + (self->getMethod().compare("post") == 0 || self->getMethod().compare("POST") == 0 + || self->getMethod().compare("put") == 0 || self->getMethod().compare("PUT") == 0 )&& nullptr != self->getHttpRequest()) { self->getHttpRequest()->setRequestData(data,size); @@ -829,7 +873,37 @@ tolua_lerror: */ static int lua_cocos2dx_XMLHttpRequest_abort(lua_State* L) { + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_send'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( 0 == argc ) + { + self->setAborted(true); + self->setReadyState(LuaMinXmlHttpRequest::UNSENT); + } return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_send'.",&tolua_err); + return 0; +#endif } static int lua_cocos2dx_XMLHttpRequest_setRequestHeader(lua_State* L) diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h index 9db8b5a042..754a4e855a 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.h @@ -72,8 +72,10 @@ public: inline int getReadyState() { return _readyState ;} inline cocos2d::network::HttpRequest* getHttpRequest() { return _httpRequest; } - inline int getStatus() { return _status; } inline std::string getStatusText() { return _statusText ;} + + inline void setStatus(int status) { _status = status; } + inline int getStatus() { return _status; } inline std::string getUrl(){return _url;} inline void setUrl(std::string url) { _url = url ;} @@ -96,6 +98,12 @@ public: inline size_t getDataSize() { return _dataSize; } + inline void setErrorFlag(bool errorFlag) { _errorFlag = errorFlag; } + inline bool getErrorFlag() { return _errorFlag; } + + inline void setAborted(bool isAborted) { _isAborted = isAborted; } + inline bool getAborted() { return _isAborted; } + private: void _gotHeader(std::string header); @@ -116,6 +124,8 @@ private: bool _withCredentialsValue; std::map _httpHeader; std::map _requestHeader; + bool _errorFlag; + bool _isAborted; }; TOLUA_API int register_xml_http_request(lua_State* L); From 042969ce351cebe4c8e6692f92c11deb5ad904cc Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Mon, 20 Oct 2014 16:14:24 +0800 Subject: [PATCH 2/3] Remove some useless rename functions in the cocos2dx.ini and cocos2dx_studio.ini --- tools/tolua/cocos2dx.ini | 6 +----- tools/tolua/cocos2dx_studio.ini | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index c08bc1d43f..c634744397 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -150,13 +150,9 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], ProgressTimer::[setReverseProgress=setReverseDirection], AnimationCache::[addAnimationsWithFile=addAnimations], - LayerGradient::[initWithColor=init], - LayerColor::[initWithColor=init], - GLProgram::[initWithVertexShaderByteArray=initWithString initWithVertexShaderFilename=init setUniformLocationWith1i=setUniformLocationI32], + GLProgram::[setUniformLocationWith1i=setUniformLocationI32], Node::[removeFromParentAndCleanup=removeFromParent removeAllChildrenWithCleanup=removeAllChildren], LabelAtlas::[create=_create], - Sprite::[initWithFile=init], - SpriteBatchNode::[initWithFile=init], Touch::[getID=getId], FileUtils::[loadFilenameLookupDictionaryFromFile=loadFilenameLookup], Director::[end=endToLua] diff --git a/tools/tolua/cocos2dx_studio.ini b/tools/tolua/cocos2dx_studio.ini index 86aac07dc8..995692a6c1 100644 --- a/tools/tolua/cocos2dx_studio.ini +++ b/tools/tolua/cocos2dx_studio.ini @@ -54,8 +54,7 @@ skip = *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType .* BaseData::[copy subtract], ActionTimelineCache::[getInstance loadActionTimelineFromXML] -rename_functions = GUIReader::[shareReader=getInstance purgeGUIReader=destroyInstance], - ActionManagerEx::[shareManager=getInstance purgeActionManager=destroyInstance], +rename_functions = ActionManagerEx::[shareManager=getInstance purgeActionManager=destroyInstance], SceneReader::[purgeSceneReader=destroyInstance] From 20bcafa143c50a971e913e73255055f6c1e150f9 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Mon, 20 Oct 2014 17:43:04 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Fix=20the=20bug=20that=20callbacks=20aren?= =?UTF-8?q?=E2=80=99t=20triggered=20when=20response=20failed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manual/network/lua_xml_http_request.cpp | 10 +++ .../XMLHttpRequestTest/XMLHttpRequestTest.lua | 62 ++++++++++++------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp index 6a078e9115..149c2446af 100644 --- a/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp +++ b/cocos/scripting/lua-bindings/manual/network/lua_xml_http_request.cpp @@ -216,6 +216,16 @@ void LuaMinXmlHttpRequest::_sendRequest() _status = 0; _statusText.clear(); } + // TODO: call back lua function + int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE ); + + if (0 != handler) + { + CCLOG("come in handler, handler is %d", handler); + cocos2d::CommonScriptData data(handler,""); + cocos2d::ScriptEvent event(cocos2d::ScriptEventType::kCommonEvent,(void*)&data); + cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } return; } diff --git a/tests/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua b/tests/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua index 2a591aca49..5680cab279 100644 --- a/tests/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua +++ b/tests/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua @@ -29,9 +29,13 @@ local function XMLHttpRequestLayer() xhr:open("GET", "http://httpbin.org/get") local function onReadyStateChange() - local statusString = "Http Status Code:"..xhr.statusText - labelStatusCode:setString(statusString) - print(xhr.response) + if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then + local statusString = "Http Status Code:"..xhr.statusText + labelStatusCode:setString(statusString) + print(xhr.response) + else + print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status) + end end xhr:registerScriptHandler(onReadyStateChange) @@ -53,8 +57,12 @@ local function XMLHttpRequestLayer() xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING xhr:open("POST", "http://httpbin.org/post") local function onReadyStateChange() - labelStatusCode:setString("Http Status Code:"..xhr.statusText) - print(xhr.response) + if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + print(xhr.response) + else + print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status) + end end xhr:registerScriptHandler(onReadyStateChange) xhr:send() @@ -76,19 +84,23 @@ local function XMLHttpRequestLayer() xhr:open("POST", "http://httpbin.org/post") local function onReadyStateChange() - local response = xhr.response - local size = table.getn(response) - local strInfo = "" - - for i = 1,size do - if 0 == response[i] then - strInfo = strInfo.."\'\\0\'" - else - strInfo = strInfo..string.char(response[i]) - end + if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then + local response = xhr.response + local size = table.getn(response) + local strInfo = "" + + for i = 1,size do + if 0 == response[i] then + strInfo = strInfo.."\'\\0\'" + else + strInfo = strInfo..string.char(response[i]) + end + end + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + print(strInfo) + else + print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status) end - labelStatusCode:setString("Http Status Code:"..xhr.statusText) - print(strInfo) end xhr:registerScriptHandler(onReadyStateChange) @@ -112,12 +124,16 @@ local function XMLHttpRequestLayer() xhr:open("POST", "http://httpbin.org/post") local function onReadyStateChange() - labelStatusCode:setString("Http Status Code:"..xhr.statusText) - local response = xhr.response - local output = json.decode(response,1) - table.foreach(output,function(i, v) print (i, v) end) - print("headers are") - table.foreach(output.headers,print) + if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + local response = xhr.response + local output = json.decode(response,1) + table.foreach(output,function(i, v) print (i, v) end) + print("headers are") + table.foreach(output.headers,print) + else + print("xhr.readyState is:", xhr.readyState, "xhr.status is: ",xhr.status) + end end xhr:registerScriptHandler(onReadyStateChange)