Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sw 001 #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
34 changes: 12 additions & 22 deletions pushcpp.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include "pushcpp_internal.h"

pushcpp::pushcpp(
const string &appKey,
ConnectionEventHandler ch,
ErrorEventHandler eh
)
{
pushcpp::pushcpp(const string &appKey,
std::function<void(const ConnectionEvent ce)> ch,
std::function<void(const int, const std::string&)> 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
Expand All @@ -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)
Expand Down Expand Up @@ -80,4 +71,3 @@ bool pushcpp::send(
free(dumped);
return ret;
}

43 changes: 16 additions & 27 deletions pushcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <set>
#include <unordered_map>
#include <vector>
#include <thread>
#include <utility>
#include <functional>

class pushcpp
{
Expand All @@ -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,
Expand Down Expand Up @@ -59,7 +50,9 @@ class pushcpp
struct ChannelData {
bool subscribed = false;
ChannelAuthHandler authHandler;
std::set<ChannelEventHandler> eventHandlers;
std::vector<std::function<void(const std::string&,
const std::string&,
const std::string&)>> eventHandlers;
std::set<std::string> presenceMemberIds;

void clear()
Expand All @@ -75,8 +68,9 @@ class pushcpp
*/
pushcpp(
const std::string &appKey,
ConnectionEventHandler ch = NULL,
ErrorEventHandler eh = NULL
std::function<void(const ConnectionEvent ce)> ch = nullptr,
std::function<void(const int, const std::string&)> eh = nullptr,
const std::string &cluster = ""
);

/**
Expand All @@ -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.
Expand All @@ -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<void(const std::string&,
const std::string&,
const std::string&)> event,
/**
* This is called for authentication requests as described
* above. presence- and private- channels require this.
Expand Down Expand Up @@ -187,6 +176,7 @@ class pushcpp
{
return m_socketId;
}
void Execute();

private:
// Dont allow copying.
Expand All @@ -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
Expand All @@ -212,11 +200,12 @@ class pushcpp
bool subscribe,
const std::string &channel
);

ErrorEventHandler m_errorEventHandler;
ConnectionEventHandler m_connectionEventHandler;
std::function<void(const ConnectionEvent ce)> m_connectionEventHandler;
std::function<void(const int, const std::string&)> m_errorEventHandler;

// The complete list of channels we (want to) subscribe to.
// This includes channels we were rejected from.
std::unordered_map<std::string, ChannelData> m_channelData;

bool request_connection_;
};
58 changes: 23 additions & 35 deletions pushcpp_eventloop.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
13 changes: 8 additions & 5 deletions pushcpp_subscriptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

bool pushcpp::subscribe(
const string &channel,
ChannelEventHandler event,
std::function<void(const std::string&,
const std::string&,
const std::string&)> 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
Expand Down