blob: ade134c86b4b0601a974e660aa7f2bd51828c584 [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
19[possibly insert more advantages here]
20
21## Example code and setup
22
23The example code for our tutorial is in [grpc/grpc-common/cpp/route_guide](https://github.com/grpc/grpc-common/cpp/route_guide). To download the example, clone the `grpc-common` repository by running the following command:
24```shell
25$ git clone https://github.com/google/grpc-common.git
26```
27
28Then change your current directory to `grpc-common/cpp/route_guide`:
29```shell
30$ cd grpc-common/cpp/route_guide
31```
32
33Although 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](https://github.com/grpc/grpc/blob/master/INSTALL).
34
35
36## Defining the service
37
38Our 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).
39
40To define a service, you specify a named `service` in your .proto file:
41
42```
43service RouteGuide {
44 ...
45}
46```
47
48Then 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:
49
50- 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.
51```
52 // Obtains the feature at a given position.
53 rpc GetFeature(Point) returns (Feature) {}
54```
55
Lisa Carey450d1122015-02-23 16:05:25 +000056- 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 +000057```
58 // Obtains the Features available within the given Rectangle. Results are
59 // streamed rather than returned at once (e.g. in a response message with a
60 // repeated field), as the rectangle may cover a large area and contain a
61 // huge number of features.
62 rpc ListFeatures(Rectangle) returns (stream Feature) {}
63```
64
Lisa Carey450d1122015-02-23 16:05:25 +000065- 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 +000066```
67 // Accepts a stream of Points on a route being traversed, returning a
68 // RouteSummary when traversal is completed.
69 rpc RecordRoute(stream Point) returns (RouteSummary) {}
70```
71
Lisa Carey450d1122015-02-23 16:05:25 +000072- 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 +000073```
Lisa Carey450d1122015-02-23 16:05:25 +000074 // Accepts a stream of RouteNotes sent while a route is being traversed,
75 // while receiving other RouteNotes (e.g. from other users).
76 rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
Lisa Carey54e0c6d2015-02-23 16:00:38 +000077```
78
79Our .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:
80```
Lisa Carey450d1122015-02-23 16:05:25 +000081// Points are represented as latitude-longitude pairs in the E7 representation
82// (degrees multiplied by 10**7 and rounded to the nearest integer).
83// Latitudes should be in the range +/- 90 degrees and longitude should be in
84// the range +/- 180 degrees (inclusive).
85message Point {
86 int32 latitude = 1;
87 int32 longitude = 2;
88}
Lisa Carey54e0c6d2015-02-23 16:00:38 +000089```
90
91
92## Generating client and server code
93
Lisa Carey7a219662015-02-23 16:42:21 +000094Next 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.
95
96For 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 followed the [installation instructions](https://github.com/grpc/grpc/blob/master/INSTALL) first):
97
98```shell
99$ make route_guide.pb.cc
100```
101
102which actually runs:
103
104[actual command]
105
106Running this command generates the following files:
107- `route_guide.pb.h`
108- `route_guide.pb.cc`
109
110These contain
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000111
112
113## Creating the server
114
Lisa Carey7a219662015-02-23 16:42:21 +0000115First let's look at how we create a `RouteGuide` server.
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000116
117There are two parts to making our `RouteGuide` service work:
Lisa Carey7a219662015-02-23 16:42:21 +0000118- Implementing the service interface generated from our service definition: doing the actual "work" of our service.
119- Running a gRPC server to listen for requests from clients and return the service responses
Lisa Carey54e0c6d2015-02-23 16:00:38 +0000120
121
122## Creating the client
123
124
125
126
127
128