winrt: Fixed connectToHost, which is meant to be synchronous

Task-number: QTBUG-46339
Change-Id: I413fef39424a0815ef4604000f85ad37ac2b4dc2
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@theqtcompany.com>
This commit is contained in:
Oliver Wolff 2015-06-19 10:24:15 +02:00 committed by Jani Heikkinen
parent 059e1df345
commit dbddb1751e
2 changed files with 27 additions and 28 deletions

View File

@ -285,11 +285,23 @@ bool QNativeSocketEngine::connectToHostByName(const QString &name, quint16 port)
return false; return false;
} }
d->socketState = QAbstractSocket::ConnectingState; d->socketState = QAbstractSocket::ConnectingState;
hr = d->connectOp->put_Completed(Callback<IAsyncActionCompletedHandler>( hr = QWinRTFunctions::await(d->connectOp);
d, &QNativeSocketEnginePrivate::handleConnectToHost).Get()); RETURN_FALSE_IF_FAILED("Connection could not be established");
Q_ASSERT_SUCCEEDED(hr); bool connectionErrors = false;
d->handleConnectionErrors(d->connectOp.Get(), &connectionErrors);
if (connectionErrors)
return false;
d->connectOp.Reset();
return d->socketState == QAbstractSocket::ConnectedState; d->socketState = QAbstractSocket::ConnectedState;
emit connectionReady();
// Delay the reader so that the SSL socket can upgrade
if (d->sslSocket)
connect(d->sslSocket, SIGNAL(encrypted()), SLOT(establishRead()));
else
establishRead();
return true;
} }
bool QNativeSocketEngine::bind(const QHostAddress &address, quint16 port) bool QNativeSocketEngine::bind(const QHostAddress &address, quint16 port)
@ -1104,47 +1116,34 @@ HRESULT QNativeSocketEnginePrivate::handleClientConnection(IStreamSocketListener
return S_OK; return S_OK;
} }
HRESULT QNativeSocketEnginePrivate::handleConnectToHost(IAsyncAction *action, AsyncStatus) void QNativeSocketEnginePrivate::handleConnectionErrors(IAsyncAction *connectAction, bool *errorsOccured)
{ {
Q_Q(QNativeSocketEngine); bool error = true;
HRESULT hr = connectAction->GetResults();
HRESULT hr = action->GetResults();
if (wasDeleted || !connectOp) // Protect against a late callback
return S_OK;
connectOp.Reset();
switch (hr) { switch (hr) {
case 0x8007274c: // A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. case 0x8007274c: // A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString); setError(QAbstractSocket::NetworkError, ConnectionTimeOutErrorString);
socketState = QAbstractSocket::UnconnectedState; socketState = QAbstractSocket::UnconnectedState;
return S_OK; break;
case 0x80072751: // A socket operation was attempted to an unreachable host. case 0x80072751: // A socket operation was attempted to an unreachable host.
setError(QAbstractSocket::HostNotFoundError, HostUnreachableErrorString); setError(QAbstractSocket::HostNotFoundError, HostUnreachableErrorString);
socketState = QAbstractSocket::UnconnectedState; socketState = QAbstractSocket::UnconnectedState;
return S_OK; break;
case 0x8007274d: // No connection could be made because the target machine actively refused it. case 0x8007274d: // No connection could be made because the target machine actively refused it.
setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString); setError(QAbstractSocket::ConnectionRefusedError, ConnectionRefusedErrorString);
socketState = QAbstractSocket::UnconnectedState; socketState = QAbstractSocket::UnconnectedState;
return S_OK; break;
default: default:
if (FAILED(hr)) { if (FAILED(hr)) {
setError(QAbstractSocket::UnknownSocketError, UnknownSocketErrorString); setError(QAbstractSocket::UnknownSocketError, UnknownSocketErrorString);
socketState = QAbstractSocket::UnconnectedState; socketState = QAbstractSocket::UnconnectedState;
return S_OK; } else {
error = false;
} }
break; break;
} }
if (errorsOccured)
socketState = QAbstractSocket::ConnectedState; *errorsOccured = error;
emit q->connectionReady();
// Delay the reader so that the SSL socket can upgrade
if (sslSocket)
q->connect(sslSocket, SIGNAL(encrypted()), SLOT(establishRead()));
else
q->establishRead();
return S_OK;
} }
HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *asyncInfo, AsyncStatus status) HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *asyncInfo, AsyncStatus status)

View File

@ -216,7 +216,7 @@ private:
ABI::Windows::Networking::Sockets::IDatagramSocketMessageReceivedEventArgs *args); ABI::Windows::Networking::Sockets::IDatagramSocketMessageReceivedEventArgs *args);
HRESULT handleClientConnection(ABI::Windows::Networking::Sockets::IStreamSocketListener *tcpListener, HRESULT handleClientConnection(ABI::Windows::Networking::Sockets::IStreamSocketListener *tcpListener,
ABI::Windows::Networking::Sockets::IStreamSocketListenerConnectionReceivedEventArgs *args); ABI::Windows::Networking::Sockets::IStreamSocketListenerConnectionReceivedEventArgs *args);
HRESULT handleConnectToHost(ABI::Windows::Foundation::IAsyncAction *, ABI::Windows::Foundation::AsyncStatus); void handleConnectionErrors(ABI::Windows::Foundation::IAsyncAction *connectAction, bool *errorsOccured);
HRESULT handleReadyRead(ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32> *asyncInfo, ABI::Windows::Foundation::AsyncStatus); HRESULT handleReadyRead(ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32> *asyncInfo, ABI::Windows::Foundation::AsyncStatus);
}; };