Hi! I have a speed problem with QTcpSocket...

I have to receive a series of variable-length packets in the classic tag-length-value format. That is, I have an header with the message type, its length and then the rest of the packet.

I am trying to use asynchronous sockets to read and store packets. I am not used to this approach because I have always used blocking sockets and threads, but anyway I would like to learn. This is my first approach (pseudo-code):

Qt Code:
  1. // This is the pseudo-code inside the readyRead() signal handling slot
  2. // packet is a struct build imitating the structure of my packet.
  3.  
  4. QDataStream in(tcpSocket)
  5. if (tcpSocket->bytesAvailable() < tag_size)
  6. return;
  7. in.readRawData((char*) &packet.tag, tag_size);
  8.  
  9. if (tcpSocket->bytesAvailable() < length_size)
  10. return;
  11. in.readRawData((char*) &packet.length, length_size);
  12.  
  13. if (tcpSocket->bytesAvailable() < packet.length)
  14. return;
  15. in.readRawData((char*) &packet.optionalPart, packet.length);
To copy to clipboard, switch view to plain text mode 

This is super-simplified... actually if the routines returns says at the second step ( tag read but length not available), the data already read from the buffer are kept and subsequent reads put the new data in queue to the buffer.

Anyway, this works fine but its slow! No packets are lost but the reception speed is so slow that the receiving buffer grows to hundreds of megabytes in a minute or two. Consider that I am trying to receive (in gross) something around 5000 packets per second, where each packet is in average 256 byte. What I get instead is around 100 packet per seconds.

Is that too much to handle with async socket?

Sorry for the pseudo-code but I am a little bit in a rush. If this topic is interesting for someone, I'll post some more details.

Thanks to anyone that is willing to give me some hints.

Bye!