Emit error if trying to connect while socket is connected or connecting.
authorJonas M. Gastal <jgastal@profusion.mobi>
Tue, 27 Dec 2011 20:00:18 +0000 (18:00 -0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 4 Jan 2012 17:46:52 +0000 (18:46 +0100)
This applies to both local and abstract sockets.

Task-number: QTBUG-22450
Change-Id: I5c58d68da95ffb6bcde5be510853359b288e5984
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Peter Hartmann <peter.hartmann@nokia.com>
src/network/socket/qabstractsocket.cpp
src/network/socket/qabstractsocket.h
src/network/socket/qlocalsocket.cpp
src/network/socket/qlocalsocket.h
src/network/socket/qlocalsocket_tcp.cpp
src/network/socket/qlocalsocket_unix.cpp
src/network/socket/qlocalsocket_win.cpp

index f84153a..b8e3015 100644 (file)
            proxy) was not found.
     \value ProxyProtocolError The connection negotiation with the proxy server
            because the response from the proxy server could not be understood.
+    \value OperationError An operation was attempted while the socket was in a state that
+           did not permit it.
 
     \value UnknownSocketError An unidentified error occurred.
     \sa QAbstractSocket::error()
@@ -1497,6 +1499,9 @@ void QAbstractSocket::connectToHostImplementation(const QString &hostName, quint
     if (d->state == ConnectedState || d->state == ConnectingState
         || d->state == ClosingState || d->state == HostLookupState) {
         qWarning("QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"", qPrintable(hostName));
+        d->socketError = QAbstractSocket::OperationError;
+        setErrorString(QAbstractSocket::tr("Trying to connect while connection is in progress"));
+        emit error(d->socketError);
         return;
     }
 
index e3c27e3..15e24de 100644 (file)
@@ -97,6 +97,7 @@ public:
         ProxyConnectionTimeoutError,
         ProxyNotFoundError,
         ProxyProtocolError,
+        OperationError,
 
         UnknownSocketError = -1
     };
index 219d2aa..93c98b7 100644 (file)
@@ -419,6 +419,8 @@ bool QLocalSocket::isSequential() const
     \value ConnectionError An error occurred with the connection.
     \value UnsupportedSocketOperationError The requested socket operation
         is not supported by the local operating system.
+    \value OperationError An operation was attempted while the socket was in a state that
+           did not permit it.
     \value UnknownSocketError An unidentified error occurred.
  */
 
index 74c54bf..4025f9b 100644 (file)
@@ -72,7 +72,8 @@ public:
         DatagramTooLargeError = QAbstractSocket::DatagramTooLargeError,
         ConnectionError = QAbstractSocket::NetworkError,
         UnsupportedSocketOperationError = QAbstractSocket::UnsupportedSocketOperationError,
-        UnknownSocketError = QAbstractSocket::UnknownSocketError
+        UnknownSocketError = QAbstractSocket::UnknownSocketError,
+        OperationError = QAbstractSocket::OperationError
     };
 
     enum LocalSocketState
index 38d2b62..4585fdd 100644 (file)
@@ -155,6 +155,9 @@ QString QLocalSocketPrivate::generateErrorString(QLocalSocket::LocalSocketError
     case QLocalSocket::UnsupportedSocketOperationError:
         errorString = QLocalSocket::tr("%1: The socket operation is not supported").arg(function);
         break;
+    case QLocalSocket::OperationError:
+        errorString = QLocalSocket::tr("%1: Operation not permitted when socket is in this state").arg(function);
+        break;
     case QLocalSocket::UnknownSocketError:
     default:
         errorString = QLocalSocket::tr("%1: Unknown error").arg(function);
index 1bdf604..ae8ec83 100644 (file)
@@ -162,6 +162,9 @@ QString QLocalSocketPrivate::generateErrorString(QLocalSocket::LocalSocketError
     case QLocalSocket::UnsupportedSocketOperationError:
         errorString = QLocalSocket::tr("%1: The socket operation is not supported").arg(function);
         break;
+    case QLocalSocket::OperationError:
+        errorString = QLocalSocket::tr("%1: Operation not permitted when socket is in this state").arg(function);
+        break;
     case QLocalSocket::UnknownSocketError:
     default:
         errorString = QLocalSocket::tr("%1: Unknown error %2").arg(function).arg(errno);
@@ -221,9 +224,12 @@ void QLocalSocketPrivate::errorOccurred(QLocalSocket::LocalSocketError error, co
 void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
 {
     Q_D(QLocalSocket);
-    if (state() == ConnectedState
-        || state() == ConnectingState)
+    if (state() == ConnectedState || state() == ConnectingState) {
+        QString errorString = d->generateErrorString(QLocalSocket::OperationError, QLatin1String("QLocalSocket::connectToserver"));
+        setErrorString(errorString);
+        emit error(QLocalSocket::OperationError);
         return;
+    }
 
     d->errorString.clear();
     d->unixSocket.setSocketState(QAbstractSocket::ConnectingState);
index e049f68..8b18c13 100644 (file)
@@ -131,8 +131,11 @@ void QLocalSocketPrivate::destroyPipeHandles()
 void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
 {
     Q_D(QLocalSocket);
-    if (state() == ConnectedState || state() == ConnectingState)
+    if (state() == ConnectedState || state() == ConnectingState) {
+        setErrorString("Trying to connect while connection is in progress");
+        emit error(QLocalSocket::OperationError);
         return;
+    }
 
     d->error = QLocalSocket::UnknownSocketError;
     d->errorString = QString();