refactor(liveupdates): use WebSocketPool over websocketpp (#6308)

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2025-11-08 13:46:10 +01:00
committed by GitHub
parent 6aeb1ca6a7
commit 7f393e2401
26 changed files with 984 additions and 973 deletions
+54 -69
View File
@@ -1,105 +1,90 @@
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "common/Literals.hpp"
#include "liveupdates/BttvLiveUpdateClient.hpp"
#include "providers/liveupdates/BasicPubSubManager.hpp"
#include <QJsonDocument>
#include <unordered_set>
#include <utility>
namespace chatterino {
using namespace chatterino::literals;
using namespace Qt::StringLiterals;
BttvLiveUpdates::BttvLiveUpdates(QString host)
class BttvLiveUpdatesPrivate
: public BasicPubSubManager<BttvLiveUpdatesPrivate, BttvLiveUpdateClient>
{
public:
BttvLiveUpdatesPrivate(BttvLiveUpdates &parent, QString host);
~BttvLiveUpdatesPrivate() override;
BttvLiveUpdatesPrivate(const BttvLiveUpdatesPrivate &) = delete;
BttvLiveUpdatesPrivate(const BttvLiveUpdatesPrivate &&) = delete;
BttvLiveUpdatesPrivate &operator=(const BttvLiveUpdatesPrivate &) = delete;
BttvLiveUpdatesPrivate &operator=(const BttvLiveUpdatesPrivate &&) = delete;
std::shared_ptr<BttvLiveUpdateClient> makeClient();
// Contains all joined Twitch channel-ids
std::unordered_set<QString> joinedChannels;
BttvLiveUpdates &parent;
friend BasicPubSubManager<BttvLiveUpdates, BttvLiveUpdateClient>;
friend BttvLiveUpdates;
};
BttvLiveUpdatesPrivate::BttvLiveUpdatesPrivate(BttvLiveUpdates &parent,
QString host)
: BasicPubSubManager(std::move(host), u"BTTV"_s)
, parent(parent)
{
}
BttvLiveUpdates::~BttvLiveUpdates()
BttvLiveUpdatesPrivate::~BttvLiveUpdatesPrivate()
{
this->stop();
}
std::shared_ptr<BttvLiveUpdateClient> BttvLiveUpdatesPrivate::makeClient()
{
return std::make_shared<BttvLiveUpdateClient>(this->parent);
}
BttvLiveUpdates::BttvLiveUpdates(QString host)
: private_(std::make_unique<BttvLiveUpdatesPrivate>(*this, std::move(host)))
{
}
BttvLiveUpdates::~BttvLiveUpdates() = default;
void BttvLiveUpdates::joinChannel(const QString &channelID,
const QString &userID)
{
if (this->joinedChannels_.insert(channelID).second)
if (this->private_->joinedChannels.insert(channelID).second)
{
this->subscribe({BttvLiveUpdateSubscriptionChannel{channelID}});
this->subscribe({BttvLiveUpdateBroadcastMe{.twitchID = channelID,
.userID = userID}});
this->private_->subscribe(
{BttvLiveUpdateSubscriptionChannel{channelID}});
this->private_->subscribe({BttvLiveUpdateBroadcastMe{
.twitchID = channelID, .userID = userID}});
}
}
void BttvLiveUpdates::partChannel(const QString &id)
{
if (this->joinedChannels_.erase(id) > 0)
if (this->private_->joinedChannels.erase(id) > 0)
{
this->unsubscribe({BttvLiveUpdateSubscriptionChannel{id}});
this->private_->unsubscribe({BttvLiveUpdateSubscriptionChannel{id}});
}
}
void BttvLiveUpdates::onMessage(
websocketpp::connection_hdl /*hdl*/,
BasicPubSubManager<BttvLiveUpdateSubscription>::WebsocketMessagePtr msg)
void BttvLiveUpdates::stop()
{
const auto &payload = QString::fromStdString(msg->get_payload());
QJsonDocument jsonDoc(QJsonDocument::fromJson(payload.toUtf8()));
this->private_->stop();
}
if (jsonDoc.isNull())
{
qCDebug(chatterinoBttv) << "Failed to parse live update JSON";
return;
}
auto json = jsonDoc.object();
auto eventType = json["name"].toString();
auto eventData = json["data"].toObject();
if (eventType == "emote_create")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid add message" << json;
return;
}
this->signals_.emoteAdded.invoke(message);
}
else if (eventType == "emote_update")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid update message" << json;
return;
}
this->signals_.emoteUpdated.invoke(message);
}
else if (eventType == "emote_delete")
{
auto message = BttvLiveUpdateEmoteRemoveMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid deletion message" << json;
return;
}
this->signals_.emoteRemoved.invoke(message);
}
else if (eventType == "lookup_user")
{
// ignored
}
else
{
qCDebug(chatterinoBttv) << "Unhandled event:" << json;
}
const liveupdates::Diag &BttvLiveUpdates::diag() const
{
return this->private_->diag;
}
} // namespace chatterino
+24 -17
View File
@@ -1,25 +1,28 @@
#pragma once
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
#include "providers/bttv/liveupdates/BttvLiveUpdateSubscription.hpp"
#include "providers/liveupdates/BasicPubSubManager.hpp"
#include "util/QStringHash.hpp"
#include <pajlada/signals/signal.hpp>
#include <QString>
#include <unordered_set>
#include <memory>
namespace chatterino {
class BttvLiveUpdates : public BasicPubSubManager<BttvLiveUpdateSubscription>
namespace liveupdates {
struct Diag;
} // namespace liveupdates
struct BttvLiveUpdateEmoteUpdateAddMessage;
struct BttvLiveUpdateEmoteRemoveMessage;
class BttvLiveUpdatesPrivate;
class BttvLiveUpdates
{
template <typename T>
using Signal =
pajlada::Signals::Signal<T>; // type-id is vector<T, Alloc<T>>
using Signal = pajlada::Signals::Signal<T>;
public:
BttvLiveUpdates(QString host);
~BttvLiveUpdates() override;
~BttvLiveUpdates();
struct {
Signal<BttvLiveUpdateEmoteUpdateAddMessage> emoteAdded;
@@ -44,15 +47,19 @@ public:
*/
void partChannel(const QString &id);
protected:
void onMessage(
websocketpp::connection_hdl hdl,
BasicPubSubManager<BttvLiveUpdateSubscription>::WebsocketMessagePtr msg)
override;
/// Stop the manager
///
/// Used in tests to check that connections are closed (through #diag()).
/// Otherwise, calling the destructor is sufficient.
void stop();
/// Statistics about the opened/closed connections and received messages
///
/// Used in tests.
const liveupdates::Diag &diag() const;
private:
// Contains all joined Twitch channel-ids
std::unordered_set<QString> joinedChannels_;
std::unique_ptr<BttvLiveUpdatesPrivate> private_;
};
} // namespace chatterino
@@ -0,0 +1,78 @@
#include "providers/bttv/liveupdates/BttvLiveUpdateClient.hpp"
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
namespace chatterino {
BttvLiveUpdateClient::BttvLiveUpdateClient(BttvLiveUpdates &manager)
: BasicPubSubClient<BttvLiveUpdateSubscription>(100)
, manager(manager)
{
}
void BttvLiveUpdateClient::onMessage(const QByteArray &msg)
{
QJsonDocument jsonDoc(QJsonDocument::fromJson(msg));
if (jsonDoc.isNull())
{
qCDebug(chatterinoBttv) << "Failed to parse live update JSON";
return;
}
auto json = jsonDoc.object();
auto eventType = json["name"].toString();
auto eventData = json["data"].toObject();
if (eventType == "emote_create")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid add message" << json;
return;
}
this->manager.signals_.emoteAdded.invoke(message);
}
else if (eventType == "emote_update")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid update message" << json;
return;
}
this->manager.signals_.emoteUpdated.invoke(message);
}
else if (eventType == "emote_delete")
{
auto message = BttvLiveUpdateEmoteRemoveMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid deletion message" << json;
return;
}
this->manager.signals_.emoteRemoved.invoke(message);
}
else if (eventType == "lookup_user")
{
// ignored
}
else
{
qCDebug(chatterinoBttv) << "Unhandled event:" << json;
}
}
} // namespace chatterino
@@ -0,0 +1,22 @@
#pragma once
#include "providers/bttv/liveupdates/BttvLiveUpdateSubscription.hpp"
#include "providers/liveupdates/BasicPubSubClient.hpp"
namespace chatterino {
class BttvLiveUpdates;
class BttvLiveUpdateClient
: public BasicPubSubClient<BttvLiveUpdateSubscription>
{
public:
BttvLiveUpdateClient(BttvLiveUpdates &manager);
void onMessage(const QByteArray &msg) /* override */;
private:
BttvLiveUpdates &manager;
};
} // namespace chatterino