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实现。
相关文章:
RpcController作用浅析
RpcController作用浅析 前面提到了RpcConsumer的实现思路,但是并没说明RpcController有什么作用,不妨看看google::protobuf::RpcController: class PROTOBUF_EXPORT RpcController {public:inline RpcController() {}virtual ~RpcControlle…...

Linux(三):Linux服务器下日常实操命令 (常年更新)
基础命令 cd命令:切换目录 cd :切换当前目录百至其它目录,比如进入/etc目录,则执行 cd /etccd / :在Linux 系统中斜杠“/”表示的是根目录。cd / ,即进入根目录.cd ~:进入用户在该系统的home目录&#…...

强大的截图软件--Snipaste
这里写目录标题 前言Snipaste贴图并置顶标注功能 下载 前言 在工作中,我们经常需要保存当前屏幕的图片,虽然系统总是会自带一些截图工具,但似乎用起来总是不那个顺手,例如我们需要对图片进行一些标注,或者将图片贴在屏…...

LeetCode·每日一题·722. 删除注释·模拟
题目 示例 思路 题意 -> 给定一段代码,将代码中的注释删除并返回。 由于注释只有两种类型: 字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。字符串/* 表示一个块注释,它表示直到下一个(非重叠&#x…...

npm更新和管理已发布的包
目录 1、更改包的可见性 1.1 将公共包设为私有 编辑 使用网站 使用命令行 1.2 将私有包公开 使用网站 使用命令行 2、将协作者添加到用户帐户拥有的私有包 2.1 授予对Web上私有用户包的访问权限 2.2 从命令行界面授予私有包访问权限 2.3 授予对私有组织包的访问权限…...
如何高效使用Gherkin
背景 时间回到2022年,我参与了一个使用了Flutter技术构建的Web前端项目。在这个项目上,我们小组的目标是实施Flutter前端自动化测试。 彼时,Flutter 2.x刚在Web端发力不久,Flutter Web上的应用和生态才刚刚开始,而在…...

[CKA]考试之调度 pod 到指定节点
由于最新的CKA考试改版,不允许存储书签,本博客致力怎么一步步从官网把答案找到,如何修改把题做对,下面开始我们的 CKA之旅 题目为: Task 创建一个Pod,名字为nginx-kusc00401,镜像地址是nginx…...

git 常用命令有哪些
Git 是我们开发工作中使用频率极高的工具,下面总结下他的基本指令有哪些,顺便温习一下。 前言 一般项目中长存2个分支: 主分支(master) 和开发分支(develop) 项目存在三种短期分支 ࿱…...
算法leetcode|66. 加一(rust重拳出击)
文章目录 66. 加一:样例 1:样例 2:样例 3:提示: 分析:题解:rust:go:c:python:java: 66. 加一: 给定一个由 整数 组成的 非…...
MySQL备份Shell脚本
将此脚本添加到crontab计划中,自动留存最新的两份备份 #!/bin/bash # 数据库配置 DB_HOST"localhost" DB_USER"root" DB_PASS"Sxbdc123!#" DB_NAME"ww"# 备份目录 BACKUP_DIR"/opt/mysqlbak"# 备份文件名称 BA…...

Python批量查字典和爬取双语例句
最近,有网友反映,我的批量查字典工具换到其它的网站就不好用了。对此,我想说的是,互联网包罗万象,网站的各种设置也有所不同,并不是所有的在线字典都可以用Python爬取的。事实上,很多网站为了防…...

uni-app、H5实现瀑布流效果封装,列可以自定义
文章目录 前言一、效果二、使用代码三、核心代码总结前言 最近做项目需要实现uni-app、H5实现瀑布流效果封装,网上搜索有很多的例子,但是代码都是不够完整的,下面来封装一个uni-app、H5都能用的代码。在小程序中,一个个item渲染可能出现问题,也通过加锁来解决问题。 一、…...

vue echart3个饼图
概览:根据UI设计需要做3个饼图且之间有关联,并且处理后端返回的数据。 参考链接: echart 官网的一个案例,3个饼图 实现思路: 根据案例,把数据处理成对应的。 参考代码: 1.处理后端数据&am…...

LEARNING TO EXPLORE USING ACTIVE NEURAL SLAM 论文阅读
论文信息 题目:LEARNING TO EXPLORE USING ACTIVE NEURAL SLAM 作者:Devendra Singh Chaplot, Dhiraj Gandhi 项目地址:https://devendrachaplot.github.io/projects/Neural-SLAM 代码地址:https://github.com/devendrachaplot/N…...

item_search-ks-根据关键词取商品列表
一、接口参数说明: item_search-根据关键词取商品列表,点击更多API调试,请移步注册API账号点击获取测试key和secret 公共参数 请求地址: https://api-gw.onebound.cn/ks/item_search 名称类型必须描述keyString是调用key(http:…...
windows运行WPscan报错:无法打开库libcurl.dll
windows运行WPscan报错:无法打开库libcurl.dll 1.问题背景2.解决方案1.问题背景 在Windows上启动WPScan时: wpscan --url xxx.ru提示如下错误: Could not open library libcurl.dll: �� ������ ��������� ������. . Could not open library libcu...

web前端框架Javascript之JavaScript 异步编程史
早期的 Web 应用中,与后台进行交互时,需要进行 form 表单的提交,然后在页面刷新后给用户反馈结果。在页面刷新过程中,后台会重新返回一段 HTML 代码,这段 HTML 中的大部分内容与之前页面基本相同,这势必造成…...

Java多线程(1)---多线程认识、四种创建方式以及线程状态
目录 前言 一.Java的多线程 1.1多线程的认识 1.2Java多线程的创建方式 1.3Java多线程的生命周期 1.4Java多线程的执行机制 二.创建多线程的四种方式 2.1继承Thread类 ⭐创建线程 ⭐Thread的构造方法和常见属性 2.2.实现Runnable接口 ⭐创建线程 ⭐使用lambda表达…...

搭建Django+pyhon+vue自动化测试平台
Django安装 使用管理员身份运行pycharm使用local 1 pip install django -i https://pypi.tuna.tsinghua.edu.cn/simple 检查django是否安装成功 1 python -m django --version 创建项目 1 1 django-admin startproject test cd 切换至创建的项目中启动django项目…...

CASAIM自动化平面度检测设备3D扫描零部件形位公差尺寸测量
平面度是表面形状的度量,指示沿该表面的所有点是否在同一平面中,当两个表面需要连接在一起形成紧密连接时,平面度检测至关重要。 CASAIM自动化平面度检测设备通过搭载领先的激光三维测头和智能检测软件自动获取零部件高质量测量数据…...

JavaSec-RCE
简介 RCE(Remote Code Execution),可以分为:命令注入(Command Injection)、代码注入(Code Injection) 代码注入 1.漏洞场景:Groovy代码注入 Groovy是一种基于JVM的动态语言,语法简洁,支持闭包、动态类型和Java互操作性,…...

【力扣数据库知识手册笔记】索引
索引 索引的优缺点 优点1. 通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度(创建索引的主要原因)。3. 可以加速表和表之间的连接,实现数据的参考完整性。4. 可以在查询过程中,…...

Vue3 + Element Plus + TypeScript中el-transfer穿梭框组件使用详解及示例
使用详解 Element Plus 的 el-transfer 组件是一个强大的穿梭框组件,常用于在两个集合之间进行数据转移,如权限分配、数据选择等场景。下面我将详细介绍其用法并提供一个完整示例。 核心特性与用法 基本属性 v-model:绑定右侧列表的值&…...
QMC5883L的驱动
简介 本篇文章的代码已经上传到了github上面,开源代码 作为一个电子罗盘模块,我们可以通过I2C从中获取偏航角yaw,相对于六轴陀螺仪的yaw,qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...
在Ubuntu中设置开机自动运行(sudo)指令的指南
在Ubuntu系统中,有时需要在系统启动时自动执行某些命令,特别是需要 sudo权限的指令。为了实现这一功能,可以使用多种方法,包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法,并提供…...

Python爬虫(一):爬虫伪装
一、网站防爬机制概述 在当今互联网环境中,具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类: 身份验证机制:直接将未经授权的爬虫阻挡在外反爬技术体系:通过各种技术手段增加爬虫获取数据的难度…...

DBAPI如何优雅的获取单条数据
API如何优雅的获取单条数据 案例一 对于查询类API,查询的是单条数据,比如根据主键ID查询用户信息,sql如下: select id, name, age from user where id #{id}API默认返回的数据格式是多条的,如下: {&qu…...

如何理解 IP 数据报中的 TTL?
目录 前言理解 前言 面试灵魂一问:说说对 IP 数据报中 TTL 的理解?我们都知道,IP 数据报由首部和数据两部分组成,首部又分为两部分:固定部分和可变部分,共占 20 字节,而即将讨论的 TTL 就位于首…...
.Net Framework 4/C# 关键字(非常用,持续更新...)
一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...
Java 二维码
Java 二维码 **技术:**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...