From 310496564dabe5f3038c64feff6f444074ee65b1 Mon Sep 17 00:00:00 2001
From: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Date: Thu, 23 Apr 2009 15:30:16 +0200
Subject: correctly handle remote disconnects

about the only error case for a PeekNamedPipe() which does not actually
want to read anything is some kind of disconnect. so ignore the error
code and just handle the error as a close.

Task-number: 247144
Reviewed-by: thiago
---
 src/network/socket/qlocalsocket_win.cpp      |  2 +-
 tests/auto/qlocalsocket/tst_qlocalsocket.cpp | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/network/socket/qlocalsocket_win.cpp b/src/network/socket/qlocalsocket_win.cpp
index e759d0b..39c9284 100644
--- a/src/network/socket/qlocalsocket_win.cpp
+++ b/src/network/socket/qlocalsocket_win.cpp
@@ -298,7 +298,7 @@ qint64 QLocalSocketPrivate::bytesAvailable()
     if (PeekNamedPipe(handle, NULL, 0, NULL, &bytes, NULL)) {
         return bytes;
     } else {
-        if (ERROR_BROKEN_PIPE == GetLastError() && !pipeClosed) {
+        if (!pipeClosed) {
             pipeClosed = true;
             QTimer::singleShot(0, q, SLOT(_q_pipeClosed()));
         }
diff --git a/tests/auto/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/qlocalsocket/tst_qlocalsocket.cpp
index e62b657..deabda6 100644
--- a/tests/auto/qlocalsocket/tst_qlocalsocket.cpp
+++ b/tests/auto/qlocalsocket/tst_qlocalsocket.cpp
@@ -95,6 +95,7 @@ private slots:
 
     void longPath();
     void waitForDisconnect();
+    void waitForDisconnectByServer();
 
     void removeServer();
 
@@ -785,6 +786,25 @@ void tst_QLocalSocket::waitForDisconnect()
     QVERIFY(timer.elapsed() < 2000);
 }
 
+void tst_QLocalSocket::waitForDisconnectByServer()
+{
+    QString name = "tst_localsocket";
+    LocalServer server;
+    QVERIFY(server.listen(name));
+    LocalSocket socket;
+    QSignalSpy spy(&socket, SIGNAL(disconnected()));
+    QVERIFY(spy.isValid());
+    socket.connectToServer(name);
+    QVERIFY(socket.waitForConnected(3000));
+    QVERIFY(server.waitForNewConnection(3000));
+    QLocalSocket *serverSocket = server.nextPendingConnection();
+    QVERIFY(serverSocket);
+    serverSocket->close();
+    QVERIFY(serverSocket->state() == QLocalSocket::UnconnectedState);
+    QVERIFY(socket.waitForDisconnected(3000));
+    QCOMPARE(spy.count(), 1);
+}
+
 void tst_QLocalSocket::removeServer()
 {
     // this is a hostile takeover, but recovering from a crash results in the same
-- 
cgit v0.12