Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化:支持boolean/object数据类型压缩 #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/*
*.log
.DS_Store
.DS_Store
*.idea
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#Pomelo-protobuf
# Pomelo-protobuf
Protobuf protocol is a high efficient binary protocol for data encode, this module implement the protobuf protocol, and used in [pomelo](https://github.com/NetEase/pomelo) for data transfer.
Of course, pomelo-protobuf can also be used independently in other projects.
##Architecture
Unlike the google protobuf, we provide a universal encoder and decoder in pomelo-protobuf. We use protos file as meta data to encode/decode messages, so you do not need to add any code to your project, instead , what you need is to add a protos.json (or two for different encoder and decoder messages) files to define the message need to encode by protobuf.The architecture of pomelo-protobuf is as follow:

![pomelo protobuf](http://pomelo.netease.com/resource/documentImage/protocol/Protobuf_pomelo.png)

##Usage
###Define protos
## Usage
### Define protos
To use pomelo-protobuf, you need to write a JSON file to define the message format. The syntax of the file is as the same as the .proto file in protobuf, but in JSON format, here is the example protos.json:

```
Expand Down Expand Up @@ -38,7 +38,7 @@ Unlike the google protobuf, we write all the protos in the same file, with a uni

To use the protos, we use a parser to parse the protos file into more machine friendly format, which is also a json format, then you can use the result to decode/encode messages.

###RootMessage support
### RootMessage support
you can write rootMessage in protos for global usage
```
{
Expand Down Expand Up @@ -74,7 +74,7 @@ you can write rootMessage in protos for global usage
}
```

###Server side and Client side
### Server side and Client side
Pomelo-protobuf has server code and client code for js.

- The server code run in Node.JS environment, use Buffer to represent the binary data.
Expand Down Expand Up @@ -138,7 +138,7 @@ To use the protbuf as browser, you need to include the /client/protobuf.js in yo
The protobuf will be a global variable, and you need to get the parsed protos from server.
The others are the same as in server side, except the encoder result will by a ByteArray instead of Buffer.

###Compatibility
### Compatibility
For the same message and proto, the encode results are **the same** for **pomelo-protobuf** and **google protobuf** .This means you can exchange binary data with google-protobuf.

Some how we has some changes in the proto file, and there are some features we do not support, there are the different:
Expand Down
73 changes: 46 additions & 27 deletions lib/client/protobuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
var constants = exports.constants = {};

constants.TYPES = {
uInt32 : 0,
sInt32 : 0,
int32 : 0,
double : 1,
string : 2,
message : 2,
float : 5
uInt32: 0,
sInt32: 0,
int32: 0,
double: 1,
string: 2,
message: 2,
float: 5,
boolean: 6,
object: 7
};

})('undefined' !== typeof protobuf ? protobuf : module.exports, this);
Expand All @@ -61,14 +63,17 @@

var Util = exports.util = {};

Util.isSimpleType = function(type){
return ( type === 'uInt32' ||
type === 'sInt32' ||
type === 'int32' ||
type === 'uInt64' ||
type === 'sInt64' ||
type === 'float' ||
type === 'double' );
Util.isSimpleType = function (type) {
return (type === 'uInt32' ||
type === 'sInt32' ||
type === 'int32' ||
type === 'uInt64' ||
type === 'sInt64' ||
type === 'float' ||
type === 'double' ||
type === 'boolean' ||
type === 'object'
);
};

})('undefined' !== typeof protobuf ? protobuf : module.exports, this);
Expand Down Expand Up @@ -374,8 +379,11 @@
return offset;
}

function encodeProp(value, type, offset, buffer, protos){
switch(type){
function encodeProp(value, type, offset, buffer, protos) {
switch (type) {
case 'boolean':
offset = writeBytes(buffer, offset, codec.encodeUInt32(value ? 1: 0));
break;
case 'uInt32':
offset = writeBytes(buffer, offset, codec.encodeUInt32(value));
break;
Expand All @@ -390,7 +398,9 @@
case 'double':
writeBytes(buffer, offset, codec.encodeDouble(value));
offset += 8;
break;
break;
case 'object':
value = JSON.stringify(value);
case 'string':
var length = codec.byteLength(value);

Expand Down Expand Up @@ -549,8 +559,10 @@
};
}

function decodeProp(type, protos){
switch(type){
function decodeProp(type, protos) {
switch (type) {
case 'boolean':
return !!codec.decodeUInt32(getBytes());
case 'uInt32':
return codec.decodeUInt32(getBytes());
case 'int32' :
Expand All @@ -564,12 +576,15 @@
var double = codec.decodeDouble(buffer, offset);
offset += 8;
return double;
case 'string' :
case 'object':
case 'string':
var length = codec.decodeUInt32(getBytes());

var str = codec.decodeStr(buffer, offset, length);
offset += length;

if (type === 'object') {
return JSON.parse(str);
}
return str;
default :
var message = protos && (protos.__messages[type] || MsgDecoder.protos['message ' + type]);
Expand All @@ -584,13 +599,17 @@
}

function decodeArray(array, type, protos){
if(util.isSimpleType(type)){
var length = codec.decodeUInt32(getBytes());

for(var i = 0; i < length; i++){
var length = codec.decodeUInt32(peekBytes());
if(length === 0){
getBytes();// 纯偏移
return ;
}
if (util.isSimpleType(type)) {
getBytes();// 简单类型也要偏移
for (var i = 0; i < length; i++) {
array.push(decodeProp(type));
}
}else{
} else {
array.push(decodeProp(type, protos));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Encoder.encodeUInt32 = function(num){
}

var result = [];
do{
do {
var tmp = n % 128;
var next = Math.floor(n/128);

Expand Down
18 changes: 10 additions & 8 deletions lib/constant.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module.exports = {
TYPES : {
uInt32 : 0,
sInt32 : 0,
int32 : 0,
double : 1,
string : 2,
message : 2,
float : 5
TYPES: {
uInt32: 0,
sInt32: 0,
int32: 0,
double: 1,
string: 2,
message: 2,
float: 5,
boolean: 6,
object: 7
}
}
Loading