mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Protect clients object list with a QMutex
This commit is contained in:
parent
3bd600ed52
commit
748b6f6e2e
17
WSServer.cpp
17
WSServer.cpp
@ -30,7 +30,8 @@ QT_USE_NAMESPACE
|
||||
WSServer::WSServer(quint16 port, QObject *parent) :
|
||||
QObject(parent),
|
||||
_wsServer(Q_NULLPTR),
|
||||
_clients()
|
||||
_clients(),
|
||||
_clMutex(QMutex::NonRecursive)
|
||||
{
|
||||
_serverThread = new QThread();
|
||||
_wsServer = new QWebSocketServer(
|
||||
@ -49,11 +50,16 @@ WSServer::WSServer(quint16 port, QObject *parent) :
|
||||
WSServer::~WSServer()
|
||||
{
|
||||
_wsServer->close();
|
||||
|
||||
_clMutex.lock();
|
||||
qDeleteAll(_clients.begin(), _clients.end());
|
||||
_clMutex.unlock();
|
||||
}
|
||||
|
||||
void WSServer::broadcast(QString message)
|
||||
{
|
||||
_clMutex.lock();
|
||||
|
||||
Q_FOREACH(WSRequestHandler *pClient, _clients) {
|
||||
if (Config::Current()->AuthRequired == true
|
||||
&& pClient->isAuthenticated() == false) {
|
||||
@ -63,6 +69,8 @@ void WSServer::broadcast(QString message)
|
||||
|
||||
pClient->sendTextMessage(message);
|
||||
}
|
||||
|
||||
_clMutex.unlock();
|
||||
}
|
||||
|
||||
void WSServer::onNewConnection()
|
||||
@ -71,9 +79,11 @@ void WSServer::onNewConnection()
|
||||
|
||||
if (pSocket) {
|
||||
WSRequestHandler *pHandler = new WSRequestHandler(pSocket);
|
||||
|
||||
connect(pHandler, &WSRequestHandler::disconnected, this, &WSServer::socketDisconnected);
|
||||
|
||||
_clMutex.lock();
|
||||
_clients << pHandler;
|
||||
_clMutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +92,10 @@ void WSServer::socketDisconnected()
|
||||
WSRequestHandler *pClient = qobject_cast<WSRequestHandler *>(sender());
|
||||
|
||||
if (pClient) {
|
||||
_clMutex.lock();
|
||||
_clients.removeAll(pClient);
|
||||
_clMutex.unlock();
|
||||
|
||||
pClient->deleteLater();
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QList>
|
||||
#include <QtCore/QMutex>
|
||||
#include <QtCore/QByteArray>
|
||||
#include "WSRequestHandler.h"
|
||||
|
||||
@ -43,6 +44,7 @@ class WSServer : public QObject
|
||||
private:
|
||||
QWebSocketServer *_wsServer;
|
||||
QList<WSRequestHandler *> _clients;
|
||||
QMutex _clMutex;
|
||||
QThread *_serverThread;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user