diff --git a/README.md b/README.md index bc7b5d1..704c9c7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ Use ----- rebar3 escriptize生成的genProto genPtoto.cmd 在_build/default/bin下面 将其复制到可以被搜索的执行路径或者工作目录 - genProto ProtoInputDir HrlOutDir ErlOutDir + genProto ProtoInputDir Lang("erl", "lua", "cs") OutDir(erl 存在两个输出目录 头文件和Erl文件的输出目录 lua和cs只有一个输入目录) + eg: genProto:convertDir(["./proto", "erl", "./test/erl/", "./test/erl/", "lua", "./test/lua/", "cs", "./test/cs/"]). # 简单描述 @@ -21,9 +22,9 @@ Use # 支持的数据类型 - erl: int8 uint8 int16 uint16 int32 uint32 int64 uint64 integer(整数 64位) number(整数或者浮点数 64位) string float(32位浮点数) double(64位浮点数) bool record(struct) 以及上面类型的列表 - lua(待修正): int8 uint8 int16 uint16 int32 uint32 int64 uint64 integer(整数 64位) number(整数或者浮点数 64位) string float(32位浮点数) double(64位浮点数) bool record 以及上面类型的列表 - c#(待修正): int8 uint8 int16 uint16 int32 uint32 int64 uint64 integer(整数 64位) number(整数或者浮点数 64位) string float(32位浮点数) double(64位浮点数) bool record 以及上面类型的列表 + erl: int8 uint8 int16 uint16 int32 uint32 int64 uint64 integer number(整数或者浮点数) string float(32位浮点数) double(64位浮点数) bool record(struct) 以及上面类型的列表 + lua: int8 uint8 int16 uint16 int32 uint32 int64 uint64 integer number(整数或者浮点数) string float(32位浮点数) double(64位浮点数) bool record 以及上面类型的列表 + c#: int8 uint8 int16 uint16 int32 uint32 int64 uint64 string float(32位浮点数) double(64位浮点数) bool record 以及上面类型的列表 # 各种数据类型的编码格式(字节序大端存储, string用utf8编码) @@ -44,9 +45,6 @@ Use record(struct): 如果是undefined 或者是空指针 则 8bit Tag 值为0, 否则 8bit的tag 值为1 + record的二进制数据 list_+上面的数据类型的时候: 16bit的tag 用于存放 数组的长度 + 按数组顺序序列化每个元素的值的二进制数据(注意:如果列表类型为record(struct) 每个值序列化的时候并不会加 8bit的tag, 直接存record的二进制数据) -### maybe TODO - lua 支持 integer number - ### 关于消息接收转发解码和发送 erlang通常会将接收到的消息由网关进程转发给其他工作进程, 建议先匹配消息id, 然后转发二进制消息到工作进程,然后由工作进程解码再处理 @@ -218,7 +216,3 @@ Use 49,50,51,52,53,54,55,...>> 67> byte_size(BAddr). 83 - - - - diff --git a/src/writeLua/gLuaGen.erl b/src/writeLua/gLuaGen.erl index 38b8e15..e36e97a 100644 --- a/src/writeLua/gLuaGen.erl +++ b/src/writeLua/gLuaGen.erl @@ -23,7 +23,7 @@ spellDecode(FieldList) -> <>. spellBuild(MsgId) -> - <<"\ttb.build = function(byteArray)\n\t\tbyteArray.write_uint16(", (integer_to_binary(MsgId))/binary, ")\n\t\treturn tb.encode(byteArray)\n\tend\n\n">>. + <<"\ttb.build = function(byteArray)\n\t\tbyteArray.setBytes({})\n\t\tbyteArray.write_uint16(", (integer_to_binary(MsgId))/binary, ")\n\t\treturn tb.encode(byteArray)\n\tend\n\n">>. spellEnd() -> <<"\treturn tb\nend\n\n">>. diff --git a/test/lua/ByteArray.lua b/test/lua/ByteArray.lua index b86a003..be21939 100644 --- a/test/lua/ByteArray.lua +++ b/test/lua/ByteArray.lua @@ -3,7 +3,7 @@ function ByteArray() local mPos = 1 -- 读写位置 local ba = {} - -- 设置字节流 解码先设置从tcp获取得来的字节数据 + -- 设置字节流 解码先设置从tcp获取得来的字节数据 或者编码之前先重置 mBuf 和 mPos ba.setBytes = function(buf) mBuf = buf mPos = 1 -- 这里必须重置读写位置为1,方能保证接下去的读操作正确 diff --git a/test/lua/protoMsg.lua b/test/lua/protoMsg.lua index 335a5b9..e56ae8f 100644 --- a/test/lua/protoMsg.lua +++ b/test/lua/protoMsg.lua @@ -13,6 +13,7 @@ function test() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(1) return tb.encode(byteArray) end @@ -49,6 +50,7 @@ function phoneNumber() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(2) return tb.encode(byteArray) end @@ -89,6 +91,7 @@ function person() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(3) return tb.encode(byteArray) end @@ -132,6 +135,7 @@ function addressBook() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(4) return tb.encode(byteArray) end @@ -157,6 +161,7 @@ function union() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(5) return tb.encode(byteArray) end @@ -179,6 +184,7 @@ function tbool() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(6) return tb.encode(byteArray) end @@ -204,6 +210,7 @@ function tint8() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(7) return tb.encode(byteArray) end @@ -229,6 +236,7 @@ function tuint8() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(8) return tb.encode(byteArray) end @@ -254,6 +262,7 @@ function tint16() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(9) return tb.encode(byteArray) end @@ -279,6 +288,7 @@ function tuint16() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(10) return tb.encode(byteArray) end @@ -328,6 +338,7 @@ function tint32() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(11) return tb.encode(byteArray) end @@ -353,6 +364,7 @@ function tuint32() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(12) return tb.encode(byteArray) end @@ -378,6 +390,7 @@ function tint64() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(13) return tb.encode(byteArray) end @@ -403,6 +416,7 @@ function tuint64() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(14) return tb.encode(byteArray) end @@ -446,6 +460,7 @@ function tinteger() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(15) return tb.encode(byteArray) end @@ -495,6 +510,7 @@ function tnumber() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(16) return tb.encode(byteArray) end @@ -520,6 +536,7 @@ function tfloat() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(17) return tb.encode(byteArray) end @@ -545,6 +562,7 @@ function tdouble() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(18) return tb.encode(byteArray) end @@ -570,6 +588,7 @@ function tstring() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(19) return tb.encode(byteArray) end @@ -599,6 +618,7 @@ function tlistbool() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(20) return tb.encode(byteArray) end @@ -628,6 +648,7 @@ function tlistint8() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(21) return tb.encode(byteArray) end @@ -657,6 +678,7 @@ function tlistuint8() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(22) return tb.encode(byteArray) end @@ -686,6 +708,7 @@ function tlistint16() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(23) return tb.encode(byteArray) end @@ -715,6 +738,7 @@ function tlistuint16() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(24) return tb.encode(byteArray) end @@ -744,6 +768,7 @@ function tlistint32() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(25) return tb.encode(byteArray) end @@ -773,6 +798,7 @@ function tlistuint32() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(26) return tb.encode(byteArray) end @@ -802,6 +828,7 @@ function tlistint64() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(27) return tb.encode(byteArray) end @@ -831,6 +858,7 @@ function tlistuint64() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(28) return tb.encode(byteArray) end @@ -860,6 +888,7 @@ function tlistinteger() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(29) return tb.encode(byteArray) end @@ -889,6 +918,7 @@ function tlistnumber() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(30) return tb.encode(byteArray) end @@ -918,6 +948,7 @@ function tlistfloat() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(31) return tb.encode(byteArray) end @@ -947,6 +978,7 @@ function tlistdouble() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(32) return tb.encode(byteArray) end @@ -976,6 +1008,7 @@ function tliststring() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(33) return tb.encode(byteArray) end @@ -1007,6 +1040,7 @@ function tlistunion() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(34) return tb.encode(byteArray) end @@ -1365,6 +1399,7 @@ function allType() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(35) return tb.encode(byteArray) end @@ -1384,6 +1419,7 @@ function testnull() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(36) return tb.encode(byteArray) end @@ -1424,6 +1460,7 @@ function person1() end tb.build = function(byteArray) + byteArray.setBytes({}) byteArray.write_uint16(1001) return tb.encode(byteArray) end