CMake使用gRPC(Protobuf) 的c++ demo

gRPC的命令参数里,

1. 如果要用pacakge,需要--proto_path的参数,
例如helloworld.proto的绝对路径是
/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/helloworld.proto
在helloworld.proto里面的pacakge是
package grpc_demo.hello_world.proto;
那--proto_path的值就是/home/user/grpc_demo_ws
如果要生成带包名的DescriptorTable,--proto_path也是必须的


2. 如果pb.cc/pb.h/grpc.pb.cc/grpc.pb.h想要和proto放在同一个目录
--grpc_out 和 --cpp_out 也都是填入/home/user/grpc_demo_ws
最后实际生成的位置是加上package路径的/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/

下载代码

https://download.csdn.net/download/howard789/87966195

client向server发送一条数据包含自己的名字(helloworld.proto),还有另外两个proto的数据结构(catInfo.proto和dogInfo.proto),server收到后,转成一条string发回给client

目录结构

CMakeLists.txt

cmake_minimum_required(VERSION 3.19)
project(grpc_demo)

set(CMAKE_CXX_STANDARD 17)
# 加载 gRPC ---------------------------------
include(cmake/common.cmake)
MESSAGE("LIB _REFLECTION :" ${_REFLECTION})
MESSAGE("LIB _GRPC_GRPCPP:" ${_GRPC_GRPCPP})
MESSAGE("LIB _PROTOBUF_LIBPROTOBUF:" ${_PROTOBUF_LIBPROTOBUF})
include_directories(/home/hao/.local/include)
# --------------------------------------------
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/grpc_demo)

#---------------------------------------------------------------------
set(PROTO_WS_DIR ${CMAKE_SOURCE_DIR})
MESSAGE("PROTO_WS_DIR:" ${PROTO_WS_DIR})




##  Cat_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/cat/proto/cat.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)

##  Dog_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/dog/proto/dog.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)

##  HelloWorld_PROTO--------------------------------------------
set(PROTO_ABSOLUTE_PATH ${PROTO_WS_DIR}/grpc_demo/hello_world/proto/helloworld.proto)
set(Regenerate_Proto ON)
include(cmake/common_generate_files.cmake)

find_package(Threads REQUIRED)

add_executable(greeter_client grpc_demo/hello_world/greeter_client.cc)
target_link_libraries(greeter_client
        helloworld_grpc_proto
        dog_grpc_proto
        cat_grpc_proto
        absl::flags
        absl::flags_parse
        ${_REFLECTION}
        ${_GRPC_GRPCPP}
        ${_PROTOBUF_LIBPROTOBUF})

add_executable(greeter_server grpc_demo/hello_world/greeter_server.cc)
target_link_libraries(greeter_server
        helloworld_grpc_proto
        dog_grpc_proto
        cat_grpc_proto
        absl::flags
        absl::flags_parse
        ${_REFLECTION}
        ${_GRPC_GRPCPP}
        ${_PROTOBUF_LIBPROTOBUF})

common.cmake

# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ route_guide example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building route_guide.

cmake_minimum_required(VERSION 3.8)

if(MSVC)
  add_definitions(-D_WIN32_WINNT=0x600)
endif()

find_package(Threads REQUIRED)

if(GRPC_AS_SUBMODULE)
  # One way to build a projects that uses gRPC is to just include the
  # entire gRPC project tree via "add_subdirectory".
  # This approach is very simple to use, but the are some potential
  # disadvantages:
  # * it includes gRPC's CMakeLists.txt directly into your build script
  #   without and that can make gRPC's internal setting interfere with your
  #   own build.
  # * depending on what's installed on your system, the contents of submodules
  #   in gRPC's third_party/* might need to be available (and there might be
  #   additional prerequisites required to build them). Consider using
  #   the gRPC_*_PROVIDER options to fine-tune the expected behavior.
  #
  # A more robust approach to add dependency on gRPC is using
  # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).

  # Include the gRPC's cmake build (normally grpc source code would live
  # in a git submodule called "third_party/grpc", but this example lives in
  # the same repository as gRPC sources, so we just look a few directories up)
  add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
  message(STATUS "Using gRPC via add_subdirectory.")

  # After using add_subdirectory, we can now use the grpc targets directly from
  # this build.
  set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  set(_REFLECTION grpc++_reflection)
  if(CMAKE_CROSSCOMPILING)
    find_program(_PROTOBUF_PROTOC protoc)
  else()
    set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  endif()
  set(_GRPC_GRPCPP grpc++)
  if(CMAKE_CROSSCOMPILING)
    find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  else()
    set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  endif()
