在protobuf里定义描述rpc方法的类型

发布于:2024-04-25 ⋅ 阅读:(26) ⋅ 点赞:(0)

service  UserServiceRpc     //在test.proto中定义
{
    rpc Login(LoginRequest)returns(LoginResponse);
    rpc GetFriendLists(GetFriendListRequest)returns(GetFriendListResponse);
}

  test.proto文件生成test.pb.cc      protoc  test.proto  --cpp_out=./    将生成的文件放到 ./ 目录下,截取一部分如下

class UserServiceRpc_Stub;

class UserServiceRpc : public ::PROTOBUF_NAMESPACE_ID::Service {
 protected:
  // This class should be treated as an abstract interface.
  inline UserServiceRpc() {};
 public:
  virtual ~UserServiceRpc();

  typedef UserServiceRpc_Stub Stub;

  static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* descriptor();

  virtual void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,
                       const ::fixbug::LoginRequest* request,
                       ::fixbug::LoginResponse* response,
                       ::google::protobuf::Closure* done);
  virtual void GetFriendLists(::PROTOBUF_NAMESPACE_ID::RpcController* controller,
                       const ::fixbug::GetFriendListRequest* request,
                       ::fixbug::GetFriendListResponse* response,
                       ::google::protobuf::Closure* done);

调用关系如图所示:

在test.proto中的LoginRequest类继承于protobuf的Message类,UserServiceRpc继承于protobuf的service类中。service类中包含两个虚函数即在UserServiceRpc中的两个方法,UserServiceRpc_Stub继承UserServiceRpc,此类多了RpcChannel指针,方法将会通过Rpcchannel指针调用CallMethod函数,我们将用自己写的类继承Rpcchannel,重写其中的CallMethod方法,将派生类对象传给基类指针从而调用派生类里的函数。


网站公告

今日签到

点亮在社区的每一天
去签到