diff --git a/fayecpp.h b/fayecpp.h index 1fe46e5..c219176 100644 --- a/fayecpp.h +++ b/fayecpp.h @@ -30,6 +30,7 @@ * Changes on version 0.1.7 (current): * - Minor libwebsockets fixes. * - Added error processing of received messages. + * - Client transport based on Libwebsockets can automatically self destruct on socket error. * * Changes on version 0.1.6: * - Added extra(ext) message field included in any Bayeux message. @@ -3025,6 +3026,7 @@ namespace FayeCpp { void onTransportConnected(); void onTransportDisconnected(); + void onTransportWillSelfDestruct(); void onClientResponceMessageReceived(const REVariantMap & message); void onClientResponceMessagesListReceived(const REVariantList & messagesList); @@ -3589,7 +3591,13 @@ namespace FayeCpp { /** @brief Faye transport protocol received message. */ - ResponceMessage + ResponceMessage, + + + /** + @brief Faye transport will self destruct + */ + ResponceTransportWillSelfDestruct } /** @brief Faye message type. diff --git a/src/client.cpp b/src/client.cpp index d9b9922..8ce1aff 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -141,6 +141,7 @@ namespace FayeCpp { case Responce::ResponceTransportDisconnected: this->onTransportDisconnected(); break; case Responce::ResponceTransportError: this->onClientError(responce); break; case Responce::ResponceMessage: this->onClientResponceReceived(responce); break; + case Responce::ResponceTransportWillSelfDestruct: this->onTransportWillSelfDestruct(); break; default: this->onClientError(responce); break; } } @@ -171,6 +172,16 @@ namespace FayeCpp { if (_delegate) _delegate->onFayeTransportDisconnected(this); } + + void Client::onTransportWillSelfDestruct() + { + if (_transport) + { + if (_transport->isSelfDestructing()) _transport = NULL; + } + + this->onTransportDisconnected(); + } void Client::onClientError(Responce * message) { diff --git a/src/transport.cpp b/src/transport.cpp index 0cb2725..4816e5c 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -146,15 +146,31 @@ namespace FayeCpp { Responce message(Responce::ResponceTransportError); message.setErrorString(error); if (_processMethod) _processMethod->invokeWithPointer(&message); } - + + void Transport::onTransportWillSelfDestruct() + { + FAYECPP_DEBUG_LOG("TRANSPORT WILL SELF DESTRUCT") + + _isSelfDestructing = true; + + Responce message(Responce::ResponceTransportWillSelfDestruct); + if (_processMethod) _processMethod->invokeWithPointer(&message); + } + bool Transport::isConnected() const { return _isConnected; } - + + bool Transport::isSelfDestructing() const + { + return _isSelfDestructing; + } + Transport::Transport(ClassMethodWrapper * processMethod) : _processMethod(processMethod), - _isConnected(false) + _isConnected(false), + _isSelfDestructing(false) { #if defined(HAVE_ASSERT_H) assert(_processMethod); @@ -195,7 +211,7 @@ namespace FayeCpp { #if defined(HAVE_SUITABLE_QT_VERSION) delete transport; #elif defined(HAVE_LIBWEBSOCKETS_H) - /// + /// will self destruct #endif } } diff --git a/src/transport.h b/src/transport.h index af820a1..65c3faf 100644 --- a/src/transport.h +++ b/src/transport.h @@ -93,7 +93,8 @@ namespace FayeCpp { Advice _advice; bool _isConnected; - + bool _isSelfDestructing; + Delegate * delegate() const; protected: @@ -110,8 +111,10 @@ namespace FayeCpp { void onDataReceived(const unsigned char * data, const size_t dataSize); void onError(const REString & error); void onError(const char * error); - + void onTransportWillSelfDestruct(); + public: + bool isSelfDestructing() const; void receivedAdvice(const REVariantMap & advice); bool isConnected() const; diff --git a/src/websocket.cpp b/src/websocket.cpp index e675e0b..a9705cb 100644 --- a/src/websocket.cpp +++ b/src/websocket.cpp @@ -320,6 +320,7 @@ namespace FayeCpp { socket->workMethod(); pthread_t * t = socket->_workThread; socket->_workThread = NULL; + socket->onTransportWillSelfDestruct(); delete socket; ThreadsJoiner::add(t); return NULL; @@ -331,6 +332,7 @@ namespace FayeCpp { socket->workMethod(); HANDLE t = socket->_workThread; socket->_workThread = NULL; + socket->onTransportWillSelfDestruct(); delete socket; ThreadsJoiner::add(t); return 0;