diff --git a/extensions/network/WebSocket.cpp b/extensions/network/WebSocket.cpp index a2a2516620..93ecea4157 100644 --- a/extensions/network/WebSocket.cpp +++ b/extensions/network/WebSocket.cpp @@ -225,7 +225,7 @@ enum WS_MSG { }; WebSocket::WebSocket() -: _readyState(STATE_CONNECTING) +: _readyState(kStateConnecting) , _port(80) , _wsHelper(NULL) , _wsInstance(NULL) @@ -335,7 +335,7 @@ bool WebSocket::init(const Delegate& delegate, void WebSocket::send(const std::string& message) { - if (_readyState == STATE_OPEN) + if (_readyState == kStateOpen) { // In main thread WsMessage* msg = new WsMessage(); @@ -353,7 +353,7 @@ void WebSocket::send(const unsigned char* binaryMsg, unsigned int len) { CCAssert(binaryMsg != NULL && len > 0, "parameter invalid."); - if (_readyState == STATE_OPEN) + if (_readyState == kStateOpen) { // In main thread WsMessage* msg = new WsMessage(); @@ -371,11 +371,11 @@ void WebSocket::close() { CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper); - if (_readyState == STATE_CLOSING || _readyState == STATE_CLOSED) + if (_readyState == kStateClosing || _readyState == kStateClosed) return; CCLOG("websocket (%p) connection closed by client", this); - _readyState = STATE_CLOSED; + _readyState = kStateClosed; WsMessage* msg = new WsMessage(); msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING; @@ -388,21 +388,21 @@ void WebSocket::close() _delegate->onClose(this); } -WebSocket::STATE WebSocket::getReadyState() +WebSocket::State WebSocket::getReadyState() { return _readyState; } int WebSocket::onSubThreadLoop() { - if (_readyState == STATE_CLOSED || _readyState == STATE_CLOSING) + if (_readyState == kStateClosed || _readyState == kStateClosing) { libwebsocket_context_destroy(_wsContext); // return 1 to exit the loop. return 1; } - if (_wsContext && _readyState != STATE_CLOSED && _readyState != STATE_CLOSING) + if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing) { libwebsocket_service(_wsContext, 0); } @@ -442,7 +442,7 @@ void WebSocket::onSubThreadStarted() _wsContext = libwebsocket_create_context(&info); if(NULL != _wsContext){ - _readyState = STATE_CONNECTING; + _readyState = kStateConnecting; std::string name; for (int i = 0; _wsProtocols[i].callback != NULL; ++i) { name += (_wsProtocols[i].name); @@ -480,14 +480,14 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, { WsMessage* msg = new WsMessage(); if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR - || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CONNECTING) - || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == STATE_CONNECTING) + || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateConnecting) + || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == kStateConnecting) ) { msg->what = WS_MSG_TO_UITHREAD_ERROR; - _readyState = STATE_CLOSING; + _readyState = kStateClosing; } - else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CLOSING) + else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateClosing) { msg->what = WS_MSG_TO_UITHREAD_CLOSE; } @@ -498,7 +498,7 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, { WsMessage* msg = new WsMessage(); msg->what = WS_MSG_TO_UITHREAD_OPEN; - _readyState = STATE_OPEN; + _readyState = kStateOpen; /* * start the ball rolling, * LWS_CALLBACK_CLIENT_WRITEABLE will come next service @@ -572,10 +572,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx, _wsHelper->quitSubThread(); - if (_readyState != STATE_CLOSED) + if (_readyState != kStateClosed) { WsMessage* msg = new WsMessage(); - _readyState = STATE_CLOSED; + _readyState = kStateClosed; msg->what = WS_MSG_TO_UITHREAD_CLOSE; _wsHelper->sendMessageToUIThread(msg); } @@ -646,7 +646,8 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg) break; case WS_MSG_TO_UITHREAD_ERROR: { - WebSocket::ERROR err = ERROR_CONNECTION_FAILS; + // FIXME: The exact error needs to be checked. + WebSocket::ErrorCode err = kErrorConnectionFailure; _delegate->onError(this, err); } break; diff --git a/extensions/network/WebSocket.h b/extensions/network/WebSocket.h index f7698fff60..068f341d60 100644 --- a/extensions/network/WebSocket.h +++ b/extensions/network/WebSocket.h @@ -57,11 +57,11 @@ public: /** * @brief Errors in websocket */ - enum ERROR + enum ErrorCode { - ERROR_TIMEOUT = 0, - ERROR_CONNECTION_FAILS, - ERROR_UNKNOWN + kErrorTimeout = 0, + kErrorConnectionFailure, + kErrorUnknown }; /** @@ -74,7 +74,7 @@ public: virtual void onOpen(WebSocket* ws) = 0; virtual void onMessage(WebSocket* ws, const Data& data) = 0; virtual void onClose(WebSocket* ws) = 0; - virtual void onError(WebSocket* ws, const ERROR& error) = 0; + virtual void onError(WebSocket* ws, const ErrorCode& error) = 0; }; @@ -107,18 +107,18 @@ public: /** * Websocket state */ - enum STATE + enum State { - STATE_CONNECTING = 0, - STATE_OPEN, - STATE_CLOSING, - STATE_CLOSED + kStateConnecting = 0, + kStateOpen, + kStateClosing, + kStateClosed }; /** * @brief Gets current state of connection. */ - STATE getReadyState(); + State getReadyState(); private: virtual void onSubThreadStarted(); @@ -134,7 +134,7 @@ private: void *user, void *in, size_t len); private: - STATE _readyState; + State _readyState; std::string _host; unsigned int _port; std::string _path; @@ -142,7 +142,7 @@ private: friend class WsThreadHelper; WsThreadHelper* _wsHelper; - struct libwebsocket* _wsInstance; + struct libwebsocket* _wsInstance; struct libwebsocket_context* _wsContext; Delegate* _delegate; int _SSLConnection; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp index e80a0a8dae..c568c521aa 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp @@ -180,7 +180,7 @@ void WebSocketTestLayer::onClose(cocos2d::extension::WebSocket* ws) CC_SAFE_DELETE(ws); } -void WebSocketTestLayer::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ERROR& error) +void WebSocketTestLayer::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error) { CCLog("Error was fired, error code: %d", error); if (ws == _wsiError) @@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender) // Menu Callbacks void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender) { - if (_wsiSendText->getReadyState() == WebSocket::STATE_OPEN) + if (_wsiSendText->getReadyState() == WebSocket::kStateOpen) { _sendTextStatus->setString("Send Text WS is waiting..."); _wsiSendText->send("Hello WebSocket, I'm a text message."); @@ -216,7 +216,7 @@ void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender) void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::CCObject *sender) { - if (_wsiSendBinary->getReadyState() == WebSocket::STATE_OPEN) + if (_wsiSendBinary->getReadyState() == WebSocket::kStateOpen) { _sendBinaryStatus->setString("Send Binary WS is waiting..."); char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0."; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h index dd4d1991cd..087c0bedaa 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest/WebSocketTest.h @@ -24,7 +24,7 @@ public: virtual void onOpen(cocos2d::extension::WebSocket* ws); virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data); virtual void onClose(cocos2d::extension::WebSocket* ws); - virtual void onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ERROR& error); + virtual void onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error); void toExtensionsMainLayer(cocos2d::CCObject *sender); diff --git a/scripting/javascript/bindings/jsb_websocket.cpp b/scripting/javascript/bindings/jsb_websocket.cpp index eea1ad41ff..12c97b5e57 100644 --- a/scripting/javascript/bindings/jsb_websocket.cpp +++ b/scripting/javascript/bindings/jsb_websocket.cpp @@ -129,7 +129,7 @@ public: CC_SAFE_DELETE(ws); } - virtual void onError(WebSocket* ws, const WebSocket::ERROR& error) + virtual void onError(WebSocket* ws, const WebSocket::ErrorCode& error) { js_proxy_t * p; JS_GET_PROXY(p, ws); @@ -371,13 +371,13 @@ void register_jsb_websocket(JSContext *cx, JSObject *global) { JSObject* jsclassObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return WebSocket; })()")); - JS_DefineProperty(cx, jsclassObj, "CONNECTING", INT_TO_JSVAL((int)WebSocket::STATE_CONNECTING) + JS_DefineProperty(cx, jsclassObj, "CONNECTING", INT_TO_JSVAL((int)WebSocket::kStateConnecting) , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); - JS_DefineProperty(cx, jsclassObj, "OPEN", INT_TO_JSVAL((int)WebSocket::STATE_OPEN) + JS_DefineProperty(cx, jsclassObj, "OPEN", INT_TO_JSVAL((int)WebSocket::kStateOpen) , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); - JS_DefineProperty(cx, jsclassObj, "CLOSING", INT_TO_JSVAL((int)WebSocket::STATE_CLOSING) + JS_DefineProperty(cx, jsclassObj, "CLOSING", INT_TO_JSVAL((int)WebSocket::kStateClosing) , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); - JS_DefineProperty(cx, jsclassObj, "CLOSED", INT_TO_JSVAL((int)WebSocket::STATE_CLOSED) + JS_DefineProperty(cx, jsclassObj, "CLOSED", INT_TO_JSVAL((int)WebSocket::kStateClosed) , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); // make the class enumerable in the registered namespace