Skip to content

Commit

Permalink
fix crash when webscoket server close connect, client receive close s…
Browse files Browse the repository at this point in the history
…ignal.
  • Loading branch information
u0u0 committed Sep 16, 2013
1 parent f62c8b5 commit 6a3b6d4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion extensions/network/WebSocket.cpp
Expand Up @@ -82,6 +82,7 @@ class WsThreadHelper : public cocos2d::CCObject
pthread_t _subThreadInstance;
WebSocket* _ws;
bool _needQuit;
bool _isQuited;
friend class WebSocket;
};

Expand All @@ -108,6 +109,7 @@ class WebSocketCallbackWrapper {
WsThreadHelper::WsThreadHelper()
: _ws(NULL)
, _needQuit(false)
, _isQuited(false)
{
_UIWsMessageQueue = new std::list<WsMessage*>();
pthread_mutex_init(&_UIWsMessageQueueMutex, NULL);
Expand Down Expand Up @@ -170,7 +172,8 @@ void* WsThreadHelper::wsThreadEntryFunc(void* arg)
}

_ws->onSubThreadEnded();

_isQuited = true;

return (void*)0;
}

Expand Down Expand Up @@ -647,6 +650,14 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
break;
case WS_MSG_TO_UITHREAD_CLOSE:
{
// wait subThread exit to avoid the crash on _ws->onSubThreadEnded()
while (!_wsHelper->_isQuited) {
#ifdef WIN32
Sleep(50);
#else
usleep(50000);
#endif
}
_delegate->onClose(this);
}
break;
Expand Down

0 comments on commit 6a3b6d4

Please sign in to comment.