Merge branch 'qtquick2'
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qpacketprotocol.cpp
index 33bc588..c24df61 100644 (file)
@@ -42,6 +42,7 @@
 #include "private/qpacketprotocol_p.h"
 
 #include <QBuffer>
+#include <QElapsedTimer>
 
 QT_BEGIN_NAMESPACE
 
@@ -114,7 +115,7 @@ Q_OBJECT
 public:
     QPacketProtocolPrivate(QPacketProtocol * parent, QIODevice * _dev)
     : QObject(parent), inProgressSize(-1), maxPacketSize(MAX_PACKET_SIZE),
-      dev(_dev)
+      waitingForPacket(false), dev(_dev)
     {
         Q_ASSERT(4 == sizeof(qint32));
 
@@ -125,7 +126,7 @@ public:
         QObject::connect(this, SIGNAL(invalidPacket()),
                          parent, SIGNAL(invalidPacket()));
         QObject::connect(dev, SIGNAL(readyRead()),
-                         this, SLOT(readyToRead()), Qt::QueuedConnection);
+                         this, SLOT(readyToRead()));
         QObject::connect(dev, SIGNAL(aboutToClose()),
                          this, SLOT(aboutToClose()));
         QObject::connect(dev, SIGNAL(bytesWritten(qint64)),
@@ -200,6 +201,7 @@ public Q_SLOTS:
                 inProgress.clear();
 
                 emit readyRead();
+                waitingForPacket = false;
 
                 // Need to get trailing data
                 readyToRead();
@@ -213,6 +215,7 @@ public:
     QByteArray inProgress;
     qint32 inProgressSize;
     qint32 maxPacketSize;
+    bool waitingForPacket;
     QIODevice * dev;
 };
 
@@ -324,6 +327,48 @@ QPacket QPacketProtocol::read()
     return rv;
 }
 
+/*
+   Returns the difference between msecs and elapsed. If msecs is -1,
+   however, -1 is returned.
+*/
+static int qt_timeout_value(int msecs, int elapsed)
+{
+    if (msecs == -1)
+        return -1;
+
+    int timeout = msecs - elapsed;
+    return timeout < 0 ? 0 : timeout;
+}
+
+/*!
+  This function locks until a new packet is available for reading and the
+  \l{QIODevice::}{readyRead()} signal has been emitted. The function
+  will timeout after \a msecs milliseconds; the default timeout is
+  30000 milliseconds.
+
+  The function returns true if the readyRead() signal is emitted and
+  there is new data available for reading; otherwise it returns false
+  (if an error occurred or the operation timed out).
+  */
+
+bool QPacketProtocol::waitForReadyRead(int msecs)
+{
+    if (!d->packets.isEmpty())
+        return true;
+
+    QElapsedTimer stopWatch;
+    stopWatch.start();
+
+    d->waitingForPacket = true;
+    do {
+        if (!d->dev->waitForReadyRead(msecs))
+            return false;
+        if (!d->waitingForPacket)
+            return true;
+        msecs = qt_timeout_value(msecs, stopWatch.elapsed());
+    } while (true);
+}
+
 /*!
   Return the QIODevice passed to the QPacketProtocol constructor.
 */