From 2a0878dc82167f0116b2fed981727544dceb9360 Mon Sep 17 00:00:00 2001 From: Fidel Yin Date: Mon, 10 Apr 2023 19:11:46 +0800 Subject: [PATCH] format code style --- src/danmuku.cpp | 5 +- src/danmuku.h | 6 +- src/global.h | 6 +- src/module.cpp | 2 +- src/module.h | 6 +- src/modules/danmu_display/danmu_config.h | 6 +- src/modules/danmu_display/danmu_display.cpp | 12 ++-- src/modules/danmu_display/danmu_display.h | 6 +- src/modules/danmu_display/danmu_loader.cpp | 2 +- src/modules/danmu_display/danmu_loader.h | 8 +-- src/modules/danmu_display/danmu_panel.h | 6 +- src/modules/danmu_display/danmu_window.h | 6 +- src/modules/live_room/live_room.cpp | 10 ++- src/modules/live_room/live_room.h | 6 +- src/modules/live_room/protocal.cpp | 17 +++-- src/modules/live_room/protocal.h | 80 ++++++++++----------- src/utils/byte_convert.h | 6 +- src/utils/decompress.h | 6 +- src/utils/network.h | 6 +- 19 files changed, 103 insertions(+), 99 deletions(-) diff --git a/src/danmuku.cpp b/src/danmuku.cpp index ef362c0..b5c3c68 100644 --- a/src/danmuku.cpp +++ b/src/danmuku.cpp @@ -21,8 +21,9 @@ Danmuku::Danmuku(QWidget *parent) layout_modules = new QVBoxLayout(ui->centralWidget); // deleted by QT - loadModule(new LiveRoom(this)); // deleted in unloadModule - loadModule(new DanmuDisplay(this)); // deleted in unloadModule + // TODO: Dynamic modules loader + loadModule(new LiveRoom(this)); // deleted by Qt + loadModule(new DanmuDisplay(this)); // deleted by Qt // Qss QFile qss(":/stylesheet/danmuku.qss"); diff --git a/src/danmuku.h b/src/danmuku.h index 26307ee..6fcf6d3 100644 --- a/src/danmuku.h +++ b/src/danmuku.h @@ -1,5 +1,5 @@ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H +#ifndef DANMUKU_DANMUKU_H_ +#define DANMUKU_DANMUKU_H_ #include #include @@ -29,4 +29,4 @@ class Danmuku : public QMainWindow { std::unordered_map modules; }; -#endif \ No newline at end of file +#endif // DANMUKU_DANMUKU_H_ \ No newline at end of file diff --git a/src/global.h b/src/global.h index f492b25..c2b8bb1 100644 --- a/src/global.h +++ b/src/global.h @@ -1,9 +1,9 @@ -#ifndef GLOBAL_H -#define GLOBAL_H +#ifndef DANMUKU_GLOBAL_H_ +#define DANMUKU_GLOBAL_H_ #include #define APP_DIR_PATH QCoreApplication::applicationDirPath() #define CONFIG_DIR_PATH "/config" -#endif \ No newline at end of file +#endif // DANMUKU_GLOBAL_H_ \ No newline at end of file diff --git a/src/module.cpp b/src/module.cpp index 5b460e9..586c1f4 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -11,7 +11,7 @@ Module::Module(const QString &name, const QList &dependencies, moduleMetadata.name = name; moduleMetadata.dependencies = dependencies; - widget = new QWidget(parent); // deleted by QT + widget = new QWidget(parent); // deleted by Qt // TODO check dependencies } diff --git a/src/module.h b/src/module.h index 5d14024..98ce4f0 100644 --- a/src/module.h +++ b/src/module.h @@ -1,5 +1,5 @@ -#ifndef MODULE_H -#define MODULE_H +#ifndef DANMUKU_MODULE_H_ +#define DANMUKU_MODULE_H_ #include #include @@ -33,4 +33,4 @@ class Module : public QObject { #define MODULE -#endif \ No newline at end of file +#endif // DANMUKU_MODULE_H_ \ No newline at end of file diff --git a/src/modules/danmu_display/danmu_config.h b/src/modules/danmu_display/danmu_config.h index 80605f5..3c1d554 100644 --- a/src/modules/danmu_display/danmu_config.h +++ b/src/modules/danmu_display/danmu_config.h @@ -1,5 +1,5 @@ -#ifndef DANMU_CONFIG_H -#define DANMU_CONFIG_H +#ifndef DANMUKU_MODULES_DANMU_DISPLAY_DANMU_CONFIG_H_ +#define DANMUKU_MODULES_DANMU_DISPLAY_DANMU_CONFIG_H_ #include #include @@ -69,4 +69,4 @@ class DanmuConfig : public QObject { QSettings settings; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_DANMU_DISPLAY_DANMU_CONFIG_H_ \ No newline at end of file diff --git a/src/modules/danmu_display/danmu_display.cpp b/src/modules/danmu_display/danmu_display.cpp index 96efbec..57cf991 100644 --- a/src/modules/danmu_display/danmu_display.cpp +++ b/src/modules/danmu_display/danmu_display.cpp @@ -4,7 +4,8 @@ #include "ui_danmu_window.h" -DanmuDisplay::DanmuDisplay(Danmuku *parent) : Module("danmu_display", {}, parent) { +DanmuDisplay::DanmuDisplay(Danmuku *parent) + : Module("danmu_display", {}, parent) { config = nullptr; updateFollowersCountTimer = nullptr; @@ -58,9 +59,10 @@ void DanmuDisplay::startDisplay() { Q_ASSERT(window == nullptr); window = new DanmuWindow(); // deleted in stop() Q_ASSERT(danmuLoader == nullptr); - danmuLoader = new DanmuLoader(window->ui->list_danmu); // deleted in stop() + danmuLoader = + new DanmuLoader(window->ui->list_danmu, this); // deleted in stop() Q_ASSERT(config == nullptr); - config = new DanmuConfig(); // deleted in stop() + config = new DanmuConfig(this); // deleted in stop() connect(config, SIGNAL(changed()), this, SLOT(applyConfig())); @@ -254,8 +256,8 @@ void DanmuDisplay::applyConfig() { // giftLoader if (config->showGift) { // showGift == true if (giftLoader.isNull()) { - giftLoader = - new DanmuLoader(window->ui->list_gift); // deleted in stop() + giftLoader = new DanmuLoader(window->ui->list_gift, + this); // deleted in stop() giftLoader->start(); window->ui->list_gift->show(); } diff --git a/src/modules/danmu_display/danmu_display.h b/src/modules/danmu_display/danmu_display.h index 8c23479..02cb279 100644 --- a/src/modules/danmu_display/danmu_display.h +++ b/src/modules/danmu_display/danmu_display.h @@ -1,5 +1,5 @@ -#ifndef DANMU_DISPLAY_H -#define DANMU_DISPLAY_H +#ifndef DANMUKU_MODULES_DANMU_DISPLAY_DANMU_DISPLAY_H_ +#define DANMUKU_MODULES_DANMU_DISPLAY_DANMU_DISPLAY_H_ #include #include @@ -63,4 +63,4 @@ class DanmuDisplay : public Module { QString giftContentFormat; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_DANMU_DISPLAY_DANMU_DISPLAY_H_ \ No newline at end of file diff --git a/src/modules/danmu_display/danmu_loader.cpp b/src/modules/danmu_display/danmu_loader.cpp index 95e4bfd..c5b3257 100644 --- a/src/modules/danmu_display/danmu_loader.cpp +++ b/src/modules/danmu_display/danmu_loader.cpp @@ -1,6 +1,6 @@ #include "danmu_loader.h" -DanmuLoader::DanmuLoader(QListWidget *list) { +DanmuLoader::DanmuLoader(QListWidget *list, QObject *parent) : QThread(parent) { this->list = list; list->installEventFilter(this); scrollBar = list->verticalScrollBar(); diff --git a/src/modules/danmu_display/danmu_loader.h b/src/modules/danmu_display/danmu_loader.h index 445e7ef..a2d3d71 100644 --- a/src/modules/danmu_display/danmu_loader.h +++ b/src/modules/danmu_display/danmu_loader.h @@ -1,5 +1,5 @@ -#ifndef DANMU_LOADER_H -#define DANMU_LOADER_H +#ifndef DANMUKU_MODULES_DANMU_DISPLAY_DANMU_LOADER_H_ +#define DANMUKU_MODULES_DANMU_DISPLAY_DANMU_LOADER_H_ #include #include @@ -13,7 +13,7 @@ class DanmuLoader : public QThread { Q_OBJECT public: - DanmuLoader(QListWidget *list); + DanmuLoader(QListWidget *list, QObject *parent = nullptr); ~DanmuLoader(); void loadItem(QListWidgetItem *item); @@ -41,4 +41,4 @@ class DanmuLoader : public QThread { QWaitCondition updateCondition; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_DANMU_DISPLAY_DANMU_LOADER_H_ \ No newline at end of file diff --git a/src/modules/danmu_display/danmu_panel.h b/src/modules/danmu_display/danmu_panel.h index e9545a3..74d7b57 100644 --- a/src/modules/danmu_display/danmu_panel.h +++ b/src/modules/danmu_display/danmu_panel.h @@ -1,5 +1,5 @@ -#ifndef DANMU_PANEL_H -#define DANMU_PANEL_H +#ifndef DANMUKU_MODULES_DANMU_DISPLAY_DANMU_PANEL_H_ +#define DANMUKU_MODULES_DANMU_DISPLAY_DANMU_PANEL_H_ #include @@ -54,4 +54,4 @@ class DanmuPanel : public QWidget { bool showGift; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_DANMU_DISPLAY_DANMU_LOADER_H_ \ No newline at end of file diff --git a/src/modules/danmu_display/danmu_window.h b/src/modules/danmu_display/danmu_window.h index e9a1c09..60ac768 100644 --- a/src/modules/danmu_display/danmu_window.h +++ b/src/modules/danmu_display/danmu_window.h @@ -1,5 +1,5 @@ -#ifndef DANMU_WINDOW_H -#define DANMU_WINDOW_H +#ifndef DANMUKU_MODULES_DANMU_DISPLAY_DANMU_WINDOW_H_ +#define DANMUKU_MODULES_DANMU_DISPLAY_DANMU_WINDOW_H_ #include @@ -30,4 +30,4 @@ class DanmuWindow : public QWidget { QPoint mouseDragPosition; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_DANMU_DISPLAY_DANMU_WINDOW_H_ \ No newline at end of file diff --git a/src/modules/live_room/live_room.cpp b/src/modules/live_room/live_room.cpp index 44d1226..488e217 100644 --- a/src/modules/live_room/live_room.cpp +++ b/src/modules/live_room/live_room.cpp @@ -8,16 +8,16 @@ LiveRoom::LiveRoom(Danmuku *parent) : Module("live_room", {}, parent) { protocal = nullptr; // Control UI - QHBoxLayout *layout = new QHBoxLayout(widget); // deleted by QT + QHBoxLayout *layout = new QHBoxLayout(widget); // deleted by Qt - input_roomID = new QLineEdit(widget); // deleted by QT + input_roomID = new QLineEdit(widget); // deleted by Qt input_roomID->setPlaceholderText(tr("直播间号")); #ifdef QT_DEBUG input_roomID->setText("14003442"); // debug #endif layout->addWidget(input_roomID); - btn_start = new QPushButton(tr("连接"), widget); // deleted by QT + btn_start = new QPushButton(tr("连接"), widget); // deleted by Qt connect(btn_start, SIGNAL(clicked()), this, SLOT(start())); layout->addWidget(btn_start); } @@ -48,6 +48,7 @@ void LiveRoom::start() { Q_ASSERT(protocal == nullptr); protocal = new Protocal(); // deleted in stop() + // protocal cannot be assigned a parent if it is passed into moveToThread() protocal->moveToThread(&protocalThread); protocalThread.start(); @@ -71,9 +72,6 @@ void LiveRoom::stop() { (Qt::ConnectionType)Qt::BlockingQueuedConnection); protocalThread.quit(); protocalThread.wait(); - // protocal->deleteLater(); - delete protocal; - protocal = nullptr; qDebug("Exit stop"); } diff --git a/src/modules/live_room/live_room.h b/src/modules/live_room/live_room.h index d83cbd9..0f27352 100644 --- a/src/modules/live_room/live_room.h +++ b/src/modules/live_room/live_room.h @@ -1,5 +1,5 @@ -#ifndef LIVE_ROOM_H -#define LIVE_ROOM_H +#ifndef DANMUKU_MODULES_LIVE_ROOM_LIVE_ROOM_H_ +#define DANMUKU_MODULES_LIVE_ROOM_LIVE_ROOM_H_ #include #include @@ -43,4 +43,4 @@ class LiveRoom : public Module { QThread protocalThread; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_LIVE_ROOM_LIVE_ROOM_H_ \ No newline at end of file diff --git a/src/modules/live_room/protocal.cpp b/src/modules/live_room/protocal.cpp index 0d8195d..14ddf62 100644 --- a/src/modules/live_room/protocal.cpp +++ b/src/modules/live_room/protocal.cpp @@ -21,10 +21,12 @@ const QHash Protocal::cmdMap = QByteArray genHead(int datalength, int opeation, int sequence) { QByteArray buffer = QByteArray(); + buffer.append(int32ToBytes( + (uint32_t)(datalength + Protocal::WS_PACKAGE_HEADER_TOTAL_LENGTH))); buffer.append( - int32ToBytes((uint32_t)(datalength + WS_PACKAGE_HEADER_TOTAL_LENGTH))); - buffer.append(int16ToBytes((uint16_t)(WS_PACKAGE_HEADER_TOTAL_LENGTH))); - buffer.append(int16ToBytes((uint16_t)(WS_HEADER_DEFAULT_VERSION))); + int16ToBytes((uint16_t)(Protocal::WS_PACKAGE_HEADER_TOTAL_LENGTH))); + buffer.append( + int16ToBytes((uint16_t)(Protocal::WS_HEADER_DEFAULT_VERSION))); buffer.append(int32ToBytes((uint32_t)(opeation))); buffer.append(int32ToBytes((uint32_t)(sequence))); return buffer; @@ -32,8 +34,8 @@ QByteArray genHead(int datalength, int opeation, int sequence) { bool checkHello(QByteArray data) { QByteArray valData("{\"code\":0}"); - valData.prepend(genHead(valData.length(), WS_OP_CONNECT_SUCCESS, - WS_HEADER_DEFAULT_SEQUENCE)); + valData.prepend(genHead(valData.length(), Protocal::WS_OP_CONNECT_SUCCESS, + Protocal::WS_HEADER_DEFAULT_SEQUENCE)); return valData == data; } @@ -66,7 +68,8 @@ void Protocal::startConnection(const int& roomID, request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"); - ws = new QWebSocket(); // deleted in stopConnection() + ws = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, + this); // deleted in stopConnection() ws->open(request); QEventLoop eventLoop; @@ -84,7 +87,7 @@ void Protocal::startConnection(const int& roomID, heartBeatTimer->start(WS_HEARTBEAT_INTERVAL_MS); } -Protocal::Protocal() { +Protocal::Protocal(QObject* parent) : QObject(parent) { ws = nullptr; heartBeatTimer = nullptr; } diff --git a/src/modules/live_room/protocal.h b/src/modules/live_room/protocal.h index 902ce94..1438f20 100644 --- a/src/modules/live_room/protocal.h +++ b/src/modules/live_room/protocal.h @@ -1,53 +1,29 @@ -#ifndef DANMU_H -#define DANMU_H +#ifndef DANMUKU_MODULES_LIVE_ROOM_PROTOCAL_H_ +#define DANMUKU_MODULES_LIVE_ROOM_PROTOCAL_H_ #include #include -const int WS_OP_USER_AUTHENTICATION = 7; -const int WS_HEADER_DEFAULT_SEQUENCE = 1; -const int WS_PACKAGE_HEADER_TOTAL_LENGTH = 16; -const int WS_HEADER_DEFAULT_VERSION = 1; -const int WS_OP_CONNECT_SUCCESS = 8; -const int WS_OP_HEARTBEAT = 2; -// Header -const int WS_PACKAGE_OFFSET = 0; -const int WS_HEADER_OFFSET = 4; -const int WS_VERSION_OFFSET = 6; -const int WS_OPERATION_OFFSET = 8; -const int WS_SEQUENCE_OFFSET = 12; - -const int WS_HEARTBEAT_INTERVAL_MS = 30000; - QByteArray genHead(int datalength, int opeation, int sequence); class Protocal : public QObject { Q_OBJECT public: - Protocal(); - - private: - void recvHeartbeatReply(const QByteArray &msg); - void recvMsg(const QJsonObject &msg); - void sendHeartbeat(); - void recvData(const QByteArray &data); + static const int WS_OP_USER_AUTHENTICATION = 7; + static const int WS_HEADER_DEFAULT_SEQUENCE = 1; + static const int WS_PACKAGE_HEADER_TOTAL_LENGTH = 16; + static const int WS_HEADER_DEFAULT_VERSION = 1; + static const int WS_OP_CONNECT_SUCCESS = 8; + static const int WS_OP_HEARTBEAT = 2; + // Header + static const int WS_PACKAGE_OFFSET = 0; + static const int WS_HEADER_OFFSET = 4; + static const int WS_VERSION_OFFSET = 6; + static const int WS_OPERATION_OFFSET = 8; + static const int WS_SEQUENCE_OFFSET = 12; - signals: - void viewersCountUpdated(const int &viewersCount); - void recvDanmu(const int uid, const QString &username, const QString &text, - const bool isAdmin, const bool isVIP, - const int userGuardLevel); - void recvGift(const int uid, const QString &username, - const QString &giftName, const int giftCount); - - public slots: - void startConnection(const int &roomID, const QJsonObject &liveRoomInfo); - void stopConnection(); - - private: - QWebSocket *ws; - QTimer *heartBeatTimer; + static const int WS_HEARTBEAT_INTERVAL_MS = 30000; enum BODY_PROTOCAL_VERSION { DEFLATE = 2, BROTLI = 3 }; enum MSG_TYPE { HEARTBEAT_REPLY = 3, MESSAGE = 5, AUTH_REPLY = 8 }; @@ -70,7 +46,31 @@ class Protocal : public QObject { CUT_OFF }; + public: + Protocal(QObject *parent = nullptr); + + private: + void recvHeartbeatReply(const QByteArray &msg); + void recvMsg(const QJsonObject &msg); + void sendHeartbeat(); + void recvData(const QByteArray &data); + + signals: + void viewersCountUpdated(const int &viewersCount); + void recvDanmu(const int uid, const QString &username, const QString &text, + const bool isAdmin, const bool isVIP, + const int userGuardLevel); + void recvGift(const int uid, const QString &username, + const QString &giftName, const int giftCount); + + public slots: + void startConnection(const int &roomID, const QJsonObject &liveRoomInfo); + void stopConnection(); + + private: + QWebSocket *ws; + QTimer *heartBeatTimer; static const QHash cmdMap; }; -#endif \ No newline at end of file +#endif // DANMUKU_MODULES_LIVE_ROOM_PROTOCAL_H_ \ No newline at end of file diff --git a/src/utils/byte_convert.h b/src/utils/byte_convert.h index c08d1fe..49e01b2 100644 --- a/src/utils/byte_convert.h +++ b/src/utils/byte_convert.h @@ -1,5 +1,5 @@ -#ifndef BYTE_CONVERT_H -#define BYTE_CONVERT_H +#ifndef DANMUKU_UTILS_BYTE_CONVERT_H_ +#define DANMUKU_UTILS_BYTE_CONVERT_H_ #include #include @@ -9,4 +9,4 @@ QByteArray int32ToBytes(uint32_t data); uint16_t bytesToInt16(QByteArray bytes); uint32_t bytesToInt32(QByteArray bytes); -#endif \ No newline at end of file +#endif // DANMUKU_UTILS_BYTE_CONVERT_H_ \ No newline at end of file diff --git a/src/utils/decompress.h b/src/utils/decompress.h index e3f4b79..b4e2718 100644 --- a/src/utils/decompress.h +++ b/src/utils/decompress.h @@ -1,9 +1,9 @@ -#ifndef DECOMPRESS_H -#define DECOMPRESS_H +#ifndef DANMUKU_UTILS_DECOMPRESS_H_ +#define DANMUKU_UTILS_DECOMPRESS_H_ #include // bool decompressDeflate(const QByteArray &data, QByteArray &result); bool decompressBrotli(const QByteArray &data, QByteArray &result); -#endif +#endif // DANMUKU_UTILS_DECOMPRESS_H_ diff --git a/src/utils/network.h b/src/utils/network.h index e176111..b2f59f7 100644 --- a/src/utils/network.h +++ b/src/utils/network.h @@ -1,9 +1,9 @@ -#ifndef NETWORK_H -#define NETWORK_H +#ifndef DANMUKU_UTILS_NETWORK_H_ +#define DANMUKU_UTILS_NETWORK_H_ #include #include QJsonObject requestJsonResponse(QString url); -#endif \ No newline at end of file +#endif // DANMUKU_UTILS_NETWORK_H_ \ No newline at end of file