From ab3ad716c022b4d8e811d58649a03e3e3483218f Mon Sep 17 00:00:00 2001 From: Egon Zemmer Date: Fri, 12 Jun 2015 19:16:40 +0200 Subject: [PATCH] Solves channel event callback segmentation fault. Add channel unsubscribe with callbacks. Code rework. --- README.md | 2 +- websocket-rails-client/channel.cpp | 22 +++--------- websocket-rails-client/channel.hpp | 6 ++-- websocket-rails-client/event.cpp | 8 +++-- websocket-rails-client/event.hpp | 2 +- websocket-rails-client/websocket.hpp | 2 +- .../websocket_connection.cpp | 2 +- .../websocket_connection.hpp | 2 +- websocket-rails-client/websocket_rails.cpp | 36 +++++++++++++++---- websocket-rails-client/websocket_rails.hpp | 3 +- 10 files changed, 47 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 7fb8542..adaadd5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/websocket-rails-client/channel.cpp b/websocket-rails-client/channel.cpp index 22c6db7..619ebeb 100644 --- a/websocket-rails-client/channel.cpp +++ b/websocket-rails-client/channel.cpp @@ -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 @@ -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); } { @@ -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); } @@ -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 Channel::flush_queue() { channel_lock guard(this->dispatcher->event_queue_mutex); while(!this->event_queue.empty()) { diff --git a/websocket-rails-client/channel.hpp b/websocket-rails-client/channel.hpp index 8abe246..69f4f2f 100644 --- a/websocket-rails-client/channel.hpp +++ b/websocket-rails-client/channel.hpp @@ -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 @@ -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); @@ -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 flush_queue(); }; diff --git a/websocket-rails-client/event.cpp b/websocket-rails-client/event.cpp index d469e64..f3da739 100644 --- a/websocket-rails-client/event.cpp +++ b/websocket-rails-client/event.cpp @@ -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 @@ -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); + } } } diff --git a/websocket-rails-client/event.hpp b/websocket-rails-client/event.hpp index 024a37e..8503de2 100644 --- a/websocket-rails-client/event.hpp +++ b/websocket-rails-client/event.hpp @@ -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 diff --git a/websocket-rails-client/websocket.hpp b/websocket-rails-client/websocket.hpp index fc3246f..824070a 100644 --- a/websocket-rails-client/websocket.hpp +++ b/websocket-rails-client/websocket.hpp @@ -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 diff --git a/websocket-rails-client/websocket_connection.cpp b/websocket-rails-client/websocket_connection.cpp index b781f2d..5d48136 100644 --- a/websocket-rails-client/websocket_connection.cpp +++ b/websocket-rails-client/websocket_connection.cpp @@ -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 diff --git a/websocket-rails-client/websocket_connection.hpp b/websocket-rails-client/websocket_connection.hpp index e1e8432..27f26e4 100644 --- a/websocket-rails-client/websocket_connection.hpp +++ b/websocket-rails-client/websocket_connection.hpp @@ -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 diff --git a/websocket-rails-client/websocket_rails.cpp b/websocket-rails-client/websocket_rails.cpp index bb78447..a3c5a6f 100644 --- a/websocket-rails-client/websocket_rails.cpp +++ b/websocket-rails-client/websocket_rails.cpp @@ -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 @@ -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 * @@ -348,7 +368,8 @@ std::vector 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); @@ -357,3 +378,4 @@ std::vector WebsocketRails::reconnectChannels() { } return results; } + diff --git a/websocket-rails-client/websocket_rails.hpp b/websocket-rails-client/websocket_rails.hpp index a081b03..2f91e23 100644 --- a/websocket-rails-client/websocket_rails.hpp +++ b/websocket-rails-client/websocket_rails.hpp @@ -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 @@ -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: