diff --git a/src/Config.cpp b/src/Config.cpp index 9ff7a653..4ed30608 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -38,13 +38,6 @@ with this program. If not, see #define QT_TO_UTF8(str) str.toUtf8().constData() -ConfigPtr Config::_instance = ConfigPtr(new Config()); - -ConfigPtr Config::Current() -{ - return _instance; -} - Config::Config() : ServerEnabled(true), ServerPort(4444), @@ -83,7 +76,7 @@ void Config::Load() Salt = config_get_string(obsConfig, SECTION_NAME, PARAM_SALT); } -void Config::Save() +void Config::Save() { config_t* obsConfig = GetConfigStore(); @@ -217,7 +210,7 @@ void Config::OnFrontendEvent(enum obs_frontend_event event, void* param) config->Load(); if (config->ServerEnabled != previousEnabled || config->ServerPort != previousPort) { - auto server = WSServer::Current(); + auto server = GetServer(); server->stop(); if (config->ServerEnabled) { @@ -232,18 +225,18 @@ void Config::OnFrontendEvent(enum obs_frontend_event event, void* param) Utils::SysTrayNotify(stopMessage, QSystemTrayIcon::MessageIcon::Information); } } - } + } } void Config::MigrateFromGlobalSettings() -{ +{ config_t* source = obs_frontend_get_global_config(); config_t* destination = obs_frontend_get_profile_config(); if(config_has_user_value(source, SECTION_NAME, PARAM_ENABLE)) { bool value = config_get_bool(source, SECTION_NAME, PARAM_ENABLE); config_set_bool(destination, SECTION_NAME, PARAM_ENABLE, value); - + config_remove_value(source, SECTION_NAME, PARAM_ENABLE); } @@ -257,7 +250,7 @@ void Config::MigrateFromGlobalSettings() if(config_has_user_value(source, SECTION_NAME, PARAM_DEBUG)) { bool value = config_get_bool(source, SECTION_NAME, PARAM_DEBUG); config_set_bool(destination, SECTION_NAME, PARAM_DEBUG, value); - + config_remove_value(source, SECTION_NAME, PARAM_DEBUG); } diff --git a/src/Config.h b/src/Config.h index 73f04521..0845f874 100644 --- a/src/Config.h +++ b/src/Config.h @@ -23,20 +23,15 @@ with this program. If not, see #include #include -class Config; -typedef QSharedPointer ConfigPtr; - class Config { public: - static ConfigPtr Current(); - Config(); ~Config(); void Load(); void Save(); void SetDefaults(); config_t* GetConfigStore(); - + void MigrateFromGlobalSettings(); void SetPassword(QString password); @@ -59,5 +54,4 @@ class Config { private: static void OnFrontendEvent(enum obs_frontend_event event, void* param); - static ConfigPtr _instance; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index 6c7c2c63..0b9d7ac9 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -396,7 +396,7 @@ QSystemTrayIcon* Utils::GetTrayIcon() { void Utils::SysTrayNotify(QString text, QSystemTrayIcon::MessageIcon icon, QString title) { - if (!Config::Current()->AlertsEnabled || + if (!GetConfig()->AlertsEnabled || !QSystemTrayIcon::isSystemTrayAvailable() || !QSystemTrayIcon::supportsMessages()) { diff --git a/src/WSEvents.cpp b/src/WSEvents.cpp index f2f829f0..15a15326 100644 --- a/src/WSEvents.cpp +++ b/src/WSEvents.cpp @@ -248,7 +248,7 @@ void WSEvents::broadcastUpdate(const char* updateType, QString json = obs_data_get_json(update); _srv->broadcast(json.toStdString()); - if (Config::Current()->DebugEnabled) { + if (GetConfig()->DebugEnabled) { blog(LOG_INFO, "Update << '%s'", json.toUtf8().constData()); } } diff --git a/src/WSRequestHandler.cpp b/src/WSRequestHandler.cpp index 22ca5b85..379f4204 100644 --- a/src/WSRequestHandler.cpp +++ b/src/WSRequestHandler.cpp @@ -142,14 +142,14 @@ WSRequestHandler::WSRequestHandler(ConnectionProperties& connProperties) : } std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) { - if (Config::Current()->DebugEnabled) { + if (GetConfig()->DebugEnabled) { blog(LOG_INFO, "Request >> '%s'", textMessage.c_str()); } OBSDataAutoRelease responseData = processRequest(textMessage); std::string response = obs_data_get_json(responseData); - if (Config::Current()->DebugEnabled) { + if (GetConfig()->DebugEnabled) { blog(LOG_INFO, "Response << '%s'", response.c_str()); } @@ -173,7 +173,7 @@ HandlerResponse WSRequestHandler::processRequest(std::string& textMessage){ _requestType = obs_data_get_string(data, "request-type"); _messageId = obs_data_get_string(data, "message-id"); - if (Config::Current()->AuthRequired + if (GetConfig()->AuthRequired && (!authNotRequired.contains(_requestType)) && (!_connProperties.isAuthenticated())) { diff --git a/src/WSRequestHandler_General.cpp b/src/WSRequestHandler_General.cpp index 4c4409f0..a53e0e36 100644 --- a/src/WSRequestHandler_General.cpp +++ b/src/WSRequestHandler_General.cpp @@ -53,16 +53,17 @@ HandlerResponse WSRequestHandler::HandleGetVersion(WSRequestHandler* req) { * @since 0.3 */ HandlerResponse WSRequestHandler::HandleGetAuthRequired(WSRequestHandler* req) { - bool authRequired = Config::Current()->AuthRequired; + bool authRequired = GetConfig()->AuthRequired; OBSDataAutoRelease data = obs_data_create(); obs_data_set_bool(data, "authRequired", authRequired); if (authRequired) { + auto config = GetConfig(); obs_data_set_string(data, "challenge", - Config::Current()->SessionChallenge.toUtf8()); + config->SessionChallenge.toUtf8()); obs_data_set_string(data, "salt", - Config::Current()->Salt.toUtf8()); + config->Salt.toUtf8()); } return req->SendOKResponse(data); @@ -92,7 +93,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) { return req->SendErrorResponse("auth not specified!"); } - if (Config::Current()->CheckAuth(auth) == false) { + if (GetConfig()->CheckAuth(auth) == false) { return req->SendErrorResponse("Authentication Failed."); } diff --git a/src/WSServer.cpp b/src/WSServer.cpp index 6ca69393..45f3516e 100644 --- a/src/WSServer.cpp +++ b/src/WSServer.cpp @@ -38,21 +38,6 @@ using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind; -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(), @@ -127,7 +112,7 @@ void WSServer::stop() } _connections.clear(); _connectionProperties.clear(); - + _threadPool.waitForDone(); while (!_server.stopped()) { @@ -141,7 +126,7 @@ void WSServer::broadcast(std::string message) { QMutexLocker locker(&_clMutex); for (connection_hdl hdl : _connections) { - if (Config::Current()->AuthRequired) { + if (GetConfig()->AuthRequired) { bool authenticated = _connectionProperties[hdl].isAuthenticated(); if (!authenticated) { continue; diff --git a/src/WSServer.h b/src/WSServer.h index c65d16bd..e72e5fb2 100644 --- a/src/WSServer.h +++ b/src/WSServer.h @@ -40,17 +40,11 @@ using websocketpp::connection_hdl; typedef websocketpp::server server; -class WSServer; -typedef QSharedPointer WSServerPtr; - class WSServer : public QObject { Q_OBJECT public: - static WSServerPtr Current(); - static void ResetCurrent(); - explicit WSServer(); virtual ~WSServer(); void start(quint16 port); @@ -61,8 +55,6 @@ public: } 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 5778aa4e..6aa2d4f1 100644 --- a/src/forms/settings-dialog.cpp +++ b/src/forms/settings-dialog.cpp @@ -41,7 +41,7 @@ SettingsDialog::SettingsDialog(QWidget* parent) : } void SettingsDialog::showEvent(QShowEvent* event) { - auto conf = Config::Current(); + auto conf = GetConfig(); ui->serverEnabled->setChecked(conf->ServerEnabled); ui->serverPort->setValue(conf->ServerPort); @@ -68,7 +68,7 @@ void SettingsDialog::AuthCheckboxChanged() { } void SettingsDialog::FormAccepted() { - auto conf = Config::Current(); + auto conf = GetConfig(); conf->ServerEnabled = ui->serverEnabled->isChecked(); conf->ServerPort = ui->serverPort->value(); @@ -81,7 +81,7 @@ void SettingsDialog::FormAccepted() { conf->SetPassword(ui->password->text()); } - if (!Config::Current()->Secret.isEmpty()) + if (!GetConfig()->Secret.isEmpty()) conf->AuthRequired = true; else conf->AuthRequired = false; @@ -93,10 +93,12 @@ void SettingsDialog::FormAccepted() { conf->Save(); - if (conf->ServerEnabled) - WSServer::Current()->start(conf->ServerPort); - else - WSServer::Current()->stop(); + auto server = GetServer(); + if (conf->ServerEnabled) { + server->start(conf->ServerPort); + } else { + server->stop(); + } } SettingsDialog::~SettingsDialog() { diff --git a/src/obs-websocket.cpp b/src/obs-websocket.cpp index 852345bc..e2eefd7b 100644 --- a/src/obs-websocket.cpp +++ b/src/obs-websocket.cpp @@ -38,6 +38,8 @@ void ___output_dummy_addref(obs_output_t*) {} OBS_DECLARE_MODULE() OBS_MODULE_USE_DEFAULT_LOCALE("obs-websocket", "en-US") +ConfigPtr _config; +WSServerPtr _server; WSEventsPtr _eventsSystem; bool obs_module_load(void) { @@ -46,14 +48,15 @@ bool obs_module_load(void) { QT_VERSION_STR, qVersion()); // Core setup - auto config = Config::Current(); - config->MigrateFromGlobalSettings(); // TODO remove this on the next minor jump - config->Load(); + _config = ConfigPtr(new Config()); + _config->MigrateFromGlobalSettings(); // TODO remove this on the next minor jump + _config->Load(); - _eventsSystem = WSEventsPtr(new WSEvents(WSServer::Current())); + _server = WSServerPtr(new WSServer()); + _eventsSystem = WSEventsPtr(new WSEvents(_server)); - if (config->ServerEnabled) { - WSServer::Current()->start(config->ServerPort); + if (_config->ServerEnabled) { + _server->start(_config->ServerPort); } // UI setup @@ -79,10 +82,18 @@ bool obs_module_load(void) { } void obs_module_unload() { - WSServer::Current()->stop(); + _server->stop(); blog(LOG_INFO, "goodbye!"); } +ConfigPtr GetConfig() { + return _config; +} + +WSServerPtr GetServer() { + return _server; +} + WSEventsPtr GetEventsSystem() { return _eventsSystem; } diff --git a/src/obs-websocket.h b/src/obs-websocket.h index 692426f6..051604a9 100644 --- a/src/obs-websocket.h +++ b/src/obs-websocket.h @@ -38,10 +38,18 @@ using OBSDataArrayAutoRelease = using OBSOutputAutoRelease = OBSRef; +class Config; +typedef std::shared_ptr ConfigPtr; + +class WSServer; +typedef std::shared_ptr WSServerPtr; + class WSEvents; typedef std::shared_ptr WSEventsPtr; -std::shared_ptr GetEventsSystem(); +ConfigPtr GetConfig(); +WSServerPtr GetServer(); +WSEventsPtr GetEventsSystem(); #define OBS_WEBSOCKET_VERSION "4.6.0"