Skip to content

Commit

Permalink
refactor: use ws wrapper in pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Jul 14, 2023
1 parent 0987d2b commit 945e810
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 298 deletions.
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,9 @@ set(SOURCE_FILES
providers/twitch/PubSubClient.cpp
providers/twitch/PubSubClient.hpp
providers/twitch/PubSubClientOptions.hpp
providers/twitch/PubSubHelpers.hpp
providers/twitch/PubSubManager.cpp
providers/twitch/PubSubManager.hpp
providers/twitch/PubSubMessages.hpp
providers/twitch/PubSubWebsocket.hpp
providers/twitch/TwitchAccount.cpp
providers/twitch/TwitchAccount.hpp
providers/twitch/TwitchAccountManager.cpp
Expand Down
78 changes: 20 additions & 58 deletions src/providers/twitch/PubSubClient.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#include "providers/twitch/PubSubClient.hpp"

#include "common/Literals.hpp"
#include "common/QLogging.hpp"
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/PubSubHelpers.hpp"
#include "providers/twitch/PubSubMessages.hpp"
#include "providers/twitch/pubsubmessages/Listen.hpp"
#include "providers/twitch/pubsubmessages/Unlisten.hpp"
#include "singletons/Settings.hpp"
#include "util/DebugCount.hpp"
#include "util/Helpers.hpp"
#include "util/RapidjsonHelpers.hpp"

#include <exception>
#include <thread>

namespace chatterino {

static const char *PING_PAYLOAD = R"({"type":"PING"})";
using namespace literals;

PubSubClient::PubSubClient(WebsocketClient &websocketClient,
WebsocketHandle handle,
constexpr const QLatin1String PING_PAYLOAD = R"({"type":"PING"})"_L1;

PubSubClient::PubSubClient(ws::Client *client, ws::Connection conn,
const PubSubClientOptions &clientOptions)
: websocketClient_(websocketClient)
, handle_(handle)
: client_(client)
, connection_(std::move(conn))
, clientOptions_(clientOptions)
{
}
Expand All @@ -42,25 +40,9 @@ void PubSubClient::stop()
this->started_ = false;
}

void PubSubClient::close(const std::string &reason,
websocketpp::close::status::value code)
void PubSubClient::close(const QString &reason)
{
WebsocketErrorCode ec;

auto conn = this->websocketClient_.get_con_from_hdl(this->handle_, ec);
if (ec)
{
qCDebug(chatterinoPubSub)
<< "Error getting con:" << ec.message().c_str();
return;
}

conn->close(code, reason, ec);
if (ec)
{
qCDebug(chatterinoPubSub) << "Error closing:" << ec.message().c_str();
return;
}
this->client_->close(this->connection_, reason);
}

bool PubSubClient::listen(PubSubListenMessage msg)
Expand All @@ -83,7 +65,7 @@ bool PubSubClient::listen(PubSubListenMessage msg)
qCDebug(chatterinoPubSub)
<< "Subscribing to" << numRequestedListens << "topics";

this->send(msg.toJson());
this->client_->sendText(this->connection_, msg.toJson());

return true;
}
Expand Down Expand Up @@ -120,7 +102,7 @@ PubSubClient::UnlistenPrefixResponse PubSubClient::unlistenPrefix(

PubSubUnlistenMessage message(topics);

this->send(message.toJson());
this->client_->sendText(this->connection_, message.toJson());

return {message.topics, message.nonce};
}
Expand Down Expand Up @@ -170,7 +152,7 @@ void PubSubClient::ping()
return;
}

if (!this->send(PING_PAYLOAD))
if (!this->client_->sendText(this->connection_, PING_PAYLOAD))
{
return;
}
Expand All @@ -179,34 +161,14 @@ void PubSubClient::ping()

auto self = this->shared_from_this();

runAfter(this->websocketClient_.get_io_service(),
this->clientOptions_.pingInterval_, [self](auto timer) {
if (!self->started_)
{
return;
}

self->ping();
});
}

bool PubSubClient::send(const char *payload)
{
WebsocketErrorCode ec;
this->websocketClient_.send(this->handle_, payload,
websocketpp::frame::opcode::text, ec);

if (ec)
{
qCDebug(chatterinoPubSub) << "Error sending message" << payload << ":"
<< ec.message().c_str();
// TODO(pajlada): Check which error code happened and maybe
// gracefully handle it

return false;
}
this->client_->runAfter(this->clientOptions_.pingInterval_, [self]() {
if (!self->started_)
{
return;
}

return true;
self->ping();
});
}

} // namespace chatterino
13 changes: 5 additions & 8 deletions src/providers/twitch/PubSubClient.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "providers/twitch/PubSubClientOptions.hpp"
#include "providers/twitch/PubSubWebsocket.hpp"
#include "providers/ws/Client.hpp"

#include <pajlada/signals/signal.hpp>
#include <QString>
Expand Down Expand Up @@ -35,15 +35,13 @@ class PubSubClient : public std::enable_shared_from_this<PubSubClient>
// The max amount of topics we may listen to with a single connection
static constexpr std::vector<QString>::size_type MAX_LISTENS = 50;

PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _handle,
PubSubClient(ws::Client *client, ws::Connection conn,
const PubSubClientOptions &clientOptions);

void start();
void stop();

void close(const std::string &reason,
websocketpp::close::status::value code =
websocketpp::close::status::normal);
void close(const QString &reason);

bool listen(PubSubListenMessage msg);
UnlistenPrefixResponse unlistenPrefix(const QString &prefix);
Expand All @@ -59,10 +57,9 @@ class PubSubClient : public std::enable_shared_from_this<PubSubClient>

private:
void ping();
bool send(const char *payload);

WebsocketClient &websocketClient_;
WebsocketHandle handle_;
ws::Client *client_;
ws::Connection connection_;
uint16_t numListens_ = 0;

std::vector<Listener> listeners_;
Expand Down
54 changes: 0 additions & 54 deletions src/providers/twitch/PubSubHelpers.hpp

This file was deleted.

Loading

0 comments on commit 945e810

Please sign in to comment.