Create Processing Thread in Server Class
I have a problem in my server class where I create a thread which is used for processing incoming data, however, once the thread is running it will block the signals from emitting for my socket.
Here is a overview of my class:
QUdpSocket * mUdpSocket;
QThread * mProcessingThread;
State = PROCESSING_DATA;
Create Processing Thread
Connect started signal to OnProcessing
Create Udp Socket
Connect readReady signal to OnNewData
Bind and Begin Listening
Read Incoming Data
Insert new data into circular buffer
while( State == PROCESSING_DATA )
Check if new data available in circular buffer
If available begin parsing data
However, if I start the thread I never receive data because the signal readReady is never emitted, but if I do not start the thread I receive data just fine.
I did fix the problem by inheriting the QThread class and everything seems to work fine, however, my question is how would I get this to work with using the QThread as a class member? The reason I have the processing in a forever loop is because I constantly receive new data on the socket, is a looped thread the best way to implement the design, or would destroying and restarting the thread at each new processing better?
Thanks a lot for any insight into this!
[[Added code markup, Tobias]]
Create a subclass of a QTcpSocket/UdpSocket with no parent, then create a QThread with the main windows as your parent.
Call moveToThread() on the QTcpsocket/UdpSocket and give it the reference to the QThread.
Do all your ready read / processing in the QTcpSocket/UdpSocket subclass.
UdpSubclass *data_comm = new UdpSubclass(); // do not inherit a perent, the move to thread will set it.
// this is your subclass that you will have to create
// you can connect your signals and slots to communicate between the MainWindow class that this is created in and the tcp subclass.
// create a slot in the class that catches your readyRead() signal / stateChanged() signal, and connect them in the constructor of the class
// connect all of your other stuff like state changes or custom slots in the constructor as well
QThread *data_comm_t = new QThread(this);
If you're in the forever-loop, it will never return, so the thread that runs the loop will never be able to do anything else (i.e. it can't emit signals or invoke slots either).
You don't need a forever-loop. QUdpSocket will emit readyRead() every time new data arrives, so you can just parse the data in a slot, like dvez43 suggested.
[quote]how would I get this to work with using the QThread as a class member?[/quote]
I'm not 100% sure about your intended design, but here are some points that might be helpful to you:
Right now, OnProcessing() is a member function of cUdpServer, so it should run in the thread that cUdpServer lives in
If you want your cUdpServer to live in a secondary thread, then it can't hold the QThread object -- instead, the whole cUdpServer will need to be passed to the new thread
If you want your cUdpServer to live in the main thread, then OnProcessing can't be its member function -- instead, you need a separate object to run code in the new thread
QThread is not a thread; it is a class that manages a thread. It usually lives in the main thread, and lets you control a different thread (e.g. start()/quit())
Finally, how busy does your main thread get? And how computationally-expensive is OnProcessing()? If neither one does any heavy computation, then you might not need a separate thread at all.