Skip to content

Commit

Permalink
refactor: use ws wrapper for liveupdates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Jul 14, 2023
1 parent 020a015 commit f60147e
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 273 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ set(SOURCE_FILES

providers/liveupdates/BasicPubSubClient.hpp
providers/liveupdates/BasicPubSubManager.hpp
providers/liveupdates/BasicPubSubWebsocket.hpp

providers/seventv/SeventvBadges.cpp
providers/seventv/SeventvBadges.hpp
Expand Down
9 changes: 4 additions & 5 deletions src/providers/bttv/BttvLiveUpdates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ void BttvLiveUpdates::partChannel(const QString &id)
}
}

void BttvLiveUpdates::onMessage(
websocketpp::connection_hdl /*hdl*/,
BasicPubSubManager<BttvLiveUpdateSubscription>::WebsocketMessagePtr msg)
void BttvLiveUpdates::onTextMessage(const ws::Connection & /*conn*/,
const QLatin1String &data)
{
const auto &payload = QString::fromStdString(msg->get_payload());
QJsonDocument jsonDoc(QJsonDocument::fromJson(payload.toUtf8()));
QJsonDocument jsonDoc(
QJsonDocument::fromJson({data.data(), data.length()}));

if (jsonDoc.isNull())
{
Expand Down
6 changes: 2 additions & 4 deletions src/providers/bttv/BttvLiveUpdates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ class BttvLiveUpdates : public BasicPubSubManager<BttvLiveUpdateSubscription>
void partChannel(const QString &id);

protected:
void onMessage(
websocketpp::connection_hdl hdl,
BasicPubSubManager<BttvLiveUpdateSubscription>::WebsocketMessagePtr msg)
override;
void onTextMessage(const ws::Connection &conn,
const QLatin1String &data) override;

private:
// Contains all joined Twitch channel-ids
Expand Down
59 changes: 12 additions & 47 deletions src/providers/liveupdates/BasicPubSubClient.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#pragma once

#include "common/QLogging.hpp"
#include "providers/liveupdates/BasicPubSubWebsocket.hpp"
#include "singletons/Settings.hpp"
#include "providers/ws/Client.hpp"
#include "util/DebugCount.hpp"
#include "util/Helpers.hpp"

#include <pajlada/signals/signal.hpp>

#include <atomic>
#include <chrono>
#include <unordered_set>
#include <utility>

namespace chatterino {

Expand All @@ -33,12 +32,11 @@ class BasicPubSubClient
// The maximum amount of subscriptions this connections can handle
const size_t maxSubscriptions;

BasicPubSubClient(liveupdates::WebsocketClient &websocketClient,
liveupdates::WebsocketHandle handle,
BasicPubSubClient(ws::Client *client, ws::Connection conn,
size_t maxSubscriptions = 100)
: maxSubscriptions(maxSubscriptions)
, websocketClient_(websocketClient)
, handle_(std::move(handle))
, client_(client)
, connection_(std::move(conn))
{
}

Expand All @@ -54,22 +52,6 @@ class BasicPubSubClient
{
}

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

if (ec)
{
qCDebug(chatterinoLiveupdates) << "Error sending message" << payload
<< ":" << ec.message().c_str();
return false;
}

return true;
}

/**
* @return true if this client subscribed to this subscription
* and the current subscriptions don't exceed the maximum
Expand Down Expand Up @@ -97,7 +79,7 @@ class BasicPubSubClient
DebugCount::increase("LiveUpdates subscriptions");

QByteArray encoded = subscription.encodeSubscribe();
this->send(encoded);
this->client_->sendText(this->connection_, encoded);

return true;
}
Expand All @@ -117,40 +99,23 @@ class BasicPubSubClient
DebugCount::decrease("LiveUpdates subscriptions");

QByteArray encoded = subscription.encodeUnsubscribe();
this->send(encoded);
this->client_->sendText(this->connection_, encoded);

return true;
}

void close(const std::string &reason,
websocketpp::close::status::value code =
websocketpp::close::status::normal)
void close(const QString &reason,
ws::Client::CloseCode code = ws::Client::CloseCode::Normal)
{
liveupdates::WebsocketErrorCode ec;

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

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

bool isStarted() const
{
return this->started_.load(std::memory_order_acquire);
}

liveupdates::WebsocketClient &websocketClient_;
ws::Client *client_;

private:
void start()
Expand All @@ -166,7 +131,7 @@ class BasicPubSubClient
this->started_.store(false, std::memory_order_release);
}

liveupdates::WebsocketHandle handle_;
ws::Connection connection_;
std::unordered_set<Subscription> subscriptions_;

std::atomic<bool> started_{false};
Expand Down
Loading

0 comments on commit f60147e

Please sign in to comment.