From 9a1b60c6ec01039e0f181cbb8f1d5d33abb60263 Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 23 Aug 2017 02:17:54 +0200 Subject: [PATCH] UDP sink plugin: use messageing system to configure address and port --- .../channeltx/udpsink/udpsinkudphandler.cpp | 45 +++++++++++++++++-- plugins/channeltx/udpsink/udpsinkudphandler.h | 33 +++++++++++++- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/plugins/channeltx/udpsink/udpsinkudphandler.cpp b/plugins/channeltx/udpsink/udpsinkudphandler.cpp index 40cd5a0cd..91b981a59 100644 --- a/plugins/channeltx/udpsink/udpsinkudphandler.cpp +++ b/plugins/channeltx/udpsink/udpsinkudphandler.cpp @@ -18,7 +18,8 @@ #include "udpsinkmsg.h" #include "udpsinkudphandler.h" -#include "util/messagequeue.h" + +MESSAGE_CLASS_DEFINITION(UDPSinkUDPHandler::MsgUDPAddressAndPort, Message) UDPSinkUDPHandler::UDPSinkUDPHandler() : m_dataSocket(0), @@ -38,6 +39,7 @@ UDPSinkUDPHandler::UDPSinkUDPHandler() : m_feedbackMessageQueue(0) { m_udpBuf = new udpBlk_t[m_minNbUDPFrames]; + connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages())); } UDPSinkUDPHandler::~UDPSinkUDPHandler() @@ -56,17 +58,16 @@ void UDPSinkUDPHandler::start() if (!m_dataConnected) { - connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()), Qt::QueuedConnection); // , Qt::QueuedConnection if (m_dataSocket->bind(m_dataAddress, m_dataPort)) { qDebug("UDPSinkUDPHandler::start: bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort); + connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()), Qt::QueuedConnection); // , Qt::QueuedConnection m_dataConnected = true; } else { - qWarning("UDPSinkUDPHandler::start: cannot bind data port %d", m_dataPort); - disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); + qWarning("UDPSinkUDPHandler::start: cannot bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort); m_dataConnected = false; } } @@ -188,6 +189,12 @@ void UDPSinkUDPHandler::advanceReadPointer(int nbBytes) } void UDPSinkUDPHandler::configureUDPLink(const QString& address, quint16 port) +{ + Message* msg = MsgUDPAddressAndPort::create(address, port); + m_inputMessageQueue.push(msg); +} + +void UDPSinkUDPHandler::applyUDPLink(const QString& address, quint16 port) { qDebug("UDPSinkUDPHandler::configureUDPLink: %s:%d", address.toStdString().c_str(), port); bool addressOK = m_dataAddress.setAddress(address); @@ -229,3 +236,33 @@ void UDPSinkUDPHandler::resizeBuffer(float sampleRate) resetReadIndex(); } + +void UDPSinkUDPHandler::handleMessages() +{ + Message* message; + + while ((message = m_inputMessageQueue.pop()) != 0) + { + if (handleMessage(*message)) + { + delete message; + } + } +} + +bool UDPSinkUDPHandler::handleMessage(const Message& cmd) +{ + if (UDPSinkUDPHandler::MsgUDPAddressAndPort::match(cmd)) + { + UDPSinkUDPHandler::MsgUDPAddressAndPort& notif = (UDPSinkUDPHandler::MsgUDPAddressAndPort&) cmd; + applyUDPLink(notif.getAddress(), notif.getPort()); + return true; + } + else + { + return false; + } +} + + + diff --git a/plugins/channeltx/udpsink/udpsinkudphandler.h b/plugins/channeltx/udpsink/udpsinkudphandler.h index b0f6a761c..a1cdb26af 100644 --- a/plugins/channeltx/udpsink/udpsinkudphandler.h +++ b/plugins/channeltx/udpsink/udpsinkudphandler.h @@ -23,8 +23,8 @@ #include #include "dsp/dsptypes.h" - -class MessageQueue; +#include "util/message.h" +#include "util/messagequeue.h" class UDPSinkUDPHandler : public QObject { @@ -61,10 +61,35 @@ public slots: void dataReadyRead(); private: + class MsgUDPAddressAndPort : public Message { + MESSAGE_CLASS_DECLARATION + + public: + const QString& getAddress() const { return m_address; } + quint16 getPort() const { return m_port; } + + static MsgUDPAddressAndPort* create(QString address, quint16 port) + { + return new MsgUDPAddressAndPort(address, port); + } + + private: + QString m_address; + quint16 m_port; + + MsgUDPAddressAndPort(QString address, quint16 port) : + Message(), + m_address(address), + m_port(port) + { } + }; + typedef char (udpBlk_t)[m_udpBlockSize]; void moveData(char *blk); void advanceReadPointer(int nbBytes); + void applyUDPLink(const QString& address, quint16 port); + bool handleMessage(const Message& message); QUdpSocket *m_dataSocket; QHostAddress m_dataAddress; @@ -83,6 +108,10 @@ private: int m_rwDelta; float m_d; MessageQueue *m_feedbackMessageQueue; + MessageQueue m_inputMessageQueue; + +private slots: + void handleMessages(); };