Qt gRPC - support for nested stream messages
-
Hello,
I want to use Qt gRPC which was provided in Qt 6.5. However, I'm facing some problems when I try to use nested messages.
My proto file looks as follows:
package TestPackage; service Tester { rpc Test (Empty) returns (stream TestReply) {} } message TestReply{ TestReplyData data = 1; int32 id = 2; } message TestReplyData { string testData1 = 1; int32 testData2 = 2; } message Empty { }
On the server side ("raw" C++ gRPC) I have following implementation:
Status Test(::grpc::ServerContext* context, const ::TestPackage::Empty* request, ::grpc::ServerWriter< ::TestPackage::TestReply>* writer) override { TestReply reply; TestPackage::TestReplyData* myData = new TestPackage::TestReplyData{}; myData->set_testdata1("testdata1"); myData->set_testdata2(1); reply.set_id(10); reply.set_allocated_data(myData); // or *reply.mutable_data() = ...; writer->Write(reply); return Status::OK; }
On the client side (Qt gRPC) I have the following implementation:
auto stream = m_client->streamTest(TestPackage::Empty()); QObject::connect(stream.get(), &QGrpcStream::messageReceived, this, [this, stream]() { auto callback = stream->read<TestPackage::TestReply>(); TestPackage::TestReplyData data = callback.data(); qDebug() << "ID: " << callback.id_proto(); qDebug() << "TestData1: " << data.testData1(); qDebug() << "TestData2: " << data.testData2(); });
the output is:
qt.protobuf: No deserializer for type TestPackage::TestReplyData* qt.grpc: "No deserializer was found for a given type." ID: 0 TestData1: "" TestData2: 0 setCachingEnabled: 18 bytesDownloaded
when there is an "id" field set only, without "set_allocated_data" on the server side (or mutable_data()) the data is sent correctly, so I assume that the problem is with setting nested messages.
TestReply reply; TestPackage::TestReplyData* myData = new TestPackage::TestReplyData{}; myData->set_testdata1("testdata1"); myData->set_testdata2(1); reply.set_id(10); //*reply.mutable_data() = *myData; writer->Write(reply); return Status::OK; client's output is: ID: 10 TestData1: "" TestData2: 0 setCachingEnabled: 7 bytesDownloaded
Would that mean that this kind of proto file is not supported fully as for now?
I also noticed that it seems that Qt gRPC does not support "oneof" in the messages, is that right?
-
@Blackqsav said in Qt gRPC - support for nested stream messages:
TestPackage::TestReply
The documentation is not ready yet but I would guess you have to provide a (de)serialization function somehow. See https://doc.qt.io/qt-6/qtgrpc-index.html
-
@Blackqsav said in Qt gRPC - support for nested stream messages:
qt.protobuf: No deserializer for type TestPackage::TestReplyData*
You need to call qRegisterProtobufTypes() to eliminate this issue.
-
Hi @Blackqsav
It could be an issue, but need some extra investigation, could you please confirm that the code generated using your scheme doesn't produce the same error with the minimal main:
int main()
{
QProtobufSerializer ser;
TestPackage::TestReplyData data;
data.deserialize(&ser, QByteArray::fromHex("0a0474657374"));
return 0;
} -
@martonmiklos Thanks for this!
I can't believe this fixed it for me. Why in the world is this required when it's not mentioned in the docs at all. The protobuf and grpc modules are in technical preview, but I am surprised how careless Qt can be with this sort of thing. At least put it in the docs!