2013-05-31 23:13:03 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "WebSocket.h"
|
2014-01-20 15:03:30 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCScheduler.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>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-07-25 10:33:19 +08:00
|
|
|
#include "libwebsockets.h"
|
|
|
|
|
2014-01-09 13:29:56 +08:00
|
|
|
#define WS_WRITE_BUFFER_SIZE 2048
|
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_BEGIN
|
2013-10-15 18:00:03 +08:00
|
|
|
|
|
|
|
namespace network {
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
class WsMessage
|
|
|
|
{
|
|
|
|
public:
|
2014-01-09 15:18:06 +08:00
|
|
|
WsMessage() : what(0), obj(nullptr){}
|
2013-05-31 23:13:03 +08:00
|
|
|
unsigned int what; // message type
|
|
|
|
void* obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Websocket thread helper, it's used for sending message between UI thread and websocket thread.
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
class WsThreadHelper : public Ref
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
WsThreadHelper();
|
|
|
|
~WsThreadHelper();
|
|
|
|
|
|
|
|
// Creates a new thread
|
|
|
|
bool createThread(const WebSocket& ws);
|
|
|
|
// Quits sub-thread (websocket thread).
|
|
|
|
void quitSubThread();
|
|
|
|
|
|
|
|
// Schedule callback function
|
|
|
|
virtual void update(float dt);
|
|
|
|
|
|
|
|
// Sends message to UI thread. It's needed to be invoked in sub-thread.
|
|
|
|
void sendMessageToUIThread(WsMessage *msg);
|
|
|
|
|
|
|
|
// Sends message to sub-thread(websocket thread). It's needs to be invoked in UI thread.
|
|
|
|
void sendMessageToSubThread(WsMessage *msg);
|
|
|
|
|
|
|
|
// Waits the sub-thread (websocket thread) to exit,
|
|
|
|
void joinSubThread();
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2013-06-25 12:51:44 +08:00
|
|
|
void wsThreadEntryFunc();
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::list<WsMessage*>* _UIWsMessageQueue;
|
|
|
|
std::list<WsMessage*>* _subThreadWsMessageQueue;
|
2013-06-25 12:51:44 +08:00
|
|
|
std::mutex _UIWsMessageQueueMutex;
|
|
|
|
std::mutex _subThreadWsMessageQueueMutex;
|
|
|
|
std::thread* _subThreadInstance;
|
2013-05-31 23:13:03 +08:00
|
|
|
WebSocket* _ws;
|
|
|
|
bool _needQuit;
|
|
|
|
friend class WebSocket;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wrapper for converting websocket callback from static function to member function of WebSocket class.
|
|
|
|
class WebSocketCallbackWrapper {
|
|
|
|
public:
|
|
|
|
|
|
|
|
static int onSocketCallback(struct libwebsocket_context *ctx,
|
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
|
|
|
{
|
|
|
|
// Gets the user data from context. We know that it's a 'WebSocket' instance.
|
|
|
|
WebSocket* wsInstance = (WebSocket*)libwebsocket_context_user(ctx);
|
|
|
|
if (wsInstance)
|
|
|
|
{
|
|
|
|
return wsInstance->onSocketCallback(ctx, wsi, reason, user, in, len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation of WsThreadHelper
|
|
|
|
WsThreadHelper::WsThreadHelper()
|
2013-06-25 12:51:44 +08:00
|
|
|
: _subThreadInstance(nullptr)
|
2014-01-09 15:18:06 +08:00
|
|
|
, _ws(nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
, _needQuit(false)
|
|
|
|
{
|
|
|
|
_UIWsMessageQueue = new std::list<WsMessage*>();
|
|
|
|
_subThreadWsMessageQueue = new std::list<WsMessage*>();
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getScheduler()->scheduleUpdateForTarget(this, 0, false);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WsThreadHelper::~WsThreadHelper()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
|
2013-06-26 10:10:23 +08:00
|
|
|
joinSubThread();
|
2013-06-25 12:51:44 +08:00
|
|
|
CC_SAFE_DELETE(_subThreadInstance);
|
2013-05-31 23:13:03 +08:00
|
|
|
delete _UIWsMessageQueue;
|
|
|
|
delete _subThreadWsMessageQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WsThreadHelper::createThread(const WebSocket& ws)
|
|
|
|
{
|
|
|
|
_ws = const_cast<WebSocket*>(&ws);
|
|
|
|
|
|
|
|
// Creates websocket thread
|
2013-06-25 12:51:44 +08:00
|
|
|
_subThreadInstance = new std::thread(&WsThreadHelper::wsThreadEntryFunc, this);
|
|
|
|
return true;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::quitSubThread()
|
|
|
|
{
|
|
|
|
_needQuit = true;
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:51:44 +08:00
|
|
|
void WsThreadHelper::wsThreadEntryFunc()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
_ws->onSubThreadStarted();
|
|
|
|
|
|
|
|
while (!_needQuit)
|
|
|
|
{
|
|
|
|
if (_ws->onSubThreadLoop())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::sendMessageToUIThread(WsMessage *msg)
|
|
|
|
{
|
2013-06-26 10:10:23 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_UIWsMessageQueueMutex);
|
2013-05-31 23:13:03 +08:00
|
|
|
_UIWsMessageQueue->push_back(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::sendMessageToSubThread(WsMessage *msg)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::joinSubThread()
|
|
|
|
{
|
2013-06-25 12:51:44 +08:00
|
|
|
if (_subThreadInstance->joinable())
|
|
|
|
{
|
|
|
|
_subThreadInstance->join();
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WsThreadHelper::update(float dt)
|
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
WsMessage *msg = nullptr;
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
// Returns quickly if no message
|
2013-06-26 10:10:23 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_UIWsMessageQueueMutex);
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
if (0 == _UIWsMessageQueue->size())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets message
|
|
|
|
msg = *(_UIWsMessageQueue->begin());
|
|
|
|
_UIWsMessageQueue->pop_front();
|
|
|
|
|
|
|
|
if (_ws)
|
|
|
|
{
|
|
|
|
_ws->onUIThreadReceiveMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum WS_MSG {
|
|
|
|
WS_MSG_TO_SUBTRHEAD_SENDING_STRING = 0,
|
|
|
|
WS_MSG_TO_SUBTRHEAD_SENDING_BINARY,
|
|
|
|
WS_MSG_TO_UITHREAD_OPEN,
|
|
|
|
WS_MSG_TO_UITHREAD_MESSAGE,
|
|
|
|
WS_MSG_TO_UITHREAD_ERROR,
|
|
|
|
WS_MSG_TO_UITHREAD_CLOSE
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocket::WebSocket()
|
2013-07-26 12:08:18 +08:00
|
|
|
: _readyState(State::CONNECTING)
|
2013-05-31 23:13:03 +08:00
|
|
|
, _port(80)
|
2013-07-26 12:08:18 +08:00
|
|
|
, _wsHelper(nullptr)
|
|
|
|
, _wsInstance(nullptr)
|
|
|
|
, _wsContext(nullptr)
|
|
|
|
, _delegate(nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
, _SSLConnection(0)
|
2013-07-26 12:08:18 +08:00
|
|
|
, _wsProtocols(nullptr)
|
2014-01-09 15:18:06 +08:00
|
|
|
, _pendingFrameDataLen(0)
|
|
|
|
, _currentDataLen(0)
|
|
|
|
, _currentData(nullptr)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocket::~WebSocket()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
CC_SAFE_RELEASE_NULL(_wsHelper);
|
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
|
|
|
{
|
2013-05-31 23:13:03 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(_wsProtocols[i].name);
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE_ARRAY(_wsProtocols);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocket::init(const Delegate& delegate,
|
|
|
|
const std::string& url,
|
2014-01-09 15:18:06 +08:00
|
|
|
const std::vector<std::string>* protocols/* = nullptr*/)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
bool useSSL = false;
|
|
|
|
std::string host = url;
|
2013-12-06 16:32:06 +08:00
|
|
|
size_t pos = 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
int port = 80;
|
|
|
|
|
|
|
|
_delegate = const_cast<Delegate*>(&delegate);
|
|
|
|
|
|
|
|
//ws://
|
|
|
|
pos = host.find("ws://");
|
2013-07-26 12:08:18 +08:00
|
|
|
if (pos == 0) host.erase(0,5);
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
pos = host.find("wss://");
|
|
|
|
if (pos == 0)
|
|
|
|
{
|
|
|
|
host.erase(0,6);
|
|
|
|
useSSL = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = host.find(":");
|
2013-12-06 16:32:06 +08:00
|
|
|
if (pos != std::string::npos) port = atoi(host.substr(pos+1, host.size()).c_str());
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2013-06-17 18:28:12 +08:00
|
|
|
pos = host.find("/", 0);
|
2013-05-31 23:13:03 +08:00
|
|
|
std::string path = "/";
|
2013-12-06 16:32:06 +08:00
|
|
|
if (pos != std::string::npos) path += host.substr(pos + 1, host.size());
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
pos = host.find(":");
|
2013-12-06 16:32:06 +08:00
|
|
|
if(pos != std::string::npos){
|
2013-05-31 23:13:03 +08:00
|
|
|
host.erase(pos, host.size());
|
2013-12-06 16:32:06 +08:00
|
|
|
}else if((pos = host.find("/")) != std::string::npos) {
|
2013-06-17 18:28:12 +08:00
|
|
|
host.erase(pos, host.size());
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_host = host;
|
|
|
|
_port = port;
|
|
|
|
_path = path;
|
|
|
|
_SSLConnection = useSSL ? 1 : 0;
|
|
|
|
|
2013-06-17 18:28:12 +08:00
|
|
|
CCLOG("[WebSocket::init] _host: %s, _port: %d, _path: %s", _host.c_str(), _port, _path.c_str());
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
size_t protocolCount = 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
if (protocols && protocols->size() > 0)
|
|
|
|
{
|
|
|
|
protocolCount = protocols->size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
protocolCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_wsProtocols = new libwebsocket_protocols[protocolCount+1];
|
|
|
|
memset(_wsProtocols, 0, sizeof(libwebsocket_protocols)*(protocolCount+1));
|
|
|
|
|
2013-08-02 09:35:45 +08:00
|
|
|
if (protocols && protocols->size() > 0)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2013-07-26 12:08:18 +08:00
|
|
|
for (std::vector<std::string>::const_iterator iter = protocols->begin(); iter != protocols->end(); ++iter, ++i)
|
|
|
|
{
|
2013-05-31 23:13:03 +08:00
|
|
|
char* name = new char[(*iter).length()+1];
|
|
|
|
strcpy(name, (*iter).c_str());
|
|
|
|
_wsProtocols[i].name = name;
|
|
|
|
_wsProtocols[i].callback = WebSocketCallbackWrapper::onSocketCallback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* name = new char[20];
|
|
|
|
strcpy(name, "default-protocol");
|
|
|
|
_wsProtocols[0].name = name;
|
|
|
|
_wsProtocols[0].callback = WebSocketCallbackWrapper::onSocketCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebSocket thread needs to be invoked at the end of this method.
|
|
|
|
_wsHelper = new WsThreadHelper();
|
|
|
|
ret = _wsHelper->createThread(*this);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
WsMessage* msg = new WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_SUBTRHEAD_SENDING_STRING;
|
|
|
|
Data* data = new Data();
|
|
|
|
data->bytes = new char[message.length()+1];
|
|
|
|
strcpy(data->bytes, message.c_str());
|
2013-12-06 16:32:06 +08:00
|
|
|
data->len = static_cast<ssize_t>(message.length());
|
2013-05-31 23:13:03 +08:00
|
|
|
msg->obj = data;
|
|
|
|
_wsHelper->sendMessageToSubThread(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
CCASSERT(binaryMsg != nullptr && len > 0, "parameter invalid.");
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState == State::OPEN)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
// In main thread
|
|
|
|
WsMessage* msg = new WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_SUBTRHEAD_SENDING_BINARY;
|
|
|
|
Data* data = new Data();
|
|
|
|
data->bytes = new char[len];
|
|
|
|
memcpy((void*)data->bytes, (void*)binaryMsg, len);
|
|
|
|
data->len = len;
|
|
|
|
msg->obj = data;
|
|
|
|
_wsHelper->sendMessageToSubThread(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::close()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getScheduler()->unscheduleAllForTarget(_wsHelper);
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState == State::CLOSING || _readyState == State::CLOSED)
|
2013-06-25 12:51:44 +08:00
|
|
|
{
|
2013-05-31 23:13:03 +08:00
|
|
|
return;
|
2013-06-25 12:51:44 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
CCLOG("websocket (%p) connection closed by client", this);
|
2013-07-26 12:08:18 +08:00
|
|
|
_readyState = State::CLOSED;
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
_wsHelper->joinSubThread();
|
|
|
|
|
|
|
|
// onClose callback needs to be invoked at the end of this method
|
|
|
|
// since websocket instance may be deleted in 'onClose'.
|
|
|
|
_delegate->onClose(this);
|
|
|
|
}
|
|
|
|
|
2013-06-03 09:55:43 +08:00
|
|
|
WebSocket::State WebSocket::getReadyState()
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
return _readyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::onSubThreadLoop()
|
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState == State::CLOSED || _readyState == State::CLOSING)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
libwebsocket_context_destroy(_wsContext);
|
|
|
|
// return 1 to exit the loop.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_wsContext && _readyState != State::CLOSED && _readyState != State::CLOSING)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
libwebsocket_service(_wsContext, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep 50 ms
|
2013-06-26 11:34:04 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
// return 0 to continue the loop.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::onSubThreadStarted()
|
|
|
|
{
|
|
|
|
struct 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 = _wsProtocols;
|
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
|
|
|
info.extensions = libwebsocket_get_internal_extensions();
|
|
|
|
#endif
|
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
|
|
|
info.user = (void*)this;
|
|
|
|
|
|
|
|
_wsContext = libwebsocket_create_context(&info);
|
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
if(nullptr != _wsContext)
|
|
|
|
{
|
|
|
|
_readyState = State::CONNECTING;
|
2013-05-31 23:13:03 +08:00
|
|
|
std::string name;
|
2013-07-26 12:08:18 +08:00
|
|
|
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
|
|
|
{
|
2013-05-31 23:13:03 +08:00
|
|
|
name += (_wsProtocols[i].name);
|
2013-07-26 12:08:18 +08:00
|
|
|
|
|
|
|
if (_wsProtocols[i+1].callback != nullptr) name += ", ";
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
_wsInstance = libwebsocket_client_connect(_wsContext, _host.c_str(), _port, _SSLConnection,
|
|
|
|
_path.c_str(), _host.c_str(), _host.c_str(),
|
|
|
|
name.c_str(), -1);
|
2014-01-26 15:59:14 +08:00
|
|
|
|
|
|
|
if(NULL == _wsInstance) {
|
|
|
|
WsMessage* msg = new WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_UITHREAD_ERROR;
|
2014-02-20 19:29:53 +08:00
|
|
|
_readyState = State::CLOSING;
|
2014-01-26 15:59:14 +08:00
|
|
|
_wsHelper->sendMessageToUIThread(msg);
|
|
|
|
}
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::onSubThreadEnded()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|
|
|
struct libwebsocket *wsi,
|
2013-07-25 10:33:19 +08:00
|
|
|
int reason,
|
2013-12-06 16:32:06 +08:00
|
|
|
void *user, void *in, ssize_t len)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
//CCLOG("socket callback for %d reason", reason);
|
2013-07-26 12:08:18 +08:00
|
|
|
CCASSERT(_wsContext == nullptr || ctx == _wsContext, "Invalid context.");
|
|
|
|
CCASSERT(_wsInstance == nullptr || wsi == nullptr || wsi == _wsInstance, "Invaild websocket instance.");
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case LWS_CALLBACK_DEL_POLL_FD:
|
|
|
|
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
WsMessage* msg = nullptr;
|
2013-05-31 23:13:03 +08:00
|
|
|
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|
2013-07-26 12:08:18 +08:00
|
|
|
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CONNECTING)
|
|
|
|
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == State::CONNECTING)
|
2013-05-31 23:13:03 +08:00
|
|
|
)
|
|
|
|
{
|
2013-06-10 11:43:43 +08:00
|
|
|
msg = new WsMessage();
|
2013-05-31 23:13:03 +08:00
|
|
|
msg->what = WS_MSG_TO_UITHREAD_ERROR;
|
2013-07-26 12:08:18 +08:00
|
|
|
_readyState = State::CLOSING;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2013-07-26 12:08:18 +08:00
|
|
|
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CLOSING)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-06-10 11:43:43 +08:00
|
|
|
msg = new WsMessage();
|
2013-05-31 23:13:03 +08:00
|
|
|
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
|
|
|
}
|
2013-06-10 11:43:43 +08:00
|
|
|
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
_wsHelper->sendMessageToUIThread(msg);
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
{
|
|
|
|
WsMessage* msg = new WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_UITHREAD_OPEN;
|
2013-07-26 12:08:18 +08:00
|
|
|
_readyState = State::OPEN;
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/*
|
|
|
|
* start the ball rolling,
|
|
|
|
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
|
|
|
*/
|
|
|
|
libwebsocket_callback_on_writable(ctx, wsi);
|
|
|
|
_wsHelper->sendMessageToUIThread(msg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
{
|
2014-01-09 13:29:56 +08:00
|
|
|
|
2013-06-26 10:10:23 +08:00
|
|
|
std::lock_guard<std::mutex> lk(_wsHelper->_subThreadWsMessageQueueMutex);
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
|
|
|
|
|
|
|
int bytesWrite = 0;
|
2014-01-09 13:29:56 +08:00
|
|
|
for (; iter != _wsHelper->_subThreadWsMessageQueue->end();)
|
2013-07-26 12:08:18 +08:00
|
|
|
{
|
2013-05-31 23:13:03 +08:00
|
|
|
WsMessage* subThreadMsg = *iter;
|
|
|
|
|
|
|
|
if ( WS_MSG_TO_SUBTRHEAD_SENDING_STRING == subThreadMsg->what
|
|
|
|
|| WS_MSG_TO_SUBTRHEAD_SENDING_BINARY == subThreadMsg->what)
|
|
|
|
{
|
|
|
|
Data* data = (Data*)subThreadMsg->obj;
|
|
|
|
|
2014-01-09 13:29:56 +08:00
|
|
|
const size_t c_bufferSize = WS_WRITE_BUFFER_SIZE;
|
|
|
|
|
|
|
|
size_t remaining = data->len - data->issued;
|
|
|
|
size_t n = std::min(remaining, c_bufferSize );
|
2014-01-20 15:03:30 +08:00
|
|
|
CCLOG("[websocket:send] total: %d, sent: %d, remaining: %d, buffer size: %d", static_cast<int>(data->len), static_cast<int>(data->issued), static_cast<int>(remaining), static_cast<int>(n));
|
2014-01-09 13:29:56 +08:00
|
|
|
|
|
|
|
unsigned char* buf = new unsigned char[LWS_SEND_BUFFER_PRE_PADDING + n + LWS_SEND_BUFFER_POST_PADDING];
|
|
|
|
|
|
|
|
memcpy((char*)&buf[LWS_SEND_BUFFER_PRE_PADDING], data->bytes + data->issued, n);
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-09 13:29:56 +08:00
|
|
|
int writeProtocol;
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-09 13:29:56 +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;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2014-01-09 13:29:56 +08:00
|
|
|
|
|
|
|
bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, (libwebsocket_write_protocol)writeProtocol);
|
|
|
|
CCLOG("[websocket:send] bytesWrite => %d", bytesWrite);
|
|
|
|
|
|
|
|
// Buffer overrun?
|
|
|
|
if (bytesWrite < 0)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2014-01-09 13:29:56 +08:00
|
|
|
break;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2014-01-09 13:29:56 +08:00
|
|
|
// Do we have another fragments to send?
|
|
|
|
else if (remaining != n)
|
2013-07-26 12:08:18 +08:00
|
|
|
{
|
2014-01-09 13:29:56 +08:00
|
|
|
data->issued += n;
|
|
|
|
break;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
2014-01-09 13:29:56 +08:00
|
|
|
// Safely done!
|
|
|
|
else
|
2013-07-26 12:08:18 +08:00
|
|
|
{
|
2014-01-09 13:29:56 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(data->bytes);
|
|
|
|
CC_SAFE_DELETE(data);
|
|
|
|
CC_SAFE_DELETE_ARRAY(buf);
|
|
|
|
_wsHelper->_subThreadWsMessageQueue->erase(iter++);
|
|
|
|
CC_SAFE_DELETE(subThreadMsg);
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get notified as soon as we can write again */
|
|
|
|
|
|
|
|
libwebsocket_callback_on_writable(ctx, wsi);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
{
|
|
|
|
|
|
|
|
CCLOG("%s", "connection closing..");
|
|
|
|
|
|
|
|
_wsHelper->quitSubThread();
|
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
if (_readyState != State::CLOSED)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
|
|
|
WsMessage* msg = new WsMessage();
|
2013-07-26 12:08:18 +08:00
|
|
|
_readyState = State::CLOSED;
|
2013-05-31 23:13:03 +08:00
|
|
|
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
|
|
|
_wsHelper->sendMessageToUIThread(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
{
|
|
|
|
if (in && len > 0)
|
|
|
|
{
|
2014-01-09 13:29:56 +08:00
|
|
|
// Accumulate the data (increasing the buffer as we go)
|
2014-01-09 15:18:06 +08:00
|
|
|
if (_currentDataLen == 0)
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
_currentData = new char[len];
|
|
|
|
memcpy (_currentData, in, len);
|
|
|
|
_currentDataLen = len;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
char *new_data = new char [_currentDataLen + len];
|
|
|
|
memcpy (new_data, _currentData, _currentDataLen);
|
|
|
|
memcpy (new_data + _currentDataLen, in, len);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_currentData);
|
|
|
|
_currentData = new_data;
|
|
|
|
_currentDataLen = _currentDataLen + len;
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
_pendingFrameDataLen = libwebsockets_remaining_packet_payload (wsi);
|
2014-01-09 13:29:56 +08:00
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
if (_pendingFrameDataLen > 0)
|
2014-01-09 13:29:56 +08:00
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
//CCLOG("%ld bytes of pending data to receive, consider increasing the libwebsocket rx_buffer_size value.", _pendingFrameDataLen);
|
2014-01-09 13:29:56 +08:00
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-09 13:29:56 +08:00
|
|
|
// If no more data pending, send it to the client thread
|
2014-01-09 15:18:06 +08:00
|
|
|
if (_pendingFrameDataLen == 0)
|
2014-01-09 13:29:56 +08:00
|
|
|
{
|
|
|
|
WsMessage* msg = new WsMessage();
|
|
|
|
msg->what = WS_MSG_TO_UITHREAD_MESSAGE;
|
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
char* bytes = nullptr;
|
2014-01-09 13:29:56 +08:00
|
|
|
Data* data = new Data();
|
|
|
|
|
|
|
|
if (lws_frame_is_binary(wsi))
|
|
|
|
{
|
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
bytes = new char[_currentDataLen];
|
2014-01-09 13:29:56 +08:00
|
|
|
data->isBinary = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
bytes = new char[_currentDataLen+1];
|
|
|
|
bytes[_currentDataLen] = '\0';
|
2014-01-09 13:29:56 +08:00
|
|
|
data->isBinary = false;
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
memcpy(bytes, _currentData, _currentDataLen);
|
2014-01-09 13:29:56 +08:00
|
|
|
|
|
|
|
data->bytes = bytes;
|
2014-01-09 15:18:06 +08:00
|
|
|
data->len = _currentDataLen;
|
2014-01-09 13:29:56 +08:00
|
|
|
msg->obj = (void*)data;
|
|
|
|
|
2014-01-09 15:18:06 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(_currentData);
|
|
|
|
_currentData = nullptr;
|
|
|
|
_currentDataLen = 0;
|
2014-01-09 13:29:56 +08:00
|
|
|
|
|
|
|
_wsHelper->sendMessageToUIThread(msg);
|
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what) {
|
|
|
|
case WS_MSG_TO_UITHREAD_OPEN:
|
|
|
|
{
|
|
|
|
_delegate->onOpen(this);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WS_MSG_TO_UITHREAD_MESSAGE:
|
|
|
|
{
|
|
|
|
Data* data = (Data*)msg->obj;
|
|
|
|
_delegate->onMessage(this, *data);
|
|
|
|
CC_SAFE_DELETE_ARRAY(data->bytes);
|
|
|
|
CC_SAFE_DELETE(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WS_MSG_TO_UITHREAD_CLOSE:
|
|
|
|
{
|
2014-02-28 17:32:54 +08:00
|
|
|
//Waiting for the subThread safety exit
|
|
|
|
_wsHelper->joinSubThread();
|
2013-05-31 23:13:03 +08:00
|
|
|
_delegate->onClose(this);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WS_MSG_TO_UITHREAD_ERROR:
|
|
|
|
{
|
2013-06-03 09:55:43 +08:00
|
|
|
// FIXME: The exact error needs to be checked.
|
2013-07-26 12:08:18 +08:00
|
|
|
WebSocket::ErrorCode err = ErrorCode::CONNECTION_FAILURE;
|
2013-05-31 23:13:03 +08:00
|
|
|
_delegate->onError(this, err);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 21:23:15 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|
2014-01-02 16:25:35 +08:00
|
|
|
|
|
|
|
NS_CC_END
|