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中有点说不过去. ...
随机推荐
- tomcat7 中的坑。 关于welcome-list和servlet-mapping
web.xml中, 使用default servlet设置了针对静态资源后缀名的过滤. 并且设置了welcome-list, 使用jetty和tomcat6启动一切正常, 但是使用tomcat7则出现 ...
- java处理json数据
如果要处理json数据首先要确定使用的json包是那个,常用的有json-lib-x.jar和jack-json-x.jar.我这里的实例代码为json-lib-2.4-jdk15.jar. 在jso ...
- 几个常用的url生成二维码的接口
找到了几个URL生成的接口,速度上可能会有差别,可试验后选用,我用过第一个,分享: <!doctype html> <html lang="en"> < ...
- stl_set.h
stl_set.h // Filename: stl_set.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http://blo ...
- Struts2 - 通过 ActionContext 访问 Web 资源
public String execute(){ //0. 获取 ActionContext 对象 //ActionContext 是 Action 的上下文对象. 可以从中获取到当往 Action ...
- POJ-2564 01背包问题
#include"cstdio" #include"cstring" #include"algorithm" using namespace ...
- Mysql备份脚本python编写
#!/usr/bin/env python #-*- coding: UTF-8 -*- ####################################################### ...
- JEECG datagrid 列表检索条件 添加下拉级联功能
$("#communityId").change( function(){ var id = $(this).children('option:selected').val(); ...
- bzoj 1819: 电子字典 Trie
题目: Description 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功 ...
- 使用UIVisualEffectView创建毛玻璃效果
UIVisuaEffectView :继承自UIView,可以看成是专门用于处理毛玻璃效果的视图,只要我们将这个特殊的View添加到其他视图(eg. ImageView )上面,被该UIVisuaEf ...