Extend interface of gRPC operation with methods supporting QProtobufMessage

Add the following methods:
 - QGrpcOperation::read(QProtobufMessage *message) const
 - QGrpcClientStream::sendMessage(const QProtobufMessage *message)
 - QGrpcBidirStream::sendMessage(const QProtobufMessage *message)

Methods allow serializing/deserializing protobuf message using the
interface base class. These methods use the serializeRawMessage
and deserializeRawMessage methods of QAbstractProtobufSerializer, so
in current implentation they work relatively slower than their template
counterparts.

[ChangeLog][GRPC] Added the following interfaces to gRPC operation
classes:
 - QGrpcOperation::read(QProtobufMessage *message) const
 - QGrpcClientStream::sendMessage(const QProtobufMessage *message)
 - QGrpcBidirStream::sendMessage(const QProtobufMessage *message)

Task-number: QTBUG-120972
Change-Id: I6fb1ec20efd680cb45ccf13833cd5b712d2dd0aa
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
This commit is contained in:
Alexey Edelev 2024-01-14 19:12:17 +01:00
parent e17d28e0d0
commit 2d0897ce66
4 changed files with 53 additions and 0 deletions

View File

@ -109,6 +109,29 @@ QByteArray QGrpcOperation::data() const
return d_func()->data; return d_func()->data;
} }
/*!
\since 6.8
Reads a message from a raw byte array stored in QGrpcOperation.
The function writes a deserialized value to \a message pointer.
If deserialization is not successful the \l QGrpcOperation::errorOccurred
signal is emitted.
\note This function has slower message deserialization compared to its
template counterpart.
*/
void QGrpcOperation::read(QProtobufMessage *message) const
{
Q_ASSERT_X(message != nullptr, "QGrpcOperation::read",
"Can't read to nullptr QProtobufMessage");
if (auto ser = serializer(); ser) {
if (!ser->deserializeRawMessage(message, data()))
emit errorOccurred(deserializationError());
}
}
/*! /*!
Getter of the metadata received from the channel. For the HTTP2 channels it Getter of the metadata received from the channel. For the HTTP2 channels it
usually contains the HTTP headers received from the server. usually contains the HTTP headers received from the server.

View File

@ -31,6 +31,8 @@ public:
return value; return value;
} }
void read(QProtobufMessage *message) const;
QGrpcMetadata metadata() const; QGrpcMetadata metadata() const;
QLatin1StringView method() const; QLatin1StringView method() const;

View File

@ -64,6 +64,18 @@ QGrpcClientStream::QGrpcClientStream(std::shared_ptr<QGrpcChannelOperation> chan
*/ */
QGrpcClientStream::~QGrpcClientStream() = default; QGrpcClientStream::~QGrpcClientStream() = default;
/*!
\since 6.8
Serializes \a message and sends it to the server.
\note This function has slower message serialization compared to its
template counterpart.
*/
void QGrpcClientStream::sendMessage(const QProtobufMessage *message)
{
sendMessage(serializer()->serializeRawMessage(message));
}
/*! /*!
\internal \internal
Sends the serialized \a data to the server. Sends the serialized \a data to the server.
@ -106,6 +118,18 @@ QGrpcBidirStream::QGrpcBidirStream(std::shared_ptr<QGrpcChannelOperation> channe
*/ */
QGrpcBidirStream::~QGrpcBidirStream() = default; QGrpcBidirStream::~QGrpcBidirStream() = default;
/*!
\since 6.8
Serializes \a message and sends it to the server.
\note This function has slower message serialization compared to its
template counterpart.
*/
void QGrpcBidirStream::sendMessage(const QProtobufMessage *message)
{
sendMessage(serializer()->serializeRawMessage(message));
}
/*! /*!
\internal \internal
Sends the serialized \a data to the server. Sends the serialized \a data to the server.

View File

@ -41,6 +41,8 @@ public:
sendMessage(serializer()->serialize<T>(&message)); sendMessage(serializer()->serialize<T>(&message));
} }
void sendMessage(const QProtobufMessage *message);
private: private:
void sendMessage(const QByteArray &data); void sendMessage(const QByteArray &data);
}; };
@ -59,6 +61,8 @@ public:
sendMessage(serializer()->serialize<T>(&message)); sendMessage(serializer()->serialize<T>(&message));
} }
void sendMessage(const QProtobufMessage *message);
Q_SIGNALS: Q_SIGNALS:
void messageReceived(); void messageReceived();