fixed #1647: Renaming enumerations to Cocos2d-x style. ERROR and ERROR_TIMEOUT is defined as macros on Windows, fuck it.

This commit is contained in:
James Chen 2013-06-03 09:55:43 +08:00
parent c29e434ead
commit f2f0f031c7
5 changed files with 40 additions and 39 deletions

View File

@ -225,7 +225,7 @@ enum WS_MSG {
}; };
WebSocket::WebSocket() WebSocket::WebSocket()
: _readyState(STATE_CONNECTING) : _readyState(kStateConnecting)
, _port(80) , _port(80)
, _wsHelper(NULL) , _wsHelper(NULL)
, _wsInstance(NULL) , _wsInstance(NULL)
@ -335,7 +335,7 @@ bool WebSocket::init(const Delegate& delegate,
void WebSocket::send(const std::string& message) void WebSocket::send(const std::string& message)
{ {
if (_readyState == STATE_OPEN) if (_readyState == kStateOpen)
{ {
// In main thread // In main thread
WsMessage* msg = new WsMessage(); 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."); CCAssert(binaryMsg != NULL && len > 0, "parameter invalid.");
if (_readyState == STATE_OPEN) if (_readyState == kStateOpen)
{ {
// In main thread // In main thread
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
@ -371,11 +371,11 @@ void WebSocket::close()
{ {
CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper); CCDirector::sharedDirector()->getScheduler()->unscheduleAllForTarget(_wsHelper);
if (_readyState == STATE_CLOSING || _readyState == STATE_CLOSED) if (_readyState == kStateClosing || _readyState == kStateClosed)
return; return;
CCLOG("websocket (%p) connection closed by client", this); CCLOG("websocket (%p) connection closed by client", this);
_readyState = STATE_CLOSED; _readyState = kStateClosed;
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING; msg->what = WS_MSG_TO_SUBTRHEAD_CLOSING;
@ -388,21 +388,21 @@ void WebSocket::close()
_delegate->onClose(this); _delegate->onClose(this);
} }
WebSocket::STATE WebSocket::getReadyState() WebSocket::State WebSocket::getReadyState()
{ {
return _readyState; return _readyState;
} }
int WebSocket::onSubThreadLoop() int WebSocket::onSubThreadLoop()
{ {
if (_readyState == STATE_CLOSED || _readyState == STATE_CLOSING) if (_readyState == kStateClosed || _readyState == kStateClosing)
{ {
libwebsocket_context_destroy(_wsContext); libwebsocket_context_destroy(_wsContext);
// return 1 to exit the loop. // return 1 to exit the loop.
return 1; return 1;
} }
if (_wsContext && _readyState != STATE_CLOSED && _readyState != STATE_CLOSING) if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing)
{ {
libwebsocket_service(_wsContext, 0); libwebsocket_service(_wsContext, 0);
} }
@ -442,7 +442,7 @@ void WebSocket::onSubThreadStarted()
_wsContext = libwebsocket_create_context(&info); _wsContext = libwebsocket_create_context(&info);
if(NULL != _wsContext){ if(NULL != _wsContext){
_readyState = STATE_CONNECTING; _readyState = kStateConnecting;
std::string name; std::string name;
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) { for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
name += (_wsProtocols[i].name); name += (_wsProtocols[i].name);
@ -480,14 +480,14 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == STATE_CONNECTING) || (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateConnecting)
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == STATE_CONNECTING) || (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == kStateConnecting)
) )
{ {
msg->what = WS_MSG_TO_UITHREAD_ERROR; 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; msg->what = WS_MSG_TO_UITHREAD_CLOSE;
} }
@ -498,7 +498,7 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
msg->what = WS_MSG_TO_UITHREAD_OPEN; msg->what = WS_MSG_TO_UITHREAD_OPEN;
_readyState = STATE_OPEN; _readyState = kStateOpen;
/* /*
* start the ball rolling, * start the ball rolling,
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
@ -572,10 +572,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
_wsHelper->quitSubThread(); _wsHelper->quitSubThread();
if (_readyState != STATE_CLOSED) if (_readyState != kStateClosed)
{ {
WsMessage* msg = new WsMessage(); WsMessage* msg = new WsMessage();
_readyState = STATE_CLOSED; _readyState = kStateClosed;
msg->what = WS_MSG_TO_UITHREAD_CLOSE; msg->what = WS_MSG_TO_UITHREAD_CLOSE;
_wsHelper->sendMessageToUIThread(msg); _wsHelper->sendMessageToUIThread(msg);
} }
@ -646,7 +646,8 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
break; break;
case WS_MSG_TO_UITHREAD_ERROR: 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); _delegate->onError(this, err);
} }
break; break;

View File

@ -57,11 +57,11 @@ public:
/** /**
* @brief Errors in websocket * @brief Errors in websocket
*/ */
enum ERROR enum ErrorCode
{ {
ERROR_TIMEOUT = 0, kErrorTimeout = 0,
ERROR_CONNECTION_FAILS, kErrorConnectionFailure,
ERROR_UNKNOWN kErrorUnknown
}; };
/** /**
@ -74,7 +74,7 @@ public:
virtual void onOpen(WebSocket* ws) = 0; virtual void onOpen(WebSocket* ws) = 0;
virtual void onMessage(WebSocket* ws, const Data& data) = 0; virtual void onMessage(WebSocket* ws, const Data& data) = 0;
virtual void onClose(WebSocket* ws) = 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 * Websocket state
*/ */
enum STATE enum State
{ {
STATE_CONNECTING = 0, kStateConnecting = 0,
STATE_OPEN, kStateOpen,
STATE_CLOSING, kStateClosing,
STATE_CLOSED kStateClosed
}; };
/** /**
* @brief Gets current state of connection. * @brief Gets current state of connection.
*/ */
STATE getReadyState(); State getReadyState();
private: private:
virtual void onSubThreadStarted(); virtual void onSubThreadStarted();
@ -134,7 +134,7 @@ private:
void *user, void *in, size_t len); void *user, void *in, size_t len);
private: private:
STATE _readyState; State _readyState;
std::string _host; std::string _host;
unsigned int _port; unsigned int _port;
std::string _path; std::string _path;
@ -142,7 +142,7 @@ private:
friend class WsThreadHelper; friend class WsThreadHelper;
WsThreadHelper* _wsHelper; WsThreadHelper* _wsHelper;
struct libwebsocket* _wsInstance; struct libwebsocket* _wsInstance;
struct libwebsocket_context* _wsContext; struct libwebsocket_context* _wsContext;
Delegate* _delegate; Delegate* _delegate;
int _SSLConnection; int _SSLConnection;

View File

@ -180,7 +180,7 @@ void WebSocketTestLayer::onClose(cocos2d::extension::WebSocket* ws)
CC_SAFE_DELETE(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); CCLog("Error was fired, error code: %d", error);
if (ws == _wsiError) if (ws == _wsiError)
@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender)
// Menu Callbacks // Menu Callbacks
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender) void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::CCObject *sender)
{ {
if (_wsiSendText->getReadyState() == WebSocket::STATE_OPEN) if (_wsiSendText->getReadyState() == WebSocket::kStateOpen)
{ {
_sendTextStatus->setString("Send Text WS is waiting..."); _sendTextStatus->setString("Send Text WS is waiting...");
_wsiSendText->send("Hello WebSocket, I'm a text message."); _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) void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::CCObject *sender)
{ {
if (_wsiSendBinary->getReadyState() == WebSocket::STATE_OPEN) if (_wsiSendBinary->getReadyState() == WebSocket::kStateOpen)
{ {
_sendBinaryStatus->setString("Send Binary WS is waiting..."); _sendBinaryStatus->setString("Send Binary WS is waiting...");
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0."; char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";

View File

@ -24,7 +24,7 @@ public:
virtual void onOpen(cocos2d::extension::WebSocket* ws); virtual void onOpen(cocos2d::extension::WebSocket* ws);
virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data); virtual void onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data);
virtual void onClose(cocos2d::extension::WebSocket* ws); 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); void toExtensionsMainLayer(cocos2d::CCObject *sender);

View File

@ -129,7 +129,7 @@ public:
CC_SAFE_DELETE(ws); 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_proxy_t * p;
JS_GET_PROXY(p, ws); 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; })()")); 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); , 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); , 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); , 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); , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
// make the class enumerable in the registered namespace // make the class enumerable in the registered namespace