PDA

View Full Version : QTcpSocket slowdown



jessiemmichael
3rd September 2009, 07:51
I implement an UI application, that handles receiving data from tcp socket in a separate thread.

I use the following code to read data:

QByteArray data;
data = m_Connection->readAll();
m_CurrentResponse->insert(m_CurrentResponse->size(), data);
while (isResponseFull(m_CurrentResponse))
{
QByteArray* response = new QByteArray(m_CurrentResponse->data(), m_CurrentResponse->size());

unsigned int len = // read the 4 bytes to get the length of the responce
response->remove(len, (m_CurrentResponse->size() - len));
m_CurrentResponse->remove(0, len);
processResponse(response);
delete(response);
}

It crashes at readAll after few minutes. The server sends image data for every 1 ms. The client application should not block while processing data.

1) Is there any issue in calling readAll when bytesAvailable is more in number?
2) setReadBufferSize does not help in setting buffer size. Is there any way to restrict to limit receiving data internally in tcpSocket object

wysota
3rd September 2009, 08:19
Where (which thread) did you create the socket? Where (which thread) do you read the data? Why do you allocate a byte array on heap?

jessiemmichael
3rd September 2009, 08:40
1) I create tcp Socket in worker thread(run)
2) I read data in slot of readyRead signal in worker thread.
3) There is no specific reason to have qbytearray in heap, will it cause any issue?

wysota
3rd September 2009, 09:18
1) I create tcp Socket in worker thread(run)
2) I read data in slot of readyRead signal in worker thread.
Does something else access the socket as well? Does the thread do anything else apart handling the socket?


3) There is no specific reason to have qbytearray in heap, will it cause any issue?
It's just harder to handle the byte array because you have to remember to delete it without giving any benefits in exchange.

jessiemmichael
3rd September 2009, 09:26
1) Worker thread is responsible for reading data, it parses the data and queues up the data accordingly. The UI thread retrieves the data and displays it.
The socket is accessed only by the worker thread.

2) I have taken care of freeing the qbytearray.

Is there any possibility that any byte of the data in QTcpSocket object get discarded internally .

wysota
3rd September 2009, 10:37
No, there is no such possibility. Try getting rid of the thread, you don't need it anyway as Qt sockets are asynchronous.

jessiemmichael
3rd September 2009, 11:14
Initially UI application was implemented without worker thread for communication.

This way of implementation causes server to slowdown when UI is busy. To overcome this issue, I plan to have thread.

Is there any other alternative solution?

wysota
3rd September 2009, 13:50
How many cores or processors do you have in your machine?

Oh, one more thing - where is the slot connected to the readyRead() signal of the socket located?

jessiemmichael
4th September 2009, 04:25
Core2 duo processer.

Signal connection is in worker thread's run method.
Slot is provided in worker thread class.

wysota
4th September 2009, 08:48
Slot is provided in worker thread class.

That's the reason for your slowdown. The slot is executed in the context of the main thread so your thread does practically nothing and the whole strain goes on the main thread. The slot has to be in an object created from within the run() method.

I don't know what you mean by "processing data" but there is a good chance you can just optimize it and put in the same thread as the gui.

jessiemmichael
4th September 2009, 09:36
Thank you very much. Having the slot in different class and creating in in the worker thread resolves this issue.