Skip to content

Commit

Permalink
Solves channel event callback segmentation fault. Add channel unsubsc…
Browse files Browse the repository at this point in the history
…ribe with callbacks. Code rework.
  • Loading branch information
phlegx committed Jun 12, 2015
1 parent ee8ee31 commit ab3ad71
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WebsocketRailsClient++ (v0.7.1)
# WebsocketRailsClient++ (v0.7.2)

WebsocketRailsClient++ is a C++ library that uses the implementation of RFC6455 (The WebSocket Protocol)
implemented in the WebSocket++ library, the Json++ light-weight JSON parser and the Boost library. It allows
Expand Down
22 changes: 4 additions & 18 deletions websocket-rails-client/channel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : channel.cpp
* Version : v0.7.1
* Version : v0.7.2
* Description : Channel Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down Expand Up @@ -53,11 +53,11 @@ Channel::Channel(std::string name, WebsocketRails & dispatcher, bool is_private,
* Functions *
************************************/

void Channel::destroy() {
void Channel::destroy(cb_func success_callback, cb_func failure_callback) {
if(this->getConnectionId() == (this->dispatcher->getConn() != 0 ? this->dispatcher->getConn()->getConnectionId() : "")) {
std::string event_name = "websocket_rails.unsubscribe";
jsonxx::Array data = this->initEventData(event_name);
Event event(data);
Event event(data, success_callback, failure_callback);
this->dispatcher->triggerEvent(event);
}
{
Expand Down Expand Up @@ -198,7 +198,7 @@ void Channel::initObject() {
}
this->setConnectionId(this->dispatcher->getConn() != 0 ? this->dispatcher->getConn()->getConnectionId() : "");
jsonxx::Array data = this->initEventData(event_name);
Event event(data, boost::bind(&Channel::successLauncher, this, _1), boost::bind(&Channel::failureLauncher, this, _1));
Event event = Event(data, this->on_success, this->on_failure);
this->dispatcher->triggerEvent(event);
}

Expand All @@ -212,20 +212,6 @@ jsonxx::Array Channel::initEventData(std::string event_name) {
}


void Channel::successLauncher(jsonxx::Object data) {
if(this->on_success) {
this->on_success(data);
}
}


void Channel::failureLauncher(jsonxx::Object data) {
if (this->on_failure) {
this->on_failure(data);
}
}


std::queue<Event> Channel::flush_queue() {
channel_lock guard(this->dispatcher->event_queue_mutex);
while(!this->event_queue.empty()) {
Expand Down
6 changes: 2 additions & 4 deletions websocket-rails-client/channel.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : channel.hpp
* Version : v0.7.1
* Version : v0.7.2
* Description : Channel Header Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down Expand Up @@ -48,7 +48,7 @@ class Channel {
/**
* Functions
**/
void destroy();
void destroy(cb_func success_callback, cb_func failure_callback);
void bind(std::string event_name, cb_func callback);
void unbindAll(std::string event_name);
void trigger(std::string event_name, jsonxx::Object event_data);
Expand Down Expand Up @@ -84,8 +84,6 @@ class Channel {
void setToken(std::string token);
void initObject();
jsonxx::Array initEventData(std::string event_name);
void successLauncher(jsonxx::Object data);
void failureLauncher(jsonxx::Object data);
std::queue<Event> flush_queue();

};
Expand Down
8 changes: 5 additions & 3 deletions websocket-rails-client/event.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : event.cpp
* Version : v0.7.1
* Version : v0.7.2
* Description : Event Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down Expand Up @@ -75,11 +75,13 @@ std::string Event::serialize() {

void Event::runCallbacks(bool success, jsonxx::Object event_data) {
if(success) {
if(this->success_callback)
if(this->success_callback) {
this->success_callback(event_data);
}
} else {
if(this->failure_callback)
if(this->failure_callback) {
this->failure_callback(event_data);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/event.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : event.hpp
* Version : v0.7.1
* Version : v0.7.2
* Description : Event Header Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : websocket.hpp
* Version : v0.7.1
* Version : v0.7.2
* Description : Websocket Header File in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket_connection.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : websocket.cpp
* Version : v0.7.1
* Version : v0.7.2
* Description : WebsocketConnection Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down
2 changes: 1 addition & 1 deletion websocket-rails-client/websocket_connection.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : websocket.hpp
* Version : v0.7.1
* Version : v0.7.2
* Description : WebsocketConnection Header Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down
36 changes: 29 additions & 7 deletions websocket-rails-client/websocket_rails.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : websocket_rails.cpp
* Version : v0.7.1
* Version : v0.7.2
* Description : WebsocketRails Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down Expand Up @@ -274,15 +274,35 @@ Channel WebsocketRails::subscribePrivate(std::string channel_name, cb_func succe


void WebsocketRails::unsubscribe(std::string channel_name) {
websocket_rails_lock guard(channel_queue_mutex);
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
return;
Channel channel;
{
websocket_rails_lock guard(channel_queue_mutex);
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
return;
}
channel = this->channel_queue[channel_name];
this->channel_queue.erase(channel_name);
}
cb_func success_callback, failure_callback;
channel.destroy(success_callback, failure_callback);
}


void WebsocketRails::unsubscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback) {
Channel channel;
{
websocket_rails_lock guard(channel_queue_mutex);
if(this->channel_queue.find(channel_name) == this->channel_queue.end()) {
return;
}
channel = this->channel_queue[channel_name];
this->channel_queue.erase(channel_name);
}
this->channel_queue[channel_name].destroy();
this->channel_queue.erase(channel_name);
channel.destroy(success_callback, failure_callback);
}



/********************************************************
* *
* PRIVATE METHODS *
Expand Down Expand Up @@ -348,7 +368,8 @@ std::vector<Channel> WebsocketRails::reconnectChannels() {
for(auto& x: this->channel_queue) {
Channel channel = x.second;
map_vec_cb_func callbacks = channel.getCallbacks();
channel.destroy();
cb_func success_callback, failure_callback;
channel.destroy(success_callback, failure_callback);
std::string channel_name = channel.getName();
this->channel_queue.erase(channel_name);
channel = channel.isPrivate() ? this->subscribePrivate(channel_name) : this->subscribe(channel_name);
Expand All @@ -357,3 +378,4 @@ std::vector<Channel> WebsocketRails::reconnectChannels() {
}
return results;
}

3 changes: 2 additions & 1 deletion websocket-rails-client/websocket_rails.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Name : websocket_rails.hpp
* Version : v0.7.1
* Version : v0.7.2
* Description : WesocketRails Header Class in C++, Ansi-style
* Author : Egon Zemmer
* Company : Phlegx Systems
Expand Down Expand Up @@ -100,6 +100,7 @@ class WebsocketRails {
Channel subscribePrivate(std::string channel_name);
Channel subscribePrivate(std::string channel_name, cb_func success_callback, cb_func failure_callback);
void unsubscribe(std::string channel_name);
void unsubscribe(std::string channel_name, cb_func success_callback, cb_func failure_callback);

private:

Expand Down

0 comments on commit ab3ad71

Please sign in to comment.