PDA

View Full Version : Thread, Timer and Socket. Comuication problem



^NyAw^
17th January 2008, 15:45
Hi,

I have this problem:

-One thread is continously doing some work and when it finish one loop, it emits a signal to own class in the main thread.
-The main thread has the own class that takes the signal and the properly slot is called. It uses a QQueue and enqueue this message.
-This own class is a QThread and has a QTcpSocket. In "run" method, it asks the QQueue if there is a message to send, and if there is one, it sends using the socket.
-This own class has the "readyRead" signal connected to a slot (in the thread).

This works ok if only doing this, but I'm getting "QSocketNotifier: socket notifiers cannot be disabled from another thread". I have readed that this is not a good solution.

I modified the own class to not inherit from QThread and the QSocketNotifier error has gone. Now the own class enqueue the message and sends the message(the message is enqueued because the response message have to be compared with the sended message).

But I have more things:

-In the main thread there is a QTimer that every 5 seconds tells the onw class to send a message, so it enqueue it and send it.
-Then, when the "readyRead" signal is emited that calls the slot "readFromSocket", I read the corresponding bytes, dequeue the first sended message and compare them.

It is like one thread create a quesion and the timer another one(different). By this I use a QQueue. Note that my class is not a Thread now.

But this is not working as expected.

Anyone knows how to do this?

Thanks,

high_flyer
17th January 2008, 16:04
Very hard to tell with out seeing the code.
Specially, since you might THINK you are doing something, but really doing something else, mostly due to problems with thread affinity, which sounds to me to be the problem her as well.

It will be good if you could post relevant code.
Specially where you create the objects you are talking about.

^NyAw^
17th January 2008, 16:09
Hi,

Sorry, but I have 24 files only to manage the communication and 4 more files to manage the application and the thread. So, I can't attach the files here.
I'm developing the Modbus protocol.

The timer is in the main thread
The class that has the socket is in the main thread
The other thread emits a signal that is catched in the main thread usign Queued connection.

Thanks,

high_flyer
17th January 2008, 16:21
So, I can't attach the files here.
That is why I said relevant code - which means showing in code snippets the same you were explaining in words.

So lets try to make things clear:
You have a socket and a polling timer in the main thread.
You have a worker thread, that emits a signal, which fires a slot in the main thread that sends something through a socket is that correct?

And then, again, what is the problem you are having?

^NyAw^
17th January 2008, 16:35
Hi,



You have a socket and a polling timer in the main thread.
You have a worker thread, that emits a signal, which fires a slot in the main thread that sends something through a socket is that correct?


Yes, this is correct.



And then, again, what is the problem you are having?


The class that contain the socket has a QQueue that enqueue the message sended, so the messages enqueued by the timer and the thread(that will be different) stay in a FIFO.
Then, when the socket recives a response from the client, I have to verify that the message sended corresponds to the response got. So dequeue the sended message and compare to the recived.

May this work? I think yes, but I'm having troubles:
Imagin that the response of the client tells me to stop the worker thread, so I sotp it, but the timer is still sending messages. Then, one of the responses can tell me to start the thread anagin.

Stops ok but it don't start again.

Thanks,

high_flyer
17th January 2008, 16:44
Imagin that the response of the client tells me to stop the worker thread, so I sotp it, but the timer is still sending messages. Then, one of the responses can tell me to start the thread anagin.
Well, why can't you stop the time when you stop the worker thread?


Stops ok but it don't start again.
But where is the problem?
If you receive a message from the socket to start the thread again, what stops you from restarting your thread again?

^NyAw^
17th January 2008, 16:48
Hi,

I founded what is happening.

The client is a third party software for testing modbus.
In theory the TCP SLAVE let me send a lot of requests and the SLAVE will send the responses. But the third party software don't do this. It is not able to get a request if it has not sended the response of the previous request.

I suposed it because of sleeping the worker thread makes it work.

Now, I will try to force to wait until the response has arrived until a new request is sended.

Thanks,