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

service UserServiceRpc //在test.proto中定义

{

rpc Login(LoginRequest)returns(LoginResponse);

rpc GetFriendLists(GetFriendListRequest)returns(GetFriendListResponse);

}

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

cpp 复制代码
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方法,将派生类对象传给基类指针从而调用派生类里的函数。

相关推荐
Sheffield2 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
YuMiao7 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
Jony_10 天前
高可用移动网络连接
网络协议
chilix10 天前
Linux 跨网段路由转发配置
网络协议
DianSan_ERP11 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅12 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo199812 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_9462055212 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel12 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)12 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库