8

I'm trying to use protobuf to generate a service using RpcChannel and RpcController. I referred to language guide of google protobuf and:

I've got sample proto file like this:

syntax = "proto2";

message SearchRequest
{
    required string Request = 1;
}

message SearchResponse
{
    required string Response = 2;
}

service SearchService {
    rpc Search (SearchRequest) returns (SearchResponse);
}

Then I compiled it with:

protoc --cpp_out=./ examples.proto

I got .h and .cc files. But when I search the generated code, I only found classes for "Request" and "Response", but not a class for "SearchService":

examples.pb.h:class SearchRequest;
examples.pb.h:class SearchResponse;
examples.pb.h:class SearchRequest : public ::google::protobuf::Message {
examples.pb.h:  // @@protoc_insertion_point(class_scope:SearchRequest)
examples.pb.h:class SearchResponse : public ::google::protobuf::Message {
examples.pb.h:  // @@protoc_insertion_point(class_scope:SearchResponse)

The language guide web-page provided an example(https://developers.google.com/protocol-buffers/docs/proto#services) which requires to use class of "SearchService": but in the generated code, there's no search service. The guide didn't provide a complete sample of RpcChannel/RpcController usages.

So how can I fix the example to make it work? I searched google but didn't find any good cpp example that gives a complete sample of how RpcChannel/RpcController could work. Any hints or links?

Thanks!

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119

1 Answers1

8

protobuf does not offer RPC implementation by itself; you should use plugin interface to create your own, or use grpc.

For example, grpc uses grpc_cpp_plugin plugin for it.

$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto

https://github.com/grpc/grpc/blob/master/examples/cpp/cpptutorial.md

xosp7tom
  • 2,131
  • 1
  • 17
  • 26
  • Thanks, if gRPC is not by default installed, then why protoc support to compile the .proto file which "service" tag is supported? And how is it used if I don't get gRPC? Thanks. – Hind Forsum Jan 26 '17 at 03:51
  • Think service tag as abstract interface or abstract specification of RPC. With it, you can implement your plugin - essentially code generator - with information from ServiceDescriptor. – xosp7tom Jan 26 '17 at 09:19
  • And historical reason. While google is open-sourcing it, google chose to split 1) serialization part 2) rpc part. serialization part was released first, while rpc part is purposely left for user's own implementation at that time. google's rpc part (grpc) is later/recently open sourced. – xosp7tom Jan 26 '17 at 09:20