RpcController作用浅析

RpcController作用浅析

前面提到了RpcConsumer的实现思路,但是并没说明RpcController有什么作用,不妨看看google::protobuf::RpcController:

class PROTOBUF_EXPORT RpcController {public:inline RpcController() {}virtual ~RpcController();// Client-side methods ---------------------------------------------// These calls may be made from the client side only.  Their results// are undefined on the server side (may crash).// Resets the RpcController to its initial state so that it may be reused in// a new call.  Must not be called while an RPC is in progress.virtual void Reset() = 0;// After a call has finished, returns true if the call failed.  The possible// reasons for failure depend on the RPC implementation.  Failed() must not// be called before a call has finished.  If Failed() returns true, the// contents of the response message are undefined.virtual bool Failed() const = 0;// If Failed() is true, returns a human-readable description of the error.virtual std::string ErrorText() const = 0;// Advises the RPC system that the caller desires that the RPC call be// canceled.  The RPC system may cancel it immediately, may wait awhile and// then cancel it, or may not even cancel the call at all.  If the call is// canceled, the "done" callback will still be called and the RpcController// will indicate that the call failed at that time.virtual void StartCancel() = 0;// Server-side methods ---------------------------------------------// These calls may be made from the server side only.  Their results// are undefined on the client side (may crash).// Causes Failed() to return true on the client side.  "reason" will be// incorporated into the message returned by ErrorText().  If you find// you need to return machine-readable information about failures, you// should incorporate it into your response protocol buffer and should// NOT call SetFailed().virtual void SetFailed(const std::string& reason) = 0;// If true, indicates that the client canceled the RPC, so the server may// as well give up on replying to it.  The server should still call the// final "done" callback.virtual bool IsCanceled() const = 0;// Asks that the given callback be called when the RPC is canceled.  The// callback will always be called exactly once.  If the RPC completes without// being canceled, the callback will be called after completion.  If the RPC// has already been canceled when NotifyOnCancel() is called, the callback// will be called immediately.//// NotifyOnCancel() must be called no more than once per request.virtual void NotifyOnCancel(Closure* callback) = 0;private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
};

可以看到RpcController是一个抽象类,里面有一些纯虚函数,
Reset()表示rpc状态、
Failed()表示rpc请求过程中是否发生了错误,
ErrorText()表示如果rpc请求出错了,那么错误的原因是什么
SetFailed()表示rpc请求出错了,需要设置错误的原因
StartCancel()、IsCanceled()、NotifyOnCancel()表示是否取消rpc以及相应的回调。
显然这个类的作用可以很好的帮助rpcClient判断rpc请求过程中是否出错、错误原因、或是什么原因rpcServer取消了
否则没有Rpccontroller的帮助,RpcClient调用rpc请求之后等待rpcServer的响应,然后反序列化响应。但如果中间错误了,那么RpcClient本地序列化response响应肯定会不成功(没必要序列化响应), 也不知道具体原因是什么,这就很不友好了。那么有了RpcController可以很轻松的解决这种问题。
老样子,实现RpcController很简单,写一个派生类继承自google::protobuf::RpcController,并重写需要提供给用户的方法
这里提供一个简单版本MyRpcController类:

#pragma once
#include <google/protobuf/service.h>
#include <string>class MyRpcController : public google::protobuf::RpcController
{
public:MyRpcController ();void Reset();bool Failed() const;std::string ErrorText() const;void SetFailed(const std::string& reason);//目前未实现具体的功能void StartCancel();bool IsCanceled() const;void NotifyOnCancel(google::protobuf::Closure* callback);private:bool m_failed; //RPC方法执行过程中的状态std::string m_errText; //Rpc方法执行过程中的错误信息
};//.cc
#include "myrpccontroller.h"MyRpcController ::MyRpcController ()
{m_failed = false;m_errText = "";
}void MyRpcController ::Reset()
{m_failed = false;m_errText = "";
}bool MyRpcController ::Failed() const
{return m_failed;
}std::string MyRpcController ::ErrorText() const
{return m_errText;
}void MyRpcController ::SetFailed(const std::string& reason)
{m_failed = true;m_errText = reason;
}//目前未实现具体的功能
void MyRpcController ::StartCancel(){}bool MyRpcController ::IsCanceled() const{return false;}void MyRpcController ::NotifyOnCancel(google::protobuf::Closure* callback){}

