From 2d0897ce66fd8a37cc6baf8c8f7ab937e719888c Mon Sep 17 00:00:00 2001 From: Alexey Edelev Date: Sun, 14 Jan 2024 19:12:17 +0100 Subject: [PATCH] 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 Reviewed-by: Tatiana Borisova --- src/grpc/qgrpcoperation.cpp | 23 +++++++++++++++++++++++ src/grpc/qgrpcoperation.h | 2 ++ src/grpc/qgrpcstream.cpp | 24 ++++++++++++++++++++++++ src/grpc/qgrpcstream.h | 4 ++++ 4 files changed, 53 insertions(+) diff --git a/src/grpc/qgrpcoperation.cpp b/src/grpc/qgrpcoperation.cpp index eb465f02..388f4550 100644 --- a/src/grpc/qgrpcoperation.cpp +++ b/src/grpc/qgrpcoperation.cpp @@ -109,6 +109,29 @@ QByteArray QGrpcOperation::data() const 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 usually contains the HTTP headers received from the server. diff --git a/src/grpc/qgrpcoperation.h b/src/grpc/qgrpcoperation.h index b4c1e940..371f43ba 100644 --- a/src/grpc/qgrpcoperation.h +++ b/src/grpc/qgrpcoperation.h @@ -31,6 +31,8 @@ public: return value; } + void read(QProtobufMessage *message) const; + QGrpcMetadata metadata() const; QLatin1StringView method() const; diff --git a/src/grpc/qgrpcstream.cpp b/src/grpc/qgrpcstream.cpp index 78dca81a..2ad05784 100644 --- a/src/grpc/qgrpcstream.cpp +++ b/src/grpc/qgrpcstream.cpp @@ -64,6 +64,18 @@ QGrpcClientStream::QGrpcClientStream(std::shared_ptr chan */ 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 Sends the serialized \a data to the server. @@ -106,6 +118,18 @@ QGrpcBidirStream::QGrpcBidirStream(std::shared_ptr channe */ 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 Sends the serialized \a data to the server. diff --git a/src/grpc/qgrpcstream.h b/src/grpc/qgrpcstream.h index b53dc4a3..0f997f70 100644 --- a/src/grpc/qgrpcstream.h +++ b/src/grpc/qgrpcstream.h @@ -41,6 +41,8 @@ public: sendMessage(serializer()->serialize(&message)); } + void sendMessage(const QProtobufMessage *message); + private: void sendMessage(const QByteArray &data); }; @@ -59,6 +61,8 @@ public: sendMessage(serializer()->serialize(&message)); } + void sendMessage(const QProtobufMessage *message); + Q_SIGNALS: void messageReceived();