Search code examples
c++grpc

Use raw bytes for gRPC payload in C++


I am working on a client/server implementation for a gRPC service, where both server and client will be written in C++. The server and client will use bidirectional streams to communicate and need to send raw bytes as the gRPC (HTTP) payload. The goal is to avoid the overhead of Protobuf and use custom serialization code. How can I use gRPC++ without any serialization?


Solution

  • Serialization is controlled by specializations of SerializationTraits. There is a built-in one for the grpc::ByteBuffer type.

    So, using something like grpc::ServerAsyncResponseWriter<grpc::ByteBuffer> or grpc::ServerWriteReactor<grpc::ByteBuffer> should already work if you can use grpc::ByteBuffer as your buffer type.

    If not, you can provide your own specialization.

    Note: the ProtoBuf codegen writes a lot of the glue code to register "handler" code for the various methods that you want to expose. If you want to use raw byte buffers, you'll need to write this code yourself. It's been a while since I looked at what protoc generates. I'd start by running codegen on the helloworld.proto file and looking at what it emits for the the Greeter::AsyncService and Greeter::CallbackService base classes.

    I've also not used the callback/reactor model in C++, only the "async"/completion queue model. I assume the callback/reactor model uses SerializationTraits as well.