diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2016-06-09 18:37:59 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2016-07-20 11:36:38 +0000 |
commit | aeb36d5292abd979b9c064877f3ed1b4da961c4e (patch) | |
tree | 0f291293d4ecf859ac9ba16034ef8bc56f84b66e | |
parent | 902a5e7aaa0ec156d19b5a7988eff1809a6a2046 (diff) |
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>
-rw-r--r-- | src/network/socket/qabstractsocket.cpp | 13 | ||||
-rw-r--r-- | tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp | 6 |
2 files changed, 9 insertions, 10 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 098739adc3..88237dc416 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -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) diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 6bb502edcb..fb6b0c6e32 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -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()); |