hyperion.ng/libsrc/boblightserver/BoblightServer.cpp
LordGrey 05d24b99c4
Fix Memory leaks (#1678)
* 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

* Revert "Move main non Thread Objects to Smart Pointers"

This reverts commit 26102ca963982e2fbc4ffb8d4db6139f0128a3cc.

* Add missing deletes

* Revert MdnsBrowser chnage

* Revert MdnsBrowser change

* Fix memory leaks related standalone grabber

* Address CodeQL finding

* delete pointer OsxFrameGrabber

---------

Co-authored-by: Paulchen-Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2024-01-13 17:04:45 +01:00

131 lines
3.1 KiB
C++

// system includes
#include <stdexcept>
// project includes
#include <boblightserver/BoblightServer.h>
#include "BoblightClientConnection.h"
// hyperion includes
#include <hyperion/Hyperion.h>
// qt incl
#include <QTcpServer>
// netUtil
#include <utils/NetUtils.h>
using namespace hyperion;
BoblightServer::BoblightServer(Hyperion* hyperion,const QJsonDocument& config)
: QObject()
, _hyperion(hyperion)
, _server(new QTcpServer(this))
, _openConnections()
, _priority(0)
, _log(nullptr)
, _port(0)
{
QString subComponent = _hyperion->property("instance").toString();
_log= Logger::getInstance("BOBLIGHT", subComponent);
Debug(_log, "Instance created");
// listen for component change
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &BoblightServer::compStateChangeRequest);
// listen new connection signal from server
connect(_server, &QTcpServer::newConnection, this, &BoblightServer::newConnection);
// init
handleSettingsUpdate(settings::BOBLSERVER, config);
}
BoblightServer::~BoblightServer()
{
stop();
}
void BoblightServer::start()
{
if ( _server->isListening() )
return;
if (NetUtils::portAvailable(_port, _log))
_server->listen(QHostAddress::Any, _port);
Info(_log, "Started on port: %d", _port);
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
}
void BoblightServer::stop()
{
if ( ! _server->isListening() )
return;
qDeleteAll(_openConnections);
_openConnections.clear();
_server->close();
Info(_log, "Stopped");
_hyperion->setNewComponentState(COMP_BOBLIGHTSERVER, _server->isListening());
}
bool BoblightServer::active() const
{
return _server->isListening();
}
void BoblightServer::compStateChangeRequest(hyperion::Components component, bool enable)
{
if (component == COMP_BOBLIGHTSERVER)
{
if (_server->isListening() != enable)
{
if (enable) start();
else stop();
}
}
}
uint16_t BoblightServer::getPort() const
{
return _server->serverPort();
}
void BoblightServer::newConnection()
{
QTcpSocket * socket = _server->nextPendingConnection();
if (socket != nullptr)
{
Info(_log, "New connection from %s ", QSTRING_CSTR(QString("Boblight@%1").arg(socket->peerAddress().toString())));
BoblightClientConnection * connection = new BoblightClientConnection(_hyperion, socket, _priority);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, &BoblightClientConnection::connectionClosed, this, &BoblightServer::closedConnection);
}
}
void BoblightServer::closedConnection(BoblightClientConnection *connection)
{
Debug(_log, "Connection closed for %s", QSTRING_CSTR(QString("Boblight@%1").arg(connection->getClientAddress())));
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}
void BoblightServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
{
if(type == settings::BOBLSERVER)
{
QJsonObject obj = config.object();
_port = obj["port"].toInt();
_priority = obj["priority"].toInt();
stop();
if(obj["enable"].toBool())
start();
}
}