Protobuf

ProtoBuf介绍

protobuf(Protocol Buffers )是google的开源项目,官网见:click这里,源码见:github。更准确的官方描述是:protobuf是google的中立于语言,平台,可扩展的用于序列化结构化数据的解决方案。
简单的说,protobuf是用来对数据进行序列化和反序列化。那么什么是数据的序列化和反序列化呢?见下文。
protobuf支持目前主流的开发语言,包括C++、Java、Python、Objective-C、C#、JavaNano、JavaScript、Ruby、Go、PHP等。只要你使用以上语言,都可以用protobuf来序列化和反序列化你的数据。

ProtoBuf下载安装


$ git clone https://github.com/google/protobuf.git
$ cd protobuf
$ git submodule update --init --recursive
$ ./autogen.sh

在使用autogen时需要注意环境配置:
* autoconf
* automake
* libtool
* make
* g++
* unzip

以上操作完成后执行:

$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.

ProtoBuf使用

注意事项

ProtoBuf传输大小

在传输消息时经常遇提示 Too Big的情况, 头都抓破了, 最后查阅后发现要想突破这个的方法.可以参考以下:
1.

		//std::string str(buf, size);
		::google::protobuf::io::CodedInputStream decoder((const unsigned __int8*)buf, size); // buf 为对应的接收到的数据.

		decoder.SetTotalBytesLimit(1024 * 1024 * 1024, 64 * 1024 * 1024);
		bool success = msg.ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();

posted @ 2018-05-22 00:59  CircleMono  阅读(308)  评论(0编辑  收藏  举报