You'll have to decide if you want to do blocking I/O or asynchronous I/O.
Currently you are mixing both.
I would suggest asynchronous I/O, i.e. the code that you have in DataClient. To make that work properly don't reimplement the thread's run() method but let the base implementation run the thread's event loop.
Something more like this
{
Q_OBJECT
signals:
void dataReady();
private slots:
void slotReadData()
{
qint64 nBytesAvailable = m_tcpSocket->bytesAvailable();
if (nBytesAvailable < (int)sizeof(quint16)) // Sufficient Data NOT Arrived
{
return;
}
// Any other Data Checking Statements
...
emit dataReady();
}
};
class DataProcessor
: public QObject{
Q_OBJECT
public:
DataProcessor()
{
// make sure m_pDataClient has the data processor as its parent, otherwise they end up in different threads.
//....
connect(m_pDataClient, SIGNAL(dataReady()), this, SLOT(slotDataReady()));
}
private slots:
void slotDataReady(); // processing method
};
class DataClient : public QObject
{
Q_OBJECT
signals:
void dataReady();
private slots:
void slotReadData()
{
qint64 nBytesAvailable = m_tcpSocket->bytesAvailable();
if (nBytesAvailable < (int)sizeof(quint16)) // Sufficient Data NOT Arrived
{
return;
}
// Any other Data Checking Statements
...
emit dataReady();
}
};
class DataProcessor : public QObject
{
Q_OBJECT
public:
DataProcessor()
{
// make sure m_pDataClient has the data processor as its parent, otherwise they end up in different threads.
//....
connect(m_pDataClient, SIGNAL(dataReady()), this, SLOT(slotDataReady()));
}
private slots:
void slotDataReady(); // processing method
};
To copy to clipboard, switch view to plain text mode
DataProcessor *processor = new DataProcessor;
processor->moveToThread(thread);
DataProcessor *processor = new DataProcessor;
QThread *thread = new QThread;
processor->moveToThread(thread);
To copy to clipboard, switch view to plain text mode
If you want the thread to stop, call thread->quit() or through a signal/slot connection (quit is a slot).
One advantage of this approach is that you can test this without the additional threads, i.e. let the data processor do its work in the main thread, or have multiple processors on the same thread, etc.
Cheers,
_
Bookmarks