SDRdaemon: channel sink thread: handle socket life cycle appropriately

pull/228/head
f4exb 2018-08-21 23:47:44 +02:00
rodzic a8f53ec70b
commit 716a77eeb2
3 zmienionych plików z 77 dodań i 19 usunięć

Wyświetl plik

@ -197,7 +197,7 @@ void SDRDaemonChannelSink::start()
}
m_sinkThread = new SDRDaemonChannelSinkThread(&m_dataQueue, m_cm256p);
m_sinkThread->startWork();
m_sinkThread->startStop(true);
m_running = true;
}
@ -207,8 +207,8 @@ void SDRDaemonChannelSink::stop()
if (m_sinkThread != 0)
{
m_sinkThread->stopWork();
delete m_sinkThread;
m_sinkThread->startStop(false);
m_sinkThread->deleteLater();
m_sinkThread = 0;
}

Wyświetl plik

@ -28,6 +28,8 @@
#include "cm256.h"
MESSAGE_CLASS_DEFINITION(SDRDaemonChannelSinkThread::MsgStartStop, Message)
SDRDaemonChannelSinkThread::SDRDaemonChannelSinkThread(SDRDaemonDataQueue *dataQueue, CM256 *cm256, QObject* parent) :
QThread(parent),
m_running(false),
@ -36,20 +38,26 @@ SDRDaemonChannelSinkThread::SDRDaemonChannelSinkThread(SDRDaemonDataQueue *dataQ
m_address(QHostAddress::LocalHost),
m_port(9090)
{
m_socket = new QUdpSocket(this);
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
connect(m_dataQueue, SIGNAL(dataBlockEnqueued()), this, SLOT(handleData()), Qt::QueuedConnection);
}
SDRDaemonChannelSinkThread::~SDRDaemonChannelSinkThread()
{
stopWork();
delete m_socket;
qDebug("SDRDaemonChannelSinkThread::~SDRDaemonChannelSinkThread");
}
void SDRDaemonChannelSinkThread::startStop(bool start)
{
MsgStartStop *msg = MsgStartStop::create(start);
m_inputMessageQueue.push(msg);
}
void SDRDaemonChannelSinkThread::startWork()
{
qDebug("SDRDaemonChannelSinkThread::startWork");
m_startWaitMutex.lock();
m_socket = new QUdpSocket(this);
start();
while(!m_running)
m_startWaiter.wait(&m_startWaitMutex, 100);
@ -59,6 +67,7 @@ void SDRDaemonChannelSinkThread::startWork()
void SDRDaemonChannelSinkThread::stopWork()
{
qDebug("SDRDaemonChannelSinkThread::stopWork");
delete m_socket;
m_running = false;
wait();
}
@ -156,3 +165,25 @@ void SDRDaemonChannelSinkThread::handleData()
}
}
}
void SDRDaemonChannelSinkThread::handleInputMessages()
{
Message* message;
while ((message = m_inputMessageQueue.pop()) != 0)
{
if (MsgStartStop::match(*message))
{
MsgStartStop* notif = (MsgStartStop*) message;
qDebug("SDRDaemonChannelSinkThread::handleInputMessages: MsgStartStop: %s", notif->getStartStop() ? "start" : "stop");
if (notif->getStartStop()) {
startWork();
} else {
stopWork();
}
delete message;
}
}
}

Wyświetl plik

@ -25,6 +25,9 @@
#include <QWaitCondition>
#include <QHostAddress>
#include "util/message.h"
#include "util/messagequeue.h"
class SDRDaemonDataQueue;
class SDRDaemonDataBlock;
class CM256;
@ -34,11 +37,29 @@ class SDRDaemonChannelSinkThread : public QThread {
Q_OBJECT
public:
class MsgStartStop : public Message {
MESSAGE_CLASS_DECLARATION
public:
bool getStartStop() const { return m_startStop; }
static MsgStartStop* create(bool startStop) {
return new MsgStartStop(startStop);
}
protected:
bool m_startStop;
MsgStartStop(bool startStop) :
Message(),
m_startStop(startStop)
{ }
};
SDRDaemonChannelSinkThread(SDRDaemonDataQueue *dataQueue, CM256 *cm256, QObject* parent = 0);
~SDRDaemonChannelSinkThread();
void startWork();
void stopWork();
void startStop(bool start);
void setAddress(QString& address) { m_address.setAddress(address); }
void setPort(unsigned int port) { m_port = port; }
@ -55,9 +76,15 @@ private:
unsigned int m_port;
QUdpSocket *m_socket;
MessageQueue m_inputMessageQueue;
void startWork();
void stopWork();
void run();
bool handleDataBlock(SDRDaemonDataBlock& dataBlock);
private slots:
void handleData();
void handleInputMessages();
};