mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
BoblightServer added
Former-commit-id: deb3479ee673d763ad2e5451fcd35a0febedb4f3
This commit is contained in:
57
libsrc/boblightserver/BoblightServer.cpp
Normal file
57
libsrc/boblightserver/BoblightServer.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
// system includes
|
||||
#include <stdexcept>
|
||||
|
||||
// project includes
|
||||
#include <boblightserver/BoblightServer.h>
|
||||
#include "BoblightClientConnection.h"
|
||||
|
||||
BoblightServer::BoblightServer(Hyperion *hyperion, uint16_t port) :
|
||||
QObject(),
|
||||
_hyperion(hyperion),
|
||||
_server(),
|
||||
_openConnections()
|
||||
{
|
||||
if (!_server.listen(QHostAddress::Any, port))
|
||||
{
|
||||
throw std::runtime_error("Boblight server could not bind to port");
|
||||
}
|
||||
|
||||
// Set trigger for incoming connections
|
||||
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
||||
}
|
||||
|
||||
BoblightServer::~BoblightServer()
|
||||
{
|
||||
foreach (BoblightClientConnection * connection, _openConnections) {
|
||||
delete connection;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t BoblightServer::getPort() const
|
||||
{
|
||||
return _server.serverPort();
|
||||
}
|
||||
|
||||
void BoblightServer::newConnection()
|
||||
{
|
||||
QTcpSocket * socket = _server.nextPendingConnection();
|
||||
|
||||
if (socket != nullptr)
|
||||
{
|
||||
std::cout << "New boblight connection" << std::endl;
|
||||
BoblightClientConnection * connection = new BoblightClientConnection(socket, _hyperion);
|
||||
_openConnections.insert(connection);
|
||||
|
||||
// register slot for cleaning up after the connection closed
|
||||
connect(connection, SIGNAL(connectionClosed(BoblightClientConnection*)), this, SLOT(closedConnection(BoblightClientConnection*)));
|
||||
}
|
||||
}
|
||||
|
||||
void BoblightServer::closedConnection(BoblightClientConnection *connection)
|
||||
{
|
||||
std::cout << "Boblight connection closed" << std::endl;
|
||||
_openConnections.remove(connection);
|
||||
|
||||
// schedule to delete the connection object
|
||||
connection->deleteLater();
|
||||
}
|
Reference in New Issue
Block a user