#gRPC Basics: C++
This tutorial provides a basic C++ programmer's introduction to working with gRPC. By walking through this example you'll learn how to:
It assumes that you have read the Getting started guide and are familiar with [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto3 version of the protocol buffers language, which is currently in alpha release: you can see the release notes for the new version in the protocol buffers Github repository.
This isn't a comprehensive guide to using gRPC in C++: more reference documentation is coming soon.
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients.
With gRPC we can define our service once in a .proto file and implement clients and servers in , which in turn - all the complexity of communication between different languages and environments is handled for you by gRPC. gRPC also
We also get all the advantages of working with protocol buffers
[possibly insert more advantages here]
The example code for our tutorial is in grpc/grpc-common/cpp/route_guide. To download the example, clone the grpc-common
repository by running the following command:
$ git clone https://github.com/google/grpc-common.git
Then change your current directory to grpc-common/cpp/route_guide
:
$ cd grpc-common/cpp/route_guide
Although we've provided the complete example so you don't need to generate the gRPC code yourself, if you want to try generating your own server and client interface code you can follow the setup instructions for the C++ gRPC libraries in grpc/grpc/INSTALL.
Our first step (as you'll know from Getting started) is to define the gRPC service and the method request and response types using [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file in grpc-common/protos/route_guide.proto
.
To define a service, you specify a named service
in your .proto file:
service RouteGuide { ... }
Then you define rpc
methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the RouteGuide
service:
// Obtains the feature at a given position. rpc GetFeature(Point) returns (Feature) {}
stream
keyword before the response type.// Obtains the Features available within the given Rectangle. Results are // streamed rather than returned at once (e.g. in a response message with a // repeated field), as the rectangle may cover a large area and contain a // huge number of features. rpc ListFeatures(Rectangle) returns (stream Feature) {}
stream
keyword before the request type.// Accepts a stream of Points on a route being traversed, returning a // RouteSummary when traversal is completed. rpc RecordRoute(stream Point) returns (RouteSummary) {}
stream
keyword before both the request and the response.// Accepts a stream of RouteNotes sent while a route is being traversed, // while receiving other RouteNotes (e.g. from other users). rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the Point
message type:
// Points are represented as latitude-longitude pairs in the E7 representation // (degrees multiplied by 10**7 and rounded to the nearest integer). // Latitudes should be in the range +/- 90 degrees and longitude should be in // the range +/- 180 degrees (inclusive). message Point { int32 latitude = 1; int32 longitude = 2; }
Now we need to ...
First let's look at implementing our R
RouteGuide
service work: