diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f714c..a437e06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,12 +12,5 @@ add_library(pushcpp target_link_libraries(pushcpp pthread) target_link_libraries(pushcpp ${JSON_LIBRARY}) -add_executable(exampleexe ./example ./pushcpp) - -target_link_libraries(exampleexe pthread pushcpp) - set_target_properties(pushcpp PROPERTIES COMPILE_FLAGS "-std=c++0x -fstack-protector-all -Wstack-protector --param ssp-buffer-size=4") - -set_target_properties(exampleexe PROPERTIES COMPILE_FLAGS - "-std=c++0x -fstack-protector-all -Wstack-protector --param ssp-buffer-size=4") diff --git a/pushcpp.cpp b/pushcpp.cpp index ef77410..0bd1e2a 100644 --- a/pushcpp.cpp +++ b/pushcpp.cpp @@ -1,26 +1,26 @@ #include "pushcpp_internal.h" -pushcpp::pushcpp( - const string &appKey, - ConnectionEventHandler ch, - ErrorEventHandler eh -) -{ +pushcpp::pushcpp(const string &appKey, + std::function ch, + std::function eh, + const std::string &cluster) + : request_connection_(false) { this->m_connectionEventHandler = ch; this->m_errorEventHandler = eh; stringstream str; - str << "ws://ws.pusherapp.com:80/app/"; + str << "ws://ws"; + if (!cluster.empty()) { + str << "-"; + str << cluster; + } + str << ".pusher.com:80/app/"; str << appKey; str << "?client=pushcpp&version=1.0&protocol=5"; m_url = str.str(); } - void pushcpp::connect() { - DEBUG("Connecting."); - assert(!this->m_eventThread); - m_eventThread = new thread(&pushcpp::EventThread, this); - assert(this->m_eventThread); + request_connection_ = true; } bool pushcpp::connected() const @@ -35,15 +35,6 @@ bool pushcpp::connected() const void pushcpp::disconnect(bool wait) { m_wantDisconnect = true; - if (wait && m_eventThread) - m_eventThread->join(); -} - -void pushcpp::join() -{ - assert(this->m_eventThread); - DEBUG("joining!"); - m_eventThread->join(); } bool pushcpp::sendRaw(const string &raw) @@ -80,4 +71,3 @@ bool pushcpp::send( free(dumped); return ret; } - diff --git a/pushcpp.h b/pushcpp.h index 4d80bc7..0dee110 100644 --- a/pushcpp.h +++ b/pushcpp.h @@ -4,7 +4,8 @@ #include #include #include -#include +#include +#include class pushcpp { @@ -13,16 +14,6 @@ class pushcpp CONNECTED = 0, DISCONNECTED = 1 }; - - typedef void (*ConnectionEventHandler)( - const ConnectionEvent ce - ); - - typedef void (*ErrorEventHandler)( - const int code, - const std::string &message - ); - typedef void (*ChannelEventHandler)( const std::string &channel, const std::string &event, @@ -59,7 +50,9 @@ class pushcpp struct ChannelData { bool subscribed = false; ChannelAuthHandler authHandler; - std::set eventHandlers; + std::vector> eventHandlers; std::set presenceMemberIds; void clear() @@ -75,8 +68,9 @@ class pushcpp */ pushcpp( const std::string &appKey, - ConnectionEventHandler ch = NULL, - ErrorEventHandler eh = NULL + std::function ch = nullptr, + std::function eh = nullptr, + const std::string &cluster = "" ); /** @@ -98,13 +92,6 @@ class pushcpp */ void disconnect(bool wait = false); - /** - * Join against the running thread after calling connect(). - * A helper for testing this library or if you want to use it standalone - * by blocking on the event loop. - */ - void join(); - /** * Subscribe to a channel. The given event handler * will be called when a channel receives a message. @@ -123,7 +110,9 @@ class pushcpp * This handler receives all channel events, including * internal ones, in case you want to act on them. */ - ChannelEventHandler event, + std::function event, /** * This is called for authentication requests as described * above. presence- and private- channels require this. @@ -187,6 +176,7 @@ class pushcpp { return m_socketId; } + void Execute(); private: // Dont allow copying. @@ -201,8 +191,6 @@ class pushcpp // The current connection, if any! void *m_websocket = NULL; - std::thread * m_eventThread = NULL; - void EventThread(); void WS_Dispatch(const std::string & message); /* Send a subscription request to Pusher. You do not need to call this @@ -212,11 +200,12 @@ class pushcpp bool subscribe, const std::string &channel ); - - ErrorEventHandler m_errorEventHandler; - ConnectionEventHandler m_connectionEventHandler; + std::function m_connectionEventHandler; + std::function m_errorEventHandler; // The complete list of channels we (want to) subscribe to. // This includes channels we were rejected from. std::unordered_map m_channelData; + + bool request_connection_; }; diff --git a/pushcpp_eventloop.cpp b/pushcpp_eventloop.cpp index 951f697..cfb8a00 100644 --- a/pushcpp_eventloop.cpp +++ b/pushcpp_eventloop.cpp @@ -1,52 +1,40 @@ #include "pushcpp_internal.h" -void pushcpp::EventThread() -{ - while (true) { - /* attempt to connect */ - DEBUG("polling thread started"); - - while ( - this->m_websocket == NULL || - ((WebSocket::pointer)this->m_websocket)-> - getReadyState() == WebSocket::CLOSED - ) { - DEBUG("Attempting to connect!"); - +void pushcpp::Execute() { + if (request_connection_) { + if (this->m_websocket == NULL || + ((WebSocket::pointer)this->m_websocket)->getReadyState() + == WebSocket::CLOSED) { + // logger attempt to connecte if (this->m_websocket != NULL) delete((WebSocket::pointer) this->m_websocket); - this->m_websocket = (void*) WebSocket::from_url(m_url); + return; } - WebSocket::pointer ws = (WebSocket::pointer) this->m_websocket; - - DEBUG("connected, (re)subscribing to channels"); - - while (ws->getReadyState() != WebSocket::CLOSED) { + // connected log (subs.. resubs..) + if (ws->getReadyState() != WebSocket::CLOSED) { ws->poll(100); - ws->dispatch([this, ws](const string & msg) { + ws->dispatch([this, ws](const string &msg) { WS_Dispatch(msg); }); - - if (m_wantDisconnect) + if (m_wantDisconnect) { ws->close(); + request_connection_ = false; + } + return; } - - DEBUG("Lost connection, readyState: %d", ws->getReadyState()); + // add log connection Lost this->m_socketId = ""; - - for (auto it = m_channelData.begin(); it != m_channelData.end(); it++) + for (auto it = m_channelData.begin(); it != m_channelData.end(); it++) { it->second.clear(); - - if (m_connectionEventHandler) + } + if (m_connectionEventHandler) { m_connectionEventHandler(ConnectionEvent::DISCONNECTED); - - if (m_wantDisconnect) - break; + } + if (m_wantDisconnect) { + m_wantDisconnect = false; + return; + } } - - m_wantDisconnect = false; - - DEBUG("thread was stopped"); } diff --git a/pushcpp_subscriptions.cpp b/pushcpp_subscriptions.cpp index e5d76a4..7f80553 100644 --- a/pushcpp_subscriptions.cpp +++ b/pushcpp_subscriptions.cpp @@ -2,15 +2,18 @@ bool pushcpp::subscribe( const string &channel, - ChannelEventHandler event, + std::function event, ChannelAuthHandler auth ) { ChannelData d = m_channelData[channel]; - - if (event != NULL) - d.eventHandlers.insert(event); - + std::cout << "Debug" << std::endl; + if (event != NULL) { + std::cout << "Debug" << std::endl; + d.eventHandlers.emplace_back(event); + } if (auth != NULL) d.authHandler = auth; else