From 7d1f0e2a69c50ea76f66c03b640f927cd1ae33e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20L?= Date: Tue, 1 Jan 2019 04:55:15 +0100 Subject: [PATCH] server: refactored singleton with QSharedPointer --- src/WSEvents.cpp | 2 +- src/WSEvents.h | 4 ++-- src/WSServer.cpp | 21 +++++++++++++++++---- src/WSServer.h | 12 ++++++++++-- src/forms/settings-dialog.cpp | 4 ++-- src/obs-websocket.cpp | 9 +++------ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/WSEvents.cpp b/src/WSEvents.cpp index 7bf30dc7..4fbd1d73 100644 --- a/src/WSEvents.cpp +++ b/src/WSEvents.cpp @@ -64,7 +64,7 @@ void* calldata_get_ptr(const calldata_t* data, const char* name) { WSEvents* WSEvents::Instance = nullptr; -WSEvents::WSEvents(WSServer* srv) { +WSEvents::WSEvents(WSServerPtr srv) { _srv = srv; obs_frontend_add_event_callback(WSEvents::FrontendEventHandler, this); diff --git a/src/WSEvents.h b/src/WSEvents.h index 7b7cf95e..086d8a4f 100644 --- a/src/WSEvents.h +++ b/src/WSEvents.h @@ -28,7 +28,7 @@ with this program. If not, see class WSEvents : public QObject { Q_OBJECT public: - explicit WSEvents(WSServer* srv); + explicit WSEvents(WSServerPtr srv); ~WSEvents(); static void FrontendEventHandler( enum obs_frontend_event event, void* privateData); @@ -51,7 +51,7 @@ class WSEvents : public QObject { void TransitionDurationChanged(int ms); private: - WSServer* _srv; + WSServerPtr _srv; OBSSource currentScene; bool pulse; diff --git a/src/WSServer.cpp b/src/WSServer.cpp index a7098d64..0c1fe7e7 100644 --- a/src/WSServer.cpp +++ b/src/WSServer.cpp @@ -34,8 +34,6 @@ using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind; -WSServer* WSServer::Instance = nullptr; - QString decodeBase64(const QString& source) { return QString::fromUtf8( @@ -45,8 +43,23 @@ QString decodeBase64(const QString& source) ); } -WSServer::WSServer(QObject* parent) - : QObject(parent), +WSServerPtr WSServer::_instance = WSServerPtr(nullptr); + +WSServerPtr WSServer::Current() +{ + if (!_instance) { + ResetCurrent(); + } + return _instance; +} + +void WSServer::ResetCurrent() +{ + _instance = WSServerPtr(new WSServer()); +} + +WSServer::WSServer() + : QObject(nullptr), _connections(), _clMutex(QMutex::Recursive) { diff --git a/src/WSServer.h b/src/WSServer.h index 8c243700..6cfd120b 100644 --- a/src/WSServer.h +++ b/src/WSServer.h @@ -21,6 +21,7 @@ with this program. If not, see #include #include +#include #include #include @@ -38,19 +39,26 @@ using websocketpp::connection_hdl; typedef websocketpp::server server; +class WSServer; +typedef QSharedPointer WSServerPtr; + class WSServer : public QObject { Q_OBJECT public: - explicit WSServer(QObject* parent = Q_NULLPTR); + static WSServerPtr Current(); + static void ResetCurrent(); + + explicit WSServer(); virtual ~WSServer(); void start(quint16 port); void stop(); void broadcast(QString message); - static WSServer* Instance; private: + static WSServerPtr _instance; + void onOpen(connection_hdl hdl); void onMessage(connection_hdl hdl, server::message_ptr message); void onClose(connection_hdl hdl); diff --git a/src/forms/settings-dialog.cpp b/src/forms/settings-dialog.cpp index 5378faaf..b0c78116 100644 --- a/src/forms/settings-dialog.cpp +++ b/src/forms/settings-dialog.cpp @@ -94,9 +94,9 @@ void SettingsDialog::FormAccepted() { conf->Save(); if (conf->ServerEnabled) - WSServer::Instance->start(conf->ServerPort); + WSServer::Current()->start(conf->ServerPort); else - WSServer::Instance->stop(); + WSServer::Current()->stop(); } SettingsDialog::~SettingsDialog() { diff --git a/src/obs-websocket.cpp b/src/obs-websocket.cpp index 7923564b..69bfc2c7 100644 --- a/src/obs-websocket.cpp +++ b/src/obs-websocket.cpp @@ -48,11 +48,10 @@ bool obs_module_load(void) { Config* config = Config::Current(); config->Load(); - WSServer::Instance = new WSServer(); - WSEvents::Instance = new WSEvents(WSServer::Instance); + WSEvents::Instance = new WSEvents(WSServer::Current()); if (config->ServerEnabled) { - WSServer::Instance->start(config->ServerPort); + WSServer::Current()->start(config->ServerPort); } // UI setup @@ -77,9 +76,7 @@ bool obs_module_load(void) { } void obs_module_unload() { - if (WSServer::Instance) { - WSServer::Instance->stop(); - } + WSServer::Current()->stop(); blog(LOG_INFO, "goodbye!"); }