Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Commit

Permalink
Client transport based on Libwebsockets can automatically self destru…
Browse files Browse the repository at this point in the history
…ct on socket error
  • Loading branch information
OlehKulykov committed Feb 11, 2015
1 parent 43d979b commit 931adf4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
10 changes: 9 additions & 1 deletion fayecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -3025,6 +3026,7 @@ namespace FayeCpp {

void onTransportConnected();
void onTransportDisconnected();
void onTransportWillSelfDestruct();

void onClientResponceMessageReceived(const REVariantMap & message);
void onClientResponceMessagesListReceived(const REVariantList & messagesList);
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
{
Expand Down
24 changes: 20 additions & 4 deletions src/transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client, void(Client::*)(Responce*), Responce> * processMethod) :
_processMethod(processMethod),
_isConnected(false)
_isConnected(false),
_isSelfDestructing(false)
{
#if defined(HAVE_ASSERT_H)
assert(_processMethod);
Expand Down Expand Up @@ -195,7 +211,7 @@ namespace FayeCpp {
#if defined(HAVE_SUITABLE_QT_VERSION)
delete transport;
#elif defined(HAVE_LIBWEBSOCKETS_H)
///
/// will self destruct
#endif
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ namespace FayeCpp {
Advice _advice;

bool _isConnected;

bool _isSelfDestructing;

Delegate * delegate() const;

protected:
Expand All @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -331,6 +332,7 @@ namespace FayeCpp {
socket->workMethod();
HANDLE t = socket->_workThread;
socket->_workThread = NULL;
socket->onTransportWillSelfDestruct();
delete socket;
ThreadsJoiner::add(t);
return 0;
Expand Down

0 comments on commit 931adf4

Please sign in to comment.