使用MyRpcController

	fixbug::UserServiceRpc_Stub stub(new MyRpcChannel());MyRpcController controllerLogin; //rpcController//rpc方法的请求参数fixbug::LoginRequest request;request.set_name("zhang san");request.set_pwd("123");//rpc方法的响应,同步的rpc调用过程fixbug::LoginResponse response;stub.Login(&controllerLogin, &request, &response, nullptr);//一次rpc调用完成,读调用的结果if(!controllerLogin.Failed() && 0 == response.result().errcode()){std::cout << "rpc login response success: " << response.success() << std::endl;}else{if(controllerLogin.Failed())std::cout<<controllerLogin.ErrorText()<<std::endl;else std::cout << "rpc login error msg : " << response.result().errmsg() << std::endl;}

至此,简单的RpcController实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/20942.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux启动oracle

一、启动方法 方法1&#xff1a; Sql代码 cd $ORACLE_HOME/bin #进入到oracle的安装目录 ./dbstart #重启服务器 ./lsnrctl start #重启监听器 ----------------------------------- 方法2&#xff1a; &#xff08;1&#xff09; 以oracle身份登录​​数据库​​&am…

C#仿热血江湖GClass11

目录 1 GClass11 1.1 GetEnumerator 1.2 Dispose 1.3 imethod_1 1.4 imethod_0 GClass1using System; using System.Collections; using System.Collections.Generic; using Sys

一文快速入门Byzer-python

目录 一、Byzer-Python介绍 二、Byzer-python工具语法糖 三、环境依赖 1. Python 环境搭建 2. Ray 环境搭建 3. Byzer-python 与 Ray 四、参数详解 五、数据处理 1. Byzer-python 处理数据 2. Byzer-python 代码说明 3. Byzer-python 读写 Excel 文件 4. Byzer-pytho…

数据持久化之Web存储

​ 前端数据持久化是指在前端&#xff08;客户端&#xff09;应用中将数据保存在本地&#xff0c;使得数据在页面刷新、关闭或重新打开后依然保持存在的过程。在Web开发中&#xff0c;前端数据持久化可以使得用户不必每次都从服务器中获取数据。 常见的前端持久化方法&#xf…

如何搭建一个口才培训的网站?需要具备哪些条件?

论文题目&#xff1a;如何搭建一个口才培训的网站及所需条件 摘要&#xff1a; 本文探讨了如何搭建一个口才培训的网站&#xff0c;并详细分析了所需的关键条件。口才培训作为一种重要的社交技能&#xff0c;能够帮助人们提升自信和影响力&#xff0c;因此具有广阔的市场前景。…

day17 | 654.最大的二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

文章目录 一、最大的二叉树二、合并二叉树三、二叉搜索树中的搜索四、验证二叉搜索树 一、最大的二叉树 654.最大的二叉树 构建二叉树的题目&#xff0c;都用前序遍历。 因为我们一定要先构建根节点&#xff0c;才能继续向后构建。 递归函数的参数和返回值&#xff1a; Tree…

AssetBundle学习

官方文档&#xff1a;AssetBundle 工作流程 - Unity 手册 (unity3d.com) 之前写的博客&#xff1a;AssetBundle学习_zaizai1007的博客-CSDN博客 使用流程图&#xff1a; 1&#xff0c;指定资源的AssetBundle属性 &#xff08;xxxa/xxx&#xff09;这里xxxa会生成目录&…

redux-promise-middleware和applyMiddleware的理解与使用

一、作用&#xff1a; applyMiddleware是一个中间件&#xff0c;通常和applyMiddleware结合使用&#xff0c;是dispatch与reducers之间的应用&#xff0c;用于处理dispatch发送的异步action操作 二、使用 1、安装redux-promise-middleware cnpm i redux-promise-middleware…

【Android】使用 CameraX 实现基础图像分析功能

1. 基础开发环境 JDK&#xff1a;JDK17 Android Studio&#xff1a;Android Studio Giraffe | 2022.3.1 Android SDK&#xff1a;Android API 34 Gradle: gradle-8.0-bin.zip CameraX Version: 1.1.0-alpha05 2. 添加相关依赖 在 build.gradle 中添加 CameraX 的相关依赖 // *…

Micropython STM32F4入门点灯第一课