elseif(GRPC_FETCHCONTENT)
  # Another way is to use CMake's FetchContent module to clone gRPC at
  # configure time. This makes gRPC's source code available to your project,
  # similar to a git submodule.
  message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
  include(FetchContent)
  FetchContent_Declare(
    grpc
    GIT_REPOSITORY https://github.com/grpc/grpc.git
    # when using gRPC, you will actually set this to an existing tag, such as
    # v1.25.0, v1.26.0 etc..
    # For the purpose of testing, we override the tag used to the commit
    # that's currently under test.
    GIT_TAG        vGRPC_TAG_VERSION_OF_YOUR_CHOICE)
  FetchContent_MakeAvailable(grpc)

  # Since FetchContent uses add_subdirectory under the hood, we can use
  # the grpc targets directly from this build.
  set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  set(_REFLECTION grpc++_reflection)
  set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
  set(_GRPC_GRPCPP grpc++)
  if(CMAKE_CROSSCOMPILING)
    find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  else()
    set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  endif()
else()
  # This branch assumes that gRPC and all its dependencies are already installed
  # on this system, so they can be located by find_package().
  message(STATUS "gRPC and all its dependencies should  be able to located by find_package().")

  # Find Protobuf installation
  # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
  option(protobuf_MODULE_COMPATIBLE TRUE)
  find_package(Protobuf CONFIG REQUIRED)
  message(STATUS "Using protobuf ${Protobuf_VERSION}")

  set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
  set(_REFLECTION gRPC::grpc++_reflection)

  message(STATUS "CMAKE_CROSSCOMPILING: ${CMAKE_CROSSCOMPILING}")

  if(CMAKE_CROSSCOMPILING)
    find_program(_PROTOBUF_PROTOC protoc)
  else()
    set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  endif()

  # Find gRPC installation
  # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
  find_package(gRPC CONFIG REQUIRED)
  message(STATUS "Using gRPC ${gRPC_VERSION}")

  set(_GRPC_GRPCPP gRPC::grpc++)
  if(CMAKE_CROSSCOMPILING)
    find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  else()
    set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
  endif()





endif()

common_generate_files.cmake

#需要的参数
#PROTO_WS_DIR #工作目录的路径,就是proto档案的package上一级目录。这里是/home/user/grpc_demo_ws
#PROTO_ABSOLUTE_PATH #proto档案的绝对路径 例如/home/user/grpc_demo_ws/grpc_demo/hello_world/proto/helloworld.proto
#Regenerate_Proto #是否重新生成档案 pb.cc pb.h


get_filename_component(PROTO_NAME "${PROTO_ABSOLUTE_PATH}" NAME_WE)
get_filename_component(PROTO_SOURCE_DIR "${PROTO_ABSOLUTE_PATH}" PATH)
file(RELATIVE_PATH PROTO_PKG_DIR ${PROTO_WS_DIR} ${PROTO_SOURCE_DIR})

# Proto file
MESSAGE("PROTO_ABSOLUTE_PATH:" ${PROTO_ABSOLUTE_PATH})
MESSAGE("PROTO_WS_DIR:" ${PROTO_WS_DIR})
MESSAGE("PROTO_PKG_DIR:" ${PROTO_PKG_DIR})
MESSAGE("PROTO_NAME:" ${PROTO_NAME})



