ProtoBuf练习(三)
任意类型
protobuf语言的任意字段类型相当于Boost库的boost::any类型数据,google.protobuf.Any是对protobuf语言的message进行封装,所以需要使用message来封装任意类型的数据,而不能像boost::any一样直接使用基础数据类型
工程目录结构
$ ls proto/
ErrorStatus.proto
proto文件
$ cat proto/ErrorStatus.proto
syntax = "proto3";
import "google/protobuf/any.proto";
message NetworkErrorDetails
{
sint32 err_no = 1;
}
message IoErrorDetails
{
sint32 err_no = 1;
}
message ErrorStatus {
string message = 1;
repeated google.protobuf.Any details = 2;
}
读写源文件
$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "ErrorStatus.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl;
ErrorStatus status;
if (!status.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
}
cout << status.message() << endl;
for (const google::protobuf::Any& detail : status.details())
{
// 检测Any类型具体对应的消息类型
if (detail.Is<IoErrorDetails>())
{
cout << "IoErrorStatus:" << endl;
IoErrorDetails io_error;
if (detail.UnpackTo(&io_error))
io_error.PrintDebugString();
else
cout << "Parse fails." << endl;
}
if (detail.Is<NetworkErrorDetails>())
{
cout << "NetworkErrorDetails:" << endl;
NetworkErrorDetails network_error;
if (detail.UnpackTo(&network_error))
network_error.PrintDebugString();
else
cout << "Parse fails." << endl;
}
}
cout << "Deserialize end." << endl;
input.close();
return 0;
}
$ cat writer.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "ErrorStatus.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
IoErrorDetails details;
details.set_err_no(1);
ErrorStatus status;
// 自动生成对象,并打包消息
status.add_details()->PackFrom(details);
status.set_message("File read operation");
fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!status.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
}
ProtoBuf练习(三)的更多相关文章
- Go - 如何编写 ProtoBuf 插件 (三) ?
目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...
- Sword protobuf学习三
#include <iostream> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> ...
- asp.net core 使用protobuf
在一些性能要求很高的应用中,使用protocol buffer序列化,优于Json.而且protocol buffer向后兼容的能力比较好. 由于Asp.net core 采用了全新的MiddleWa ...
- Protobuf实现Android Socket通讯开发教程
本节为您介绍Protobuf实现Android Socket通讯开发教程,因此,我们需要先了理一下protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式 ...
- ProtocolBuffers (二) android与PC,C#与Java 利用protobuf 进行无障碍通讯【Socket】
protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式的数据. 谷歌使用其内部几乎RPC协议和文件格式的所有协议缓冲区. 参考文档 http://c ...
- erlang抽象码与basho的protobuf
erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...
- Corba、protocol buffer、SOA的区别 (转)
From: http://www.zhihu.com/question/20279489 Google的protocol buffers?这个跟corba.soa没啥关系,不同层次的概念,没法比.pr ...
- 【阿里云产品公测】大数据下精确快速搜索OpenSearch
[阿里云产品公测]大数据下精确快速搜索OpenSearch 作者:阿里云用户小柒2012 相信做过一两个项目的人都会遇到上级要求做一个类似百度或者谷歌的站内搜索功能.传统的sql查询只能使用like ...
- protocol buffer和当年corba ,和现在SOA有啥异同点
CORBA是对象管理集团(OMG)的一个标准,使得不同语言编写的,运行在不同计算机上的能够协同工作.标准包括分布式计算的通讯协议(GIOP和IIOP),可映射到多种语言的接口描述语言(IDL),对象请 ...
- .NET 中的序列化 & 反序列化
序列化:将对象的状态信息及类型信息,转换为一种易于传输或存储形式(流,即字节序列)的过程. 下图为序列化过程图示,图片来自微软官方文档: 反序列化:与序列化相反,将流转换为对象的过程. 常用的有二进制 ...
随机推荐
- java:eclipse4.4 安装tomcat插件
一.Eclipse 4.4下载地址:http://www.eclipse.org/downloads/ (Eclipse官网).Eclipse4.4版本默认已经集成了Ant和Maven插件,这点挺不错 ...
- Java集合类--->入门下篇
HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...
- IBM V3500存储恢复步骤实例(linux)
本环境是一有台IBM3500存储,将存储挂载至linux的/data目录,模拟测试当主服务器挂了,将数据恢复到另一台服务器,存储有两个地址,我配置的是192.168.80.59是用于web管理,192 ...
- C++中函数模版和普通函数的区别
函数模版和同名普通函数在同一个作用域中,会优先调用那个函数? 函数模型在进行调用的时候会进行严格的类型匹配,而普通函数在调用的时候,会进行函数参数类型转换(前提是自动类型转换). 调用函数模版,本质是 ...
- linux命令学习笔记(18):locate 命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及 路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 d ...
- 【C/C++】scanf,printf 函数
摘自http://www.cplusplus.com 1. scanf 函数 int scanf ( const char * format, ... ); Parameters format C s ...
- android开发 MyEclipse下测试连接MySQL数据库
1.首先要加载MySQL驱动包. 步骤:右击项目找到build path->configure build path->libraries——>add External JARs添加 ...
- 清理:db上面的过期的binlog,释放磁盘空间。 (转)
如果10台以内的db的话,自己手动ssh进去,clean就足以,但是上百台呢,就要写脚本了.大概思路:在 一台db跳转机上面, 写一个脚本,访问slave,远程获取正在复制的master上面的binl ...
- select元素选择时间以及jQuery对select的属性操作
<select class="input04" id="1" name="in_class1" onchange="setc ...
- Python:str.ljust()、str.rjust()、str.center()函数
str.ljust().str.rjust().str.center()函数 功能:调整字符串站位宽度,并确定字符串对齐方式: #可以用其它字符填充字符: #字符串长度 = 字符串个数(包含空格.标点 ...