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 中的序列化 & 反序列化
序列化:将对象的状态信息及类型信息,转换为一种易于传输或存储形式(流,即字节序列)的过程. 下图为序列化过程图示,图片来自微软官方文档: 反序列化:与序列化相反,将流转换为对象的过程. 常用的有二进制 ...
随机推荐
- DoTween插件
doteeen(hotween第二个版本) hotween leantween gokit itween(多个相同动画类型的插件):性能最高的是dotween 引用命名空间:using DG.Twee ...
- 十八 Django框架,生成二维码
用Python来生成二维码,需要qrcode模块,qrcode模块依赖Image 模块,所以首先安装这两个模块 生成二维码保存图片在本地 import qrcode img = qrcode.make ...
- WebElement接口获取值
通过WebElement接口获取值 size 获取元素的尺寸 text 获取元素的文本 get_attribute(name) 获取属性值 location 获取元素坐标,先找到要获取的元素,再调用该 ...
- jQuery插件--图片文字向上向左循环滚动
需要引用jquery 调用非常简单: 一. 向上滚动 $(".scroll_two").jScroll({vertical: true}); <div class=" ...
- 2017人工智能元年,AI在喧嚣和质疑中一路走来
前百度首席科学家吴恩达说:就像100年前的电力.20年前的互联网一样,AI也会改变每一个产业! 有人说,现在就像1995年,那一年,第一家互联网公司--网景上市,一天之内大涨208%,互联网正式登上历 ...
- freeMarker(十四)——XML处理指南之必要的XML处理
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.基本内容 假设程序员在数据模型中放置了一个XML文档,就是名为 d ...
- 如何使 vlc 支持 fdk-aac 编码(windows平台
可能是由于fdk-aac开源协议的原因,VLC默认是不支持fdk-aac编码的,fdk-aac 是非常优秀的AAC编码库,并且支持AAC-LD AAC-ELD, 对于要求低延迟的场景下很有用. 可以通 ...
- HTML5视音频标签参考
本文将介绍HTML5中的视音频标签和对应的DOM对象.是相关资料的中文化版本,可以作为编写相关应用的简易中文参考手册. 一些约定 所有浏览器:指支持HTML5的常见桌面浏览器,包括IE9+.Firef ...
- plsql developer v12.1的使用
1.下载oracle客户端: 配置安装路径到PATH,我的是首先定义了一个环境变量ORACLE_CLIENT_PATH(这个路径其实就是官网下来的zip包解压缩后的路径),然后将这个变量附加到PATH ...
- HDOJ1242(延时迷宫BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...