blob: 65fd5c7eec24938450a7a6adffe6b82ff2ebba9d [file] [log] [blame] [view]
Lisa Carey54e0c6d2015-02-23 16:00:38 +00001#gRPC Basics: C++
2
3This tutorial provides a basic C++ programmer's introduction to working with gRPC. By walking through this example you'll learn how to:
4
5- Define a service in a .proto file.
6- Generate server and client code using the protocol buffer compiler.
7- Use the C++ gRPC API to write a simple client and server for your service.
8
9It assumes that you have read the [Getting started](https://github.com/grpc/grpc-common) 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](https://github.com/google/protobuf/releases) for the new version in the protocol buffers Github repository.
10
11This isn't a comprehensive guide to using gRPC in C++: more reference documentation is coming soon.
12
13## Why use gRPC?
14
15Our 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.
16
Lisa Carey7a219662015-02-23 16:42:21 +000017With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.
Lisa Carey54e0c6d2015-02-23 16:00:38 +000018
Lisa Carey54e0c6d2015-02-23 16:00:38 +000019## Example code and setup
20
Yang Gaoe1ea9622015-02-24 15:09:26 -080021The example code for our tutorial is in [grpc/grpc-common/cpp/route_guide](https://github.com/grpc/grpc-common/tree/master/cpp/route_guide). To download the example, clone the `grpc-common` repository by running the following command:
Lisa Carey54e0c6d2015-02-23 16:00:38 +000022```shell
23$ git clone https://github.com/google/grpc-common.git
24```
25
26Then change your current directory to `grpc-common/cpp/route_guide`:
27```shell
28$ cd grpc-common/cpp/route_guide
29```
30
Lisa Carey14184fa2015-02-24 16:56:30 +000031Although 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 in [the C++ quick start guide](https://github.com/grpc/grpc-common/tree/master/cpp).
Lisa Carey54e0c6d2015-02-23 16:00:38 +000032
33
34## Defining the service
35
36Our first step (as you'll know from [Getting started](https://github.com/grpc/grpc-common)) 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`](https://github.com/grpc/grpc-common/blob/master/protos/route_guide.proto).
37
38To define a service, you specify a named `service` in your .proto file:
39
40```
41service RouteGuide {
42 ...
43}
44```
45
46Then 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:
47
48- A *simple RPC* where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call.
49```
Yang Gaode0c6532015-02-24 15:52:22 -080050 // Obtains the feature at a given position.
Lisa Carey54e0c6d2015-02-23 16:00:38 +000051 rpc GetFeature(Point) returns (Feature) {}
52```
53
Lisa Carey450d1122015-02-23 16:05:25 +000054- A *server-side streaming RPC* where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. As you can see in our example, you specify a server-side streaming method by placing the `stream` keyword before the *response* type.
Lisa Carey54e0c6d2015-02-23 16:00:38 +000055```
56 // Obtains the Features available within the given Rectangle. Results are
57 // streamed rather than returned at once (e.g. in a response message with a
58 // repeated field), as the rectangle may cover a large area and contain a
59 // huge number of features.
60 rpc ListFeatures(Rectangle) returns (stream Feature) {}
61```
62
Lisa Carey450d1122015-02-23 16:05:25 +000063- A *client-side streaming RPC* where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a server-side streaming method by placing the `stream` keyword before the *request* type.
Lisa Carey54e0c6d2015-02-23 16:00:38 +000064```
65 // Accepts a stream of Points on a route being traversed, returning a
66 // RouteSummary when traversal is completed.
67 rpc RecordRoute(stream Point) returns (RouteSummary) {}
68```
69
Lisa Carey450d1122015-02-23 16:05:25 +000070- A *bidirectional streaming RPC* where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response.
Lisa Carey54e0c6d2015-02-23 16:00:38 +000071```
Lisa Carey450d1122015-02-23 16:05:25 +000072 // Accepts a stream of RouteNotes sent while a route is being traversed,
73 // while receiving other RouteNotes (e.g. from other users).
74 rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
Lisa Carey54e0c6d2015-02-23 16:00:38 +000075```
76
77Our .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:
78```
Lisa Carey450d1122015-02-23 16:05:25 +000079// Points are represented as latitude-longitude pairs in the E7 representation
80// (degrees multiplied by 10**7 and rounded to the nearest integer).
81// Latitudes should be in the range +/- 90 degrees and longitude should be in
82// the range +/- 180 degrees (inclusive).
83message Point {
84 int32 latitude = 1;
85 int32 longitude = 2;
86}
Lisa Carey54e0c6d2015-02-23 16:00:38 +000087```
88
89
90## Generating client and server code
91
Lisa Carey7a219662015-02-23 16:42:21 +000092Next we need to generate the gRPC client and server interfaces from our .proto service definition. We do this using the protocol buffer compiler `protoc` with a special gRPC C++ plugin.
93
Lisa Carey14184fa2015-02-24 16:56:30 +000094For simplicity, we've provided a [makefile](https://github.com/grpc/grpc-common/blob/master/cpp/route_guide/Makefile) that runs `protoc` for you with the appropriate plugin, input, and output (if you want to run this yourself, make sure you've installed protoc and followed the gRPC code [installation instructions](https://github.com/grpc/grpc/blob/master/INSTALL) first):
Lisa Carey7a219662015-02-23 16:42:21 +000095
96```shell
97$ make route_guide.pb.cc
98```
99
100which actually runs:
101
Yang Gaocdbb60c2015-02-24 15:01:36 -0800102```shell
103$ protoc -I ../../protos --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto
104```
Lisa Carey7a219662015-02-23 16:42:21 +0000105
106Running this command generates the following files:
Lisa Carey453eca32015-02-23 16:58:19 +0000107- `route_guide.pb.h`, the header which declares your generated classes
108- `route_guide.pb.cc`, which contains the implementation of your classes
Lisa Carey7a219662015-02-23 16:42:21 +0000109
Lisa Carey453eca32015-02-23 16:58:19 +0000110These contain:
111- All the protocol buffer code to populate, serialize, and retrieve our request and response message types
Lisa Carey14184fa2015-02-24 16:56:30 +0000112- A class called `RouteGuide` that contains
113 - a remote interface type (or *stub*) for clients to call with the methods defined in the `RouteGuide` service.
114 - two abstract interfaces for servers to implement, also with the methods defined in the `RouteGuide` service.
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000115
116
Lisa Carey14184fa2015-02-24 16:56:30 +0000117<a name="server"></a>
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000118## Creating the server
119
Lisa Carey14184fa2015-02-24 16:56:30 +0000120First let's look at how we create a `RouteGuide` server. If you're only interested in creating gRPC clients, you can skip this section and go straight to [Creating the client](#client) (though you might find it interesting anyway!).
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000121
Lisa Carey14184fa2015-02-24 16:56:30 +0000122There are two parts to making our `RouteGuide` service do its job:
Lisa Carey7a219662015-02-23 16:42:21 +0000123- Implementing the service interface generated from our service definition: doing the actual "work" of our service.
Lisa Carey14184fa2015-02-24 16:56:30 +0000124- Running a gRPC server to listen for requests from clients and return the service responses.
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000125
Yang Gaode0c6532015-02-24 15:52:22 -0800126You can find our example `RouteGuide` server in [grpc-common/cpp/route_guide/route_guide_server.cc](https://github.com/grpc/grpc-common/blob/master/cpp/route_guide/route_guide_server.cc). Let's take a closer look at how it works.
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000127
Lisa Carey14184fa2015-02-24 16:56:30 +0000128### Implementing RouteGuide
129
130As you can see, our server has a `RouteGuideImpl` class that implements the generated `RouteGuide::Service` interface:
131
132```cpp
133class RouteGuideImpl final : public RouteGuide::Service {
134...
135}
136```
Lisa Careyfea91522015-02-24 18:07:45 +0000137In this case we're implementing the *synchronous* version of `RouteGuide`, which provides our default gRPC server behaviour. It's also possible to implement an asynchronous interface, `RouteGuide::AsyncService`, which allows you to further customize your server's threading behaviour, though we won't look at this in this tutorial.
Lisa Carey14184fa2015-02-24 16:56:30 +0000138
139`RouteGuideImpl` implements all our service methods. Let's look at the simplest type first, `GetFeature`, which just gets a `Point` from the client and returns the corresponding feature information from its database in a `Feature`.
140
141```cpp
142 Status GetFeature(ServerContext* context, const Point* point,
143 Feature* feature) override {
144 feature->set_name(GetFeatureName(*point, feature_list_));
145 feature->mutable_location()->CopyFrom(*point);
146 return Status::OK;
147 }
148```
149
150The method is passed a context object for the RPC, the client's `Point` protocol buffer request, and a `Feature` protocol buffer to fill in with the response information. In the method we populate the `Feature` with the appropriate information, and then `return` with an `OK` status to tell gRPC that we've finished dealing with the RPC and that the `Feature` can be returned to the client.
151
152Now let's look at something a bit more complicated - a streaming RPC. `ListFeatures` is a server-side streaming RPC, so we need to send back multiple `Feature`s to our client.
153
154```cpp
155 Status ListFeatures(ServerContext* context, const Rectangle* rectangle,
156 ServerWriter<Feature>* writer) override {
157 auto lo = rectangle->lo();
158 auto hi = rectangle->hi();
159 long left = std::min(lo.longitude(), hi.longitude());
160 long right = std::max(lo.longitude(), hi.longitude());
161 long top = std::max(lo.latitude(), hi.latitude());
162 long bottom = std::min(lo.latitude(), hi.latitude());
163 for (const Feature& f : feature_list_) {
164 if (f.location().longitude() >= left &&
165 f.location().longitude() <= right &&
166 f.location().latitude() >= bottom &&
167 f.location().latitude() <= top) {
168 writer->Write(f);
169 }
170 }
171 return Status::OK;
172 }
173```
174
175As you can see, instead of getting simple request and response objects in our method parameters, this time we get a request object (the `Rectangle` in which our client wants to find `Feature`s) and a special `ServerWriter` object. In the method, we populate as many `Feature` objects as we need to return, writing them to the `ServerWriter` using its `Write()` method. Finally, as in our simple RPC, we `return Status::OK` to tell gRPC that we've finished writing responses.
176
177If you look at the client-side streaming method `RecordRoute` you'll see it's quite similar, except this time we get a `ServerReader` instead of a request object and a single response. We use the `ServerReader`s `Read()` method to repeatedly read in our client's requests to a request object (in this case a `Point`) until there are no more messages: the server needs to check the return value of `Read()` after each call. If `true`, the stream is still good and it can continue reading; if `false` the message stream has ended.
178
179```cpp
180while (stream->Read(&point)) {
181 ...//process client input
182}
183```
184Finally, let's look at our bidirectional streaming RPC `RouteChat()`.
185
186```cpp
187 Status RouteChat(ServerContext* context,
188 ServerReaderWriter<RouteNote, RouteNote>* stream) override {
189 std::vector<RouteNote> received_notes;
190 RouteNote note;
191 while (stream->Read(&note)) {
192 for (const RouteNote& n : received_notes) {
193 if (n.location().latitude() == note.location().latitude() &&
194 n.location().longitude() == note.location().longitude()) {
195 stream->Write(n);
196 }
197 }
198 received_notes.push_back(note);
199 }
200
201 return Status::OK;
202 }
203```
204
205This time we get a `ServerReaderWriter` that can be used to read *and* write messages. The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently.
206
207### Starting the server
208
209Once we've implemented all our methods, we also need to start up a gRPC server so that clients can actually use our service. The following snippet shows how we do this for our `RouteGuide` service:
210
211```cpp
212void RunServer(const std::string& db_path) {
213 std::string server_address("0.0.0.0:50051");
214 RouteGuideImpl service(db_path);
215
216 ServerBuilder builder;
217 builder.AddPort(server_address);
218 builder.RegisterService(&service);
219 std::unique_ptr<Server> server(builder.BuildAndStart());
220 std::cout << "Server listening on " << server_address << std::endl;
Yang Gao44e98222015-02-24 16:06:02 -0800221 server->Wait();
Lisa Carey14184fa2015-02-24 16:56:30 +0000222}
223```
224As you can see, we build and start our server using a `ServerBuilder`. To do this, we:
225
2261. Create an instance of our service implementation class `RouteGuideImpl`.
2272. Create an instance of the factory `ServerBuilder` class.
2283. Specify the address and port we want to use to listen for client requests using the builder's `AddPort()` method.
2294. Register our service implementation with the builder.
2305. Call `BuildAndStart()` on the builder to create and start an RPC server for our service.
Yang Gao44e98222015-02-24 16:06:02 -08002315. Call `Wait()` on the server to do a blocking wait until process is killed or `Shutdown()` is called.
Lisa Carey14184fa2015-02-24 16:56:30 +0000232
233<a name="client"></a>
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000234## Creating the client
235
Lisa Careyf0db7b72015-02-24 18:10:40 +0000236In this section, we'll look at creating a C++ client for our `RouteGuide` service. You can see our complete example client code in [grpc-common/cpp/route_guide/route_guide_client.cc](https://github.com/grpc/grpc-common/blob/master/cpp/route_guide/route_guide_client.cc).
Lisa Carey14184fa2015-02-24 16:56:30 +0000237
238### Creating a stub
239
240To call service methods, we first need to create a *stub*.
241
242First we need to create a gRPC *channel* for our stub, specifying the server address and port we want to connect to and any special channel arguments - in our case we'll use the default `ChannelArguments`:
243
244```cpp
245grpc::CreateChannelDeprecated("localhost:50051", ChannelArguments());
246```
247
248Now we can use the channel to create our stub using the `NewStub` method provided in the `RouteGuide` class we generated from our .proto.
249
250```cpp
251 public:
252 RouteGuideClient(std::shared_ptr<ChannelInterface> channel,
253 const std::string& db)
254 : stub_(RouteGuide::NewStub(channel)) {
255 ...
256 }
257```
258
259### Calling service methods
260
Lisa Careyfea91522015-02-24 18:07:45 +0000261Now let's look at how we call our service methods. Note that in this tutorial we're calling the *blocking/synchronous* versions of each method: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception.
262
Lisa Carey88a49f62015-02-24 18:09:38 +0000263#### Simple RPC
264
Lisa Carey14184fa2015-02-24 16:56:30 +0000265Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local method.
266
267```cpp
268 Point point;
269 Feature feature;
270 point = MakePoint(409146138, -746188906);
271 GetOneFeature(point, &feature);
272
273...
274
275 bool GetOneFeature(const Point& point, Feature* feature) {
276 ClientContext context;
277 Status status = stub_->GetFeature(&context, point, feature);
278 ...
279 }
280```
281
Lisa Careyfea91522015-02-24 18:07:45 +0000282As you can see, we create and populate a request protocol buffer object (in our case `Point`), and create a response protocol buffer object for the server to fill in. We also create a `ClientContext` object for our call - you can optionally set RPC configuration values on this object, such as deadlines, though for now we'll use the default settings. Note that you cannot reuse this object between calls. Finally, we call the method on the stub, passing it the context, request, and response. If the method returns `OK`, then we can read the response information from the server from our response object.
Lisa Carey14184fa2015-02-24 16:56:30 +0000283
284```cpp
285 std::cout << "Found feature called " << feature->name() << " at "
286 << feature->location().latitude()/kCoordFactor_ << ", "
287 << feature->location().longitude()/kCoordFactor_ << std::endl;
288```
289
Lisa Carey88a49f62015-02-24 18:09:38 +0000290#### Streaming RPCs
291
Lisa Careyfea91522015-02-24 18:07:45 +0000292Now let's look at our streaming methods. If you've already read [Creating the server](#server) some of this may look very familiar - streaming RPCs are implemented in a similar way on both sides. Here's where we call the server-side streaming method `ListFeatures`, which returns a stream of geographical `Feature`s:
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000293
Lisa Careyfea91522015-02-24 18:07:45 +0000294```cpp
295 std::unique_ptr<ClientReader<Feature> > reader(
296 stub_->ListFeatures(&context, rect));
297 while (reader->Read(&feature)) {
298 std::cout << "Found feature called "
299 << feature.name() << " at "
300 << feature.location().latitude()/kCoordFactor_ << ", "
301 << feature.location().latitude()/kCoordFactor_ << std::endl;
302 }
303 Status status = reader->Finish();
304```
305
306Instead of passing the method a context, request, and response, we pass it a context and request and get a `ClientReader` object back. The client can use the `ClientReader` to read the server's responses. We use the `ClientReader`s `Read()` method to repeatedly read in the server's responses to a response protocol buffer object (in this case a `Feature`) until there are no more messages: the client needs to check the return value of `Read()` after each call. If `true`, the stream is still good and it can continue reading; if `false` the message stream has ended. Finally, we call `Finish()` on the stream to complete the call and get our RPC status.
307
308The client-side streaming method `RecordRoute` is similar, except there we pass the method a context and response object and get back a `ClientWriter`.
309
310```cpp
311 std::unique_ptr<ClientWriter<Point> > writer(
312 stub_->RecordRoute(&context, &stats));
313 for (int i = 0; i < kPoints; i++) {
314 const Feature& f = feature_list_[feature_distribution(generator)];
315 std::cout << "Visiting point "
316 << f.location().latitude()/kCoordFactor_ << ", "
317 << f.location().longitude()/kCoordFactor_ << std::endl;
318 if (!writer->Write(f.location())) {
319 // Broken stream.
320 break;
321 }
322 std::this_thread::sleep_for(std::chrono::milliseconds(
323 delay_distribution(generator)));
324 }
325 writer->WritesDone();
326 Status status = writer->Finish();
327 if (status.IsOk()) {
328 std::cout << "Finished trip with " << stats.point_count() << " points\n"
329 << "Passed " << stats.feature_count() << " features\n"
330 << "Travelled " << stats.distance() << " meters\n"
331 << "It took " << stats.elapsed_time() << " seconds"
332 << std::endl;
333 } else {
334 std::cout << "RecordRoute rpc failed." << std::endl;
335 }
336```
337
338Once we've finished writing our client's requests to the stream using `Write()`, we need to call `WritesDone()` on the stream to let gRPC know that we've finished writing, then `Finish()` to complete the call and get our RPC status. If the status is `OK`, our response object that we initially passed to `RecordRoute()` will be populated with the server's response.
339
340Finally, let's look at our bidirectional streaming RPC `RouteChat()`. In this case, we just pass a context to the method and get back a `ClientReaderWriter`, which we can use to both write and read messages.
341
342```cpp
343 std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
344 stub_->RouteChat(&context));
345```
346
347The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently.
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000348
Lisa Carey14184fa2015-02-24 16:56:30 +0000349## Try it out!
350
Yang Gao9a2ff4f2015-02-24 16:13:02 -0800351Build client and server:
352```shell
353$ make
354```
355Run the server, which will listen on port 50051:
356```shell
357$ ./route_guide_server
358```
359Run the client (in a different terminal):
360```shell
361$ ./route_guide_client
362```
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000363