server: connection properties reference

This commit is contained in:
Stéphane Lepin 2019-05-02 12:51:07 +02:00
parent 0cf17cf3db
commit 56fc6ae47c
5 changed files with 16 additions and 13 deletions

View File

@ -132,7 +132,7 @@ QSet<QString> WSRequestHandler::authNotRequired {
"Authenticate"
};
WSRequestHandler::WSRequestHandler(QSharedPointer<QVariantHash> connProperties) :
WSRequestHandler::WSRequestHandler(QVariantHash& connProperties) :
_messageId(0),
_requestType(""),
data(nullptr),
@ -174,7 +174,7 @@ HandlerResponse WSRequestHandler::processRequest(std::string& textMessage){
if (Config::Current()->AuthRequired
&& (!authNotRequired.contains(_requestType))
&& (_connProperties->value(PROP_AUTHENTICATED).toBool() == false))
&& (_connProperties.value(PROP_AUTHENTICATED).toBool() == false))
{
return SendErrorResponse("Not Authenticated");
}

View File

@ -36,7 +36,7 @@ class WSRequestHandler : public QObject {
Q_OBJECT
public:
explicit WSRequestHandler(QSharedPointer<QVariantHash> connProperties);
explicit WSRequestHandler(QVariantHash& connProperties);
~WSRequestHandler();
std::string processIncomingMessage(std::string& textMessage);
bool hasField(QString name);
@ -44,7 +44,7 @@ class WSRequestHandler : public QObject {
private:
const char* _messageId;
const char* _requestType;
QSharedPointer<QVariantHash> _connProperties;
QVariantHash& _connProperties;
OBSDataAutoRelease data;
HandlerResponse processRequest(std::string& textMessage);

View File

@ -82,7 +82,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return req->SendErrorResponse("missing request parameters");
}
if (req->_connProperties->value(PROP_AUTHENTICATED).toBool() == true) {
if (req->_connProperties.value(PROP_AUTHENTICATED).toBool() == true) {
return req->SendErrorResponse("already authenticated");
}
@ -95,7 +95,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return req->SendErrorResponse("Authentication Failed.");
}
req->_connProperties->insert(PROP_AUTHENTICATED, true);
req->_connProperties.insert(PROP_AUTHENTICATED, true);
return req->SendOKResponse();
}

View File

@ -142,7 +142,7 @@ void WSServer::broadcast(std::string message)
QMutexLocker locker(&_clMutex);
for (connection_hdl hdl : _connections) {
if (Config::Current()->AuthRequired) {
bool authenticated = _connectionProperties[hdl]->value(PROP_AUTHENTICATED).toBool();
bool authenticated = _connectionProperties[hdl].value(PROP_AUTHENTICATED).toBool();
if (!authenticated) {
continue;
}
@ -155,7 +155,7 @@ void WSServer::onOpen(connection_hdl hdl)
{
QMutexLocker locker(&_clMutex);
_connections.insert(hdl);
_connectionProperties[hdl] = QSharedPointer<QVariantHash>(new QVariantHash());
_connectionProperties[hdl] = QVariantHash();
locker.unlock();
QString clientIp = getRemoteEndpoint(hdl);
@ -174,13 +174,16 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
std::string payload = message->get_payload();
QMutexLocker locker(&_clMutex);
auto connProperties = _connectionProperties[hdl];
QVariantHash& connProperties = _connectionProperties[hdl];
locker.unlock();
WSRequestHandler handler(connProperties);
std::string response = handler.processIncomingMessage(payload);
_server.send(hdl, response, websocketpp::frame::opcode::text);
boost::atomic<QVariantHash> atomicTest;
atomicTest.load().insert("jambon", true);
});
}

View File

@ -18,14 +18,14 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#pragma once
#include <map>
#include <set>
#include <QtCore/QObject>
#include <QtCore/QMutex>
#include <QtCore/QSharedPointer>
#include <QtCore/QVariantHash>
#include <QtCore/QThreadPool>
#include <map>
#include <set>
#include <boost/atomic.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
@ -73,7 +73,7 @@ private:
server _server;
quint16 _serverPort;
std::set<connection_hdl, std::owner_less<connection_hdl>> _connections;
std::map<connection_hdl, QSharedPointer<QVariantHash>, std::owner_less<connection_hdl>> _connectionProperties;
std::map<connection_hdl, QVariantHash, std::owner_less<connection_hdl>> _connectionProperties;
QMutex _clMutex;
QThreadPool _threadPool;
};