Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google protobuf compiler doesn't generate class for service tag?

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!

like image 787
Hind Forsum Avatar asked Jan 25 '17 07:01

Hind Forsum


People also ask

What is the difference between proto2 and Proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.

Is gRPC same as protobuf?

Protobuf is the most commonly used IDL (Interface Definition Language) for gRPC. It's where you basically store your data and function contracts in the form of a proto file.

What does oneof mean in protobuf?

Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.


1 Answers

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

like image 138
xosp7tom Avatar answered Sep 25 '22 17:09

xosp7tom