当前位置: 首页 > news >正文

ProviderRpc发送服务二将远程调用来的信息反序列化后调用服务方的方法,并将服务方的结果返回给发送方

 在Provider的实现中,OnMessage函数中,处理接收到的连接RPC请求。将接收到的RPC请求(包含请求的对象,请求方法和 请求参数),接收到这些信息之后进行反序列化。得到这些参数之后我们即将要做的事情是去调用相应的函数。怎么调用呢???

我们调用的方法名字如下,方法参数如下,需要根据发送来的信息找到该函数。

  void Login(::google::protobuf::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done){// 框架给业务上报了请求参数LoginRequest,应用获取相应数据做本地业务std::string name = request->name();std::string pwd = request->pwd();// 做本地业务bool login_result = Login(name, pwd); // 把响应写入  包括错误码、错误消息、返回值fixbug::ResultCode *code = response->mutable_result();code->set_errcode(0);code->set_errmsg("");response->set_sucess(login_result);// 执行回调操作   执行响应对象数据的序列化和网络发送(都是由框架来完成的)done->Run();}

google::protobuf::Service *service = it->second.m_service; // 获取service对象  new UserServiceconst google::protobuf::MethodDescriptor *method = mit->second; // 获取method对象  Login// 生成rpc方法调用的请求request和响应response参数google::protobuf::Message *request = service->GetRequestPrototype(method).New();if (!request->ParseFromString(args_str)){std::cout << "request parse error, content:" << args_str << std::endl;return;}google::protobuf::Message *response = service->GetResponsePrototype(method).New();// 给下面的method方法的调用,绑定一个Closure的回调函数google::protobuf::Closure *done = google::protobuf::NewCallback<RpcProvider, const muduo::net::TcpConnectionPtr&, google::protobuf::Message*>(this, &RpcProvider::SendRpcResponse, conn, response);// 在框架上根据远端rpc请求,调用当前rpc节点上发布的方法// new UserService().Login(controller, request, response, done)service->CallMethod(method, nullptr, request, response, done);
}// Closure的回调操作,用于序列化rpc的响应和网络发送
void RpcProvider::SendRpcResponse(const muduo::net::TcpConnectionPtr& conn, google::protobuf::Message *response)
{std::string response_str;if (response->SerializeToString(&response_str)) // response进行序列化{// 序列化成功后,通过网络把rpc方法执行的结果发送会rpc的调用方conn->send(response_str);}else{std::cout << "serialize response_str error!" << std::endl; }conn->shutdown(); // 模拟http的短链接服务,由rpcprovider主动断开连接
}

 我们需要调用的函数如下:

  void Login(::google::protobuf::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done)

 首先获取调用的服务对象和方法:

 google::protobuf::Service *service = it->second.m_service; // 获取service对象  new UserService

    const google::protobuf::MethodDescriptor *method = mit->second; // 获取method对象  Login

 获取方法的参数:request和response,这些

google::protobuf::Message *request = service->GetRequestPrototype(method).New();

    if (!request->ParseFromString(args_str))

    {

        std::cout << "request parse error, content:" << args_str << std::endl;

        return;

    }

    google::protobuf::Message *response = service->GetResponsePrototype(method).New();

http://www.lryc.cn/news/416387.html

相关文章:

  • Io 35
  • java基础概念11-方法
  • 大模型应用中的思维树(Tree of Thought)是什么?
  • 学习记录(11):训练图片分类的算法
  • 上网防泄密,这些雷区不要碰!九招教你如何防泄密
  • 数据库篇--八股文学习第十五天| 一条SQL查询语句是如何执行的?,事务的四大特性有哪些?,数据库的事务隔离级别有哪些?
  • elk + filebeat + kafka实验和RSync同步
  • 子类到底能继承父类中的哪些内容?
  • 【超详细公式】曝光值(EV)、光圈(AV)、快门(TV)、感光度(SV)、照度(Lux)
  • 【Java】增强for遍历集合。
  • 【Qt】管理创建子项目
  • 力扣——238.移动零
  • 编程的魅力
  • 想提升跨境电商运营?浏览器多开为你助力!
  • 使用QML的ListView自制树形结构图TreeView
  • 2.MySQL面试题之索引
  • 复制CodeIgniter新版的array_group_by辅助函数
  • 合并两个 ES (Elasticsearch) 的数据
  • Linux网络协议.之 tcp,udp,socket网络编程(四).之网络转换函数htonl,ntohs等介绍
  • LXC和udev知识点
  • 基于springboot+vue+uniapp的智慧校园管理系统小程序
  • 论文辅导 | 基于概率密度估计与时序Transformer网络的风功率日前区间预测
  • 金蝶云星空单据体数量汇总-分组列信息
  • 树状数组基础知识以及相关习题
  • 2023大数据-架构师案例(八)
  • 【Python】Python:探索未来科技的风向标
  • Java语言程序设计——篇十一(6)
  • 2024年有哪些好用的文件加密软件?十款常用加密软件推荐
  • 书生大模型学习笔记3 - 书生开源大模型链路体系
  • 【竞技宝】奥运会:法国国奥淘汰埃及国奥晋级决赛