From 2c3f3ac3c67852eccc91e1c68e249c3a34920b95 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 23 Nov 2015 12:47:29 +0800 Subject: [PATCH] more sproto wire protocol examples --- lualib-src/sproto/README.md | 92 +++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/lualib-src/sproto/README.md b/lualib-src/sproto/README.md index e397549d..8fcf5e34 100644 --- a/lualib-src/sproto/README.md +++ b/lualib-src/sproto/README.md @@ -219,10 +219,28 @@ If n is even (and not zero), the value of this field is n/2-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 +} +``` + Example 1: ``` @@ -243,7 +261,8 @@ person { name = "Bob", age = 40, children = { - { name = "Alice" , age = 13, marital = false }, + { name = "Alice" , age = 13 }, + { name = "Carol" , age = 5 }, } } @@ -256,13 +275,76 @@ person { 03 00 00 00 (sizeof "Bob") 42 6F 62 ("Bob") -11 00 00 00 (sizeof struct) -03 00 (fn = 3) +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) -02 00 (id = 2, value = false) 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, value in data part) + +15 00 00 00 (sizeof numbers) +04 ( sizeof int32 ) +01 00 00 00 (1) +02 00 00 00 (2) +03 00 00 00 (3) +04 00 00 00 (4) +05 00 00 00 (5) +``` + +Example 4: +``` +data { + numbers = { + (1<<32)+1, + (1<<32)+2, + (1<<32)+3, + } +} + +01 00 (fn = 1) +00 00 (id = 0, value in data part) + +19 00 00 00 (sizeof numbers) +08 ( sizeof int64 ) +01 00 00 00 01 00 00 00 ( (1<32) + 1) +02 00 00 00 01 00 00 00 ( (1<32) + 2) +03 00 00 00 01 00 00 00 ( (1<32) + 3) +``` + +Example 5: +``` +data { + bools = { false, true, false } +} + +02 00 (fn = 2) +01 00 (skip id) +00 00 (id = 2, value in data part) + +03 00 00 00 (sizeof bools) +00 (false) +01 (true) +00 (false) ``` 0 Packing @@ -270,7 +352,7 @@ person { The algorithm is very similar to [Cap'n proto](http://kentonv.github.io/capnproto/), but 0x00 is not treated specially. -In packed format, the message if padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. +In packed format, the message is padding to 8. Each 8 byte is reduced to a tag byte followed by zero to eight content bytes. The bits of the tag byte correspond to the bytes of the unpacked word, with the least-significant bit corresponding to the first byte. Each zero bit indicates that the corresponding byte is zero. The non-zero bytes are packed following the tag.