WinRT: Unregister callbacks when socket engine is destroyed

Change-Id: I66e8fff2556ce23a66db1148bdb68e9a448227b2
Reviewed-by: Andrew Knight <andrew.knight@intopalo.com>
This commit is contained in:
Oliver Wolff 2015-07-31 14:55:14 +02:00
parent d8130786b6
commit 1840cc982a
2 changed files with 11 additions and 4 deletions

View File

@ -318,8 +318,7 @@ bool QNativeSocketEngine::bind(const QHostAddress &address, quint16 port)
return false;
}
EventRegistrationToken token;
d->tcpListener->add_ConnectionReceived(Callback<ClientConnectedHandler>(d, &QNativeSocketEnginePrivate::handleClientConnection).Get(), &token);
d->tcpListener->add_ConnectionReceived(Callback<ClientConnectedHandler>(d, &QNativeSocketEnginePrivate::handleClientConnection).Get(), &d->connectionToken);
hr = d->tcpListener->BindEndpointAsync(hostAddress.Get(), portString.Get(), &op);
if (FAILED(hr)) {
qErrnoWarning(hr, "Unable to bind socket."); // ### Set error message
@ -779,9 +778,8 @@ bool QNativeSocketEnginePrivate::createNewSocket(QAbstractSocket::SocketType soc
qWarning("Failed to create stream socket");
return false;
}
EventRegistrationToken token;
socketDescriptor = qintptr(socket.Detach());
udpSocket()->add_MessageReceived(Callback<DatagramReceivedHandler>(this, &QNativeSocketEnginePrivate::handleNewDatagram).Get(), &token);
udpSocket()->add_MessageReceived(Callback<DatagramReceivedHandler>(this, &QNativeSocketEnginePrivate::handleNewDatagram).Get(), &connectionToken);
break;
}
default:
@ -807,11 +805,19 @@ QNativeSocketEnginePrivate::QNativeSocketEnginePrivate()
, closingDown(false)
, socketDescriptor(-1)
, sslSocket(Q_NULLPTR)
, connectionToken( { -1 } )
{
}
QNativeSocketEnginePrivate::~QNativeSocketEnginePrivate()
{
if (socketDescriptor == -1 || connectionToken.value == -1)
return;
if (socketType == QAbstractSocket::UdpSocket)
udpSocket()->remove_MessageReceived(connectionToken);
else if (socketType == QAbstractSocket::TcpSocket)
tcpListener->remove_ConnectionReceived(connectionToken);
}
void QNativeSocketEnginePrivate::setError(QAbstractSocket::SocketError error, ErrorString errorString) const

View File

@ -210,6 +210,7 @@ private:
QList<ABI::Windows::Networking::Sockets::IStreamSocket *> currentConnections;
QEventLoop eventLoop;
QAbstractSocket *sslSocket;
EventRegistrationToken connectionToken;
HRESULT handleBindCompleted(ABI::Windows::Foundation::IAsyncAction *, ABI::Windows::Foundation::AsyncStatus);
HRESULT handleNewDatagram(ABI::Windows::Networking::Sockets::IDatagramSocket *socket,