mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
native UDP listener enhancements (#48)
- uses new logger - can specifiy which ip address to listen on - if its a multicast address, multicast is enabled
This commit is contained in:
@@ -10,19 +10,38 @@
|
||||
#include "utils/ColorRgb.h"
|
||||
#include "HyperionConfig.h"
|
||||
|
||||
UDPListener::UDPListener(const int priority, const int timeout, uint16_t port) :
|
||||
UDPListener::UDPListener(const int priority, const int timeout, const std::string& address, quint16 listenPort) :
|
||||
QObject(),
|
||||
_hyperion(Hyperion::getInstance()),
|
||||
_server(),
|
||||
_openConnections(),
|
||||
_priority(priority),
|
||||
_timeout(timeout),
|
||||
_ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK)
|
||||
_ledColors(Hyperion::getInstance()->getLedCount(), ColorRgb::BLACK),
|
||||
_log(Logger::getInstance("UDPLISTENER"))
|
||||
{
|
||||
_server = new QUdpSocket(this);
|
||||
if (!_server->bind(QHostAddress::Any, port))
|
||||
QHostAddress listenAddress = QHostAddress(QHostAddress::Any);
|
||||
|
||||
if (address.empty()) {
|
||||
listenAddress = QHostAddress::Any;
|
||||
} else {
|
||||
listenAddress = QHostAddress( QString::fromStdString(address) );
|
||||
}
|
||||
|
||||
if (!_server->bind(listenAddress, listenPort))
|
||||
{
|
||||
throw std::runtime_error("UDPLISTENER ERROR: server could not bind to port");
|
||||
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 (listenAddress.QHostAddress::isMulticast() ) { // needs qt >= 5.6
|
||||
if (listenAddress.isInSubnet(QHostAddress::parseSubnet("224.0.0.0/4"))) {
|
||||
if (_server->joinMulticastGroup(listenAddress)) {
|
||||
Info(_log, "Multicast enabled");
|
||||
} else {
|
||||
Warning(_log, "Multicast failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set trigger for incoming connections
|
||||
@@ -52,7 +71,6 @@ void UDPListener::readPendingDatagrams()
|
||||
_server->readDatagram(datagram.data(), datagram.size(),
|
||||
&sender, &senderPort);
|
||||
|
||||
// std::cout << "UDPLISTENER INFO: new packet from " << std::endl;
|
||||
processTheDatagram(&datagram);
|
||||
|
||||
}
|
||||
@@ -61,11 +79,8 @@ void UDPListener::readPendingDatagrams()
|
||||
|
||||
void UDPListener::processTheDatagram(const QByteArray * datagram)
|
||||
{
|
||||
// std::cout << "udp message: " << datagram->data() << std::endl;
|
||||
|
||||
int packlen = datagram->size()/3;
|
||||
int ledlen = _ledColors.size();
|
||||
// int maxled = std::min(datagram->size()/3, _ledColors.size());
|
||||
int maxled = std::min(packlen , ledlen);
|
||||
|
||||
for (int ledIndex=0; ledIndex < maxled; ledIndex++) {
|
||||
@@ -73,9 +88,7 @@ void UDPListener::processTheDatagram(const QByteArray * datagram)
|
||||
rgb.red = datagram->at(ledIndex*3+0);
|
||||
rgb.green = datagram->at(ledIndex*3+1);
|
||||
rgb.blue = datagram->at(ledIndex*3+2);
|
||||
// printf("%02x%02x%02x%02x ", ledIndex, rgb.red, rgb.green, rgb.blue);
|
||||
}
|
||||
// printf ("\n");
|
||||
|
||||
_hyperion->setColors(_priority, _ledColors, _timeout, -1);
|
||||
|
||||
|
Reference in New Issue
Block a user