# Generated sources
set(_proto_srcs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.pb.cc") #输出档案的绝对路径
set(_proto_hdrs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.pb.h")
set(_grpc_srcs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.grpc.pb.cc")
set(_grpc_hdrs "${PROTO_WS_DIR}/${PROTO_PKG_DIR}/${PROTO_NAME}.grpc.pb.h")


if(Regenerate_Proto)
    MESSAGE("Regenerate_Proto" ${PROTO_NAME}  " = ON")
    add_custom_command(
            OUTPUT "${_proto_srcs}" "${_proto_hdrs}" "${_grpc_srcs}" "${_grpc_hdrs}"   #输出档案的绝对路径
            COMMAND ${_PROTOBUF_PROTOC}
            ARGS
            --proto_path "${PROTO_WS_DIR}"
            --grpc_out "${PROTO_WS_DIR}"
            --cpp_out "${PROTO_WS_DIR}"
            -I "${PROTO_WS_DIR}/${PROTO_PKG_DIR}"
            --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
            "${PROTO_ABSOLUTE_PATH}"
            DEPENDS "${PROTO_ABSOLUTE_PATH}")
endif()


# Include generated *.pb.h files
include_directories("${PROTO_WS_DIR}/${PROTO_PKG_DIR}")


set(LIB_NAME ${PROTO_NAME}_grpc_proto)
MESSAGE(" ********************************* ${PROTO_NAME} 生成的 LIB_NAME名称 = ${LIB_NAME}")
# hw_grpc_proto
add_library(${LIB_NAME}
        ${_grpc_srcs}
        ${_grpc_hdrs}
        ${_proto_srcs}
        ${_proto_hdrs})
target_link_libraries(${LIB_NAME}
        ${_REFLECTION}
        ${_GRPC_GRPCPP}
        ${_PROTOBUF_LIBPROTOBUF})

cat.proto

syntax = "proto3";

package grpc_demo.cat.proto;

message CatInfo{
  string name = 1;
  bool isMale = 2;
}

dog.proto


syntax = "proto3";

package grpc_demo.dog.proto;

message DogInfo{
  string name = 1;
  int32 age = 2;
}

helloworld.proto

// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package grpc_demo.hello_world.proto;

import "grpc_demo/cat/proto/cat.proto";
import "grpc_demo/dog/proto/dog.proto";



// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}

  rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
  grpc_demo.cat.proto.CatInfo catInfo=2;
  grpc_demo.dog.proto.DogInfo dogInfo=3;

}

// The response message containing the greetings
message HelloReply {
  string message = 1;

}

greeter_client.cc


#include <iostream>
#include <memory>
#include <string>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include <grpcpp/grpcpp.h>
#include "helloworld.grpc.pb.h"


ABSL_FLAG(std::string, target, "localhost:50051", "Server address");

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using grpc_demo::hello_world::proto::Greeter;
using grpc_demo::hello_world::proto::HelloReply;
using grpc_demo::hello_world::proto::HelloRequest;

class GreeterClient {
 public:
  GreeterClient(std::shared_ptr<Channel> channel)
      : stub_(Greeter::NewStub(channel)) {}

  // Assembles the client's payload, sends it and presents the response back
  // from the server.
  std::string SayHello(const std::string& user,grpc_demo::cat::proto::CatInfo* catinfo,grpc_demo::dog::proto::DogInfo* doginfo) {
    // Data we are sending to the server.
    HelloRequest request;
    request.set_name(user);



    request.set_allocated_catinfo(catinfo);
    request.set_allocated_doginfo(doginfo);



    // Container for the data we expect from the server.
    HelloReply reply;

    // Context for the client. It could be used to convey extra information to
    // the server and/or tweak certain RPC behaviors.
    ClientContext context;

    // The actual RPC.
    Status status = stub_->SayHello(&context, request, &reply);

    // Act upon its status.
    if (status.ok()) {
      return reply.message();
    } else {
      std::cout << status.error_code() << ": " << status.error_message()
                << std::endl;
      return "RPC failed";
    }
  }

 private:
  std::unique_ptr<Greeter::Stub> stub_;
};

int main(int argc, char** argv) {
  absl::ParseCommandLine(argc, argv);
  // Instantiate the client. It requires a channel, out of which the actual RPCs
  // are created. This channel models a connection to an endpoint specified by
  // the argument "--target=" which is the only expected argument.
  std::string target_str = absl::GetFlag(FLAGS_target);
  // We indicate that the channel isn't authenticated (use of
  // InsecureChannelCredentials()).
  GreeterClient greeter(
      grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  std::string user("铲屎官John");
    grpc_demo::cat::proto::CatInfo* catinfo=new grpc_demo::cat::proto::CatInfo();
    catinfo->set_ismale(false);
    catinfo->set_name("mimi");

    grpc_demo::dog::proto::DogInfo* doginfo=new grpc_demo::dog::proto::DogInfo();
    doginfo->set_name("happy");
    doginfo->set_age(12);
  std::string reply = greeter.SayHello(user,catinfo,doginfo);
  std::cout << "client收到server回复: " << reply << std::endl;

  return 0;
}

greeter_server.cc

/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <iostream>
#include <memory>
#include <string>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/str_format.h"

#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>

#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using grpc_demo::hello_world::proto::Greeter;
using grpc_demo::hello_world::proto::HelloReply;
using grpc_demo::hello_world::proto::HelloRequest;

ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");

// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {

      std::cout << "收到client的消息 "<< std::endl;


      std::string prefix("你好 ");

    reply->set_message(prefix + request->name()
    +",Server收到 CatInfo:"+ request->catinfo().name()
    +"," + std::to_string(request->catinfo().ismale())
    +",DogInfo:"+ request->doginfo().name()
    +"," + std::to_string(request->doginfo().age())
    );
    return Status::OK;
  }
};

void RunServer(uint16_t port) {
  std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
  GreeterServiceImpl service;

  grpc::EnableDefaultHealthCheckService(true);
  grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  ServerBuilder builder;
  // Listen on the given address without any authentication mechanism.
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  // Register "service" as the instance through which we'll communicate with
  // clients. In this case it corresponds to an *synchronous* service.
  builder.RegisterService(&service);
  // Finally assemble the server.
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "Server listening on " << server_address << std::endl;

  // Wait for the server to shutdown. Note that some other thread must be
  // responsible for shutting down the server for this call to ever return.
  server->Wait();
}

int main(int argc, char** argv) {
  absl::ParseCommandLine(argc, argv);
  RunServer(absl::GetFlag(FLAGS_port));
  return 0;
}

其余*.pb.cc *.pb.h 是用cmake生成的

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值