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中有点说不过去. ...
随机推荐
- 囤题&&发布记录
声明 && 温馨提示 by ljh2000 听说有人很喜欢狙我......看我不把你们抓起来嘿嘿嘿! 为了采取措施,不让被狙成为生活常态(雾 ,我要闭关锁国辣,我要开始(屯田)囤题 ...
- Hibernate和Struts分页查询
分页查询数据库方法 /** * 分页查询方法 * @param sql,pageNO,maxResult * @return List */ public List<Object> que ...
- Spark-Spark setMaster & WordCount Demo
Spark setMaster源码 /** * The master URL to connect to, such as "local" to run locally with ...
- django admin扩展user表password验证及set_password
一般如果扩展了django user内置表,在admin后台创建新用户的时候密码将会变成明文,故而导致登录不成功.所以我们在admin.py可以通过form自定义进行对password进行操作,可以双 ...
- Java 时间和日期类型的 Hibernate 映射
以下情况下必须显式指定 Hibernate 映射类型 一个 Java 类型可能对应多个 Hibernate 映射类型. 例如: 如果持久化类的属性为 java.util.Date 类型, 对应的 Hi ...
- HUE,大数据的SQL Shell
1. HUE需要安装SASL 发现异常: Could not install table: Error creating table sample_07: Could not start SASL: ...
- android开发中 解决服务器端解析MySql数据时中文显示乱码的情况
首先,还是确认自己MySql账户和密码 1.示例 账户:root 密码:123456 有三个字段 分别是_id .username(插入有中文数据).password 1)首先我们知道 ...
- 简单的RBAC用户角色权限控制
Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ...
- loadrunner手动生成脚本函数
1.点击insert
- js遍历for,forEach, for in,for of
ECMAScript5(es5)有三种for循环 简单for for in forEach ECMAScript6(es6)新增 for of 简单for for是循环的基础语法,也是最常用的循环结构 ...