QLocalSocket/Win: allow delayed close to work

This mechanism was neither properly designed nor correctly tested
initially on Windows.

[ChangeLog][QtNetwork][Important Behavior Changes] QLocalSocket on
Windows now implements delayed closing, which is consistent with
the behavior on Unix.

Change-Id: Ic3bc427e68eea7f18201f6129df19fbc87d68101
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
This commit is contained in:
Alex Trotsenko 2021-07-12 20:00:50 +03:00
parent f18d8fd1fb
commit 921ff400bb
3 changed files with 12 additions and 20 deletions

View File

@ -208,7 +208,14 @@ QT_BEGIN_NAMESPACE
/*! /*!
\fn void QLocalSocket::close() \fn void QLocalSocket::close()
\reimp
Closes the I/O device for the socket and calls disconnectFromServer()
to close the socket's connection.
See QIODevice::close() for a description of the actions that occur when an I/O
device is closed.
\sa abort()
*/ */
/*! /*!

View File

@ -377,21 +377,11 @@ bool QLocalSocket::canReadLine() const
void QLocalSocket::close() void QLocalSocket::close()
{ {
Q_D(QLocalSocket); Q_D(QLocalSocket);
if (openMode() == NotOpen)
return;
QIODevice::close(); QIODevice::close();
d->serverName = QString(); d->serverName = QString();
d->fullServerName = QString(); d->fullServerName = QString();
disconnectFromServer();
if (state() != UnconnectedState) {
if (bytesToWrite() > 0) {
disconnectFromServer();
return;
}
d->_q_pipeClosed();
}
} }
bool QLocalSocket::flush() bool QLocalSocket::flush()
@ -481,10 +471,6 @@ bool QLocalSocket::waitForDisconnected(int msecs)
qWarning("QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState"); qWarning("QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState");
return false; return false;
} }
if (!openMode().testFlag(QIODevice::ReadOnly)) {
qWarning("QLocalSocket::waitForDisconnected isn't supported for write only pipes.");
return false;
}
QDeadlineTimer deadline(msecs); QDeadlineTimer deadline(msecs);
while (!d->pipeReader->isPipeClosed()) { while (!d->pipeReader->isPipeClosed()) {

View File

@ -1498,17 +1498,16 @@ void tst_QLocalSocket::writeToClientAndDisconnect()
QVERIFY(server.waitForNewConnection(200)); QVERIFY(server.waitForNewConnection(200));
QLocalSocket* clientSocket = server.nextPendingConnection(); QLocalSocket* clientSocket = server.nextPendingConnection();
QVERIFY(clientSocket); QVERIFY(clientSocket);
server.close();
char buffer[100]; char buffer[100];
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
for (int i = 0; i < chunks; ++i) for (int i = 0; i < chunks; ++i)
QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), qint64(sizeof(buffer))); QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), qint64(sizeof(buffer)));
while (clientSocket->bytesToWrite())
QVERIFY(clientSocket->waitForBytesWritten());
clientSocket->close(); clientSocket->close();
server.close(); QVERIFY(clientSocket->waitForDisconnected());
client.waitForDisconnected(); QVERIFY(client.waitForDisconnected());
QCOMPARE(readChannelFinishedSpy.count(), 1); QCOMPARE(readChannelFinishedSpy.count(), 1);
const QByteArray received = client.readAll(); const QByteArray received = client.readAll();
QCOMPARE(received.size(), qint64(sizeof(buffer) * chunks)); QCOMPARE(received.size(), qint64(sizeof(buffer) * chunks));