Micropython STM32F4入门点灯第一课 &#x1f4cc;固件刷可参考前面一篇《STM32刷Micropython固件参考指南》&#x1f4cd;固件下载&#xff1a;https://micropython.org/download/?mcustm32f4&#x1f516;本例程基于STM32F4DISC&#xff0c;主控芯片STM32F407VGT6&#x1f4…

ospf复习

工作过程 启动OSPF配置之后&#xff0c;OSPF会向本地所有激活OSPF的接口发送hello包&#xff0c;以组播 224.0.0.5&#xff08;所有运行OSPFV2协议的设备监听的地址&#xff09;的形式发送。hello包中将携带本 地的RID及本地已知邻居的RID&#xff0c;之后&#xff0c;将收集到…

vue 混入(mixin)的使用

在 vue 组件内&#xff0c;如果想将一些公共功能&#xff0c;如组件、方法、钩子函数等复用&#xff0c;混入是一个很好的选择。 现在开始我们的混入使用吧 1、我们可以创建一个目录mixins&#xff0c;在创建一个comment.js文件如图&#xff1a; // 在 common.js 里写你想共享…

20230803激活手机realme GT Neo3

20230803激活手机realme GT Neo3 缘起&#xff1a; 新买的手机&#xff1a;realme GT Neo3 需要确认&#xff1a; 1、4K录像&#xff0c;时间不限制。 【以前的很多手机都是限制8/10/12/16分钟】 2、通话自动录音 3、定时开关机。 4、GPS记录轨迹不要拉直线&#xff1a;户外助…

小程序学习(四):WXML模板语法

WXML模板语法-数据绑定 1.数据绑定的基本原则 ①在data中定义数据 ②在WXML中使用数据 2.动态绑定属性 WXML模板语法-事件绑定 3.什么是事件 4.小程序中常用的事件 5.事件对象的属性列表 6.target和currentTarget的区别 7.bindtap的语法格式 8.在事件处理函数中为data中的数据…

DC-2靶机

文章目录 信息收集漏洞发现漏洞利用 DC-2介绍 DC-2环境下载 请注意&#xff0c;您需要将渗透测试设备上的 hosts 文件设置为&#xff1a; 192.168.0.145 dc-2 显然&#xff0c;将 192.168.0.145 替换为 DC-2 的实际 IP 地址。 它将使生活变得更加简单&#xff08;如果没有它&am…

LeetCode513. 找树左下角的值

513. 找树左下角的值 文章目录 [513. 找树左下角的值](https://leetcode.cn/problems/find-bottom-left-tree-value/)一、题目二、题解方法一&#xff1a;递归法&#xff08;层序遍历&#xff0c;深度优先搜索&#xff09;**不足之处以及如何改进** 方法二&#xff1a;迭代 一、…

Gitignore忽略文件

默认情况下&#xff0c;Git会监视我们项目中的所有内容&#xff0c;但是有些内容比如mode_modules中的内容&#xff0c;我们不希望他被Git所管理。 我们可以在我们项目目录中添加一个 .gitignore 文件来设置那些需要git忽略的文件。

屏幕取色器Mac版_苹果屏幕取色工具_屏幕取色器工具

Sip for Mac 是Mac系统平台上的一款老牌的颜色拾取工具&#xff0c;是设计师和前端开发工作者必不可少的屏幕取色软件&#xff0c;你只需要用鼠标点一下即可轻松地对屏幕上的任何颜色进行采样和编码&#xff0c;并将颜色数据自动存到剪切板&#xff0c;方便随时粘贴出来。 Sip…

GPIO简介

一、GPIO GPIO&#xff08;General-purpose input/output&#xff09;即通用型输入输出&#xff0c;GPIO可以控制连接在其之上的引脚实现信号的输入和输出 芯片的引脚与外部设备相连&#xff0c;从而实现与外部硬件设备的通讯、控制及信号采集等功能 LED实验步骤 实验步骤 以L…

使用Golang反射技术实现一套有默认值的配置解析库

在实际开发中&#xff0c;我们往往会给一个逻辑设计一套配置文件&#xff0c;用于根据不同环境加载不同配置。 比如生产环境和测试环境数据库的地址不一样&#xff0c;我们就需要在配置文件中设置不同的值。但是配置文件中又有一些相同值的配置项&#xff0c;比如数据库的名称等…