Introduction
======
Sproto is an efficient serialization library for C, and focuses on lua binding. It's like Google protocol buffers, but much faster.
The design is simple. It only supports a few types that lua supports. It can be easily bound to other dynamic languages, or be used directly in C.
In my i5-2500 @3.3GHz CPU, the benchmark is below:
The schema in sproto:
```
.Person {
name 0 : string
id 1 : integer
email 2 : string
.PhoneNumber {
number 0 : string
type 1 : integer
}
phone 3 : *PhoneNumber
}
.AddressBook {
person 0 : *Person
}
```
It's equal to:
```
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
message PhoneNumber {
required string number = 1;
optional int32 type = 2 ;
}
repeated PhoneNumber phone = 4;
}
message AddressBook {
repeated Person person = 1;
}
```
Use the data:
```lua
local ab = {
person = {
{
name = "Alice",
id = 10000,
phone = {
{ number = "123456789" , type = 1 },
{ number = "87654321" , type = 2 },
}
},
{
name = "Bob",
id = 20000,
phone = {
{ number = "01234567890" , type = 3 },
}
}
}
}
```
library| encode 1M times | decode 1M times | size
-------| --------------- | --------------- | ----
sproto | 2.15s | 7.84s | 83 bytes
sproto (nopack) |1.58s | 6.93s | 130 bytes
pbc-lua | 6.94s | 16.9s | 69 bytes
lua-cjson | 4.92s | 8.30s | 183 bytes
* pbc-lua is a google protocol buffers library https://github.com/cloudwu/pbc
* lua-cjson is a json library https://github.com/efelix/lua-cjson
Lua API
=======
```lua
local parser = require "sprotoparser"
```
* `parser.parse` parses a sproto schema to a binary string.
The parser is needed for parsing the sproto schema. You can use it to generate binary string offline. The schema text and the parser is not needed when your program is running.
```lua
local sproto = require "sproto.core"
```
* `sproto.newproto(sp)` creates a sproto object by a schema string (generates by parser).
* `sproto.querytype(sp, typename)` queries a type object from a sproto object by typename.
* `sproto.encode(st, luatable)` encodes a lua table by a type object, and generates a string message.
* `sproto.decode(st, message)` decodes a message string generated by sproto.encode with type.
* `sproto.pack(sprotomessage)` packs a string encoded by sproto.encode to reduce the size.
* `sproto.unpack(packedmessage)` unpacks the string packed by sproto.pack.
The sproto supports protocol tag for RPC. Use `sproto.protocol(tagorname)` to convert the protocol name to the tag id, or convert back from tag id to the name, and returns the request/response message type objects of this protocol.
RPC API
=======
There is a lua wrapper for the core API for RPC .
Read testrpc.lua for detail.
Schema Language
==========
Like Protocol Buffers (but unlike json), sproto messages are strongly-typed and are not self-describing. You must define your message structure in a special language.
You can use sprotoparser library to parse the schema text to a binary string, so that the sproto library can use it.
You can parse them offline and save the string, or you can parse them during your program running.
The schema text is like this:
```
# This is a comment.
.Person { # . means a user defined type
name 0 : string # string is a build-in type.
id 1 : integer
email 2 : string
.PhoneNumber { # user defined type can be nest.
number 0 : string
type 1 : integer
}
phone 3 : *PhoneNumber # *PhoneNumber means an array of PhoneNumber.
}
.AddressBook {
person 0 : *Person(id) # (id) is optional, means Person.id is main index.
}
foobar 1 { # define a new protocol (for RPC used) with tag 1
request Person # Associate the type Person with foobar.request
response { # define the foobar.response type
ok 0 : boolean
}
}
```
A schema text can be self-described by the sproto schema language.
```
.type {
.field {
name 0 : string
buildin 1 : integer
type 2 : integer
tag 3 : integer
array 4 : boolean
key 5 : integer # If key exists, array must be true, and it's a map.
}
name 0 : string
fields 1 : *field
}
.protocol {
name 0 : string
tag 1 : integer
request 2 : integer # index
response 3 : integer # index
}
.group {
type 0 : *type
protocol 1 : *protocol
}
```
Types
=======
* **string** : binary string
* **integer** : integer, the max length of an integer is signed 64bit.
* **boolean** : true or false
You can add * before the typename to declare an array.
You can also specify a main index, the array whould be encode as an unordered map.
User defined type can be any name in alphanumeric characters except the build-in typenames, and nested types are supported.
* Where are double or real types?
I have been using Google protocol buffers for many years in many projects, and I found the real types were seldom used. If you really need it, you can use string to serialize the double numbers.
* Where is enum?
In lua, enum types are not very useful. You can use integer to define an enum table in lua.
Wire protocol
========
Each integer number must be serialized in little-endian format.
The sproto message must be a user defined type struct, and a struct is encoded in three parts. The header, the field part, and the data part.
The tag and small integer or boolean will be encoded in field part, and others are in data part.
All the fields must be encoded in ascending order (by tag, base 0). The tags of fields can be discontinuous, if a field is nil. (default value in lua), don't encode it in message.
The header is a 16bit integer. It is the number of fields.
Each field in field part is a 16bit integer (n). If n is zero, that means the field data is encoded in data part ;
If n is even (and not zero), the value of this field is n/2-1 , and the tag increases 1;
If n is odd, that means the tags is not continuous, and we should add current tag by (n+1)/2 .
Arrays are always encode in data part, 4 bytes header for the size, and the following bytes is the contents. See the example 2 for the struct array; example 3/4 for the integer array ; example 5 for the boolean array.
Fot integer array, an additional byte (4 or 8) to indicate the value is 32bit or 64bit.
Read the examples below to see more details.
Notice: If the tag is not declared in schema, the decoder will simply ignore the field for protocol version compatibility.
```
.Person {
name 0 : string
age 1 : integer
marital 2 : boolean
children 3 : *Person
}
.Data {
numbers 0 : *integer
bools 1 : *boolean
number 2 : integer
bignumber 3 : integer
}
```
Example 1:
```
person { name = "Alice" , age = 13, marital = false }
03 00 (fn = 3)
00 00 (id = 0, value in data part)
1C 00 (id = 1, value = 13)
02 00 (id = 2, value = false)
05 00 00 00 (sizeof "Alice")
41 6C 69 63 65 ("Alice")
```
Example 2:
```
person {
name = "Bob",
age = 40,
children = {
{ name = "Alice" , age = 13 },
{ name = "Carol" , age = 5 },
}
}
04 00 (fn = 4)
00 00 (id = 0, value in data part)
52 00 (id = 1, value = 40)
01 00 (skip id = 2)
00 00 (id = 3, value in data part)
03 00 00 00 (sizeof "Bob")
42 6F 62 ("Bob")
26 00 00 00 (sizeof children)
0F 00 00 00 (sizeof child 1)
02 00 (fn = 2)
00 00 (id = 0, value in data part)
1C 00 (id = 1, value = 13)
05 00 00 00 (sizeof "Alice")
41 6C 69 63 65 ("Alice")
0F 00 00 00 (sizeof child 2)
02 00 (fn = 2)
00 00 (id = 0, value in data part)
0C 00 (id = 1, value = 5)
05 00 00 00 (sizeof "Carol")
43 61 72 6F 6C ("Carol")
```
Example 3:
```
data {
numbers = { 1,2,3,4,5 }
}
01 00 (fn = 1)
00 00 (id = 0, v
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
skynet实现的斗地主服务端源码 (1400个子文件)
0028ce9e741071852a07cd17604bf7857e0f4a 153B
0028ce9e741071852a07cd17604bf7857e0f4a 153B
04dc7ae2fd5d37fa393d92b52f44ffcc80a99d 11KB
04dc7ae2fd5d37fa393d92b52f44ffcc80a99d 11KB
04dd950539b949b36838a1624ab0358e9aafc9 259B
04dd950539b949b36838a1624ab0358e9aafc9 259B
06077a224b04d4d45cfc1b5ad8c2995025c98e 4KB
06077a224b04d4d45cfc1b5ad8c2995025c98e 4KB
0b380ab6ebc9b374cce66465a5a5f3c206863e 690B
0b380ab6ebc9b374cce66465a5a5f3c206863e 690B
0e372dfcf262de73e0370fff2787bb5d7231ee 751B
0e372dfcf262de73e0370fff2787bb5d7231ee 751B
2b16dadf43572e321544ce07a625d01d331e35 83B
2b16dadf43572e321544ce07a625d01d331e35 83B
2cfa416baf1947a9845f55096bbb7841a3e4be 3KB
2cfa416baf1947a9845f55096bbb7841a3e4be 3KB
32d6a9087c96bc28121f21073e00ddd1db8561 374B
32d6a9087c96bc28121f21073e00ddd1db8561 374B
3507c5ed5fc4a38f0c7fd01e869025241fc4bc 155B
3507c5ed5fc4a38f0c7fd01e869025241fc4bc 155B
365cdd46a0b6f274c1a336676b9bfaa0cdc814 828B
365cdd46a0b6f274c1a336676b9bfaa0cdc814 828B
3c2937a99bf7058f3ae247d2311dbd3704f578 567B
3c2937a99bf7058f3ae247d2311dbd3704f578 567B
3d309c3cefbe60fd3a53417dcea497c2b10068 1KB
3d309c3cefbe60fd3a53417dcea497c2b10068 1KB
3d3b39bba4aab05f4681b336be7df391ad86e0 11KB
3d3b39bba4aab05f4681b336be7df391ad86e0 11KB
42c7fc7af407024cca2cb3c5e3d0e97bedafd4 774B
42c7fc7af407024cca2cb3c5e3d0e97bedafd4 774B
452f99c87a999e983958bb53cb9039be7304e7 125B
452f99c87a999e983958bb53cb9039be7304e7 125B
467c45759a8d3ef1f0081c23e5b571ef4d0dc9 623B
467c45759a8d3ef1f0081c23e5b571ef4d0dc9 623B
49944cdd185a7e0ba931511922c02bdfa72819 760B
49944cdd185a7e0ba931511922c02bdfa72819 760B
50c4b5152de0e9c27f67f210b2dfbc1eb8c8a5 11KB
50c4b5152de0e9c27f67f210b2dfbc1eb8c8a5 11KB
53e932d2c5ac0ff02f271b6732487d2314175c 374B
53e932d2c5ac0ff02f271b6732487d2314175c 374B
57782bb4bbffbe6cb50cc88040e4a101c7e8b2 754B
57782bb4bbffbe6cb50cc88040e4a101c7e8b2 754B
5e4e6fbdc45d9d21717e220ee3482b29e4ed79 215B
5e4e6fbdc45d9d21717e220ee3482b29e4ed79 215B
606823eadce61d0e0e6d3a282d77f19a9019e4 627B
606823eadce61d0e0e6d3a282d77f19a9019e4 627B
6079151bf2e1ba2efaf37e4d4d12e03894f482 175B
6079151bf2e1ba2efaf37e4d4d12e03894f482 175B
66580c5850986422405bc3cdf740f6629fc17c 313B
66580c5850986422405bc3cdf740f6629fc17c 313B
6b88b9f20db22f8db979208f9a9c2ed29db285 674B
6b88b9f20db22f8db979208f9a9c2ed29db285 674B
6ba80fe7aa36a1246369e609545808ff5ebec8 5KB
6ba80fe7aa36a1246369e609545808ff5ebec8 5KB
6d77aace7821c32929c7477f86bc246420fab1 117B
6d77aace7821c32929c7477f86bc246420fab1 117B
71e3c6f9c2d206307cfe50ba731f381655b70b 170B
71e3c6f9c2d206307cfe50ba731f381655b70b 170B
77235cfaeafd4968df5e01b964325e8d6f9ced 872B
77235cfaeafd4968df5e01b964325e8d6f9ced 872B
791f1b31cb3ea51176ec8797fde4f5874bb5dc 220B
791f1b31cb3ea51176ec8797fde4f5874bb5dc 220B
7bb20785fc0540369286e2e8ac801c15f2b2b6 662B
7bb20785fc0540369286e2e8ac801c15f2b2b6 662B
7ca0cac64c9f20bf0dedd7cdfc80bae1a27ecf 624B
7ca0cac64c9f20bf0dedd7cdfc80bae1a27ecf 624B
86f9b3c82050fa6f82dd945ec9db2127b682f2 484B
86f9b3c82050fa6f82dd945ec9db2127b682f2 484B
89450e194086c5f9feba8c7addee0aa2ca78b2 5KB
89450e194086c5f9feba8c7addee0aa2ca78b2 5KB
8ecac3ff482c55e305206aed5b34f717b18a65 567B
8ecac3ff482c55e305206aed5b34f717b18a65 567B
904e4c3e910ded26f0e80d18b1d336c49d08da 281B
904e4c3e910ded26f0e80d18b1d336c49d08da 281B
924ae4bbfb2f3151198d2f4f39c37696c653bb 374B
924ae4bbfb2f3151198d2f4f39c37696c653bb 374B
947a019b677bc528054c2827bc4bbd22efc1e5 167B
947a019b677bc528054c2827bc4bbd22efc1e5 167B
99eacb6d4d06089c0b98a710e5181488b124ba 11KB
99eacb6d4d06089c0b98a710e5181488b124ba 11KB
9a10df5c0097517dcda47864ee388fdd1be1b5 1KB
9a10df5c0097517dcda47864ee388fdd1be1b5 1KB
9a6eb000a8de522748e7458cbd8ae0860292c0 11KB
9a6eb000a8de522748e7458cbd8ae0860292c0 11KB
9b75db82ca7c0a5e710d2b6816df3b8bcbf9a1 163B
9b75db82ca7c0a5e710d2b6816df3b8bcbf9a1 163B
9dcac0cdfffc2362e5dd5a0a7d88e774456656 536B
9dcac0cdfffc2362e5dd5a0a7d88e774456656 536B
liblua.a 423KB
liblua.a 326KB
a8569a558fda77e636655a9ecdc4cf8d1705a1 378B
a8569a558fda77e636655a9ecdc4cf8d1705a1 378B
configure.ac 57KB
b0a6d01adb53330bd1bcb893bf8227955e8363 1KB
b0a6d01adb53330bd1bcb893bf8227955e8363 1KB
b85c24b029af8604ed7921b2e47f9509f8d429 281B
b85c24b029af8604ed7921b2e47f9509f8d429 281B
Makefile.bak 2KB
z.bat 237B
beb0409fa3cebf59feb3989fd8ae7ec9cddfe0 374B
共 1400 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
haihsl123456789
- 粉丝: 21
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于paddle搭建神经网络实现水果识别分类python源码+数据集(高分项目).zip
- 三菱plc编程口通信学习笔记.doc
- 与HIS接口解决方案...doc
- 专利技术交底书-案例模板-软件类-样板示例.doc
- C# 超简单的人脸识别库 .zip
- 实现人脸识别资源代码.zip
- 东北大学-微机原理实验报告及答案-8255并行接口.doc
- 实现人脸检测,人脸识别,人脸标记68个坐标点 人脸对齐 人脸特征测试等 同时可以自行训练人脸识别库.zip
- 基于中科视拓 seetaface6基础源码和JNI源码编译而来的项目.zip
- Python基于opencv人脸识别的考勤系统源码+代码说明(毕业设计).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功