ProtoBuf练习(一)
基础数据类型
protobuf语言的基础字段类型相当于C++语言的基础类型
工程目录结构
$ ls proto/
TFixed.proto TInt.proto TScalar.proto TStr.proto
proto文件
$ cat TScalar.proto
syntax = "proto3";
//导入其他message
import "TInt.proto";
import "TFixed.proto";
import "TStr.proto";
//使用其他message作为字段类型,如果message使用了package则需要使用package.的方式引用
message TScalar {
scalar.TInt int_val = 1;
TFixed fixed_val = 2;
TStr str_val = 3;
};
$ cat TInt.proto
syntax = "proto3";
// 设置包名,在生成的C++源码中将变成namespace
package scalar;
message TInt {
double ddouble_val = 1;
float float_val = 2;
int32 int32_val = 3;
uint32 uint32_val = 4;
sint32 sint32_val = 5;
bool bool_val = 6;
};
$ cat TFixed.proto
syntax = "proto3";
message TFixed {
fixed32 fixed32_val = 1;
fixed64 fixed64_val = 2;
sfixed32 sfixed32_val = 3;
sfixed64 sfixed64_val = 4;
};
$ cat TStr.proto
syntax = "proto3";
message TStr {
string string_val = 1;
bytes bytes_val = 2;
};
读写源文件
$ cat writer.cpp
#include <fstream>
#include <iostream>
#include "TScalar.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
TScalar msg;
scalar::TInt* iMsg = new scalar::TInt();
TFixed* fMsg = new TFixed();
TStr* sMsg = new TStr();
// 使用protobuf自己的内存管理
msg.set_allocated_int_val(iMsg);
msg.set_allocated_fixed_val(fMsg);
msg.set_allocated_str_val(sMsg);
iMsg->set_ddouble_val(11.01);
iMsg->set_float_val(11.01);
iMsg->set_int32_val(-1);
iMsg->set_uint32_val(1);
iMsg->set_sint32_val(-1);
iMsg->set_bool_val(true);
fMsg->set_fixed32_val(1);
fMsg->set_fixed64_val(1);
fMsg->set_sfixed32_val(-1);
fMsg->set_sfixed64_val(-1);
sMsg->set_string_val("中");
sMsg->set_bytes_val("文");
fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!msg.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
}
$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "TScalar.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl;
TScalar msg;
//如果writer依次写入TInt、TFixed、TStr,然后在reader依次读取TInt、TFixed、TStr,这时只有第一个message能正常解析,后两条message解析失败,因为ParseFromIstream会一次性把input流内容读完,导致后面的message解析时读取的输入流已经为空,所以表面上看起来就好像protocol buffer只能序列化和反序列化一条message
if (!msg.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
}
msg.int_val().PrintDebugString();
msg.fixed_val().PrintDebugString();
msg.str_val().PrintDebugString();
cout << "Deserialize end." << endl;
input.close();
return 0;
}
ProtoBuf练习(一)的更多相关文章
- python通过protobuf实现rpc
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...
- Protobuf使用规范分享
一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
- protobuf的编译安装
github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- protobuf学习(2)-相关学习资料
protobuf官方git地址 protobuf官方英文文档 (你懂的需要FQ) protobuf中文翻译文档 protobuf概述 (官方翻译 推荐阅读) protobuf入门 ...
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- c# (ENUM)枚举组合类型的谷歌序列化Protobuf
c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...
- dubbox 增加google-gprc/protobuf支持
好久没写东西了,今年实在太忙,基本都在搞业务开发,晚上来补一篇,作为今年的收官博客.google-rpc 正式发布以来,受到了不少人的关注,这么知名的rpc框架,不集成到dubbox中有点说不过去. ...
随机推荐
- Codeforces Round #250 (Div. 2) A, B, C
A. The Child and Homework time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Oracle的PL_SQL的结构
--PL/SQL的结构 declare --声明变量和常量关键字 v_name nvarchar2(); v_age integer;--常规变量声明 v_product table_name.col ...
- Idea_学习_05_Intellij Idea自动添加注释的方法
二.参考资料 1. Intellij Idea自动添加注释的方法
- 2018.6.21 HOLTEK HT49R70A-1 Source Code analysis
Cange note: “Reading TMR1H will latch the contents of TMR1H and TMR1L counter to the destination”? F ...
- 关于from..import 与import导入模块问题
问题来源:导入PyQt5里面的模块时老是出错 1.from PyQt5 import QtWidgets.QApplication,QtWidgets.QtDialog #出错2.from PyQt5 ...
- Java 使用itext生成pdf以及下载
使用方法: 1.需要两个jar包: iText-5.0.6.jar //必须使用该版本,否则缺少相关的方法 TextAsian.jar //是为了文档中正常显示中文所必须引用的包 TextAsi ...
- 【leetcode刷题笔记】Populating Next Right Pointers in Each Node II
What if the given tree could be any binary tree? Would your previous solution still work? Note: You ...
- 表达式(exp)
题目大意 给定一个逻辑表达式,求每一个数满足$\in[1,n]$的使的表达式为真的方案数. 题解 题目限制较奇怪且数据范围较小,所以可以考虑直接暴力. 考虑枚举每一个变量一共出现了$k$种数值,再枚举 ...
- 搭建DNS服务器-bind
1. 安装 yum install -y bind-chroot yum install -y bind-utils service named-chroot start 2. 修改配置 增加一 ...
- #include <deque>
deque \(deque\)头文件主要包括一个双端队列容器.是一个支持在两端插入两端删除的线性储存空间,与vector和queue相似.与\(vector\)比起来,\(deque\)可以在\(O( ...