Skip to content

Demo and guide of using Protocol Buffers and gRPC for C/C++ programer.

Notifications You must be signed in to change notification settings

duruyao/proto-grpc-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo of Using Protocol Buffers and gRPC by C++

1. Rre-requisites

编译、运行此项目代码需要预安装 Protocol BuffersgRPC

1.1. Protocol Buffers

1.2. gRPC

2. Demo Project

2.1. Compiling Source

执行下述命令编译此项目:

mkdir -p build && rm -rf build/* &&                             \
pushd build && /usr/bin/cmake .. &&                             \
/usr/bin/cmake --build . --target clean -- -j 16 &&             \
/usr/bin/cmake --build . --target proto_2_cxx -- -j 16 &&       \
/usr/bin/cmake --build . --target grpc_2_cxx -- -j 16 &&        \
/usr/bin/cmake --build . --target rpc_server_demo -- -j 16 &&   \
/usr/bin/cmake --build . --target rpc_client_demo -- -j 16 &&   \
popd

2.2. Run Demo

执行build/src/server/rpc_server_demo <IPV4:PORT>启动 RPC 服务端:

+-------------------------------+
| RPC-SERVER-DEMO               |
| date:   2021.05.24            |
| author: [email protected] |
+-------------------------------+

RPC Server listen on IPV4:PORT

执行build/src/client/rpc_client_demo <IPV4:PORT>启动 RPC 客户端:

+-------------------------------+
| RPC-CLIENT-DEMO               |
| date:   2021.05.24            |
| author: [email protected] |
+-------------------------------+

RPC Client connect to IPV4:PORT

---> [Point]
latitude: 302131223
longitude: 1202193223

<--- [Feature]
name: "point_2"
location {
  latitude: 302131223
  longitude: 1202193223
}

2.3. Working Sequence

此项目的工作时序如下:

  • 客户端发送给服务端 序列化 的请求数据,接收服务端返回的 序列化 的应答数据,并 反序列化 为一个 C++ 中的对象,进一步处理。

  • 服务端接收客户端发送的 序列化 的请求数据,反序列化 为一个 C++ 中的对象,进一步处理后,返回一个 序列化 的应答数据。

  • 其中数据序列化均方式为 Protocol Buffers,通信框架为 gRPC

2.4. Code Structure

此项目的代码结构如下:

.
├── CMakeLists.txt
├── src
│   ├── CMakeLists.txt
│   ├── client
│   │   ├── CMakeLists.txt
│   │   ├── demo.cpp
│   │   ├── rpc_client.cpp 
│   │   └── rpc_client.h
│   ├── proto
│   │   ├── gen
│   │   │   ├── route_guide.grpc.pb.cc
│   │   │   ├── route_guide.grpc.pb.h
│   │   │   ├── route_guide.pb.cc
│   │   │   └── route_guide.pb.h
│   │   └── route_guide.proto
│   └── server
│       ├── CMakeLists.txt
│       ├── demo.cpp
│       ├── rpc_server.cpp
│       ├── rpc_server.h
│       ├── rpc_service.cpp
│       └── rpc_service.h
└── tools
    └── cmake
        ├── use_absl.cmake
        ├── use_grpc.cmake
        ├── use_hiksdk.cmake
        └── use_proto.cmake

2.5 Tips

在你的 CMakeLists.txt 中引入 use_proto.cmake,设置.proto源文件目录proto_in_dir和生成 C++ 文件目录proto_out_dir,之后项目将增添了一个名为proto_2_cxxtarget

可以参考 CMakeLists.txt

set(proto_in_dir ${PROJECT_SOURCE_DIR}/src/proto)           ## contains of .proto files
set(proto_out_dir ${PROJECT_SOURCE_DIR}/src/proto/gen)      ## contains of .cc, .h files
include(${PROJECT_SOURCE_DIR}/tools/cmake/use_proto.cmake)

编译名为proto_2_cxxtarget

/usr/bin/camke --build . --target proto_2_cxx -- -j 16

About

Demo and guide of using Protocol Buffers and gRPC for C/C++ programer.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published