QAbstractSocket: ensure bind()+connect() works on delayed close

While connecting, the socket goes through the HostLookupState. In
this state, the socket engine is not yet created, unless the socket
had previously been bound. When it has been bound, we should keep
the socket engine even if the user initiates a delayed close by
using the write()+close() sequence.

Change-Id: Iefebcb33cd72cb49617acbac8e02af9d8209c869
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Alex Trotsenko 2016-06-09 18:37:59 +03:00
parent 902a5e7aaa
commit aeb36d5292
2 changed files with 9 additions and 10 deletions

View File

@ -2651,9 +2651,8 @@ void QAbstractSocket::setPeerName(const QString &name)
}
/*!
Closes the I/O device for the socket, disconnects the socket's connection with the
host, closes the socket, and resets the name, address, port number and underlying
socket descriptor.
Closes the I/O device for the socket and calls disconnectFromHost()
to close the socket's connection.
See QIODevice::close() for a description of the actions that occur when an I/O
device is closed.
@ -2669,13 +2668,6 @@ void QAbstractSocket::close()
QIODevice::close();
if (d->state != UnconnectedState)
disconnectFromHost();
d->localPort = 0;
d->peerPort = 0;
d->localAddress.clear();
d->peerAddress.clear();
d->peerName.clear();
d->cachedSocketDescriptor = -1;
}
/*!
@ -2778,6 +2770,7 @@ void QAbstractSocket::disconnectFromHost()
d->peerPort = 0;
d->localAddress.clear();
d->peerAddress.clear();
d->peerName.clear();
d->setWriteChannelCount(0);
#if defined(QABSTRACTSOCKET_DEBUG)

View File

@ -673,6 +673,12 @@ void tst_QTcpSocket::bindThenResolveHost()
const quint16 port = 80;
socket->connectToHost(hostName, port);
// Additionally, initiate a delayed close before the socket connects
// to ensure that we don't lose the socket engine in HostLookupState.
// After a connection has been established, socket should send all
// the pending data and close the socket engine automatically.
QVERIFY(socket->putChar(0));
socket->close();
QVERIFY2(socket->waitForConnected(), (hostName.toLocal8Bit() + ": " + QByteArray::number(port) + ' '
+ QtNetworkSettings::msgSocketError(*socket)).constData());