server: refactored singleton with QSharedPointer

This commit is contained in:
Stéphane L 2019-01-01 04:55:15 +01:00
parent c245c24752
commit 7d1f0e2a69
6 changed files with 35 additions and 17 deletions

View File

@ -64,7 +64,7 @@ void* calldata_get_ptr(const calldata_t* data, const char* name) {
WSEvents* WSEvents::Instance = nullptr; WSEvents* WSEvents::Instance = nullptr;
WSEvents::WSEvents(WSServer* srv) { WSEvents::WSEvents(WSServerPtr srv) {
_srv = srv; _srv = srv;
obs_frontend_add_event_callback(WSEvents::FrontendEventHandler, this); obs_frontend_add_event_callback(WSEvents::FrontendEventHandler, this);

View File

@ -28,7 +28,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
class WSEvents : public QObject { class WSEvents : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit WSEvents(WSServer* srv); explicit WSEvents(WSServerPtr srv);
~WSEvents(); ~WSEvents();
static void FrontendEventHandler( static void FrontendEventHandler(
enum obs_frontend_event event, void* privateData); enum obs_frontend_event event, void* privateData);
@ -51,7 +51,7 @@ class WSEvents : public QObject {
void TransitionDurationChanged(int ms); void TransitionDurationChanged(int ms);
private: private:
WSServer* _srv; WSServerPtr _srv;
OBSSource currentScene; OBSSource currentScene;
bool pulse; bool pulse;

View File

@ -34,8 +34,6 @@ using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2; using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind; using websocketpp::lib::bind;
WSServer* WSServer::Instance = nullptr;
QString decodeBase64(const QString& source) QString decodeBase64(const QString& source)
{ {
return QString::fromUtf8( return QString::fromUtf8(
@ -45,8 +43,23 @@ QString decodeBase64(const QString& source)
); );
} }
WSServer::WSServer(QObject* parent) WSServerPtr WSServer::_instance = WSServerPtr(nullptr);
: QObject(parent),
WSServerPtr WSServer::Current()
{
if (!_instance) {
ResetCurrent();
}
return _instance;
}
void WSServer::ResetCurrent()
{
_instance = WSServerPtr(new WSServer());
}
WSServer::WSServer()
: QObject(nullptr),
_connections(), _connections(),
_clMutex(QMutex::Recursive) _clMutex(QMutex::Recursive)
{ {

View File

@ -21,6 +21,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QObject> #include <QObject>
#include <QMutex> #include <QMutex>
#include <QSharedPointer>
#include <QVariantHash> #include <QVariantHash>
#include <map> #include <map>
@ -38,19 +39,26 @@ using websocketpp::connection_hdl;
typedef websocketpp::server<websocketpp::config::asio> server; typedef websocketpp::server<websocketpp::config::asio> server;
class WSServer;
typedef QSharedPointer<WSServer> WSServerPtr;
class WSServer : public QObject class WSServer : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit WSServer(QObject* parent = Q_NULLPTR); static WSServerPtr Current();
static void ResetCurrent();
explicit WSServer();
virtual ~WSServer(); virtual ~WSServer();
void start(quint16 port); void start(quint16 port);
void stop(); void stop();
void broadcast(QString message); void broadcast(QString message);
static WSServer* Instance;
private: private:
static WSServerPtr _instance;
void onOpen(connection_hdl hdl); void onOpen(connection_hdl hdl);
void onMessage(connection_hdl hdl, server::message_ptr message); void onMessage(connection_hdl hdl, server::message_ptr message);
void onClose(connection_hdl hdl); void onClose(connection_hdl hdl);

View File

@ -94,9 +94,9 @@ void SettingsDialog::FormAccepted() {
conf->Save(); conf->Save();
if (conf->ServerEnabled) if (conf->ServerEnabled)
WSServer::Instance->start(conf->ServerPort); WSServer::Current()->start(conf->ServerPort);
else else
WSServer::Instance->stop(); WSServer::Current()->stop();
} }
SettingsDialog::~SettingsDialog() { SettingsDialog::~SettingsDialog() {

View File

@ -48,11 +48,10 @@ bool obs_module_load(void) {
Config* config = Config::Current(); Config* config = Config::Current();
config->Load(); config->Load();
WSServer::Instance = new WSServer(); WSEvents::Instance = new WSEvents(WSServer::Current());
WSEvents::Instance = new WSEvents(WSServer::Instance);
if (config->ServerEnabled) { if (config->ServerEnabled) {
WSServer::Instance->start(config->ServerPort); WSServer::Current()->start(config->ServerPort);
} }
// UI setup // UI setup
@ -77,9 +76,7 @@ bool obs_module_load(void) {
} }
void obs_module_unload() { void obs_module_unload() {
if (WSServer::Instance) { WSServer::Current()->stop();
WSServer::Instance->stop();
}
blog(LOG_INFO, "goodbye!"); blog(LOG_INFO, "goodbye!");
} }