Merge pull request #45 from WenhaiLin/master

[JSB]Fixed crash when WebSocket is closed.
This commit is contained in:
Wenhai Lin
2016-04-27 14:22:12 +08:00
9 changed files with 83 additions and 92 deletions
+2 -2
View File
@@ -203,9 +203,9 @@ LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dxandroid_static
# define the macro to compile through support/zip_support/ioapi.c
LOCAL_CFLAGS := -DUSE_FILE32API -fexceptions
LOCAL_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat
LOCAL_CPPFLAGS := -Wno-deprecated-declarations
LOCAL_EXPORT_CFLAGS := -DUSE_FILE32API
LOCAL_EXPORT_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat
LOCAL_EXPORT_CPPFLAGS := -Wno-deprecated-declarations
include $(BUILD_STATIC_LIBRARY)
+3 -1
View File
@@ -635,7 +635,9 @@ void EventDispatcher::removeEventListener(EventListener* listener)
if (iter->second->empty())
{
_priorityDirtyFlagMap.erase(listener->getTypeKey());
auto typeKey = listener->getTypeKey();
if(_priorityDirtyFlagMap.find(typeKey) != _priorityDirtyFlagMap.end())
_priorityDirtyFlagMap.erase(typeKey);
auto list = iter->second;
iter = _listenerMap.erase(iter);
CC_SAFE_DELETE(list);
+14 -7
View File
@@ -167,14 +167,21 @@ long getCharacterCountInUTF8String(const std::string& utf8)
} //namespace StringUtils {
namespace
{
int wcslen_internal(const unsigned short* str)
{
if (str == nullptr)
return -1;
int i=0;
while(*str++) i++;
return i;
}
}
int cc_wcslen(const unsigned short* str)
{
if (str == nullptr)
return -1;
int i=0;
while(*str++) i++;
return i;
return wcslen_internal(str);
}
void cc_utf8_trim_ws(std::vector<unsigned short>* str)
@@ -224,7 +231,7 @@ std::vector<unsigned short> cc_utf16_vec_from_utf16_str(const unsigned short* st
if (str == nullptr)
return str_new;
int len = cc_wcslen(str);
int len = wcslen_internal(str);
for (int i = 0; i < len; ++i)
{
@@ -267,7 +274,7 @@ char * cc_utf16_to_utf8 (const unsigned short *str,
std::u16string utf16;
int utf16Len = len < 0 ? cc_wcslen(str) : len;
int utf16Len = len < 0 ? wcslen_internal(str) : len;
for (int i = 0; i < utf16Len; ++i)
{
+57 -73
View File
@@ -35,6 +35,7 @@
#include <mutex>
#include <queue>
#include <list>
#include <unordered_set>
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
@@ -135,9 +136,9 @@ public:
protected:
void wsThreadEntryFunc();
private:
std::list<WsMessage*>* _subThreadWsMessageQueue;
std::list<WsMessage*> _subThreadWsMessageQueue;
std::mutex _subThreadWsMessageQueueMutex;
std::thread* _subThreadInstance;
std::thread _subThreadInstance;
WebSocket* _ws;
friend class WebSocket;
@@ -165,24 +166,19 @@ public:
};
WsThreadHelper::WsThreadHelper()
: _subThreadInstance(nullptr)
, _ws(nullptr)
: _ws(nullptr)
{
_subThreadWsMessageQueue = new (std::nothrow) std::list<WsMessage*>();
}
WsThreadHelper::~WsThreadHelper()
{
joinWebSocketThread();
CC_SAFE_DELETE(_subThreadInstance);
delete _subThreadWsMessageQueue;
}
bool WsThreadHelper::createWebSocketThread(const WebSocket& ws)
{
_ws = const_cast<WebSocket*>(&ws);
_subThreadInstance = new (std::nothrow) std::thread(&WebSocket::onSubThreadLoop, _ws);
_subThreadInstance = std::thread(&WebSocket::onSubThreadLoop, _ws);
return true;
}
@@ -195,14 +191,14 @@ void WsThreadHelper::sendMessageToCocosThread(const std::function<void()>& cb)
void WsThreadHelper::sendMessageToWebSocketThread(WsMessage *msg)
{
std::lock_guard<std::mutex> lk(_subThreadWsMessageQueueMutex);
_subThreadWsMessageQueue->push_back(msg);
_subThreadWsMessageQueue.push_back(msg);
}
void WsThreadHelper::joinWebSocketThread()
{
if (_subThreadInstance->joinable())
if (_subThreadInstance.joinable())
{
_subThreadInstance->join();
_subThreadInstance.join();
}
}
@@ -262,17 +258,15 @@ enum WS_MSG {
WS_MSG_TO_SUBTRHEAD_SENDING_BINARY,
};
static std::vector<WebSocket*>* __websocketInstances = nullptr;
static std::unordered_set<WebSocket*>* __websocketInstances = nullptr;
void WebSocket::closeAllConnections()
{
if (__websocketInstances != nullptr)
{
auto copyInstances = *__websocketInstances;
ssize_t count = __websocketInstances->size();
for (ssize_t i = count-1; i >=0 ; i--)
for (auto&& instance : copyInstances)
{
WebSocket* instance = copyInstances.at(i);
instance->close();
}
@@ -287,7 +281,6 @@ WebSocket::WebSocket()
, _wsHelper(nullptr)
, _wsInstance(nullptr)
, _wsContext(nullptr)
, _isDestroyed(std::make_shared<std::atomic<bool>>(false))
, _delegate(nullptr)
, _SSLConnection(0)
, _wsProtocols(nullptr)
@@ -296,25 +289,36 @@ WebSocket::WebSocket()
_receivedData.reserve(WS_RESERVE_RECEIVE_BUFFER_SIZE);
if (__websocketInstances == nullptr)
{
__websocketInstances = new (std::nothrow) std::vector<WebSocket*>();
__websocketInstances = new (std::nothrow) std::unordered_set<WebSocket*>();
}
__websocketInstances->push_back(this);
__websocketInstances->insert(this);
auto eventDispatcher = Director::getInstance()->getEventDispatcher();
auto isDestroyed = _isDestroyed;
auto instance = this;
_resetDirectorListener = eventDispatcher->addCustomEventListener(Director::EVENT_RESET,
[this, isDestroyed](EventCustom*)
[instance](EventCustom*)
{
if (*isDestroyed)
return;
close();
if (__websocketInstances && __websocketInstances->find(instance) != __websocketInstances->end())
{
instance->close();
}
});
}
WebSocket::~WebSocket()
{
*_isDestroyed = true;
if (__websocketInstances != nullptr)
{
auto iter = __websocketInstances->find(this);
if (iter != __websocketInstances->end())
{
__websocketInstances->erase(iter);
}
else
{
LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this);
}
}
if (Director::DirectorInstance) {
Director::DirectorInstance->getEventDispatcher()->removeEventListener(_resetDirectorListener);
@@ -331,19 +335,6 @@ WebSocket::~WebSocket()
}
}
CC_SAFE_DELETE_ARRAY(_wsProtocols);
if (__websocketInstances != nullptr)
{
auto iter = std::find(__websocketInstances->begin(), __websocketInstances->end(), this);
if (iter != __websocketInstances->end())
{
__websocketInstances->erase(iter);
}
else
{
LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this);
}
}
}
bool WebSocket::init(const Delegate& delegate,
@@ -533,7 +524,7 @@ void WebSocket::onSubThreadLoop()
{
_readStateMutex.unlock();
_wsHelper->_subThreadWsMessageQueueMutex.lock();
bool isEmpty = _wsHelper->_subThreadWsMessageQueue->empty();
bool isEmpty = _wsHelper->_subThreadWsMessageQueue.empty();
_wsHelper->_subThreadWsMessageQueueMutex.unlock();
if (!isEmpty)
{
@@ -644,15 +635,15 @@ void WebSocket::onClientWritable()
{
std::lock_guard<std::mutex> lk(_wsHelper->_subThreadWsMessageQueueMutex);
if (_wsHelper->_subThreadWsMessageQueue->empty())
if (_wsHelper->_subThreadWsMessageQueue.empty())
{
return;
}
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue.begin();
ssize_t bytesWrite = 0;
if (iter != _wsHelper->_subThreadWsMessageQueue->end())
if (iter != _wsHelper->_subThreadWsMessageQueue.end())
{
WsMessage* subThreadMsg = *iter;
Data* data = (Data*)subThreadMsg->obj;
@@ -683,7 +674,7 @@ void WebSocket::onClientWritable()
delete frame;
CC_SAFE_FREE(data->bytes);
CC_SAFE_DELETE(data);
_wsHelper->_subThreadWsMessageQueue->erase(iter);
_wsHelper->_subThreadWsMessageQueue.erase(iter);
CC_SAFE_DELETE(subThreadMsg);
return;
}
@@ -725,7 +716,7 @@ void WebSocket::onClientWritable()
delete ((WebSocketFrame*)data->ext);
data->ext = nullptr;
CC_SAFE_DELETE(data);
_wsHelper->_subThreadWsMessageQueue->erase(iter);
_wsHelper->_subThreadWsMessageQueue.erase(iter);
CC_SAFE_DELETE(subThreadMsg);
closeAsync();
@@ -764,7 +755,7 @@ void WebSocket::onClientWritable()
delete ((WebSocketFrame*)data->ext);
data->ext = nullptr;
CC_SAFE_DELETE(data);
_wsHelper->_subThreadWsMessageQueue->erase(iter);
_wsHelper->_subThreadWsMessageQueue.erase(iter);
CC_SAFE_DELETE(subThreadMsg);
LOGD("-----------------------------------------------------------\n");
@@ -809,8 +800,8 @@ void WebSocket::onClientReceivedData(void* in, ssize_t len)
frameData->push_back('\0');
}
auto isDestroyed = _isDestroyed;
_wsHelper->sendMessageToCocosThread([this, frameData, frameSize, isBinary, isDestroyed](){
auto instance = this;
_wsHelper->sendMessageToCocosThread([instance, frameData, frameSize, isBinary](){
// In UI thread
LOGD("Notify data len %d to Cocos thread.\n", (int)frameSize);
@@ -819,13 +810,9 @@ void WebSocket::onClientReceivedData(void* in, ssize_t len)
data.bytes = (char*)frameData->data();
data.len = frameSize;
if (*isDestroyed)
if (__websocketInstances && __websocketInstances->find(instance) != __websocketInstances->end())
{
LOGD("WebSocket instance was destroyed!\n");
}
else
{
_delegate->onMessage(this, data);
instance->_delegate->onMessage(instance, data);
}
delete frameData;
@@ -845,12 +832,11 @@ void WebSocket::onConnectionOpened()
_readyState = State::OPEN;
_readStateMutex.unlock();
auto isDestroyed = _isDestroyed;
_wsHelper->sendMessageToCocosThread([this, isDestroyed](){
if (*isDestroyed) {
LOGD("WebSocket instance was destroyed!\n");
} else {
_delegate->onOpen(this);
auto instance = this;
_wsHelper->sendMessageToCocosThread([instance](){
if (__websocketInstances && __websocketInstances->find(instance) != __websocketInstances->end())
{
instance->_delegate->onOpen(instance);
}
});
}
@@ -863,12 +849,11 @@ void WebSocket::onConnectionError()
_readyState = State::CLOSING;
_readStateMutex.unlock();
auto isDestroyed = _isDestroyed;
_wsHelper->sendMessageToCocosThread([this, isDestroyed](){
if (*isDestroyed) {
LOGD("WebSocket instance was destroyed!\n");
} else {
_delegate->onError(this, ErrorCode::CONNECTION_FAILURE);
auto instance = this;
_wsHelper->sendMessageToCocosThread([instance](){
if (__websocketInstances && __websocketInstances->find(instance) != __websocketInstances->end())
{
instance->_delegate->onError(instance, ErrorCode::CONNECTION_FAILURE);
}
});
}
@@ -887,14 +872,13 @@ void WebSocket::onConnectionClosed()
_readyState = State::CLOSED;
_readStateMutex.unlock();
auto isDestroyed = _isDestroyed;
_wsHelper->sendMessageToCocosThread([this, isDestroyed](){
if (*isDestroyed) {
LOGD("WebSocket instance was destroyed!\n");
} else {
auto instance = this;
_wsHelper->sendMessageToCocosThread([instance](){
if (__websocketInstances && __websocketInstances->find(instance) != __websocketInstances->end())
{
//Waiting for the subThread safety exit
_wsHelper->joinWebSocketThread();
_delegate->onClose(this);
instance->_wsHelper->joinWebSocketThread();
instance->_delegate->onClose(instance);
}
});
}
+1 -2
View File
@@ -30,7 +30,6 @@
#ifndef __CC_WEBSOCKET_H__
#define __CC_WEBSOCKET_H__
#include <atomic>
#include <string>
#include <vector>
#include <mutex>
@@ -238,7 +237,7 @@ private:
struct lws* _wsInstance;
struct lws_context* _wsContext;
std::shared_ptr<std::atomic<bool>> _isDestroyed;
Delegate* _delegate;
int _SSLConnection;
struct lws_protocols* _wsProtocols;
-4
View File
@@ -34,8 +34,4 @@ LOCAL_EXPORT_LDLIBS := -lGLESv1_CM \
-llog \
-landroid
LOCAL_CPPFLAGS := -Wno-extern-c-compat
LOCAL_EXPORT_CPPFLAGS := -Wno-extern-c-compat
include $(BUILD_STATIC_LIBRARY)
@@ -301,6 +301,10 @@ void GLProgramCache::reloadDefaultGLPrograms()
p = getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
p->reset();
loadDefaultGLProgram(p, kShaderType_CameraClear);
p = getGLProgram(GLProgram::SHADER_NAME_POSITION_GRAYSCALE);
p->reset();
loadDefaultGLProgram(p, kShaderType_UIGrayScale);
}
void GLProgramCache::reloadDefaultGLProgramsRelativeToLights()
@@ -159,9 +159,8 @@ public:
JS::RootedValue args(cx, OBJECT_TO_JSVAL(jsobj));
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate.ref()), "onclose", 1, args.address());
auto copy = &p->obj;
JS::RemoveObjectRoot(cx, &p->obj);
jsb_remove_proxy(p);
JS::RemoveObjectRoot(cx, copy);
}
// Delete WebSocket instance
@@ -37,7 +37,7 @@ namespace ui {
class EditBox;
class EditBoxImplCommon : public EditBoxImpl
class CC_GUI_DLL EditBoxImplCommon : public EditBoxImpl
{
public:
/**