protobuf简介和使用
1.Protocol Buffers简介
Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。现阶段支持C++、JAVA、Python等三种编程语言。
2.protobuf相比Xml的优点
message helloworld {
required int32 id = 1; // ID
required string str = 2; // str
}
5.编译 .proto
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/helloworld.proto
protoc -I=. --cpp_out=. ./helloworld.proto
命令将生成:
helloworld.pb.h , 定义了 C++ 类的头文件
helloworld.pb.cc , C++ 类的实现文件
6.测试程序
#include "helloworld.pb.h" //包含生成的头文件
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
helloworld msg;
msg.set_id(101);
msg.set_str("hello");
// 序列化消息
char buff[1024] = {0};
msg.SerializeToArray(buff, 1024);
//解析消息
helloworld msgread;
msgread.ParseFromArray(buff, 1024);
cout << msgread.id() << endl;
cout << msgread.str() << endl;
}
7.编译运行
g++ -o main main.cpp helloworld.pb.cc -lprotobuf -lpthread
./main
protobuf简介和使用的更多相关文章
- Protobuf(一)——Protobuf简介
Protobuf简介 什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: Google Protocol Buffer( 简称 Proto ...
- 消息中间件NetMQ结合Protobuf简介
概述 对于稍微熟悉这两个优秀的项目来说,每个内容单独介绍都不为过,本文只是简介并探讨如何将两部分内容合并起来,使其在某些场景下更适合.更高效. NetMQ:ZeroMQ的.Net版本,ZeroMQ简单 ...
- protobuf简介
#1,简介 把某种数据结构的信息,以某种格式保存起来: 主要用于数据存储,传输协议格式. #2,优点 性能好 反观XML的缺点:解析的开销惊人,不适用于事件性能敏感的场合:为了有较好的可读性,引入一些 ...
- Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下"Protocol ...
- [转]Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...
- Protobuf 简介及简单应用
Protobuf 是 protocol buffers 的缩写. 根据官网的说法, protocol buffers 与平台无关, 与语言无关, 实现数据序列化的一种手段. 正如名字一样, proto ...
- 【转】深入 ProtoBuf - 简介
之前在网络通信和通用数据交换等应用场景中经常使用的技术是 JSON 或 XML,而在最近的开发中接触到了 Google 的 ProtoBuf. 在查阅相关资料学习 ProtoBuf 以及研读其源码之后 ...
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- Google 开源技术protobuf
http://blog.csdn.net/hguisu/article/details/20721109#0-tsina-1-1601-397232819ff9a47a7b7e80a40613cfe1 ...
随机推荐
- python爬取返利网中值得买中的数据
先使用以前的方法将返利网的数据爬取下来,scrapy框架还不熟练,明日再战scrapy 查找目标数据使用的是beautifulsoup模块. 1.观察网页,寻找规律 打开值得买这块内容 1>分析 ...
- Android NDK 项目依赖简单示例
目录文件结构如图, 进入main目录执行命令 .ndkbuild NDK_MODULE_PATH=../ 说明 .ndkbuild请替换成有效的ndk-build的命令 所有文件下载 http://p ...
- shell中创建mysql库和执行sql脚本
以前执行oracle脚本都是放到plsql中执行 mysql 脚本执行: (1).先创建一个worlddb库 (2).导入sql脚本: 这就ok啦,哈哈.
- Meteor常用技能
调试: 服务器端 console.log() 会输出到终端命令行 客户端的 console.log() 会输出到浏览器控制台 Mongo Shell: 启动方式:meteor mongo 清空数据:m ...
- Unity3D 中的协程
若干文章: 1.Coroutine,你究竟干了什么? 2.Radical Coroutines 3.Extended Unity Coroutines
- for语句嵌套循坏性能的剖析
日常工作中,处理数据难免会遇到遍历,for循环可能是我们用的比较多的了.本节就来探讨下for语句嵌套循环的性能,猜想下面两个语句的性能. 语句1 ; i < ; i++){ ; j < ; ...
- Android 判断字符串是否为空
TextUtils.isEmpty(str) 可以判断字符串是否为null或者"",当是的时候为true,否的时候为false
- blade and soul pvp guide
PvP PvP in Blade and Soul is categorized into two types, a personal PvP called Arena and a large-sca ...
- MIUI5(红米、小米)打开开发者模式
在miui5系统中系统默认隐藏原生android的开发者模式选项,要想启动该模式需要按照以下操作: 设置-关于手机- 连续点击安卓版本4下. 然后再返回主设置页面下,你会发现开发者选项已经出现.
- sql 生成指定相同数量数据
select *from 表名 ,(SELECT NUMBER FROM master..spt_values WHERE number BETWEEN 1 AND 2 AND TYPE='P' ...