2022-10-20 11:49:27 +00:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
|
// Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com>
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
|
|
|
|
|
2023-02-08 14:46:54 +00:00
|
|
|
#include <QtCore/private/qobject_p.h>
|
2024-04-15 11:01:07 +00:00
|
|
|
#include <QtCore/qatomic.h>
|
2023-09-05 10:39:18 +00:00
|
|
|
#include <QtCore/qeventloop.h>
|
2024-04-15 11:01:07 +00:00
|
|
|
#include <QtCore/qpointer.h>
|
|
|
|
|
#include <QtGrpc/private/qtgrpcglobal_p.h>
|
|
|
|
|
#include <QtGrpc/qgrpcchanneloperation.h>
|
|
|
|
|
#include <QtGrpc/qgrpcoperation.h>
|
2023-02-08 14:46:54 +00:00
|
|
|
|
2022-10-20 11:49:27 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
2023-08-06 20:07:05 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
|
2022-10-20 11:49:27 +00:00
|
|
|
/*!
|
2022-12-16 08:55:22 +00:00
|
|
|
\class QGrpcOperation
|
2023-03-10 09:49:15 +00:00
|
|
|
\inmodule QtGrpc
|
2022-12-16 08:55:22 +00:00
|
|
|
\brief The QGrpcOperation class implements common logic to
|
2023-08-06 20:07:05 +00:00
|
|
|
handle the gRPC communication from the client side.
|
2022-12-08 12:53:08 +00:00
|
|
|
*/
|
2022-10-20 11:49:27 +00:00
|
|
|
|
|
|
|
|
/*!
|
2024-04-15 11:01:07 +00:00
|
|
|
\fn template <typename T> std::optional<T> QGrpcOperation::read() const
|
|
|
|
|
|
|
|
|
|
Reads a message from a raw byte array stored within this QGrpcOperation
|
|
|
|
|
instance.
|
|
|
|
|
|
|
|
|
|
Returns an optional deserialized message. On failure, \c {std::nullopt} is
|
|
|
|
|
returned.
|
2022-10-20 11:49:27 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
The error can be retrieved using \l deserializationError.
|
2022-12-08 12:53:08 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
\sa read, deserializationError, deserializationErrorString
|
2022-12-08 12:53:08 +00:00
|
|
|
*/
|
2022-10-20 11:49:27 +00:00
|
|
|
|
|
|
|
|
/*!
|
2023-01-27 19:49:36 +00:00
|
|
|
\fn void QGrpcOperation::finished()
|
2022-12-08 12:53:08 +00:00
|
|
|
|
|
|
|
|
This signal indicates the end of communication for this call.
|
|
|
|
|
|
2023-09-05 10:39:18 +00:00
|
|
|
If this signal is emitted by the stream then this stream is successfully
|
2022-12-08 12:53:08 +00:00
|
|
|
closed either by client or server.
|
|
|
|
|
*/
|
2022-10-20 11:49:27 +00:00
|
|
|
|
|
|
|
|
/*!
|
2024-04-15 11:01:07 +00:00
|
|
|
\fn void QGrpcOperation::errorOccurred(const QGrpcStatus &status)
|
2022-10-20 11:49:27 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
This signal is emitted when an error with \a status occurs in the channel.
|
2024-01-14 19:22:07 +00:00
|
|
|
|
2024-04-19 12:18:32 +00:00
|
|
|
\sa QGrpcClientBase::errorOccurred
|
2022-12-08 12:53:08 +00:00
|
|
|
*/
|
2022-10-20 11:49:27 +00:00
|
|
|
|
2023-02-08 14:46:54 +00:00
|
|
|
class QGrpcOperationPrivate : public QObjectPrivate
|
|
|
|
|
{
|
|
|
|
|
Q_DECLARE_PUBLIC(QGrpcOperation)
|
|
|
|
|
public:
|
2023-09-05 10:39:18 +00:00
|
|
|
QGrpcOperationPrivate(std::shared_ptr<QGrpcChannelOperation> _channelOperation)
|
|
|
|
|
: channelOperation(std::move(_channelOperation))
|
2023-04-21 10:01:26 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 14:46:54 +00:00
|
|
|
QByteArray data;
|
2023-08-06 20:07:05 +00:00
|
|
|
std::shared_ptr<QGrpcChannelOperation> channelOperation;
|
2023-09-05 10:39:18 +00:00
|
|
|
QAtomicInteger<bool> isFinished{ false };
|
2023-02-08 14:46:54 +00:00
|
|
|
};
|
|
|
|
|
|
2024-04-09 11:01:28 +00:00
|
|
|
QGrpcOperation::QGrpcOperation(std::shared_ptr<QGrpcChannelOperation> channelOperation,
|
|
|
|
|
QObject *parent)
|
|
|
|
|
: QObject(*new QGrpcOperationPrivate(std::move(channelOperation)), parent)
|
2022-10-20 11:49:27 +00:00
|
|
|
{
|
2023-08-06 20:07:05 +00:00
|
|
|
[[maybe_unused]] bool valid =
|
|
|
|
|
QObject::connect(d_func()->channelOperation.get(), &QGrpcChannelOperation::dataReady,
|
|
|
|
|
this, [this](const QByteArray &data) {
|
|
|
|
|
Q_D(QGrpcOperation);
|
|
|
|
|
d->data = data;
|
|
|
|
|
});
|
|
|
|
|
Q_ASSERT_X(valid, "QGrpcOperation::QGrpcOperation",
|
|
|
|
|
"Unable to make connection to the 'dataReady' signal");
|
|
|
|
|
|
|
|
|
|
valid = QObject::connect(d_func()->channelOperation.get(),
|
|
|
|
|
&QGrpcChannelOperation::errorOccurred, this,
|
2023-09-05 10:39:18 +00:00
|
|
|
[this](const auto &status) {
|
|
|
|
|
d_func()->isFinished.storeRelaxed(true);
|
|
|
|
|
emit this->errorOccurred(status);
|
|
|
|
|
});
|
2023-08-06 20:07:05 +00:00
|
|
|
Q_ASSERT_X(valid, "QGrpcOperation::QGrpcOperation",
|
|
|
|
|
"Unable to make connection to the 'errorOccurred' signal");
|
|
|
|
|
|
|
|
|
|
valid = QObject::connect(d_func()->channelOperation.get(), &QGrpcChannelOperation::finished,
|
2023-09-05 10:39:18 +00:00
|
|
|
this, [this]() {
|
|
|
|
|
d_func()->isFinished.storeRelaxed(true);
|
|
|
|
|
emit this->finished();
|
|
|
|
|
});
|
2023-08-06 20:07:05 +00:00
|
|
|
Q_ASSERT_X(valid, "QGrpcOperation::QGrpcOperation",
|
|
|
|
|
"Unable to make connection to the 'finished' signal");
|
2022-10-20 11:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 12:03:04 +00:00
|
|
|
QGrpcOperation::~QGrpcOperation() = default;
|
|
|
|
|
|
2023-02-08 14:46:54 +00:00
|
|
|
/*!
|
|
|
|
|
\internal
|
|
|
|
|
Getter of the data received from the channel.
|
|
|
|
|
*/
|
2024-03-26 13:08:51 +00:00
|
|
|
QByteArray QGrpcOperation::data() const noexcept
|
2023-02-08 14:46:54 +00:00
|
|
|
{
|
|
|
|
|
return d_func()->data;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 18:12:17 +00:00
|
|
|
/*!
|
|
|
|
|
\since 6.8
|
2024-04-15 11:01:07 +00:00
|
|
|
Reads a message from a raw byte array which is stored within this
|
|
|
|
|
QGrpcOperation instance.
|
2024-01-14 18:12:17 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
The function writes the deserialized value to the \a message pointer.
|
2024-01-14 18:12:17 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
If the deserialization is successful, this function returns \c true.
|
|
|
|
|
Otherwise, it returns \c false, and the error can be retrieved with \l
|
|
|
|
|
deserializationError.
|
2024-01-14 18:12:17 +00:00
|
|
|
|
2024-04-15 11:01:07 +00:00
|
|
|
\sa read, deserializationError, deserializationErrorString
|
2024-01-14 18:12:17 +00:00
|
|
|
*/
|
2024-04-15 11:01:07 +00:00
|
|
|
bool QGrpcOperation::read(QProtobufMessage *message) const
|
2024-01-14 18:12:17 +00:00
|
|
|
{
|
|
|
|
|
Q_ASSERT_X(message != nullptr, "QGrpcOperation::read",
|
|
|
|
|
"Can't read to nullptr QProtobufMessage");
|
2024-04-15 11:01:07 +00:00
|
|
|
const auto ser = d_func()->channelOperation->serializer();
|
|
|
|
|
return ser && ser->deserialize(message, data());
|
2024-01-14 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 12:28:31 +00:00
|
|
|
/*!
|
|
|
|
|
\since 6.8
|
|
|
|
|
|
|
|
|
|
Returns the last deserialization error.
|
|
|
|
|
|
|
|
|
|
\sa QAbstractProtobufSerializer::deserializationError
|
|
|
|
|
*/
|
|
|
|
|
QAbstractProtobufSerializer::DeserializationError QGrpcOperation::deserializationError() const
|
|
|
|
|
{
|
|
|
|
|
const auto ser = d_func()->channelOperation->serializer();
|
|
|
|
|
if (!ser)
|
|
|
|
|
return QAbstractProtobufSerializer::NoDeserializerError;
|
|
|
|
|
return ser->deserializationError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\since 6.8
|
|
|
|
|
|
|
|
|
|
Returns the last deserialization error string.
|
|
|
|
|
|
|
|
|
|
\sa QAbstractProtobufSerializer::deserializationErrorString
|
|
|
|
|
*/
|
|
|
|
|
QString QGrpcOperation::deserializationErrorString() const
|
|
|
|
|
{
|
|
|
|
|
const auto ser = d_func()->channelOperation->serializer();
|
|
|
|
|
if (!ser)
|
|
|
|
|
return QStringLiteral("serializer not available");
|
|
|
|
|
return ser->deserializationErrorString();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 14:39:05 +00:00
|
|
|
/*!
|
2023-08-06 20:07:05 +00:00
|
|
|
Getter of the metadata received from the channel. For the HTTP2 channels it
|
|
|
|
|
usually contains the HTTP headers received from the server.
|
2023-05-17 14:39:05 +00:00
|
|
|
*/
|
2024-04-02 12:51:17 +00:00
|
|
|
const QGrpcMetadata &QGrpcOperation::metadata() const noexcept
|
2023-05-17 14:39:05 +00:00
|
|
|
{
|
2023-08-06 20:07:05 +00:00
|
|
|
return d_func()->channelOperation->serverMetadata();
|
2023-05-17 14:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-13 16:07:11 +00:00
|
|
|
/*!
|
2024-04-09 11:01:28 +00:00
|
|
|
Getter of the method that this operation was initialized with.
|
2023-08-13 16:07:11 +00:00
|
|
|
*/
|
2024-03-26 13:08:51 +00:00
|
|
|
QLatin1StringView QGrpcOperation::method() const noexcept
|
2023-08-13 16:07:11 +00:00
|
|
|
{
|
|
|
|
|
return d_func()->channelOperation->method();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 14:39:05 +00:00
|
|
|
/*!
|
2023-08-06 20:07:05 +00:00
|
|
|
\internal
|
|
|
|
|
Returns a pointer to the assigned channel-side QGrpcChannelOperation.
|
2023-05-17 14:39:05 +00:00
|
|
|
*/
|
2024-04-09 12:15:47 +00:00
|
|
|
QGrpcChannelOperation *QGrpcOperation::channelOperation() const noexcept
|
2023-05-17 14:39:05 +00:00
|
|
|
{
|
2023-08-06 20:07:05 +00:00
|
|
|
return d_func()->channelOperation.get();
|
2023-05-17 14:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-16 15:29:54 +00:00
|
|
|
/*!
|
|
|
|
|
Attempts to cancel the operation in a channel and immediately emits
|
|
|
|
|
\l{QGrpcOperation::errorOccurred} with the \l{QGrpcStatus::Cancelled}
|
|
|
|
|
status code.
|
|
|
|
|
|
|
|
|
|
Any manipulation of the operation after this call has no effect.
|
|
|
|
|
*/
|
2023-08-06 20:07:05 +00:00
|
|
|
void QGrpcOperation::cancel()
|
|
|
|
|
{
|
2023-09-05 10:39:18 +00:00
|
|
|
d_func()->isFinished.storeRelaxed(true);
|
2023-08-06 20:07:05 +00:00
|
|
|
emit d_func()->channelOperation->cancelled();
|
2024-04-02 13:23:55 +00:00
|
|
|
emit errorOccurred(QGrpcStatus{ QGrpcStatus::Cancelled,
|
2024-04-05 11:20:04 +00:00
|
|
|
tr("Operation is cancelled by client") });
|
2023-08-06 20:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-05 10:39:18 +00:00
|
|
|
/*!
|
|
|
|
|
Returns true when QGrpcOperation finished its workflow,
|
|
|
|
|
meaning it was finished, canceled, or error occurred, otherwise returns false.
|
|
|
|
|
*/
|
2024-03-26 13:08:51 +00:00
|
|
|
bool QGrpcOperation::isFinished() const noexcept
|
2023-09-05 10:39:18 +00:00
|
|
|
{
|
|
|
|
|
return d_func()->isFinished.loadRelaxed();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 11:49:27 +00:00
|
|
|
QT_END_NAMESPACE
|
2022-12-09 12:03:04 +00:00
|
|
|
|
|
|
|
|
#include "moc_qgrpcoperation.cpp"
|