mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Udplistener start/stop during runtime (#56)
* start implementening runtime start/stop for udplistener * add signal slots for start/stop/statusChanged for better connectivity with other qt stuff * fix typo
This commit is contained in:
@@ -18,38 +18,14 @@ UDPListener::UDPListener(const int priority, const int timeout, const std::strin
|
||||
_priority(priority),
|
||||
_timeout(timeout),
|
||||
_ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK),
|
||||
_log(Logger::getInstance("UDPLISTENER"))
|
||||
_log(Logger::getInstance("UDPLISTENER")),
|
||||
_isActive(false),
|
||||
_bondage(shared ? QAbstractSocket::ShareAddress : QAbstractSocket::DefaultForPlatform)
|
||||
{
|
||||
_server = new QUdpSocket(this);
|
||||
|
||||
QAbstractSocket::BindFlag bondage = QAbstractSocket::DefaultForPlatform;
|
||||
if (shared) {
|
||||
bondage = QAbstractSocket::ShareAddress;
|
||||
}
|
||||
QHostAddress listenAddress = QHostAddress(QHostAddress::Any);
|
||||
QHostAddress mcastGroup;
|
||||
|
||||
if (!address.empty()) {
|
||||
listenAddress = QHostAddress( QString::fromStdString(address) );
|
||||
}
|
||||
if (listenAddress.isInSubnet(QHostAddress::parseSubnet("224.0.0.0/4"))) {
|
||||
mcastGroup = listenAddress;
|
||||
listenAddress = QHostAddress(QHostAddress::AnyIPv4);
|
||||
}
|
||||
|
||||
if (!_server->bind(listenAddress, listenPort, bondage))
|
||||
{
|
||||
Warning(_log, "Could not bind to %s:%d parsed from %s", listenAddress.toString().toStdString().c_str(), listenPort, address.c_str());
|
||||
} else {
|
||||
Info(_log, "Started, listening on %s:%d", listenAddress.toString().toStdString().c_str(), listenPort);
|
||||
if (!mcastGroup.isNull()) {
|
||||
if (_server->joinMulticastGroup(mcastGroup)) {
|
||||
Info(_log, "Multicast listening on %s",mcastGroup.toString().toStdString().c_str());
|
||||
} else {
|
||||
Warning(_log, "Multicast failed for %s", mcastGroup.toString().toStdString().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
QHostAddress listenAddress = address.empty()
|
||||
? QHostAddress::AnyIPv4
|
||||
: QHostAddress( QString::fromStdString(address) );
|
||||
|
||||
// Set trigger for incoming connections
|
||||
connect(_server, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
||||
@@ -58,9 +34,50 @@ UDPListener::UDPListener(const int priority, const int timeout, const std::strin
|
||||
UDPListener::~UDPListener()
|
||||
{
|
||||
// clear the current channel
|
||||
stop();
|
||||
delete _server;
|
||||
_hyperion->clear(_priority);
|
||||
}
|
||||
|
||||
|
||||
void UDPListener::start()
|
||||
{
|
||||
if ( active() )
|
||||
return;
|
||||
|
||||
QHostAddress mcastGroup;
|
||||
if (_listenAddress.isInSubnet(QHostAddress::parseSubnet("224.0.0.0/4"))) {
|
||||
mcastGroup = _listenAddress;
|
||||
}
|
||||
|
||||
if (!_server->bind(_listenAddress, _listenPort, _bondage))
|
||||
{
|
||||
Warning(_log, "Could not bind to %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
|
||||
}
|
||||
else
|
||||
{
|
||||
Info(_log, "Started, listening on %s:%d", _listenAddress.toString().toStdString().c_str(), _listenPort);
|
||||
if (!mcastGroup.isNull()) {
|
||||
bool joinGroupOK = _server->joinMulticastGroup(_listenAddress);
|
||||
InfoIf ( joinGroupOK, _log, "Multicast enabled");
|
||||
WarningIf( ! joinGroupOK, _log, "Multicast failed");
|
||||
}
|
||||
_isActive = true;
|
||||
emit statusChanged(_isActive);
|
||||
}
|
||||
}
|
||||
|
||||
void UDPListener::stop()
|
||||
{
|
||||
if ( ! active() )
|
||||
return;
|
||||
|
||||
_server->close();
|
||||
_isActive = false;
|
||||
emit statusChanged(_isActive);
|
||||
}
|
||||
|
||||
|
||||
uint16_t UDPListener::getPort() const
|
||||
{
|
||||
return _server->localPort();
|
||||
|
Reference in New Issue
Block a user