diff options
author | Kari Oikarinen <kari.oikarinen@qt.io> | 2019-10-07 12:15:09 +0300 |
---|---|---|
committer | Kari Oikarinen <kari.oikarinen@qt.io> | 2019-11-01 10:09:43 +0300 |
commit | 2d680b27f3c56aacbec3191f38b7b1898c8747b1 (patch) | |
tree | fb1eddc500c0f421b812e097b87225c9b6921119 | |
parent | 26f9cb7ce5a81eac9743d0dd183f59ceb7e73569 (diff) |
Clarify Q{Abstract,Local}Socket::waitForDisconnected() documentation
Make it clear that the functions will return false if the socket was already
disconnected.
Fix the QLocalSocket example snippet to handle that case correctly by checking
state() before attempting to wait.
Fixes: QTBUG-50711
Change-Id: I4ab4062446a0041a35a3a1d65a19202ffa103298
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
4 files changed, 18 insertions, 13 deletions
diff --git a/src/network/doc/snippets/code/src_network_socket_qabstractsocket.cpp b/src/network/doc/snippets/code/src_network_socket_qabstractsocket.cpp index 3db16b50e6..e19cb40666 100644 --- a/src/network/doc/snippets/code/src_network_socket_qabstractsocket.cpp +++ b/src/network/doc/snippets/code/src_network_socket_qabstractsocket.cpp @@ -57,9 +57,10 @@ if (socket->waitForConnected(1000)) //! [1] socket->disconnectFromHost(); - if (socket->state() == QAbstractSocket::UnconnectedState || - socket->waitForDisconnected(1000)) +if (socket->state() == QAbstractSocket::UnconnectedState + || socket->waitForDisconnected(1000)) { qDebug("Disconnected!"); +} //! [1] diff --git a/src/network/doc/snippets/code/src_network_socket_qlocalsocket_unix.cpp b/src/network/doc/snippets/code/src_network_socket_qlocalsocket_unix.cpp index 181f9c1686..deafca831d 100644 --- a/src/network/doc/snippets/code/src_network_socket_qlocalsocket_unix.cpp +++ b/src/network/doc/snippets/code/src_network_socket_qlocalsocket_unix.cpp @@ -57,6 +57,8 @@ if (socket->waitForConnected(1000)) //! [1] socket->disconnectFromServer(); -if (socket->waitForDisconnected(1000)) +if (socket->state() == QLocalSocket::UnconnectedState + || socket->waitForDisconnected(1000)) { qDebug("Disconnected!"); +} //! [1] diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 9c8f29e18a..b1ea9a4133 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -2359,11 +2359,12 @@ bool QAbstractSocket::waitForBytesWritten(int msecs) } /*! - Waits until the socket has disconnected, up to \a msecs - milliseconds. If the connection has been disconnected, this - function returns \c true; otherwise it returns \c false. In the case - where it returns \c false, you can call error() to determine - the cause of the error. + Waits until the socket has disconnected, up to \a msecs milliseconds. If the + connection was successfully disconnected, this function returns \c true; + otherwise it returns \c false (if the operation timed out, if an error + occurred, or if this QAbstractSocket is already disconnected). In the case + where it returns \c false, you can call error() to determine the cause of + the error. The following example waits up to one second for a connection to be closed: diff --git a/src/network/socket/qlocalsocket.cpp b/src/network/socket/qlocalsocket.cpp index 1c34cd13ee..af7cdb76d2 100644 --- a/src/network/socket/qlocalsocket.cpp +++ b/src/network/socket/qlocalsocket.cpp @@ -287,11 +287,12 @@ QT_BEGIN_NAMESPACE /*! \fn bool QLocalSocket::waitForDisconnected(int msecs) - Waits until the socket has disconnected, up to \a msecs - milliseconds. If the connection has been disconnected, this - function returns \c true; otherwise it returns \c false. In the case - where it returns \c false, you can call error() to determine - the cause of the error. + Waits until the socket has disconnected, up to \a msecs milliseconds. If the + connection was successfully disconnected, this function returns \c true; + otherwise it returns \c false (if the operation timed out, if an error + occurred, or if this QLocalSocket is already disconnected). In the case + where it returns \c false, you can call error() to determine the cause of + the error. The following example waits up to one second for a connection to be closed: |