protobuf的使用:

项目地址:https://github.com/chrisdew/protobuf

peed

The Protobuf for Node add-on relies on the protobuf C++ runtime but it does not require any generation, compilation or linkage of generated C++ code. It works reflectively and is thus able to deal with arbitrary schemas. This means however, that it is not as fast as with generated code. Simple measurements show that unmarshalling is about between 20% and 50% faster than V8's native JSON support.

Calling C++ services is faster since it avoids marshalling to bytes but transfers data directly between the JS objects and the protocol messages. Also, in this case you compile the generated service interface into your code, so reflection is faster.

Building and running

You need to have node.js 0.4 and protobuf 2.4.0a installed. protobuf_for_node depends on the respective versions of v8 and eio shipped by node. Use branch node2 for node0.2/protobuf 2.3.0. If you want to use a later protobuf version, you may need to re-generate the example protoservice code: cd example; protoc --cpp_out=. protoservice.proto.

To build protobuf for node, do:

PROTOBUF=<protobuf prefix> /path/to/node/bin/node-waf configure clean build

This will build a) the protobuf_for_node library (build/default/libprotobuf_for_node_lib.so). It is linked to by your service-exporting add-ons and by the b) protobuf_for_node add-on (build/default/protobuf_for_node.node).

You need a) and the protobuf library in your DYLD_LIBRARY_PATH and b) in your NODE_PATH if you require('protobuf_for_node').

 

1.

需要一个*.proto文件,可以生成desc文件,是使用google的一个命令行工具:protoc --descriptor_set_out=buftest.desc --include_imports buftest.proto生成的。运行了出错,没有这个命令行工具。查找下载,发现无法访问网站,需要设置代理,设置代理后:具体教程在:http://dbua.iteye.com/blog/1633079http://www.cnblogs.com/Anker/archive/2013/07/24/3209764.html,已测试可以正常生成desc文件

*.proto文件基本格式如下:message Person {
required string name = 1;
required int32 id = 2;
ptional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}

.proto文件编写规则:

Proto支持的数据类型:http://blog.csdn.net/superbfly/article/details/17920383

1)对于required的字段而言,初值是必须要提供的,否则字段的便是未初始化的。在Debug模式的buffer库下编译的话,序列化话的时候可能会失败,而且在反序列化的时候对于该字段的解析会总是失败的。所以,对于修饰符为required的字段,请在序列化的时候务必给予初始化。

    2)对于optional的字段而言,如果未进行初始化,那么一个默认值将赋予该字段,当然也可以指定默认值,如上述proto定义中的PhoneType字段类型。

    3)对于repeated的字段而言,该字段可以重复多个,google提供的这个addressbook例子便有个很好的该修饰符的应用场景,即每个人可能有多个电话号码。在高级语言里面,我们可以通过数组来实现,而在proto义文件中可以使用repeated来修饰,从而达到相同目的。当然,出现0次也是包含在内的。      

    其中字段标签标示了字段在二进制流中存放的位置,这个是必须的,而且序列化与反序列化的时候相同的字段的Tag值必须对应,否则反序列化会出现意想不到的问题。

Prot文件编写范例:http://my.oschina.net/cxh3905/blog/293000

2.在项目中使用'protobuf',结合我们编写的.protobuf文件生成的desc文件,对数据进行序列化。(使用demo测试成功)

代码:

var fs = require('fs');var Schema = require('protobuf').Schema; // "schema" contains all message types defined in buftest.proto|desc.var schema = new Schema(fs.readFileSync('buftest.desc')); // The "BufTest" message.var BufTest = schema['com.chrisdew.buftest.BufTest']; var ob = { num: 42 };ob.payload = new Buffer("Hello World"); var proto = BufTest.serialize(ob);console.log('proto.length:', proto.length); var outOb = BufTest.parse(proto);console.log('unserialised:', JSON.stringify(outOb)); var payload = new Buffer(outOb.payload);console.log(payload);

 

如果有和客户端进行数据传输,客户端也必须有对应解码文件,才能对我们发过去的数据进行解码,as3的在这里有说明:http://bbs.9ria.com/thread-62331-1-1.html

protoBuf更详细的教程:http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/