hyperion.ng/libsrc/jsonserver/JsonServer.cpp
LordGrey 31df065c0f
Start SmartPointers (#1679)
* Refactor to fix #1671

* Add GUI/NonGUI mode to info page

* Do not show lock config, if in non-UI mode

* Updae Changelog

* Correct includes

* Ensure key member initialization - RGB Channels

* Ensure key member initialization - WebServer

* Update RGBChannels

* Fix initialization order

* Fix key when inserting new logger in LoggerMap,
Prepare logBuffer-JSON snapshot view in LoggerManager,
Increase buffered loglines to 500

* Fix Memory leak in GrabberWrapper

* Fix Memory leak in BlackBorderProcessor

* Fix Memory leak in BlackBorderProcessor

* use ninja generator under macos

* Fix BGEffectHandler destruction

* Fix Mdns code

* Clear list after applying qDeleteAll

* Fix deletion of CecHandler

* Fix memory leak caused by wrong buffer allocation

* Remove extra pixel consistently

* Change mDNS to Qt SmartPointers

* Correct removal

* Fix usage of _width/_height (they are the output resolution, not the screen resolution)
That avoids unnecessary resizing of the output image with every transferFrame call

* Move main non Thread Objects to Smart Pointers

* Refactor Hyperion Daemon unsing smartpointers

* Correction

* Correct typos/ align text

* Fix startGrabberDispmanx

* Fix startGrabberDispmanx

* Address CodeQL finding

* Create Screen grabbers via Template

* Fix typo

* Change way of logging

* Revert change

* Address deprecation warning

* Correct auto screen grabber evaluation

---------

Co-authored-by: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2024-02-25 17:35:39 +01:00

127 lines
2.7 KiB
C++

// system includes
#include <stdexcept>
// qt includes
#include <QTcpServer>
#include <QTcpSocket>
#include <QJsonDocument>
#include <QByteArray>
// project includes
#include "HyperionConfig.h"
#include <jsonserver/JsonServer.h>
#include "JsonClientConnection.h"
#include <utils/NetOrigin.h>
// Constants
namespace {
const char SERVICE_TYPE[] = "jsonapi";
} //End of constants
JsonServer::JsonServer(const QJsonDocument& config)
: QObject()
, _server(new QTcpServer(this))
, _openConnections()
, _log(Logger::getInstance("JSONSERVER"))
, _netOrigin(NetOrigin::getInstance())
, _config(config)
{
Debug(_log, "JSON API server created");
}
JsonServer::~JsonServer()
{
stop();
qDeleteAll(_openConnections);
_openConnections.clear();
}
void JsonServer::initServer()
{
// Set trigger for incoming connections
connect(_server, &QTcpServer::newConnection, this, &JsonServer::newConnection);
// init
handleSettingsUpdate(settings::JSONSERVER, _config);
}
void JsonServer::start()
{
if(!_server->isListening())
{
if (!_server->listen(QHostAddress::Any, _port))
{
Error(_log,"Could not bind to port '%d', please use an available port", _port);
}
else
{
Info(_log, "Started on port %d", _port);
emit publishService(SERVICE_TYPE, _port);
}
}
}
void JsonServer::stop()
{
if(!_server->isListening())
{
return;
}
_server->close();
Info(_log, "JSON-Server stopped");
}
void JsonServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if(type == settings::JSONSERVER)
{
QJsonObject obj = config.object();
if(_port != obj["port"].toInt())
{
_port = obj["port"].toInt();
stop();
start();
}
}
}
uint16_t JsonServer::getPort() const
{
return _port;
}
void JsonServer::newConnection()
{
while(_server->hasPendingConnections())
{
if (QTcpSocket * socket = _server->nextPendingConnection())
{
if(_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress()))
{
Debug(_log, "New connection from: %s",QSTRING_CSTR(socket->peerAddress().toString()));
JsonClientConnection * connection = new JsonClientConnection(socket, _netOrigin->isLocalAddress(socket->peerAddress(), socket->localAddress()));
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, &JsonClientConnection::connectionClosed, this, &JsonServer::closedConnection);
}
else
socket->close();
}
}
}
void JsonServer::closedConnection()
{
JsonClientConnection* connection = qobject_cast<JsonClientConnection*>(sender());
Debug(_log, "Connection closed for %s", QSTRING_CSTR(connection->getClientAddress().toString()));
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}