mirror of https://github.com/axmolengine/axmol.git
issue 2430:use enum class
This commit is contained in:
parent
f94a02ac01
commit
0b8de88ddc
|
@ -134,7 +134,7 @@ bool AssetsManager::checkUpdate()
|
|||
|
||||
if (res != 0)
|
||||
{
|
||||
sendErrorMessage(ERROR_NETWORK);
|
||||
sendErrorMessage(ErrorCode::NETWORK);
|
||||
CCLOG("can not get version file content, error code is %d", res);
|
||||
curl_easy_cleanup(_curl);
|
||||
return false;
|
||||
|
@ -143,7 +143,7 @@ bool AssetsManager::checkUpdate()
|
|||
string recordedVersion = UserDefault::getInstance()->getStringForKey(KEY_OF_VERSION);
|
||||
if (recordedVersion == _version)
|
||||
{
|
||||
sendErrorMessage(ERROR_NO_NEW_VERSION);
|
||||
sendErrorMessage(ErrorCode::NO_NEW_VERSION);
|
||||
CCLOG("there is not new version");
|
||||
// Set resource search path.
|
||||
setSearchPath();
|
||||
|
@ -173,7 +173,7 @@ void AssetsManager::downloadAndUncompress()
|
|||
// Uncompress zip file.
|
||||
if (! uncompress())
|
||||
{
|
||||
sendErrorMessage(AssetsManager::ERROR_UNCOMPRESS);
|
||||
sendErrorMessage(ErrorCode::UNCOMPRESS);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -403,7 +403,7 @@ bool AssetsManager::downLoad()
|
|||
FILE *fp = fopen(outFileName.c_str(), "wb");
|
||||
if (! fp)
|
||||
{
|
||||
sendErrorMessage(ERROR_CREATE_FILE);
|
||||
sendErrorMessage(ErrorCode::CREATE_FILE);
|
||||
CCLOG("can not create file %s", outFileName.c_str());
|
||||
return false;
|
||||
}
|
||||
|
@ -420,7 +420,7 @@ bool AssetsManager::downLoad()
|
|||
curl_easy_cleanup(_curl);
|
||||
if (res != 0)
|
||||
{
|
||||
sendErrorMessage(ERROR_NETWORK);
|
||||
sendErrorMessage(ErrorCode::NETWORK);
|
||||
CCLOG("error when download package");
|
||||
fclose(fp);
|
||||
return false;
|
||||
|
|
|
@ -44,19 +44,19 @@ class AssetsManagerDelegateProtocol;
|
|||
class AssetsManager
|
||||
{
|
||||
public:
|
||||
enum ErrorCode
|
||||
enum class ErrorCode
|
||||
{
|
||||
// Error caused by creating a file to store downloaded data
|
||||
ERROR_CREATE_FILE,
|
||||
CREATE_FILE,
|
||||
/** Error caused by network
|
||||
-- network unavaivable
|
||||
-- timeout
|
||||
-- ...
|
||||
*/
|
||||
ERROR_NETWORK,
|
||||
NETWORK,
|
||||
/** There is not a new version
|
||||
*/
|
||||
ERROR_NO_NEW_VERSION,
|
||||
NO_NEW_VERSION,
|
||||
/** Error caused in uncompressing stage
|
||||
-- can not open zip file
|
||||
-- can not read file global information
|
||||
|
@ -64,13 +64,7 @@ public:
|
|||
-- can not create a directory
|
||||
-- ...
|
||||
*/
|
||||
ERROR_UNCOMPRESS,
|
||||
|
||||
// keep compatability
|
||||
kCreateFile = ERROR_CREATE_FILE,
|
||||
kNetwork = ERROR_NETWORK,
|
||||
kNoNewVersion = ERROR_NO_NEW_VERSION,
|
||||
kUncompress = ERROR_UNCOMPRESS,
|
||||
UNCOMPRESS,
|
||||
};
|
||||
|
||||
/* @brief Creates a AssetsManager with new package url, version code url and storage path.
|
||||
|
|
|
@ -100,20 +100,19 @@ SIOClientImpl::SIOClientImpl(const std::string& host, int port) :
|
|||
s << host << ":" << port;
|
||||
_uri = s.str();
|
||||
|
||||
_ws = NULL;
|
||||
|
||||
_ws = nullptr;
|
||||
}
|
||||
|
||||
SIOClientImpl::~SIOClientImpl() {
|
||||
|
||||
SIOClientImpl::~SIOClientImpl()
|
||||
{
|
||||
if (_connected) disconnect();
|
||||
|
||||
CC_SAFE_DELETE(_clients);
|
||||
CC_SAFE_DELETE(_ws);
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::handshake() {
|
||||
void SIOClientImpl::handshake()
|
||||
{
|
||||
log("SIOClientImpl::handshake() called");
|
||||
|
||||
std::stringstream pre;
|
||||
|
@ -135,8 +134,8 @@ void SIOClientImpl::handshake() {
|
|||
return;
|
||||
}
|
||||
|
||||
void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response) {
|
||||
|
||||
void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response)
|
||||
{
|
||||
log("SIOClientImpl::handshakeResponse() called");
|
||||
|
||||
if (0 != strlen(response->getHttpRequest()->getTag()))
|
||||
|
@ -185,18 +184,21 @@ void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response
|
|||
int heartbeat = 0, timeout = 0;
|
||||
|
||||
pos = res.find(":");
|
||||
if(pos >= 0) {
|
||||
if(pos >= 0)
|
||||
{
|
||||
sid = res.substr(0, pos);
|
||||
res.erase(0, pos+1);
|
||||
}
|
||||
|
||||
pos = res.find(":");
|
||||
if(pos >= 0){
|
||||
if(pos >= 0)
|
||||
{
|
||||
heartbeat = atoi(res.substr(pos+1, res.size()).c_str());
|
||||
}
|
||||
|
||||
pos = res.find(":");
|
||||
if(pos >= 0){
|
||||
if(pos >= 0)
|
||||
{
|
||||
timeout = atoi(res.substr(pos+1, res.size()).c_str());
|
||||
}
|
||||
|
||||
|
@ -210,8 +212,8 @@ void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response
|
|||
|
||||
}
|
||||
|
||||
void SIOClientImpl::openSocket() {
|
||||
|
||||
void SIOClientImpl::openSocket()
|
||||
{
|
||||
log("SIOClientImpl::openSocket() called");
|
||||
|
||||
std::stringstream s;
|
||||
|
@ -226,23 +228,21 @@ void SIOClientImpl::openSocket() {
|
|||
return;
|
||||
}
|
||||
|
||||
bool SIOClientImpl::init() {
|
||||
|
||||
bool SIOClientImpl::init()
|
||||
{
|
||||
log("SIOClientImpl::init() successful");
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::connect() {
|
||||
|
||||
void SIOClientImpl::connect()
|
||||
{
|
||||
this->handshake();
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::disconnect() {
|
||||
|
||||
if(_ws->getReadyState() == WebSocket::kStateOpen) {
|
||||
|
||||
void SIOClientImpl::disconnect()
|
||||
{
|
||||
if(_ws->getReadyState() == WebSocket::State::OPEN)
|
||||
{
|
||||
std::string s = "0::";
|
||||
|
||||
_ws->send(s);
|
||||
|
@ -250,7 +250,6 @@ void SIOClientImpl::disconnect() {
|
|||
log("Disconnect sent");
|
||||
|
||||
_ws->close();
|
||||
|
||||
}
|
||||
|
||||
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
|
||||
|
@ -258,79 +257,71 @@ void SIOClientImpl::disconnect() {
|
|||
_connected = false;
|
||||
|
||||
SocketIO::instance()->removeSocket(_uri);
|
||||
|
||||
}
|
||||
|
||||
SIOClientImpl* SIOClientImpl::create(const std::string& host, int port) {
|
||||
|
||||
SIOClientImpl* SIOClientImpl::create(const std::string& host, int port)
|
||||
{
|
||||
SIOClientImpl *s = new SIOClientImpl(host, port);
|
||||
|
||||
if(s && s->init()) {
|
||||
|
||||
if (s && s->init())
|
||||
{
|
||||
return s;
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SIOClient* SIOClientImpl::getClient(const std::string& endpoint) {
|
||||
|
||||
SIOClient* SIOClientImpl::getClient(const std::string& endpoint)
|
||||
{
|
||||
return static_cast<SIOClient*>(_clients->objectForKey(endpoint));
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client) {
|
||||
|
||||
void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
|
||||
{
|
||||
_clients->setObject(client, endpoint);
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::connectToEndpoint(const std::string& endpoint) {
|
||||
|
||||
void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
|
||||
{
|
||||
std::string path = endpoint == "/" ? "" : endpoint;
|
||||
|
||||
std::string s = "1::" + path;
|
||||
|
||||
_ws->send(s);
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint) {
|
||||
|
||||
void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
|
||||
{
|
||||
_clients->removeObjectForKey(endpoint);
|
||||
|
||||
if(_clients->count() == 0 || endpoint == "/") {
|
||||
|
||||
if(_clients->count() == 0 || endpoint == "/")
|
||||
{
|
||||
log("SIOClientImpl::disconnectFromEndpoint out of endpoints, checking for disconnect");
|
||||
|
||||
if(_connected) this->disconnect();
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string path = endpoint == "/" ? "" : endpoint;
|
||||
|
||||
std::string s = "0::" + path;
|
||||
|
||||
_ws->send(s);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::heartbeat(float dt) {
|
||||
|
||||
void SIOClientImpl::heartbeat(float dt)
|
||||
{
|
||||
std::string s = "2::";
|
||||
|
||||
_ws->send(s);
|
||||
|
||||
log("Heartbeat sent");
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SIOClientImpl::send(std::string endpoint, std::string s) {
|
||||
void SIOClientImpl::send(std::string endpoint, std::string s)
|
||||
{
|
||||
std::stringstream pre;
|
||||
|
||||
std::string path = endpoint == "/" ? "" : endpoint;
|
||||
|
@ -342,11 +333,10 @@ void SIOClientImpl::send(std::string endpoint, std::string s) {
|
|||
log("sending message: %s", msg.c_str());
|
||||
|
||||
_ws->send(msg);
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::string args) {
|
||||
|
||||
void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::string args)
|
||||
{
|
||||
std::stringstream pre;
|
||||
|
||||
std::string path = endpoint == "/" ? "" : endpoint;
|
||||
|
@ -358,33 +348,30 @@ void SIOClientImpl::emit(std::string endpoint, std::string eventname, std::strin
|
|||
log("emitting event with data: %s", msg.c_str());
|
||||
|
||||
_ws->send(msg);
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::onOpen(cocos2d::extension::WebSocket* ws) {
|
||||
|
||||
void SIOClientImpl::onOpen(cocos2d::extension::WebSocket* ws)
|
||||
{
|
||||
_connected = true;
|
||||
|
||||
SocketIO::instance()->addSocket(_uri, this);
|
||||
|
||||
DictElement* e = NULL;
|
||||
|
||||
CCDICT_FOREACH(_clients, e) {
|
||||
|
||||
CCDICT_FOREACH(_clients, e)
|
||||
{
|
||||
SIOClient *c = static_cast<SIOClient*>(e->getObject());
|
||||
|
||||
c->onOpen();
|
||||
|
||||
}
|
||||
|
||||
Director::getInstance()->getScheduler()->scheduleSelector(schedule_selector(SIOClientImpl::heartbeat), this, (_heartbeat * .9), false);
|
||||
|
||||
log("SIOClientImpl::onOpen socket connected!");
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data) {
|
||||
|
||||
void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::Data& data)
|
||||
{
|
||||
log("SIOClientImpl::onMessage received: %s", data.bytes);
|
||||
|
||||
int control = atoi(&data.bytes[0]);
|
||||
|
@ -406,13 +393,13 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
|||
payload.erase(0, pos+1);
|
||||
|
||||
pos = payload.find(":");
|
||||
if(pos >= 0) {
|
||||
|
||||
if(pos >= 0)
|
||||
{
|
||||
endpoint = payload.substr(0, pos);
|
||||
payload.erase(0, pos+1);
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
endpoint = payload;
|
||||
}
|
||||
|
||||
|
@ -424,7 +411,8 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
|||
c = getClient(endpoint);
|
||||
if (c == NULL) log("SIOClientImpl::onMessage client lookup returned NULL");
|
||||
|
||||
switch(control) {
|
||||
switch(control)
|
||||
{
|
||||
case 0:
|
||||
log("Received Disconnect Signal for Endpoint: %s\n", endpoint.c_str());
|
||||
if(c) c->receivedDisconnect();
|
||||
|
@ -448,11 +436,13 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
|||
case 5:
|
||||
log("Event Received with data: %s \n", s_data.c_str());
|
||||
|
||||
if(c) {
|
||||
if(c)
|
||||
{
|
||||
eventname = "";
|
||||
pos = s_data.find(":");
|
||||
pos2 = s_data.find(",");
|
||||
if(pos2 > pos) {
|
||||
if(pos2 > pos)
|
||||
{
|
||||
s_data = s_data.substr(pos+1, pos2-pos-1);
|
||||
std::remove_copy(s_data.begin(), s_data.end(),
|
||||
std::back_inserter(eventname), '"');
|
||||
|
@ -477,29 +467,25 @@ void SIOClientImpl::onMessage(cocos2d::extension::WebSocket* ws, const cocos2d::
|
|||
return;
|
||||
}
|
||||
|
||||
void SIOClientImpl::onClose(cocos2d::extension::WebSocket* ws) {
|
||||
|
||||
if(_clients->count() > 0) {
|
||||
|
||||
void SIOClientImpl::onClose(cocos2d::extension::WebSocket* ws)
|
||||
{
|
||||
if(_clients->count() > 0)
|
||||
{
|
||||
DictElement *e;
|
||||
|
||||
CCDICT_FOREACH(_clients, e) {
|
||||
|
||||
CCDICT_FOREACH(_clients, e)
|
||||
{
|
||||
SIOClient *c = static_cast<SIOClient *>(e->getObject());
|
||||
|
||||
c->receivedDisconnect();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this->release();
|
||||
|
||||
}
|
||||
|
||||
void SIOClientImpl::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error) {
|
||||
|
||||
|
||||
void SIOClientImpl::onError(cocos2d::extension::WebSocket* ws, const cocos2d::extension::WebSocket::ErrorCode& error)
|
||||
{
|
||||
}
|
||||
|
||||
//begin SIOClient methods
|
||||
|
@ -512,56 +498,58 @@ SIOClient::SIOClient(const std::string& host, int port, const std::string& path,
|
|||
, _delegate(&delegate)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
SIOClient::~SIOClient(void) {
|
||||
|
||||
if(_connected) {
|
||||
SIOClient::~SIOClient(void)
|
||||
{
|
||||
if (_connected)
|
||||
{
|
||||
_socket->disconnectFromEndpoint(_path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::onOpen() {
|
||||
|
||||
if(_path != "/") {
|
||||
|
||||
void SIOClient::onOpen()
|
||||
{
|
||||
if (_path != "/")
|
||||
{
|
||||
_socket->connectToEndpoint(_path);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::onConnect() {
|
||||
|
||||
void SIOClient::onConnect()
|
||||
{
|
||||
_connected = true;
|
||||
_delegate->onConnect(this);
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::send(std::string s) {
|
||||
|
||||
if(_connected) {
|
||||
void SIOClient::send(std::string s)
|
||||
{
|
||||
if (_connected)
|
||||
{
|
||||
_socket->send(_path, s);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_delegate->onError(this, "Client not yet connected");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::emit(std::string eventname, std::string args) {
|
||||
|
||||
if(_connected) {
|
||||
void SIOClient::emit(std::string eventname, std::string args)
|
||||
{
|
||||
if(_connected)
|
||||
{
|
||||
_socket->emit(_path, eventname, args);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_delegate->onError(this, "Client not yet connected");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::disconnect() {
|
||||
|
||||
void SIOClient::disconnect()
|
||||
{
|
||||
_connected = false;
|
||||
|
||||
_socket->disconnectFromEndpoint(_path);
|
||||
|
@ -569,31 +557,28 @@ void SIOClient::disconnect() {
|
|||
_delegate->onClose(this);
|
||||
|
||||
this->release();
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::receivedDisconnect() {
|
||||
|
||||
void SIOClient::receivedDisconnect()
|
||||
{
|
||||
_connected = false;
|
||||
|
||||
_delegate->onClose(this);
|
||||
|
||||
this->release();
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::on(const std::string& eventName, SIOEvent e) {
|
||||
|
||||
void SIOClient::on(const std::string& eventName, SIOEvent e)
|
||||
{
|
||||
_eventRegistry[eventName] = e;
|
||||
|
||||
}
|
||||
|
||||
void SIOClient::fireEvent(const std::string& eventName, const std::string& data) {
|
||||
|
||||
void SIOClient::fireEvent(const std::string& eventName, const std::string& data)
|
||||
{
|
||||
log("SIOClient::fireEvent called with event name: %s and data: %s", eventName.c_str(), data.c_str());
|
||||
|
||||
if(_eventRegistry[eventName]) {
|
||||
|
||||
if(_eventRegistry[eventName])
|
||||
{
|
||||
SIOEvent e = _eventRegistry[eventName];
|
||||
|
||||
e(this, data);
|
||||
|
@ -602,71 +587,75 @@ void SIOClient::fireEvent(const std::string& eventName, const std::string& data)
|
|||
}
|
||||
|
||||
log("SIOClient::fireEvent no event with name %s found", eventName.c_str());
|
||||
|
||||
}
|
||||
|
||||
//begin SocketIO methods
|
||||
SocketIO *SocketIO::_inst = NULL;
|
||||
|
||||
SocketIO::SocketIO() {
|
||||
SocketIO *SocketIO::_inst = nullptr;
|
||||
|
||||
SocketIO::SocketIO()
|
||||
{
|
||||
_sockets = Dictionary::create();
|
||||
_sockets->retain();
|
||||
|
||||
}
|
||||
|
||||
SocketIO::~SocketIO(void) {
|
||||
SocketIO::~SocketIO(void)
|
||||
{
|
||||
CC_SAFE_DELETE(_sockets);
|
||||
delete _inst;
|
||||
}
|
||||
|
||||
SocketIO* SocketIO::instance() {
|
||||
|
||||
if(!_inst)
|
||||
_inst = new SocketIO();
|
||||
SocketIO* SocketIO::instance()
|
||||
{
|
||||
if(!_inst) _inst = new SocketIO();
|
||||
|
||||
return _inst;
|
||||
|
||||
}
|
||||
|
||||
SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string& uri) {
|
||||
|
||||
SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string& uri)
|
||||
{
|
||||
std::string host = uri;
|
||||
int port = 0;
|
||||
int pos = 0;
|
||||
|
||||
pos = host.find("//");
|
||||
if(pos >= 0) {
|
||||
if (pos >= 0)
|
||||
{
|
||||
host.erase(0, pos+2);
|
||||
}
|
||||
|
||||
pos = host.find(":");
|
||||
if(pos >= 0){
|
||||
if (pos >= 0)
|
||||
{
|
||||
port = atoi(host.substr(pos+1, host.size()).c_str());
|
||||
}
|
||||
|
||||
pos = host.find("/", 0);
|
||||
std::string path = "/";
|
||||
if(pos >= 0){
|
||||
if (pos >= 0)
|
||||
{
|
||||
path += host.substr(pos + 1, host.size());
|
||||
}
|
||||
|
||||
pos = host.find(":");
|
||||
if(pos >= 0){
|
||||
if (pos >= 0)
|
||||
{
|
||||
host.erase(pos, host.size());
|
||||
}else if((pos = host.find("/"))>=0) {
|
||||
}
|
||||
else if ((pos = host.find("/"))>=0)
|
||||
{
|
||||
host.erase(pos, host.size());
|
||||
}
|
||||
|
||||
std::stringstream s;
|
||||
s << host << ":" << port;
|
||||
|
||||
SIOClientImpl* socket = NULL;
|
||||
SIOClient *c = NULL;
|
||||
SIOClientImpl* socket = nullptr;
|
||||
SIOClient *c = nullptr;
|
||||
|
||||
socket = SocketIO::instance()->getSocket(s.str());
|
||||
|
||||
if(socket == NULL) {
|
||||
if(socket == nullptr)
|
||||
{
|
||||
//create a new socket, new client, connect
|
||||
socket = SIOClientImpl::create(host, port);
|
||||
|
||||
|
@ -675,40 +664,37 @@ SIOClient* SocketIO::connect(SocketIO::SIODelegate& delegate, const std::string&
|
|||
socket->addClient(path, c);
|
||||
|
||||
socket->connect();
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
//check if already connected to endpoint, handle
|
||||
c = socket->getClient(path);
|
||||
|
||||
if(c == NULL) {
|
||||
|
||||
if(c == NULL)
|
||||
{
|
||||
c = new SIOClient(host, port, path, socket, delegate);
|
||||
|
||||
socket->addClient(path, c);
|
||||
|
||||
socket->connectToEndpoint(path);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return c;
|
||||
|
||||
}
|
||||
|
||||
SIOClientImpl* SocketIO::getSocket(const std::string& uri) {
|
||||
|
||||
SIOClientImpl* SocketIO::getSocket(const std::string& uri)
|
||||
{
|
||||
return static_cast<SIOClientImpl*>(_sockets->objectForKey(uri));
|
||||
|
||||
}
|
||||
|
||||
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket) {
|
||||
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket)
|
||||
{
|
||||
_sockets->setObject(socket, uri);
|
||||
}
|
||||
|
||||
void SocketIO::removeSocket(const std::string& uri) {
|
||||
void SocketIO::removeSocket(const std::string& uri)
|
||||
{
|
||||
_sockets->removeObjectForKey(uri);
|
||||
}
|
||||
|
||||
|
|
|
@ -211,14 +211,14 @@ enum WS_MSG {
|
|||
};
|
||||
|
||||
WebSocket::WebSocket()
|
||||
: _readyState(kStateConnecting)
|
||||
: _readyState(State::CONNECTING)
|
||||
, _port(80)
|
||||
, _wsHelper(NULL)
|
||||
, _wsInstance(NULL)
|
||||
, _wsContext(NULL)
|
||||
, _delegate(NULL)
|
||||
, _wsHelper(nullptr)
|
||||
, _wsInstance(nullptr)
|
||||
, _wsContext(nullptr)
|
||||
, _delegate(nullptr)
|
||||
, _SSLConnection(0)
|
||||
, _wsProtocols(NULL)
|
||||
, _wsProtocols(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -227,7 +227,8 @@ WebSocket::~WebSocket()
|
|||
close();
|
||||
CC_SAFE_RELEASE_NULL(_wsHelper);
|
||||
|
||||
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
|
||||
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(_wsProtocols[i].name);
|
||||
}
|
||||
CC_SAFE_DELETE_ARRAY(_wsProtocols);
|
||||
|
@ -247,9 +248,7 @@ bool WebSocket::init(const Delegate& delegate,
|
|||
|
||||
//ws://
|
||||
pos = host.find("ws://");
|
||||
if (pos == 0){
|
||||
host.erase(0,5);
|
||||
}
|
||||
if (pos == 0) host.erase(0,5);
|
||||
|
||||
pos = host.find("wss://");
|
||||
if (pos == 0)
|
||||
|
@ -259,15 +258,11 @@ bool WebSocket::init(const Delegate& delegate,
|
|||
}
|
||||
|
||||
pos = host.find(":");
|
||||
if(pos >= 0){
|
||||
port = atoi(host.substr(pos+1, host.size()).c_str());
|
||||
}
|
||||
if (pos >= 0) port = atoi(host.substr(pos+1, host.size()).c_str());
|
||||
|
||||
pos = host.find("/", 0);
|
||||
std::string path = "/";
|
||||
if(pos >= 0){
|
||||
path += host.substr(pos + 1, host.size());
|
||||
}
|
||||
if (pos >= 0) path += host.substr(pos + 1, host.size());
|
||||
|
||||
pos = host.find(":");
|
||||
if(pos >= 0){
|
||||
|
@ -299,7 +294,8 @@ bool WebSocket::init(const Delegate& delegate,
|
|||
if (protocols)
|
||||
{
|
||||
int i = 0;
|
||||
for (std::vector<std::string>::const_iterator iter = protocols->begin(); iter != protocols->end(); ++iter, ++i) {
|
||||
for (std::vector<std::string>::const_iterator iter = protocols->begin(); iter != protocols->end(); ++iter, ++i)
|
||||
{
|
||||
char* name = new char[(*iter).length()+1];
|
||||
strcpy(name, (*iter).c_str());
|
||||
_wsProtocols[i].name = name;
|
||||
|
@ -323,7 +319,7 @@ bool WebSocket::init(const Delegate& delegate,
|
|||
|
||||
void WebSocket::send(const std::string& message)
|
||||
{
|
||||
if (_readyState == kStateOpen)
|
||||
if (_readyState == State::OPEN)
|
||||
{
|
||||
// In main thread
|
||||
WsMessage* msg = new WsMessage();
|
||||
|
@ -339,9 +335,9 @@ void WebSocket::send(const std::string& message)
|
|||
|
||||
void WebSocket::send(const unsigned char* binaryMsg, unsigned int len)
|
||||
{
|
||||
CCASSERT(binaryMsg != NULL && len > 0, "parameter invalid.");
|
||||
CCASSERT(binaryMsg != nullptr && len > 0, "parameter invalid.");
|
||||
|
||||
if (_readyState == kStateOpen)
|
||||
if (_readyState == State::OPEN)
|
||||
{
|
||||
// In main thread
|
||||
WsMessage* msg = new WsMessage();
|
||||
|
@ -359,13 +355,13 @@ void WebSocket::close()
|
|||
{
|
||||
Director::getInstance()->getScheduler()->unscheduleAllForTarget(_wsHelper);
|
||||
|
||||
if (_readyState == kStateClosing || _readyState == kStateClosed)
|
||||
if (_readyState == State::CLOSING || _readyState == State::CLOSED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CCLOG("websocket (%p) connection closed by client", this);
|
||||
_readyState = kStateClosed;
|
||||
_readyState = State::CLOSED;
|
||||
|
||||
_wsHelper->joinSubThread();
|
||||
|
||||
|
@ -381,14 +377,14 @@ WebSocket::State WebSocket::getReadyState()
|
|||
|
||||
int WebSocket::onSubThreadLoop()
|
||||
{
|
||||
if (_readyState == kStateClosed || _readyState == kStateClosing)
|
||||
if (_readyState == State::CLOSED || _readyState == State::CLOSING)
|
||||
{
|
||||
libwebsocket_context_destroy(_wsContext);
|
||||
// return 1 to exit the loop.
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (_wsContext && _readyState != kStateClosed && _readyState != kStateClosing)
|
||||
if (_wsContext && _readyState != State::CLOSED && _readyState != State::CLOSING)
|
||||
{
|
||||
libwebsocket_service(_wsContext, 0);
|
||||
}
|
||||
|
@ -424,15 +420,15 @@ void WebSocket::onSubThreadStarted()
|
|||
|
||||
_wsContext = libwebsocket_create_context(&info);
|
||||
|
||||
if(NULL != _wsContext){
|
||||
_readyState = kStateConnecting;
|
||||
std::string name;
|
||||
for (int i = 0; _wsProtocols[i].callback != NULL; ++i) {
|
||||
name += (_wsProtocols[i].name);
|
||||
if (_wsProtocols[i+1].callback != NULL)
|
||||
if(nullptr != _wsContext)
|
||||
{
|
||||
name += ", ";
|
||||
}
|
||||
_readyState = State::CONNECTING;
|
||||
std::string name;
|
||||
for (int i = 0; _wsProtocols[i].callback != nullptr; ++i)
|
||||
{
|
||||
name += (_wsProtocols[i].name);
|
||||
|
||||
if (_wsProtocols[i+1].callback != nullptr) name += ", ";
|
||||
}
|
||||
_wsInstance = libwebsocket_client_connect(_wsContext, _host.c_str(), _port, _SSLConnection,
|
||||
_path.c_str(), _host.c_str(), _host.c_str(),
|
||||
|
@ -451,8 +447,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
void *user, void *in, size_t len)
|
||||
{
|
||||
//CCLOG("socket callback for %d reason", reason);
|
||||
CCASSERT(_wsContext == NULL || ctx == _wsContext, "Invalid context.");
|
||||
CCASSERT(_wsInstance == NULL || wsi == NULL || wsi == _wsInstance, "Invaild websocket instance.");
|
||||
CCASSERT(_wsContext == nullptr || ctx == _wsContext, "Invalid context.");
|
||||
CCASSERT(_wsInstance == nullptr || wsi == nullptr || wsi == _wsInstance, "Invaild websocket instance.");
|
||||
|
||||
switch (reason)
|
||||
{
|
||||
|
@ -460,17 +456,17 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
case LWS_CALLBACK_PROTOCOL_DESTROY:
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
{
|
||||
WsMessage* msg = NULL;
|
||||
WsMessage* msg = nullptr;
|
||||
if (reason == LWS_CALLBACK_CLIENT_CONNECTION_ERROR
|
||||
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateConnecting)
|
||||
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == kStateConnecting)
|
||||
|| (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CONNECTING)
|
||||
|| (reason == LWS_CALLBACK_DEL_POLL_FD && _readyState == State::CONNECTING)
|
||||
)
|
||||
{
|
||||
msg = new WsMessage();
|
||||
msg->what = WS_MSG_TO_UITHREAD_ERROR;
|
||||
_readyState = kStateClosing;
|
||||
_readyState = State::CLOSING;
|
||||
}
|
||||
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == kStateClosing)
|
||||
else if (reason == LWS_CALLBACK_PROTOCOL_DESTROY && _readyState == State::CLOSING)
|
||||
{
|
||||
msg = new WsMessage();
|
||||
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
||||
|
@ -486,7 +482,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
{
|
||||
WsMessage* msg = new WsMessage();
|
||||
msg->what = WS_MSG_TO_UITHREAD_OPEN;
|
||||
_readyState = kStateOpen;
|
||||
_readyState = State::OPEN;
|
||||
|
||||
/*
|
||||
* start the ball rolling,
|
||||
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
|
||||
|
@ -503,8 +500,8 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
std::list<WsMessage*>::iterator iter = _wsHelper->_subThreadWsMessageQueue->begin();
|
||||
|
||||
int bytesWrite = 0;
|
||||
for (; iter != _wsHelper->_subThreadWsMessageQueue->end(); ++iter) {
|
||||
|
||||
for (; iter != _wsHelper->_subThreadWsMessageQueue->end(); ++iter)
|
||||
{
|
||||
WsMessage* subThreadMsg = *iter;
|
||||
|
||||
if ( WS_MSG_TO_SUBTRHEAD_SENDING_STRING == subThreadMsg->what
|
||||
|
@ -531,10 +528,12 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
|
||||
bytesWrite = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], data->len, writeProtocol);
|
||||
|
||||
if (bytesWrite < 0) {
|
||||
if (bytesWrite < 0)
|
||||
{
|
||||
CCLOGERROR("%s", "libwebsocket_write error...");
|
||||
}
|
||||
if (bytesWrite < data->len) {
|
||||
if (bytesWrite < data->len)
|
||||
{
|
||||
CCLOGERROR("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
|
||||
}
|
||||
|
||||
|
@ -562,10 +561,10 @@ int WebSocket::onSocketCallback(struct libwebsocket_context *ctx,
|
|||
|
||||
_wsHelper->quitSubThread();
|
||||
|
||||
if (_readyState != kStateClosed)
|
||||
if (_readyState != State::CLOSED)
|
||||
{
|
||||
WsMessage* msg = new WsMessage();
|
||||
_readyState = kStateClosed;
|
||||
_readyState = State::CLOSED;
|
||||
msg->what = WS_MSG_TO_UITHREAD_CLOSE;
|
||||
_wsHelper->sendMessageToUIThread(msg);
|
||||
}
|
||||
|
@ -637,7 +636,7 @@ void WebSocket::onUIThreadReceiveMessage(WsMessage* msg)
|
|||
case WS_MSG_TO_UITHREAD_ERROR:
|
||||
{
|
||||
// FIXME: The exact error needs to be checked.
|
||||
WebSocket::ErrorCode err = ERROR_CONNECTION_FAILURE;
|
||||
WebSocket::ErrorCode err = ErrorCode::CONNECTION_FAILURE;
|
||||
_delegate->onError(this, err);
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -63,15 +63,22 @@ public:
|
|||
/**
|
||||
* @brief Errors in websocket
|
||||
*/
|
||||
enum ErrorCode
|
||||
enum class ErrorCode
|
||||
{
|
||||
ERROR_TIME_OUT,
|
||||
ERROR_CONNECTION_FAILURE,
|
||||
ERROR_UNKNOWN,
|
||||
TIME_OUT,
|
||||
CONNECTION_FAILURE,
|
||||
UNKNOWN,
|
||||
};
|
||||
|
||||
kErrorTimeout = ERROR_TIME_OUT,
|
||||
kErrorConnectionFailure = ERROR_CONNECTION_FAILURE,
|
||||
kErrorUnknown = ERROR_UNKNOWN,
|
||||
/**
|
||||
* Websocket state
|
||||
*/
|
||||
enum class State
|
||||
{
|
||||
CONNECTING,
|
||||
OPEN,
|
||||
CLOSING,
|
||||
CLOSED,
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -114,17 +121,6 @@ public:
|
|||
*/
|
||||
void close();
|
||||
|
||||
/**
|
||||
* Websocket state
|
||||
*/
|
||||
enum State
|
||||
{
|
||||
kStateConnecting = 0,
|
||||
kStateOpen,
|
||||
kStateClosing,
|
||||
kStateClosed
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Gets current state of connection.
|
||||
*/
|
||||
|
|
|
@ -201,7 +201,7 @@ void WebSocketTestLayer::toExtensionsMainLayer(cocos2d::Object *sender)
|
|||
// Menu Callbacks
|
||||
void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
||||
{
|
||||
if (_wsiSendText->getReadyState() == WebSocket::kStateOpen)
|
||||
if (_wsiSendText->getReadyState() == WebSocket::State::OPEN)
|
||||
{
|
||||
_sendTextStatus->setString("Send Text WS is waiting...");
|
||||
_wsiSendText->send("Hello WebSocket, I'm a text message.");
|
||||
|
@ -216,7 +216,7 @@ void WebSocketTestLayer::onMenuSendTextClicked(cocos2d::Object *sender)
|
|||
|
||||
void WebSocketTestLayer::onMenuSendBinaryClicked(cocos2d::Object *sender)
|
||||
{
|
||||
if (_wsiSendBinary->getReadyState() == WebSocket::kStateOpen)
|
||||
if (_wsiSendBinary->getReadyState() == WebSocket::State::OPEN)
|
||||
{
|
||||
_sendBinaryStatus->setString("Send Binary WS is waiting...");
|
||||
char buf[] = "Hello WebSocket,\0 I'm\0 a\0 binary\0 message\0.";
|
||||
|
|
Loading…
Reference in New Issue