diff --git a/examples/grpc/chat/client/simplechatengine.cpp b/examples/grpc/chat/client/simplechatengine.cpp index 66b6ca86..127229e4 100644 --- a/examples/grpc/chat/client/simplechatengine.cpp +++ b/examples/grpc/chat/client/simplechatengine.cpp @@ -63,25 +63,22 @@ void SimpleChatEngine::login(const QString &name, const QString &password) // ![1] auto stream = m_client->messageList(qtgrpc::examples::chat::None()); auto streamPtr = stream.get(); - auto finishedConnection = std::make_shared(); - *finishedConnection = QObject::connect(streamPtr, &QGrpcServerStream::finished, this, - [this, finishedConnection, - stream = std::move(stream)](const QGrpcStatus &status) { - if (!status.isOk()) { - qCritical() << "Stream error(" << status.code() - << "):" << status.message(); - } - if (status.code() - == QtGrpc::StatusCode::Unauthenticated) { - emit authFailed(); - } else if (status.code() != QtGrpc::StatusCode::Ok) { - emit networkError(status.message()); - setState(Disconnected); - } else { - setState(Disconnected); - } - disconnect(*finishedConnection); - }); + QObject::connect( + streamPtr, &QGrpcServerStream::finished, this, + [this, stream = std::move(stream)](const QGrpcStatus &status) { + if (!status.isOk()) { + qCritical() << "Stream error(" << status.code() << "):" << status.message(); + } + if (status.code() == QtGrpc::StatusCode::Unauthenticated) { + emit authFailed(); + } else if (status.code() != QtGrpc::StatusCode::Ok) { + emit networkError(status.message()); + setState(Disconnected); + } else { + setState(Disconnected); + } + }, + Qt::SingleShotConnection); QObject::connect(streamPtr, &QGrpcServerStream::messageReceived, this, [this, name, password, stream = streamPtr]() { diff --git a/src/grpc/qgrpcclientbase.cpp b/src/grpc/qgrpcclientbase.cpp index 432b6a69..60b0f6c1 100644 --- a/src/grpc/qgrpcclientbase.cpp +++ b/src/grpc/qgrpcclientbase.cpp @@ -144,13 +144,13 @@ void QGrpcClientBasePrivate::addStream(QGrpcOperation *grpcStream) activeStreams.remove(grpcStream); }); - auto finishedConnection = std::make_shared(); - *finishedConnection = QObject::connect(grpcStream, &QGrpcOperation::finished, q, - [this, grpcStream, finishedConnection] { - Q_ASSERT(activeStreams.contains(grpcStream)); - activeStreams.remove(grpcStream); - QObject::disconnect(*finishedConnection); - }); + QObject::connect( + grpcStream, &QGrpcOperation::finished, q, + [this, grpcStream] { + Q_ASSERT(activeStreams.contains(grpcStream)); + activeStreams.remove(grpcStream); + }, + Qt::SingleShotConnection); const auto it = activeStreams.insert(grpcStream); Q_ASSERT(it.second); } diff --git a/src/grpc/qgrpchttp2channel.cpp b/src/grpc/qgrpchttp2channel.cpp index 0efeeba4..1cef5011 100644 --- a/src/grpc/qgrpchttp2channel.cpp +++ b/src/grpc/qgrpchttp2channel.cpp @@ -360,18 +360,17 @@ void Http2Handler::attachStream(QHttp2Stream *stream_) }); Q_ASSERT(parentChannel != nullptr); - auto errorConnection = std::make_shared(); - *errorConnection = QObject::connect( + QObject::connect( m_stream.get(), &QHttp2Stream::errorOccurred, parentChannel, - [parentChannel, errorConnection, this](quint32 http2ErrorCode, const QString &errorString) { + [parentChannel, this](quint32 http2ErrorCode, const QString &errorString) { if (!m_operation.expired()) { auto channelOp = m_operation.lock(); emit channelOp->finished(QGrpcStatus{ http2ErrorToStatusCode(http2ErrorCode), errorString }); } parentChannel->deleteHandler(this); - QObject::disconnect(*errorConnection); - }); + }, + Qt::SingleShotConnection); QObject::connect(m_stream.get(), &QHttp2Stream::dataReceived, channelOpPtr, [channelOpPtr, parentChannel, this](const QByteArray &data, bool endStream) { diff --git a/src/grpcquick/qqmlgrpcfunctionalhandlers.cpp b/src/grpcquick/qqmlgrpcfunctionalhandlers.cpp index 711e785d..771349c8 100644 --- a/src/grpcquick/qqmlgrpcfunctionalhandlers.cpp +++ b/src/grpcquick/qqmlgrpcfunctionalhandlers.cpp @@ -38,22 +38,18 @@ void connectMultipleReceiveOperationFinished(QJSEngine *jsEngine, auto *operationPtr = operation.get(); QtGrpcQuickFunctional::validateEngineAndOperation(jsEngine, operationPtr); - auto finishConnection = std::make_shared(); - *finishConnection = QObject::connect(operationPtr, &QGrpcOperation::finished, jsEngine, - [successCallback, errorCallback, jsEngine, - finishConnection, - operation = std::move(operation)](const QGrpcStatus - &status) { - // We take 'operation' by copy so that its lifetime - // is extended until this lambda is destroyed. - if (QtGrpcQuickFunctional:: - checkReceivedStatus(jsEngine, status, - errorCallback) - && successCallback.isCallable()) { - successCallback.call(); - } - QObject::disconnect(*finishConnection); - }); + QObject::connect( + operationPtr, &QGrpcOperation::finished, jsEngine, + [successCallback, errorCallback, jsEngine, + operation = std::move(operation)](const QGrpcStatus &status) { + // We take 'operation' by copy so that its lifetime + // is extended until this lambda is destroyed. + if (QtGrpcQuickFunctional::checkReceivedStatus(jsEngine, status, errorCallback) + && successCallback.isCallable()) { + successCallback.call(); + } + }, + Qt::SingleShotConnection); } void handleReceivedMessageImpl(QJSEngine *jsEngine, std::optional message, @@ -77,19 +73,16 @@ void Private::connectSingleReceiveOperationFinishedImpl(QJSEngine *jsEngine, auto *operationPtr = operation.get(); QtGrpcQuickFunctional::validateEngineAndOperation(jsEngine, operationPtr); - auto finishConnection = std::make_shared(); - *finishConnection = QObject:: - connect(operationPtr, &QGrpcCallReply::finished, jsEngine, - [jsEngine, successCallback, errorCallback, finishConnection, impl, - operation = std::move(operation)](const QGrpcStatus &status) { - // We take 'operation' by copy so that its lifetime - // is extended until this lambda is destroyed. - if (QtGrpcQuickFunctional::checkReceivedStatus(jsEngine, status, - errorCallback)) - impl(jsEngine, operation.get(), successCallback, errorCallback); - - QObject::disconnect(*finishConnection); - }); + QObject::connect( + operationPtr, &QGrpcCallReply::finished, jsEngine, + [jsEngine, successCallback, errorCallback, impl, + operation = std::move(operation)](const QGrpcStatus &status) { + // We take 'operation' by copy so that its lifetime + // is extended until this lambda is destroyed. + if (QtGrpcQuickFunctional::checkReceivedStatus(jsEngine, status, errorCallback)) + impl(jsEngine, operation.get(), successCallback, errorCallback); + }, + Qt::SingleShotConnection); } void Private::makeServerStreamConnectionsImpl(QJSEngine *jsEngine, diff --git a/tests/manual/grpc/benchmarks/bench_qtgrpcclient/bench_qtgrpcclient.cpp b/tests/manual/grpc/benchmarks/bench_qtgrpcclient/bench_qtgrpcclient.cpp index 70c51c26..e3423492 100644 --- a/tests/manual/grpc/benchmarks/bench_qtgrpcclient/bench_qtgrpcclient.cpp +++ b/tests/manual/grpc/benchmarks/bench_qtgrpcclient/bench_qtgrpcclient.cpp @@ -66,27 +66,24 @@ void QtGrpcClientBenchmark::unaryCallHelper(qt::bench::UnaryCallRequest &request auto reply = mClient.UnaryCall(request); auto *replyPtr = reply.get(); - auto connection = std::make_shared(); - *connection = QObject::connect(replyPtr, &QGrpcCallReply::finished, &mClient, - [connection, reply = std::move(reply), this, &request, - &writes](const QGrpcStatus &status) { - if (writes == 0) - mTimer.start(); - if (status.isOk()) { - if (++writes < mCalls) { - unaryCallHelper(request, writes); - } else { - Client::printRpcResult("UnaryCall", - mTimer.nsecsElapsed(), - writes); - mLoop.quit(); - } - } else { - qDebug() << "FAILED: " << status; - mLoop.quit(); - } - QObject::disconnect(*connection); - }); + QObject::connect( + replyPtr, &QGrpcCallReply::finished, &mClient, + [reply = std::move(reply), this, &request, &writes](const QGrpcStatus &status) { + if (writes == 0) + mTimer.start(); + if (status.isOk()) { + if (++writes < mCalls) { + unaryCallHelper(request, writes); + } else { + Client::printRpcResult("UnaryCall", mTimer.nsecsElapsed(), writes); + mLoop.quit(); + } + } else { + qDebug() << "FAILED: " << status; + mLoop.quit(); + } + }, + Qt::SingleShotConnection); } void QtGrpcClientBenchmark::serverStreaming()