Skip to content

Commit

Permalink
Merge branch 'master' into 0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
robotconscience committed Mar 6, 2016
2 parents ffa4a49 + ef1d7d6 commit da28a48
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
14 changes: 11 additions & 3 deletions libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace ofxLibwebsockets {
string channel;
string protocol;
int version;
bool reconnect;
int reconnectInterval;

// advanced: timeout options
// names are from libwebsockets (ka == keep alive)
Expand All @@ -36,7 +38,7 @@ namespace ofxLibwebsockets {

Client();
~Client();

// Note: the boolean returned here == libwebsockets setup success
// You will receive an "onOpen" event on successful connect
// and "onClose" on unsuccessful
Expand Down Expand Up @@ -81,7 +83,11 @@ namespace ofxLibwebsockets {
Connection * getConnection(){
return connection;
}


// Note: you do not need to call this function! It is called
// automatically and only used for reconnecting
void update(ofEventArgs& args);

protected:
ClientOptions defaultOptions;
void onClose( Event& args );
Expand All @@ -95,6 +101,8 @@ namespace ofxLibwebsockets {

//wrap protocol
Protocol clientProtocol;


bool bShouldReconnect;
uint64_t lastReconnectTime;
};
};
51 changes: 38 additions & 13 deletions libs/ofxLibwebsockets/src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace ofxLibwebsockets {

ClientOptions defaultClientOptions(){
ClientOptions opts;
opts.host = "localhost";
opts.port = 80;
opts.bUseSSL = false;
opts.channel = "/";
opts.protocol = "NULL";
opts.version = -1; //use latest version
opts.host = "localhost";
opts.port = 80;
opts.bUseSSL = false;
opts.channel = "/";
opts.protocol = "NULL";
opts.version = -1; //use latest version
opts.reconnect = true;
opts.reconnectInterval = 1000;

opts.ka_time = 0;
opts.ka_probes = 0;
Expand All @@ -33,14 +35,16 @@ namespace ofxLibwebsockets {
reactors.push_back(this);

defaultOptions = defaultClientOptions();


ofAddListener( ofEvents().update, this, &Client::update);
ofAddListener( clientProtocol.oncloseEvent, this, &Client::onClose);
}


//--------------------------------------------------------------
Client::~Client(){
exit();
ofRemoveListener( ofEvents().update, this, &Client::update);
}

//--------------------------------------------------------------
Expand All @@ -67,7 +71,9 @@ namespace ofxLibwebsockets {
address = options.host;
port = options.port;
channel = options.channel;

defaultOptions = options;
bShouldReconnect = defaultOptions.reconnect;

/*
enum lws_log_levels {
LLL_ERR = 1 << 0,
Expand Down Expand Up @@ -172,6 +178,9 @@ namespace ofxLibwebsockets {

//--------------------------------------------------------------
void Client::close(){
// Self-initiated call to close() means we shouldn't try to reconnect
bShouldReconnect = false;

if (isThreadRunning()){
waitForThread(true);
} else {
Expand Down Expand Up @@ -200,7 +209,9 @@ namespace ofxLibwebsockets {
if ( context != NULL ){
closeAndFree = true;
lwsconnection = NULL;
}
}

lastReconnectTime = ofGetElapsedTimeMillis();
}

//--------------------------------------------------------------
Expand Down Expand Up @@ -230,7 +241,18 @@ namespace ofxLibwebsockets {
connection->sendBinary(data,size);
}
}


//--------------------------------------------------------------
void Client::update(ofEventArgs& args) {
if (!isConnected() && bShouldReconnect) {
uint64_t now = ofGetElapsedTimeMillis();
if (now - lastReconnectTime > defaultOptions.reconnectInterval) {
lastReconnectTime = now;
connect( defaultOptions );
}
}
}

//--------------------------------------------------------------
void Client::threadedFunction(){
while ( isThreadRunning() ){
Expand All @@ -244,9 +266,12 @@ namespace ofxLibwebsockets {
if (context != NULL && lwsconnection != NULL){
//libwebsocket_callback_on_writable(context,lwsconnection);
connection->update();
lock();
int n = libwebsocket_service(context, waitMillis);
unlock();

if (lock())
{
int n = libwebsocket_service(context, waitMillis);
unlock();
}
} else {
stopThread();
if ( context != NULL ){
Expand Down
9 changes: 6 additions & 3 deletions libs/ofxLibwebsockets/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ namespace ofxLibwebsockets {
//unlock();
}
}
lock();
libwebsocket_service(context, waitMillis);
unlock();

if (lock())
{
libwebsocket_service(context, waitMillis);
unlock();
}
}
}
}

0 comments on commit da28a48

Please sign in to comment.