[Qt-interest] problem using sockets and threads

Jason H scorp1us at yahoo.com
Wed Feb 9 23:09:14 CET 2011


You never want to wait for anything. Let the OS call your handler when there is 
something there. Be doing other things in the mean time. (I realize this is hard 
to conceive of when you want to wait for a full object or message, process it 
and return a result - an inherently serial operation) But all you really need is 
a byte collector and a way to tell you're done reading.

Assume temporarily I am reading data from a socket. 
I need to know when I am done. For HTTP this is Content-Length header, or the 
End-of-Mime boundary or end of the chunked encoding. For a QDataStream object, 
it always puts the length of object bytes to be read first. Very simply:

onReadyRead()
{
      m_buffer.append(socket->read(socket->bytesAvailable());
   while(            m_buffer.length() > 4)
      if (m_buffer.length() > 4 && bytesToRead==-1) 
      {
          bytesToRead=m_buffer.left(4).toString().toInt();
      }
     if (bytesToRead > -1) {
        bytesLeft = bytesToRead-(m_buffer.length()-4);
        if (bytesLeft<=0) {
           handleCompleteObject(m_buffer.mid(4, bytesToRead));
           m_buffer = m_buffer.mid(4+bytesToRead); //remove the object
           bytesToRead = -1; 
        }
     } //bytesToRead > -1
  } //while
}

Of course, the more simple way would be to use QDataStream and just send 
whatever bytes are availible to the QIODevice...




----- Original Message ----
From: pmqt71 <pmqt71 at gmail.com>
To: Thiago Macieira <thiago at kde.org>; qt-interest at trolltech.com
Sent: Wed, February 9, 2011 11:05:29 AM
Subject: Re: [Qt-interest] problem using sockets and threads

So I could use socket's signal-slots, bytesAvailable, waitForReadyRead
etc.. to achieve non-blocking I/O, right?

But I think I should avoid instructions like:

    while (pSocket->bytesAvailable() < blockSize) {
        if (!pSocket->waitForReadyRead(timeout)) {
            ...

because a slow client may take time to send the entire message causing
the other to wait (this is the reason why I was using threads). Of
course if you have other hints...

I'm trying.
thanks
pm


2011/2/9 Thiago Macieira <thiago at kde.org>:
> On Tuesday, 8 de February de 2011 23:47:00 pmqt71 wrote:
>>  AFAIK in a single threaded socket app, a slow
>> client will degrade performances for all the clients.
>> Is Qt able to provide a kind of socket IO parallelism with no threads?
>
> Of course. Asynchronous and non-blocking I/O have been done on Unix since the
> dawn of time (well, of time_t anyway).
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>  Senior Product Manager - Nokia, Qt Development Frameworks
>      PGP/GPG: 0x6EF45358; fingerprint:
>      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest



      



More information about the Qt-interest-old mailing list