2013-05-31 23:13:03 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
http://www.cocos2d-x.org
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2013-06-06 14:16:57 +08:00
|
|
|
|
|
|
|
"[WebSocket module] is based in part on the work of the libwebsockets project
|
|
|
|
(http://libwebsockets.org)"
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-25 23:57:40 +08:00
|
|
|
#include "network/WebSocket.h"
|
2017-03-07 13:35:59 +08:00
|
|
|
#include "network/Uri.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
2016-02-29 18:55:51 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
2017-01-19 13:55:14 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-07-25 10:33:19 +08:00
|
|
|
|
2013-06-25 12:51:44 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
2013-05-31 23:13:03 +08:00
|
|
|
#include <queue>
|
2014-08-29 15:39:52 +08:00
|
|
|
#include <list>
|
2013-05-31 23:13:03 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-07-25 10:33:19 +08:00
|
|
|
#include "libwebsockets.h"
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
#define NS_NETWORK_BEGIN namespace cocos2d { namespace network {
|
|
|
|
#define NS_NETWORK_END }}
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
#define WS_RX_BUFFER_SIZE (65536)
|
|
|
|
#define WS_RESERVE_RECEIVE_BUFFER_SIZE (4096)
|
|
|
|
|
|
|
|
#define LOG_TAG "WebSocket.cpp"
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
// log, CCLOG aren't threadsafe, since we uses sub threads for parsing pcm data, threadsafe log output
|
|
|
|
// is needed. Define the following macros (ALOGV, ALOGD, ALOGI, ALOGW, ALOGE) for threadsafe log output.
|
|
|
|
|
|
|
|
//FIXME: Move _winLog, winLog to a separated file
|
|
|
|
static void _winLog(const char *format, va_list args)
|
|
|
|
{
|
|
|
|
static const int MAX_LOG_LENGTH = 16 * 1024;
|
|
|
|
int bufferSize = MAX_LOG_LENGTH;
|
|
|
|
char* buf = nullptr;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
buf = new (std::nothrow) char[bufferSize];
|
|
|
|
if (buf == nullptr)
|
|
|
|
return; // not enough memory
|
|
|
|
|
|
|
|
int ret = vsnprintf(buf, bufferSize - 3, format, args);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bufferSize *= 2;
|
|
|
|
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
strcat(buf, "\n");
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
int len = strlen(buf);
|
|
|
|
char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
|
|
|
|
WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
|
|
|
|
|
|
|
|
tempBuf[MAX_LOG_LENGTH] = 0;
|
|
|
|
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
|
|
|
|
OutputDebugStringW(wszBuf);
|
|
|
|
|
|
|
|
pos += MAX_LOG_LENGTH;
|
|
|
|
|
|
|
|
} while (pos < len);
|
|
|
|
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wsLog(const char * format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
_winLog(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define wsLog printf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define QUOTEME_(x) #x
|
|
|
|
#define QUOTEME(x) QUOTEME_(x)
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
// Since CCLOG isn't thread safe, we uses LOGD for multi-thread logging.
|
2017-02-20 09:44:20 +08:00
|
|
|
#ifdef ANDROID
|
|
|
|
#if COCOS2D_DEBUG > 0
|
2017-01-19 13:55:14 +08:00
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG,__VA_ARGS__)
|
2016-01-07 23:15:11 +08:00
|
|
|
#else
|
2017-02-20 09:44:20 +08:00
|
|
|
#define LOGD(...)
|
2016-01-07 23:15:11 +08:00
|
|
|
#endif
|
2017-02-20 09:44:20 +08:00
|
|
|
|
|
|
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
|
2016-01-07 23:15:11 +08:00
|
|
|
#else
|
2017-02-20 09:44:20 +08:00
|
|
|
#if COCOS2D_DEBUG > 0
|
|
|
|
#define LOGD(fmt, ...) wsLog("D/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define LOGD(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LOGE(fmt, ...) wsLog("E/" LOG_TAG " (" QUOTEME(__LINE__) "): " fmt "", ##__VA_ARGS__)
|
2016-01-07 23:15:11 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void printWebSocketLog(int level, const char *line)
|
|
|
|
{
|
|
|
|
#if COCOS2D_DEBUG > 0
|
|
|
|
static const char * const log_level_names[] = {
|
|
|
|
"ERR",
|
|
|
|
"WARN",
|
|
|
|
"NOTICE",
|
|
|
|
"INFO",
|
|
|
|
"DEBUG",
|
|
|
|
"PARSER",
|
|
|
|
"HEADER",
|
|
|
|
"EXTENSION",
|
|
|
|
"CLIENT",
|
|
|
|
"LATENCY",
|
|
|
|
};
|
|
|
|
|
|
|
|
char buf[30] = {0};
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < LLL_COUNT; n++) {
|
|
|
|
if (level != (1 << n))
|
|
|
|
continue;
|
|
|
|
sprintf(buf, "%s: ", log_level_names[n]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("%s%s\n", buf, line);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
#endif // #if COCOS2D_DEBUG > 0
|
|
|
|
}
|
2014-01-09 13:29:56 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
NS_NETWORK_BEGIN
|
|
|
|
|
|
|
|
enum WS_MSG {
|
|
|
|
WS_MSG_TO_SUBTRHEAD_SENDING_STRING = 0,
|
|
|
|
WS_MSG_TO_SUBTRHEAD_SENDING_BINARY,
|
|
|
|
WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<WebSocket*>* __websocketInstances = nullptr;
|
|
|
|
static std::mutex __instanceMutex;
|
|
|
|
static struct lws_context* __wsContext = nullptr;
|
|
|
|
static WsThreadHelper* __wsHelper = nullptr;
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
static std::string getFileNameForPath(const std::string& filePath)
|
|
|
|
{
|
|
|
|
std::string fileName = filePath;
|
|
|
|
const size_t lastSlashIdx = fileName.find_last_of("\\/");
|
|
|
|
if (std::string::npos != lastSlashIdx)
|
|
|
|
{
|
|
|
|
fileName.erase(0, lastSlashIdx + 1);
|
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct lws_protocols __defaultProtocols[2];
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
static lws_context_creation_info convertToContextCreationInfo(const struct lws_protocols* protocols, bool peerServerCert)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
lws_context_creation_info info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
/*
|
|
|
|
* create the websocket context. This tracks open connections and
|
|
|
|
* knows how to route any traffic and which protocol version to use,
|
|
|
|
* and if each connection is client or server side.
|
|
|
|
*
|
|
|
|
* For this client-only demo, we tell it to not listen on any port.
|
|
|
|
*/
|
|
|
|
|
|
|
|
info.port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
info.protocols = protocols;
|
2013-10-15 18:00:03 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
// FIXME: Disable 'permessage-deflate' extension temporarily because of issues:
|
|
|
|
// https://github.com/cocos2d/cocos2d-x/issues/16045, https://github.com/cocos2d/cocos2d-x/issues/15767
|
|
|
|
// libwebsockets issue: https://github.com/warmcat/libwebsockets/issues/593
|
|
|
|
// Currently, we couldn't find out the exact reason.
|
|
|
|
// libwebsockets official said it's probably an issue of user code
|
|
|
|
// since 'libwebsockets' passed AutoBahn stressed Test.
|
|
|
|
|
|
|
|
// info.extensions = exts;
|
|
|
|
|
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
2017-02-13 15:15:23 +08:00
|
|
|
if (peerServerCert)
|
|
|
|
{
|
|
|
|
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED;
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
info.user = nullptr;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
class WsMessage
|
|
|
|
{
|
|
|
|
public:
|
2017-01-19 13:55:14 +08:00
|
|
|
WsMessage() : id(++__id), what(0), data(nullptr), user(nullptr){}
|
2016-01-07 23:15:11 +08:00
|
|
|
unsigned int id;
|
2013-05-31 23:13:03 +08:00
|
|
|
unsigned int what; // message type
|
2017-01-19 13:55:14 +08:00
|
|
|
void* data;
|
|
|
|
void* user;
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
static unsigned int __id;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
unsigned int WsMessage::__id = 0;
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Websocket thread helper, it's used for sending message between UI thread and websocket thread.
|
|
|
|
*/
|
2016-01-07 23:15:11 +08:00
|
|
|
class WsThreadHelper
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
WsThreadHelper();
|
|
|
|
~WsThreadHelper();
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
// Creates a new thread
|
2017-01-19 13:55:14 +08:00
|
|
|
bool createWebSocketThread();
|
2016-01-13 21:04:48 +08:00
|
|
|
// Quits websocket thread.
|
|
|
|
void quitWebSocketThread();
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-01-11 09:56:27 +08:00
|
|
|
// Sends message to Cocos thread. It's needed to be invoked in Websocket thread.
|
|
|
|
void sendMessageToCocosThread(const std::function<void()>& cb);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-01-11 09:56:27 +08:00
|
|
|
// Sends message to Websocket thread. It's needs to be invoked in Cocos thread.
|
|
|
|
void sendMessageToWebSocketThread(WsMessage *msg);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
// Waits the sub-thread (websocket thread) to exit,
|
2016-01-11 09:56:27 +08:00
|
|
|
void joinWebSocketThread();
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
void onSubThreadStarted();
|
|
|
|
void onSubThreadLoop();
|
|
|
|
void onSubThreadEnded();
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
protected:
|
2013-06-25 12:51:44 +08:00
|
|
|
void wsThreadEntryFunc();
|
2017-01-19 13:55:14 +08:00
|
|
|
public:
|
2013-05-31 23:13:03 +08:00
|
|
|
std::list<WsMessage*>* _subThreadWsMessageQueue;
|
2013-06-25 12:51:44 +08:00
|
|
|
std::mutex _subThreadWsMessageQueueMutex;
|
|
|
|
std::thread* _subThreadInstance;
|
2017-01-19 13:55:14 +08:00
|
|
|
private:
|
2013-05-31 23:13:03 +08:00
|
|
|
bool _needQuit;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wrapper for converting websocket callback from static function to member function of WebSocket class.
|
|
|
|
class WebSocketCallbackWrapper {
|
|
|
|
public:
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
static int onSocketCallback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
// Gets the user data from context. We know that it's a 'WebSocket' instance.
|
2016-01-07 23:15:11 +08:00
|
|
|
if (wsi == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
int ret = 0;
|
|
|
|
WebSocket* ws = (WebSocket*)lws_wsi_user(wsi);
|
|
|
|
if (ws != nullptr && __websocketInstances != nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
if (std::find(__websocketInstances->begin(), __websocketInstances->end(), ws) != __websocketInstances->end())
|
|
|
|
{
|
|
|
|
ret = ws->onSocketCallback(wsi, reason, in, len);
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// LOGD("ws instance is nullptr.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation of WsThreadHelper
|
|
|
|
WsThreadHelper::WsThreadHelper()
|
2013-06-25 12:51:44 +08:00
|
|
|
: _subThreadInstance(nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
, _needQuit(false)
|
|
|
|
{
|
2016-01-11 09:56:27 +08:00
|
|
|
_subThreadWsMessageQueue = new (std::nothrow) std::list<WsMessage*>();
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WsThreadHelper::~WsThreadHelper()
|
|
|
|
{
|
2016-01-11 09:56:27 +08:00
|
|
|
joinWebSocketThread();
|
2013-06-25 12:51:44 +08:00
|
|
|
CC_SAFE_DELETE(_subThreadInstance);
|
2013-05-31 23:13:03 +08:00
|
|
|
delete _subThreadWsMessageQueue;
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
bool WsThreadHelper::createWebSocketThread()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
// Creates websocket thread
|
2016-01-11 09:56:27 +08:00
|
|
|
_subThreadInstance = new (std::nothrow) std::thread(&WsThreadHelper::wsThreadEntryFunc, this);
|
2013-06-25 12:51:44 +08:00
|
|
|
return true;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:04:48 +08:00
|
|
|
void WsThreadHelper::quitWebSocketThread()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
_needQuit = true;
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
void WsThreadHelper::onSubThreadLoop()
|
|
|
|
{
|
|
|
|
if (__wsContext)
|
|
|
|
{
|
|
|
|
// _readyStateMutex.unlock();
|
|
|
|
__wsHelper->_subThreadWsMessageQueueMutex.lock();
|
|
|
|
bool isEmpty = __wsHelper->_subThreadWsMessageQueue->empty();
|
|
|
|
|
|
|
|
if (!isEmpty)
|
|
|
|
{
|
|
|
|
auto iter = __wsHelper->_subThreadWsMessageQueue->begin();
|
|
|
|
for (; iter != __wsHelper->_subThreadWsMessageQueue->end(); )
|
|
|
|
{
|
|
|
|
auto msg = (*iter);
|
|
|
|
auto ws = (WebSocket*)msg->user;
|
|
|
|
// TODO: ws may be a invalid pointer
|
|
|
|
if (msg->what == WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION)
|
|
|
|
{
|
|
|
|
ws->onClientOpenConnectionRequest();
|
2017-04-10 14:16:55 +08:00
|
|
|
delete *iter;
|
2017-01-19 13:55:14 +08:00
|
|
|
iter = __wsHelper->_subThreadWsMessageQueue->erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__wsHelper->_subThreadWsMessageQueueMutex.unlock();
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
// The second parameter passed to 'lws_service' means the timeout in milliseconds while polling websocket events.
|
|
|
|
// The lower value the better, otherwise, it may trigger high CPU usage.
|
|
|
|
// We set 2ms in 'lws_service' then sleep 3ms to make lower CPU cost.
|
|
|
|
// Since messages are received in websocket thread and user code is in cocos thread, we need to post event to
|
|
|
|
// cocos thread and trigger user callbacks by 'Scheduler::performFunctionInCocosThread'. If game's fps is set
|
|
|
|
// to 60 (16.66ms), the latency will be (2ms + 3ms + 16.66ms + internet delay) > 21ms
|
|
|
|
lws_service(__wsContext, 2);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(3));
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::onSubThreadStarted()
|
|
|
|
{
|
|
|
|
int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO/* | LLL_DEBUG | LLL_PARSER | LLL_HEADER*/ | LLL_EXT | LLL_CLIENT | LLL_LATENCY;
|
|
|
|
lws_set_log_level(log_level, printWebSocketLog);
|
|
|
|
|
|
|
|
memset(__defaultProtocols, 0, sizeof(2 * sizeof(struct lws_protocols)));
|
|
|
|
|
|
|
|
__defaultProtocols[0].name = "";
|
|
|
|
__defaultProtocols[0].callback = WebSocketCallbackWrapper::onSocketCallback;
|
|
|
|
__defaultProtocols[0].rx_buffer_size = WS_RX_BUFFER_SIZE;
|
|
|
|
__defaultProtocols[0].id = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
lws_context_creation_info creationInfo = convertToContextCreationInfo(__defaultProtocols, true);
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsContext = lws_create_context(&creationInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::onSubThreadEnded()
|
|
|
|
{
|
|
|
|
if (__wsContext != nullptr)
|
|
|
|
{
|
|
|
|
lws_context_destroy(__wsContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:51:44 +08:00
|
|
|
void WsThreadHelper::wsThreadEntryFunc()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2016-01-09 18:07:48 +08:00
|
|
|
LOGD("WebSocket thread start, helper instance: %p\n", this);
|
2017-01-19 13:55:14 +08:00
|
|
|
onSubThreadStarted();
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
while (!_needQuit)
|
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
onSubThreadLoop();
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
onSubThreadEnded();
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2016-01-09 18:07:48 +08:00
|
|
|
LOGD("WebSocket thread exit, helper instance: %p\n", this);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 09:56:27 +08:00
|
|
|
void WsThreadHelper::sendMessageToCocosThread(const std::function<void()>& cb)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2016-01-07 23:15:11 +08:00
|
|
|
Director::getInstance()->getScheduler()->performFunctionInCocosThread(cb);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 09:56:27 +08:00
|
|
|
void WsThreadHelper::sendMessageToWebSocketThread(WsMessage *msg)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-06-26 10:10:23 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_subThreadWsMessageQueueMutex);
|
2013-05-31 23:13:03 +08:00
|
|
|
_subThreadWsMessageQueue->push_back(msg);
|
|
|
|
}
|
|
|
|
|
2016-01-11 09:56:27 +08:00
|
|
|
void WsThreadHelper::joinWebSocketThread()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-06-25 12:51:44 +08:00
|
|
|
if (_subThreadInstance->joinable())
|
|
|
|
{
|
|
|
|
_subThreadInstance->join();
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
// Define a WebSocket frame
|
|
|
|
class WebSocketFrame
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2016-01-07 23:15:11 +08:00
|
|
|
public:
|
|
|
|
WebSocketFrame()
|
|
|
|
: _payload(nullptr)
|
|
|
|
, _payloadLength(0)
|
|
|
|
, _frameLength(0)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2015-06-22 20:58:37 +08:00
|
|
|
}
|
2014-03-25 16:14:21 +08:00
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
bool init(unsigned char* buf, ssize_t len)
|
|
|
|
{
|
|
|
|
if (buf == nullptr && len > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!_data.empty())
|
2015-06-22 20:58:37 +08:00
|
|
|
{
|
2016-01-07 23:15:11 +08:00
|
|
|
LOGD("WebSocketFrame was initialized, should not init it again!\n");
|
|
|
|
return false;
|
2015-06-22 20:58:37 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-01-13 20:48:03 +08:00
|
|
|
_data.reserve(LWS_PRE + len);
|
|
|
|
_data.resize(LWS_PRE, 0x00);
|
2016-01-07 23:15:11 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
_data.insert(_data.end(), buf, buf + len);
|
|
|
|
}
|
|
|
|
|
2016-01-13 20:48:03 +08:00
|
|
|
_payload = _data.data() + LWS_PRE;
|
2016-01-07 23:15:11 +08:00
|
|
|
_payloadLength = len;
|
|
|
|
_frameLength = len;
|
|
|
|
return true;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
void update(ssize_t issued)
|
|
|
|
{
|
|
|
|
_payloadLength -= issued;
|
|
|
|
_payload += issued;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* getPayload() const { return _payload; }
|
|
|
|
ssize_t getPayloadLength() const { return _payloadLength; }
|
|
|
|
ssize_t getFrameLength() const { return _frameLength; }
|
|
|
|
private:
|
|
|
|
unsigned char* _payload;
|
|
|
|
ssize_t _payloadLength;
|
|
|
|
|
|
|
|
ssize_t _frameLength;
|
|
|
|
std::vector<unsigned char> _data;
|
|
|
|
};
|
|
|
|
//
|
|
|
|
|
2016-01-09 18:07:48 +08:00
|
|
|
void WebSocket::closeAllConnections()
|
|
|
|
{
|
|
|
|
if (__websocketInstances != nullptr)
|
|
|
|
{
|
|
|
|
ssize_t count = __websocketInstances->size();
|
|
|
|
for (ssize_t i = count-1; i >=0 ; i--)
|
|
|
|
{
|
|
|
|
WebSocket* instance = __websocketInstances->at(i);
|
|
|
|
instance->close();
|
|
|
|
}
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
std::lock_guard<std::mutex> lk(__instanceMutex);
|
2016-01-09 18:07:48 +08:00
|
|
|
__websocketInstances->clear();
|
2017-04-10 14:16:55 +08:00
|
|
|
delete __websocketInstances;
|
2016-01-09 18:07:48 +08:00
|
|
|
__websocketInstances = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
WebSocket::WebSocket()
|
2013-07-26 12:08:18 +08:00
|
|
|
: _readyState(State::CONNECTING)
|
|
|
|
, _wsInstance(nullptr)
|
2017-02-20 09:44:20 +08:00
|
|
|
, _lwsProtocols(nullptr)
|
2016-04-07 15:06:19 +08:00
|
|
|
, _isDestroyed(std::make_shared<std::atomic<bool>>(false))
|
2013-07-26 12:08:18 +08:00
|
|
|
, _delegate(nullptr)
|
2017-01-19 13:55:14 +08:00
|
|
|
, _closeState(CloseState::NONE)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2016-01-07 23:15:11 +08:00
|
|
|
// reserve data buffer to avoid allocate memory frequently
|
|
|
|
_receivedData.reserve(WS_RESERVE_RECEIVE_BUFFER_SIZE);
|
2016-01-09 18:07:48 +08:00
|
|
|
if (__websocketInstances == nullptr)
|
|
|
|
{
|
2016-01-11 09:56:27 +08:00
|
|
|
__websocketInstances = new (std::nothrow) std::vector<WebSocket*>();
|
2016-01-09 18:07:48 +08:00
|
|
|
}
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2016-01-09 18:07:48 +08:00
|
|
|
__websocketInstances->push_back(this);
|
2016-02-29 18:55:51 +08:00
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> isDestroyed = _isDestroyed;
|
2016-02-29 18:55:51 +08:00
|
|
|
_resetDirectorListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_RESET, [this, isDestroyed](EventCustom*){
|
|
|
|
if (*isDestroyed)
|
|
|
|
return;
|
|
|
|
close();
|
|
|
|
});
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocket::~WebSocket()
|
|
|
|
{
|
2016-01-09 18:07:48 +08:00
|
|
|
LOGD("In the destructor of WebSocket (%p)\n", this);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
std::lock_guard<std::mutex> lk(__instanceMutex);
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2016-01-09 18:07:48 +08:00
|
|
|
if (__websocketInstances != nullptr)
|
|
|
|
{
|
|
|
|
auto iter = std::find(__websocketInstances->begin(), __websocketInstances->end(), this);
|
|
|
|
if (iter != __websocketInstances->end())
|
|
|
|
{
|
|
|
|
__websocketInstances->erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-11 09:56:27 +08:00
|
|
|
LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this);
|
2016-01-09 18:07:48 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
if (__websocketInstances == nullptr || __websocketInstances->empty())
|
|
|
|
{
|
|
|
|
__wsHelper->quitWebSocketThread();
|
|
|
|
LOGD("before join ws thread\n");
|
|
|
|
__wsHelper->joinWebSocketThread();
|
|
|
|
LOGD("after join ws thread\n");
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(__wsHelper);
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:55:51 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener);
|
|
|
|
|
2016-01-22 17:43:30 +08:00
|
|
|
*_isDestroyed = true;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
bool WebSocket::init(const Delegate& delegate,
|
|
|
|
const std::string& url,
|
2017-01-19 13:55:14 +08:00
|
|
|
const std::vector<std::string>* protocols/* = nullptr*/,
|
|
|
|
const std::string& caFilePath/* = ""*/)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
_delegate = const_cast<Delegate*>(&delegate);
|
2017-02-20 09:44:20 +08:00
|
|
|
_url = url;
|
2017-01-19 13:55:14 +08:00
|
|
|
_caFilePath = caFilePath;
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-03-07 13:35:59 +08:00
|
|
|
if (_url.empty())
|
2017-01-19 13:55:14 +08:00
|
|
|
return false;
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
if (protocols != nullptr && !protocols->empty())
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
size_t size = protocols->size();
|
2017-02-20 09:44:20 +08:00
|
|
|
_lwsProtocols = (struct lws_protocols*)malloc((size + 1) * sizeof(struct lws_protocols));
|
|
|
|
memset(_lwsProtocols, 0, (size + 1) * sizeof(struct lws_protocols));
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
static uint32_t __wsId = 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
2013-07-26 12:08:18 +08:00
|
|
|
{
|
2017-02-20 09:44:20 +08:00
|
|
|
_lwsProtocols[i].callback = WebSocketCallbackWrapper::onSocketCallback;
|
2017-01-19 13:55:14 +08:00
|
|
|
size_t nameLen = protocols->at(i).length();
|
|
|
|
char* name = (char*)malloc(nameLen + 1);
|
|
|
|
name[nameLen] = '\0';
|
|
|
|
strcpy(name, protocols->at(i).c_str());
|
2017-02-20 09:44:20 +08:00
|
|
|
_lwsProtocols[i].name = name;
|
|
|
|
_lwsProtocols[i].id = ++__wsId;
|
|
|
|
_lwsProtocols[i].rx_buffer_size = WS_RX_BUFFER_SIZE;
|
|
|
|
_lwsProtocols[i].per_session_data_size = 0;
|
|
|
|
_lwsProtocols[i].user = nullptr;
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
_clientSupportedProtocols += name;
|
2017-01-19 13:55:14 +08:00
|
|
|
if (i < (size - 1))
|
|
|
|
{
|
2017-02-20 09:44:20 +08:00
|
|
|
_clientSupportedProtocols += ",";
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-03-07 09:35:14 +08:00
|
|
|
bool isWebSocketThreadCreated = true;
|
2017-01-19 13:55:14 +08:00
|
|
|
if (__wsHelper == nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsHelper = new (std::nothrow) WsThreadHelper();
|
2017-03-07 09:35:14 +08:00
|
|
|
isWebSocketThreadCreated = false;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
WsMessage* msg = new (std::nothrow) WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION;
|
|
|
|
msg->user = this;
|
|
|
|
__wsHelper->sendMessageToWebSocketThread(msg);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-03-07 09:35:14 +08:00
|
|
|
// fixed https://github.com/cocos2d/cocos2d-x/issues/17433
|
|
|
|
// createWebSocketThread has to be after message WS_MSG_TO_SUBTHREAD_CREATE_CONNECTION was sent.
|
|
|
|
// And websocket thread should only be created once.
|
|
|
|
if (!isWebSocketThreadCreated)
|
|
|
|
{
|
|
|
|
__wsHelper->createWebSocketThread();
|
|
|
|
}
|
2017-03-06 14:18:58 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
return true;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::send(const std::string& message)
|
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState == State::OPEN)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
// In main thread
|
2014-08-28 07:31:57 +08:00
|
|
|
Data* data = new (std::nothrow) Data();
|
2016-01-07 23:15:11 +08:00
|
|
|
data->bytes = (char*)malloc(message.length() + 1);
|
|
|
|
// Make sure the last byte is '\0'
|
|
|
|
data->bytes[message.length()] = '\0';
|
2013-05-31 23:13:03 +08:00
|
|
|
strcpy(data->bytes, message.c_str());
|
2013-12-06 16:32:06 +08:00
|
|
|
data->len = static_cast<ssize_t>(message.length());
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
WsMessage* msg = new (std::nothrow) WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_SUBTRHEAD_SENDING_STRING;
|
2017-01-19 13:55:14 +08:00
|
|
|
msg->data = data;
|
|
|
|
msg->user = this;
|
|
|
|
__wsHelper->sendMessageToWebSocketThread(msg);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("Couldn't send message since websocket wasn't opened!\n");
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState == State::OPEN)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
// In main thread
|
2014-08-28 07:31:57 +08:00
|
|
|
Data* data = new (std::nothrow) Data();
|
2016-01-07 23:15:11 +08:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
// If data length is zero, allocate 1 byte for safe.
|
|
|
|
data->bytes = (char*)malloc(1);
|
|
|
|
data->bytes[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->bytes = (char*)malloc(len);
|
|
|
|
memcpy((void*)data->bytes, (void*)binaryMsg, len);
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
data->len = len;
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
WsMessage* msg = new (std::nothrow) WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_SUBTRHEAD_SENDING_BINARY;
|
2017-01-19 13:55:14 +08:00
|
|
|
msg->data = data;
|
|
|
|
msg->user = this;
|
|
|
|
__wsHelper->sendMessageToWebSocketThread(msg);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("Couldn't send message since websocket wasn't opened!\n");
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::close()
|
2016-01-22 14:22:33 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
if (_closeState != CloseState::NONE)
|
2016-01-22 17:43:30 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("close was invoked, don't invoke it again!\n");
|
2016-01-22 17:43:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
_closeState = CloseState::SYNC_CLOSING;
|
|
|
|
LOGD("close: WebSocket (%p) is closing...\n", this);
|
|
|
|
{
|
|
|
|
_readyStateMutex.lock();
|
|
|
|
if (_readyState == State::CLOSED)
|
|
|
|
{
|
|
|
|
// If readState is closed, it means that onConnectionClosed was invoked in websocket thread,
|
|
|
|
// but the callback of performInCocosThread has not been triggered. We need to invoke
|
|
|
|
// onClose to release the websocket instance.
|
|
|
|
_readyStateMutex.unlock();
|
|
|
|
_delegate->onClose(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
_readyState = State::CLOSING;
|
2017-01-19 13:55:14 +08:00
|
|
|
_readyStateMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lkClose(_closeMutex);
|
|
|
|
_closeCondition.wait(lkClose);
|
|
|
|
_closeState = CloseState::SYNC_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 5 milliseconds for onConnectionClosed to exit!
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
2016-01-22 15:20:43 +08:00
|
|
|
_delegate->onClose(this);
|
2016-01-22 14:22:33 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2016-01-22 14:22:33 +08:00
|
|
|
void WebSocket::closeAsync()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
if (_closeState != CloseState::NONE)
|
|
|
|
{
|
|
|
|
LOGD("close was invoked, don't invoke it again!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_closeState = CloseState::ASYNC_CLOSING;
|
|
|
|
|
|
|
|
LOGD("closeAsync: WebSocket (%p) is closing...\n", this);
|
|
|
|
std::lock_guard<std::mutex> lk(_readyStateMutex);
|
2017-02-13 15:15:23 +08:00
|
|
|
if (_readyState == State::CLOSED || _readyState == State::CLOSING)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
LOGD("closeAsync: WebSocket (%p) was closed, no need to close it again!\n", this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
_readyState = State::CLOSING;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 09:55:43 +08:00
|
|
|
WebSocket::State WebSocket::getReadyState()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_readyStateMutex);
|
2013-05-31 23:13:03 +08:00
|
|
|
return _readyState;
|
|
|
|
}
|
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
struct lws_vhost* WebSocket::createVhost(struct lws_protocols* protocols, int& sslConnection)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
auto fileUtils = FileUtils::getInstance();
|
|
|
|
bool isCAFileExist = fileUtils->isFileExist(_caFilePath);
|
|
|
|
if (isCAFileExist)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
_caFilePath = fileUtils->fullPathForFilename(_caFilePath);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
lws_context_creation_info info = convertToContextCreationInfo(protocols, isCAFileExist);
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2017-02-20 09:44:20 +08:00
|
|
|
if (sslConnection != 0)
|
2017-02-13 15:15:23 +08:00
|
|
|
{
|
|
|
|
if (isCAFileExist)
|
2016-01-13 20:59:10 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
// if ca file is in the apk, try to extract it to writable path
|
|
|
|
std::string writablePath = fileUtils->getWritablePath();
|
|
|
|
std::string caFileName = getFileNameForPath(_caFilePath);
|
|
|
|
std::string newCaFilePath = writablePath + caFileName;
|
|
|
|
|
|
|
|
if (fileUtils->isFileExist(newCaFilePath))
|
|
|
|
{
|
|
|
|
LOGD("CA file (%s) in writable path exists!", newCaFilePath.c_str());
|
|
|
|
_caFilePath = newCaFilePath;
|
|
|
|
info.ssl_ca_filepath = _caFilePath.c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fileUtils->isFileExist(_caFilePath))
|
|
|
|
{
|
|
|
|
std::string fullPath = fileUtils->fullPathForFilename(_caFilePath);
|
|
|
|
LOGD("Found CA file: %s", fullPath.c_str());
|
|
|
|
if (fullPath[0] != '/')
|
|
|
|
{
|
|
|
|
LOGD("CA file is in APK");
|
|
|
|
auto caData = fileUtils->getDataFromFile(fullPath);
|
|
|
|
if (!caData.isNull())
|
|
|
|
{
|
|
|
|
FILE* fp = fopen(newCaFilePath.c_str(), "wb");
|
|
|
|
if (fp != nullptr)
|
|
|
|
{
|
|
|
|
LOGD("New CA file path: %s", newCaFilePath.c_str());
|
|
|
|
fwrite(caData.getBytes(), caData.getSize(), 1, fp);
|
|
|
|
fclose(fp);
|
|
|
|
_caFilePath = newCaFilePath;
|
|
|
|
info.ssl_ca_filepath = _caFilePath.c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Open new CA file failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "CA file is empty!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("CA file isn't in APK!");
|
|
|
|
_caFilePath = fullPath;
|
|
|
|
info.ssl_ca_filepath = _caFilePath.c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "CA file doesn't exist!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2017-02-13 15:15:23 +08:00
|
|
|
info.ssl_ca_filepath = _caFilePath.c_str();
|
2017-01-19 13:55:14 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
LOGD("WARNING: CA Root file isn't set. SSL connection will not peer server certificate\n");
|
2017-02-20 09:44:20 +08:00
|
|
|
sslConnection = sslConnection | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 15:15:23 +08:00
|
|
|
|
|
|
|
lws_vhost* vhost = lws_create_vhost(__wsContext, &info);
|
|
|
|
|
|
|
|
return vhost;
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
void WebSocket::onClientOpenConnectionRequest()
|
|
|
|
{
|
|
|
|
if (nullptr != __wsContext)
|
2013-07-26 12:08:18 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
// client_no_context_takeover extension is not supported in the current version, it will cause connection fail
|
|
|
|
// It may be a bug of lib websocket build
|
|
|
|
// "permessage-deflate; client_no_context_takeover; client_max_window_bits"
|
|
|
|
"permessage-deflate; client_max_window_bits"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
|
|
|
{ nullptr, nullptr, nullptr /* terminator */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
_readyStateMutex.lock();
|
2013-07-26 12:08:18 +08:00
|
|
|
_readyState = State::CONNECTING;
|
2017-01-19 13:55:14 +08:00
|
|
|
_readyStateMutex.unlock();
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-03-07 13:35:59 +08:00
|
|
|
Uri uri = Uri::parse(_url);
|
2017-03-09 10:14:39 +08:00
|
|
|
LOGD("scheme: %s, host: %s, port: %d, path: %s\n", uri.getScheme().c_str(), uri.getHostName().c_str(), static_cast<int>(uri.getPort()), uri.getPathEtc().c_str());
|
2017-02-20 09:44:20 +08:00
|
|
|
|
|
|
|
int sslConnection = 0;
|
2017-03-07 13:35:59 +08:00
|
|
|
if (uri.isSecure())
|
2017-02-20 09:44:20 +08:00
|
|
|
sslConnection = LCCSCF_USE_SSL;
|
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
struct lws_vhost* vhost = nullptr;
|
2017-02-20 09:44:20 +08:00
|
|
|
if (_lwsProtocols != nullptr)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
2017-02-20 09:44:20 +08:00
|
|
|
vhost = createVhost(_lwsProtocols, sslConnection);
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-20 09:44:20 +08:00
|
|
|
vhost = createVhost(__defaultProtocols, sslConnection);
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-03-09 10:14:39 +08:00
|
|
|
int port = static_cast<int>(uri.getPort());
|
|
|
|
if (port == 0)
|
|
|
|
port = uri.isSecure() ? 443 : 80;
|
|
|
|
|
|
|
|
const std::string& hostName = uri.getHostName();
|
|
|
|
std::string path = uri.getPathEtc();
|
|
|
|
const std::string& authority = uri.getAuthority();
|
|
|
|
if (path.empty())
|
|
|
|
path = "/";
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
struct lws_client_connect_info connectInfo;
|
|
|
|
memset(&connectInfo, 0, sizeof(connectInfo));
|
|
|
|
connectInfo.context = __wsContext;
|
2017-03-09 10:14:39 +08:00
|
|
|
connectInfo.address = hostName.c_str();
|
|
|
|
connectInfo.port = port;
|
2017-02-20 09:44:20 +08:00
|
|
|
connectInfo.ssl_connection = sslConnection;
|
2017-03-09 10:14:39 +08:00
|
|
|
connectInfo.path = path.c_str();
|
|
|
|
connectInfo.host = hostName.c_str();
|
|
|
|
connectInfo.origin = authority.c_str();
|
2017-02-20 09:44:20 +08:00
|
|
|
connectInfo.protocol = _clientSupportedProtocols.empty() ? nullptr : _clientSupportedProtocols.c_str();
|
2017-01-19 13:55:14 +08:00
|
|
|
connectInfo.ietf_version_or_minus_one = -1;
|
|
|
|
connectInfo.userdata = this;
|
|
|
|
connectInfo.client_exts = exts;
|
2017-02-13 15:15:23 +08:00
|
|
|
connectInfo.vhost = vhost;
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
_wsInstance = lws_client_connect_via_info(&connectInfo);
|
2014-01-26 15:59:14 +08:00
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
if (nullptr == _wsInstance)
|
|
|
|
{
|
|
|
|
onConnectionError();
|
2017-01-19 13:55:14 +08:00
|
|
|
return;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
LOGE("Create websocket context failed!");
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int WebSocket::onClientWritable()
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
// LOGD("onClientWritable ... \n");
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
std::lock_guard<std::mutex> readMutex(_readyStateMutex);
|
2017-02-13 15:15:23 +08:00
|
|
|
if (_readyState == State::CLOSING)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
LOGD("Closing websocket (%p) connection.\n", this);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
do
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
std::lock_guard<std::mutex> lk(__wsHelper->_subThreadWsMessageQueueMutex);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
if (__wsHelper->_subThreadWsMessageQueue->empty())
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
break;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
std::list<WsMessage*>::iterator iter = __wsHelper->_subThreadWsMessageQueue->begin();
|
|
|
|
|
|
|
|
while (iter != __wsHelper->_subThreadWsMessageQueue->end())
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
WsMessage* msg = *iter;
|
|
|
|
if (msg->user == this)
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
break;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
else
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
++iter;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
ssize_t bytesWrite = 0;
|
|
|
|
if (iter != __wsHelper->_subThreadWsMessageQueue->end())
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
WsMessage* subThreadMsg = *iter;
|
|
|
|
|
|
|
|
Data* data = (Data*)subThreadMsg->data;
|
|
|
|
|
|
|
|
const ssize_t c_bufferSize = WS_RX_BUFFER_SIZE;
|
|
|
|
|
|
|
|
const ssize_t remaining = data->len - data->issued;
|
|
|
|
const ssize_t n = std::min(remaining, c_bufferSize);
|
|
|
|
|
|
|
|
WebSocketFrame* frame = nullptr;
|
|
|
|
|
|
|
|
if (data->ext)
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
frame = (WebSocketFrame*)data->ext;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
frame = new (std::nothrow) WebSocketFrame();
|
|
|
|
bool success = frame && frame->init((unsigned char*)(data->bytes + data->issued), n);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
data->ext = frame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // If frame initialization failed, delete the frame and drop the sending data
|
|
|
|
// These codes should never be called.
|
|
|
|
LOGD("WebSocketFrame initialization failed, drop the sending data, msg(%d)\n", (int)subThreadMsg->id);
|
|
|
|
delete frame;
|
|
|
|
CC_SAFE_FREE(data->bytes);
|
|
|
|
CC_SAFE_DELETE(data);
|
|
|
|
__wsHelper->_subThreadWsMessageQueue->erase(iter);
|
|
|
|
CC_SAFE_DELETE(subThreadMsg);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int writeProtocol;
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
if (data->issued == 0)
|
|
|
|
{
|
|
|
|
if (WS_MSG_TO_SUBTRHEAD_SENDING_STRING == subThreadMsg->what)
|
|
|
|
{
|
|
|
|
writeProtocol = LWS_WRITE_TEXT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writeProtocol = LWS_WRITE_BINARY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have more than 1 fragment
|
|
|
|
if (data->len > c_bufferSize)
|
|
|
|
writeProtocol |= LWS_WRITE_NO_FIN;
|
|
|
|
} else {
|
|
|
|
// we are in the middle of fragments
|
|
|
|
writeProtocol = LWS_WRITE_CONTINUATION;
|
|
|
|
// and if not in the last fragment
|
|
|
|
if (remaining != n)
|
|
|
|
writeProtocol |= LWS_WRITE_NO_FIN;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
bytesWrite = lws_write(_wsInstance, frame->getPayload(), frame->getPayloadLength(), (lws_write_protocol)writeProtocol);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
// Handle the result of lws_write
|
|
|
|
// Buffer overrun?
|
|
|
|
if (bytesWrite < 0)
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("ERROR: msg(%u), lws_write return: %d, but it should be %d, drop this message.\n", subThreadMsg->id, (int)bytesWrite, (int)n);
|
|
|
|
// socket error, we need to close the socket connection
|
|
|
|
CC_SAFE_FREE(data->bytes);
|
|
|
|
delete ((WebSocketFrame*)data->ext);
|
|
|
|
data->ext = nullptr;
|
|
|
|
CC_SAFE_DELETE(data);
|
|
|
|
__wsHelper->_subThreadWsMessageQueue->erase(iter);
|
|
|
|
CC_SAFE_DELETE(subThreadMsg);
|
|
|
|
|
|
|
|
closeAsync();
|
|
|
|
}
|
|
|
|
else if (bytesWrite < frame->getPayloadLength())
|
|
|
|
{
|
|
|
|
frame->update(bytesWrite);
|
|
|
|
LOGD("frame wasn't sent completely, bytesWrite: %d, remain: %d\n", (int)bytesWrite, (int)frame->getPayloadLength());
|
|
|
|
}
|
|
|
|
// Do we have another fragments to send?
|
|
|
|
else if (remaining > frame->getFrameLength() && bytesWrite == frame->getPayloadLength())
|
|
|
|
{
|
|
|
|
// A frame was totally sent, plus data->issued to send next frame
|
2016-01-07 23:15:11 +08:00
|
|
|
LOGD("msg(%u) append: %d + %d = %d\n", subThreadMsg->id, (int)data->issued, (int)frame->getFrameLength(), (int)(data->issued + frame->getFrameLength()));
|
2017-01-19 13:55:14 +08:00
|
|
|
data->issued += frame->getFrameLength();
|
|
|
|
delete ((WebSocketFrame*)data->ext);
|
|
|
|
data->ext = nullptr;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
// Safely done!
|
2016-01-07 23:15:11 +08:00
|
|
|
else
|
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("Safely done, msg(%d)!\n", subThreadMsg->id);
|
|
|
|
if (remaining == frame->getFrameLength())
|
|
|
|
{
|
|
|
|
LOGD("msg(%u) append: %d + %d = %d\n", subThreadMsg->id, (int)data->issued, (int)frame->getFrameLength(), (int)(data->issued + frame->getFrameLength()));
|
|
|
|
LOGD("msg(%u) was totally sent!\n", subThreadMsg->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("ERROR: msg(%u), remaining(%d) < bytesWrite(%d)\n", subThreadMsg->id, (int)remaining, (int)frame->getFrameLength());
|
|
|
|
LOGD("Drop the msg(%u)\n", subThreadMsg->id);
|
|
|
|
closeAsync();
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
CC_SAFE_FREE(data->bytes);
|
|
|
|
delete ((WebSocketFrame*)data->ext);
|
|
|
|
data->ext = nullptr;
|
|
|
|
CC_SAFE_DELETE(data);
|
|
|
|
__wsHelper->_subThreadWsMessageQueue->erase(iter);
|
|
|
|
CC_SAFE_DELETE(subThreadMsg);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("-----------------------------------------------------------\n");
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
if (_wsInstance != nullptr)
|
|
|
|
{
|
|
|
|
lws_callback_on_writable(_wsInstance);
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int WebSocket::onClientReceivedData(void* in, ssize_t len)
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
|
|
|
// In websocket thread
|
|
|
|
static int packageIndex = 0;
|
|
|
|
packageIndex++;
|
|
|
|
if (in != nullptr && len > 0)
|
|
|
|
{
|
|
|
|
LOGD("Receiving data:index:%d, len=%d\n", packageIndex, (int)len);
|
|
|
|
|
|
|
|
unsigned char* inData = (unsigned char*)in;
|
|
|
|
_receivedData.insert(_receivedData.end(), inData, inData + len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-25 01:53:22 +08:00
|
|
|
LOGD("Empty message received, index=%d!\n", packageIndex);
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no more data pending, send it to the client thread
|
|
|
|
size_t remainingSize = lws_remaining_packet_payload(_wsInstance);
|
|
|
|
int isFinalFragment = lws_is_final_fragment(_wsInstance);
|
|
|
|
// LOGD("remainingSize: %d, isFinalFragment: %d\n", (int)remainingSize, isFinalFragment);
|
|
|
|
|
|
|
|
if (remainingSize == 0 && isFinalFragment)
|
|
|
|
{
|
2016-01-11 09:56:27 +08:00
|
|
|
std::vector<char>* frameData = new (std::nothrow) std::vector<char>(std::move(_receivedData));
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
// reset capacity of received data buffer
|
|
|
|
_receivedData.reserve(WS_RESERVE_RECEIVE_BUFFER_SIZE);
|
|
|
|
|
|
|
|
ssize_t frameSize = frameData->size();
|
|
|
|
|
2016-05-13 10:44:17 +08:00
|
|
|
bool isBinary = (lws_frame_is_binary(_wsInstance) != 0);
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
if (!isBinary)
|
|
|
|
{
|
|
|
|
frameData->push_back('\0');
|
|
|
|
}
|
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> isDestroyed = _isDestroyed;
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsHelper->sendMessageToCocosThread([this, frameData, frameSize, isBinary, isDestroyed](){
|
2016-01-07 23:15:11 +08:00
|
|
|
// In UI thread
|
|
|
|
LOGD("Notify data len %d to Cocos thread.\n", (int)frameSize);
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
data.isBinary = isBinary;
|
|
|
|
data.bytes = (char*)frameData->data();
|
|
|
|
data.len = frameSize;
|
|
|
|
|
2016-01-22 17:43:30 +08:00
|
|
|
if (*isDestroyed)
|
|
|
|
{
|
|
|
|
LOGD("WebSocket instance was destroyed!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_delegate->onMessage(this, data);
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
|
|
|
delete frameData;
|
|
|
|
});
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int WebSocket::onConnectionOpened()
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-02-20 09:44:20 +08:00
|
|
|
const lws_protocols* lwsSelectedProtocol = lws_get_protocol(_wsInstance);
|
|
|
|
_selectedProtocol = lwsSelectedProtocol->name;
|
|
|
|
LOGD("onConnectionOpened...: %p, client protocols: %s, server selected protocol: %s\n", this, _clientSupportedProtocols.c_str(), _selectedProtocol.c_str());
|
2016-01-07 23:15:11 +08:00
|
|
|
/*
|
|
|
|
* start the ball rolling,
|
|
|
|
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
|
|
|
*/
|
|
|
|
lws_callback_on_writable(_wsInstance);
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(_readyStateMutex);
|
|
|
|
if (_readyState == State::CLOSING || _readyState == State::CLOSED)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_readyState = State::OPEN;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> isDestroyed = _isDestroyed;
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsHelper->sendMessageToCocosThread([this, isDestroyed](){
|
2016-01-22 17:43:30 +08:00
|
|
|
if (*isDestroyed)
|
|
|
|
{
|
|
|
|
LOGD("WebSocket instance was destroyed!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_delegate->onOpen(this);
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
});
|
2017-01-19 13:55:14 +08:00
|
|
|
return 0;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int WebSocket::onConnectionError()
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(_readyStateMutex);
|
2017-02-13 15:15:23 +08:00
|
|
|
LOGD("WebSocket (%p) onConnectionError, state: %d ...\n", this, (int)_readyState);
|
|
|
|
if (_readyState == State::CLOSED)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_readyState = State::CLOSING;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> isDestroyed = _isDestroyed;
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsHelper->sendMessageToCocosThread([this, isDestroyed](){
|
2016-01-22 17:43:30 +08:00
|
|
|
if (*isDestroyed)
|
|
|
|
{
|
|
|
|
LOGD("WebSocket instance was destroyed!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_delegate->onError(this, ErrorCode::CONNECTION_FAILURE);
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
});
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
onConnectionClosed();
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
return 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int WebSocket::onConnectionClosed()
|
2016-01-07 23:15:11 +08:00
|
|
|
{
|
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_readyStateMutex);
|
|
|
|
LOGD("WebSocket (%p) onConnectionClosed, state: %d ...\n", this, (int)_readyState);
|
|
|
|
if (_readyState == State::CLOSED)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-19 13:55:14 +08:00
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
if (_readyState == State::CLOSING)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
if (_closeState == CloseState::SYNC_CLOSING)
|
2017-01-19 13:55:14 +08:00
|
|
|
{
|
2017-02-13 15:15:23 +08:00
|
|
|
LOGD("onConnectionClosed, WebSocket (%p) is closing by client synchronously.\n", this);
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lkClose(_closeMutex);
|
|
|
|
_closeCondition.notify_one();
|
|
|
|
if (_closeState == CloseState::SYNC_CLOSED)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (_closeState == CloseState::ASYNC_CLOSING)
|
|
|
|
{
|
|
|
|
LOGD("onConnectionClosed, WebSocket (%p) is closing by client asynchronously.\n", this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("onConnectionClosed, WebSocket (%p) is closing by server.\n", this);
|
2017-01-19 13:55:14 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 15:15:23 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD("onConnectionClosed, WebSocket (%p) is closing by server.\n", this);
|
|
|
|
}
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2017-02-13 15:15:23 +08:00
|
|
|
_readyState = State::CLOSED;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2016-04-07 15:06:19 +08:00
|
|
|
std::shared_ptr<std::atomic<bool>> isDestroyed = _isDestroyed;
|
2017-01-19 13:55:14 +08:00
|
|
|
__wsHelper->sendMessageToCocosThread([this, isDestroyed](){
|
2016-01-22 17:43:30 +08:00
|
|
|
if (*isDestroyed)
|
|
|
|
{
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("WebSocket instance (%p) was destroyed!\n", this);
|
2016-01-22 17:43:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_delegate->onClose(this);
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
});
|
2017-01-19 13:55:14 +08:00
|
|
|
|
|
|
|
LOGD("WebSocket (%p) onConnectionClosed DONE!\n", this);
|
|
|
|
return 0;
|
2016-01-07 23:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::onSocketCallback(struct lws *wsi,
|
2013-07-25 10:33:19 +08:00
|
|
|
int reason,
|
2017-01-19 13:55:14 +08:00
|
|
|
void *in, ssize_t len)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2016-01-13 18:53:38 +08:00
|
|
|
//LOGD("socket callback for %d reason\n", reason);
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
int ret = 0;
|
2016-01-07 23:15:11 +08:00
|
|
|
switch (reason)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
2017-01-19 13:55:14 +08:00
|
|
|
ret = onConnectionOpened();
|
2013-05-31 23:13:03 +08:00
|
|
|
break;
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2016-01-13 18:53:38 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
2017-01-19 13:55:14 +08:00
|
|
|
ret = onConnectionError();
|
2013-05-31 23:13:03 +08:00
|
|
|
break;
|
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
case LWS_CALLBACK_WSI_DESTROY:
|
|
|
|
ret = onConnectionClosed();
|
2013-05-31 23:13:03 +08:00
|
|
|
break;
|
|
|
|
|
2016-01-07 23:15:11 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2017-01-19 13:55:14 +08:00
|
|
|
ret = onClientReceivedData(in, len);
|
2013-05-31 23:13:03 +08:00
|
|
|
break;
|
2016-01-13 21:07:04 +08:00
|
|
|
|
2016-01-13 18:53:38 +08:00
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
2017-01-19 13:55:14 +08:00
|
|
|
ret = onClientWritable();
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
|
|
|
|
case LWS_CALLBACK_LOCK_POLL:
|
|
|
|
case LWS_CALLBACK_UNLOCK_POLL:
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_PROTOCOL_INIT:
|
|
|
|
LOGD("protocol init...");
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
|
|
|
LOGD("protocol destroy...");
|
2016-01-13 18:53:38 +08:00
|
|
|
break;
|
2013-05-31 23:13:03 +08:00
|
|
|
default:
|
2017-01-19 13:55:14 +08:00
|
|
|
LOGD("WebSocket (%p) Unhandled websocket event: %d\n", this, reason);
|
2013-05-31 23:13:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-01-07 23:15:11 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
return ret;
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|
2014-01-02 16:25:35 +08:00
|
|
|
|
2017-01-19 13:55:14 +08:00
|
|
|
NS_NETWORK_END
|