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(WSServer* srv) {
WSEvents::WSEvents(WSServerPtr srv) {
_srv = srv;
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 {
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;

View File

@ -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)
{

View File

@ -21,6 +21,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <QObject>
#include <QMutex>
#include <QSharedPointer>
#include <QVariantHash>
#include <map>
@ -38,19 +39,26 @@ using websocketpp::connection_hdl;
typedef websocketpp::server<websocketpp::config::asio> server;
class WSServer;
typedef QSharedPointer<WSServer> 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);

View File

@ -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() {

View File

@ -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!");
}