From e8f85db2924d23f5bb7f56f2bf3be230c56079e8 Mon Sep 17 00:00:00 2001
From: Petri Hodju <petrihodju@yahoo.com>
Date: Fri, 27 Jan 2017 21:19:03 +0200
Subject: [PATCH] Revert "Rework toggling the state of read notifier on
 unbuffered socket"

- The reverted commit is suspect for causing the QUdpSocket to emit
  readyRead signal twice per datagram when using QueuedConnection.
  On handling the second signal the hasPendingDatagrams() return false,
  which typically means that the handling code will not call readDatagram().
  This might be the reason why the readyRead signal stops to be emitted.

  The real root cause needs to be still investigated more carefully.
  Reverting the commit seems to fix the problem, but why...

This reverts commit ccb693299e6ef633d3ac330961c872a03815c93c.

Change-Id: If8c144f2307093a42d6d8611c274bddebde84205
---
 src/network/socket/qabstractsocket.cpp | 12 +++---------
 src/network/socket/qabstractsocket_p.h |  1 -
 src/network/socket/qudpsocket.cpp      |  3 +--
 3 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp
index 57c4019..7a98f72 100644
--- a/src/network/socket/qabstractsocket.cpp
+++ b/src/network/socket/qabstractsocket.cpp
@@ -564,7 +564,6 @@ QAbstractSocketPrivate::QAbstractSocketPrivate()
       cachedSocketDescriptor(-1),
       readBufferMaxSize(0),
       isBuffered(false),
-      hasPendingData(false),
       connectTimer(0),
       disconnectTimer(0),
       hostLookupId(-1),
@@ -595,7 +594,6 @@ void QAbstractSocketPrivate::resetSocketLayer()
     qDebug("QAbstractSocketPrivate::resetSocketLayer()");
 #endif
 
-    hasPendingData = false;
     if (socketEngine) {
         socketEngine->close();
         socketEngine->disconnect();
@@ -693,6 +691,9 @@ bool QAbstractSocketPrivate::canReadNotification()
     qDebug("QAbstractSocketPrivate::canReadNotification()");
 #endif
 
+    if (!isBuffered)
+        socketEngine->setReadNotificationEnabled(false);
+
     // If buffered, read data from the socket into the read buffer
     if (isBuffered) {
         const qint64 oldBufferSize = buffer.size();
@@ -722,12 +723,6 @@ bool QAbstractSocketPrivate::canReadNotification()
             // to indicate that the data was discarded.
             return !q->isReadable();
         }
-    } else {
-        if (hasPendingData) {
-            socketEngine->setReadNotificationEnabled(false);
-            return true;
-        }
-        hasPendingData = true;
     }
 
     emitReadyRead();
@@ -2476,7 +2471,6 @@ qint64 QAbstractSocket::readData(char *data, qint64 maxSize)
         d->state = QAbstractSocket::UnconnectedState;
     } else {
         // Only do this when there was no error
-        d->hasPendingData = false;
         d->socketEngine->setReadNotificationEnabled(true);
     }
 
diff --git a/src/network/socket/qabstractsocket_p.h b/src/network/socket/qabstractsocket_p.h
index 1578d7b..81bbb45 100644
--- a/src/network/socket/qabstractsocket_p.h
+++ b/src/network/socket/qabstractsocket_p.h
@@ -145,7 +145,6 @@ public:
 
     qint64 readBufferMaxSize;
     bool isBuffered;
-    bool hasPendingData;
 
     QTimer *connectTimer;
     QTimer *disconnectTimer;
diff --git a/src/network/socket/qudpsocket.cpp b/src/network/socket/qudpsocket.cpp
index 37b385d..c7c6980 100644
--- a/src/network/socket/qudpsocket.cpp
+++ b/src/network/socket/qudpsocket.cpp
@@ -452,7 +452,6 @@ QNetworkDatagram QUdpSocket::receiveDatagram(qint64 maxSize)
     QNetworkDatagram result(QByteArray(maxSize, Qt::Uninitialized));
     qint64 readBytes = d->socketEngine->readDatagram(result.d->data.data(), maxSize, &result.d->header,
                                                      QAbstractSocketEngine::WantAll);
-    d->hasPendingData = false;
     d->socketEngine->setReadNotificationEnabled(true);
     if (readBytes < 0)
         d->setErrorAndEmit(d->socketEngine->error(), d->socketEngine->errorString());
@@ -499,8 +498,8 @@ qint64 QUdpSocket::readDatagram(char *data, qint64 maxSize, QHostAddress *addres
         readBytes = d->socketEngine->readDatagram(data, maxSize);
     }
 
-    d->hasPendingData = false;
     d->socketEngine->setReadNotificationEnabled(true);
+
     if (readBytes < 0) {
         if (readBytes == -2) {
             // No pending datagram. Treat as a temporary error.
-- 
2.9.3

