编译、运行此项目代码需要预安装 Protocol Buffers 和 gRPC。
-
Protocol Buffers 源码编译安装指南:Installation Guide of Protocol Buffers
-
Protocol Buffers 编程使用指南:Usage Guide of Protocol Buffers
-
gRPC 源码编译安装指南:Installation Guide of gRPC
-
gRPC 编程使用指南:Usage Guide of gRPC
执行下述命令编译此项目:
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
执行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
}
此项目的工作时序如下:
-
客户端发送给服务端 序列化 的请求数据,接收服务端返回的 序列化 的应答数据,并 反序列化 为一个 C++ 中的对象,进一步处理。
-
服务端接收客户端发送的 序列化 的请求数据,反序列化 为一个 C++ 中的对象,进一步处理后,返回一个 序列化 的应答数据。
-
其中数据序列化均方式为 Protocol Buffers,通信框架为 gRPC。
此项目的代码结构如下:
.
├── 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
-
src/client:RPC 客户端源代码目录
-
src/server:RPC 服务端源代码目录
-
src/proto:
.proto
源文件目录 -
src/proto/gen:
.proto
生成的 C++ 源代码目录 -
tools/cmake:自定义 CMake 工具目录
-
tools/cmake/use_proto.cmake:编译
.proto
文件生成 C++ 源文件(.pb.cc
、.pb.h
) -
tools/cmake/use_grpc.cmake:编译
.proto
文件生成 C++ 源文件(.grpc.pb.cc
、grpc.pb.h
)
-
在你的 CMakeLists.txt
中引入 use_proto.cmake,设置.proto
源文件目录proto_in_dir
和生成 C++ 文件目录proto_out_dir
,之后项目将增添了一个名为proto_2_cxx
的 target。
可以参考 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_cxx
的 target:
/usr/bin/camke --build . --target proto_2_cxx -- -j 16