mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Config + WSServer: provide singletons from plugin entry point
This commit is contained in:
parent
3ba8a77d9a
commit
b4d89d5666
@ -38,13 +38,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
@ -23,20 +23,15 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QSharedPointer>
|
||||
|
||||
class Config;
|
||||
typedef QSharedPointer<Config> 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;
|
||||
};
|
||||
|
@ -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())
|
||||
{
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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()))
|
||||
{
|
||||
|
@ -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.");
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -40,17 +40,11 @@ using websocketpp::connection_hdl;
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> server;
|
||||
|
||||
class WSServer;
|
||||
typedef QSharedPointer<WSServer> 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);
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -38,10 +38,18 @@ using OBSDataArrayAutoRelease =
|
||||
using OBSOutputAutoRelease =
|
||||
OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>;
|
||||
|
||||
class Config;
|
||||
typedef std::shared_ptr<Config> ConfigPtr;
|
||||
|
||||
class WSServer;
|
||||
typedef std::shared_ptr<WSServer> WSServerPtr;
|
||||
|
||||
class WSEvents;
|
||||
typedef std::shared_ptr<WSEvents> WSEventsPtr;
|
||||
|
||||
std::shared_ptr<WSEvents> GetEventsSystem();
|
||||
ConfigPtr GetConfig();
|
||||
WSServerPtr GetServer();
|
||||
WSEventsPtr GetEventsSystem();
|
||||
|
||||
#define OBS_WEBSOCKET_VERSION "4.6.0"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user