• Home
Name
Date
Size
#Lines
LOC

..--

MakefileD12-May-20243.2 KiB11179

README.mdD12-May-20242.4 KiB8558

greeter_client.ccD12-May-20242.8 KiB9448

greeter_server.ccD12-May-20242.4 KiB7740

README.md

1# gRPC C++ Message Compression Tutorial
2
3### Prerequisite
4Make sure you have run the [hello world example](../helloworld) or understood the basics of gRPC. We will not dive into the details that have been discussed in the hello world example.
5
6### Get the tutorial source code
7
8The example code for this and our other examples lives in the `examples` directory. Clone this repository at the [latest stable release tag](https://github.com/grpc/grpc/releases) to your local machine  by running the following command:
9
10
11```sh
12$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
13```
14
15Change your current directory to examples/cpp/compression
16
17```sh
18$ cd examples/cpp/compression/
19```
20
21### Generating gRPC code
22
23To generate the client and server side interfaces:
24
25```sh
26$ make helloworld.grpc.pb.cc helloworld.pb.cc
27```
28Which internally invokes the proto-compiler as:
29
30```sh
31$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
32$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
33```
34
35### Writing a client and a server
36
37The client and the server can be based on the hello world example.
38
39Additionally, we can configure the compression settings.
40
41In the client, set the default compression algorithm of the channel via the channel arg.
42
43```cpp
44  ChannelArguments args;
45  // Set the default compression algorithm for the channel.
46  args.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP);
47  GreeterClient greeter(grpc::CreateCustomChannel(
48      "localhost:50051", grpc::InsecureChannelCredentials(), args));
49```
50
51Each call's compression configuration can be overwritten by client context.
52
53```cpp
54    // Overwrite the call's compression algorithm to DEFLATE.
55    context.set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
56```
57
58In the server, set the default compression algorithm via the server builder.
59
60```cpp
61  ServerBuilder builder;
62  // Set the default compression algorithm for the server.
63  builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_GZIP);
64```
65
66Each call's compression configuration can be overwritten by server context.
67
68```cpp
69    // Overwrite the call's compression algorithm to DEFLATE.
70    context->set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
71```
72
73For a working example, refer to [greeter_client.cc](greeter_client.cc) and [greeter_server.cc](greeter_server.cc).
74
75Build and run the (compressing) client and the server by the following commands.
76
77```sh
78make
79./greeter_server
80```
81
82```sh
83./greeter_client
84```
85