瀏覽代碼

ft: lua序列化代码修正

master
SisMaker 1 年之前
父節點
當前提交
2e38712d88
共有 8 個檔案被更改,包括 480 行新增1133 行删除
  1. +4
    -4
      src/writeErl/gErlCode.erl
  2. +4
    -4
      src/writeErl/gErlGen.erl
  3. +2
    -0
      src/writeLua/gLuaField.erl
  4. +1
    -1
      src/writeLua/gLuaGen.erl
  5. +4
    -4
      test/erl/protoMsg.erl
  6. +284
    -248
      test/lua/ByteArray.lua
  7. +0
    -271
      test/lua/ByteArray_back.lua
  8. +181
    -601
      test/lua/protoMsg.lua

+ 4
- 4
src/writeErl/gErlCode.erl 查看文件

@ -12,10 +12,10 @@
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(minF32, 1.175494351E-38).
-define(maxF32, 3.402823466E+38).
-define(minF64, 2.2250738585072014E-308).
-define(maxF64, 1.7976931348623158E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).

+ 4
- 4
src/writeErl/gErlGen.erl 查看文件

@ -31,10 +31,10 @@ protoErlHeader() ->
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(minF32, 1.175494351E-38).
-define(maxF32, 3.402823466E+38).
-define(minF64, 2.2250738585072014E-308).
-define(maxF64, 1.7976931348623158E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).

+ 2
- 0
src/writeLua/gLuaField.erl 查看文件

@ -27,6 +27,8 @@
, {<<"uint64">>, <<"uint64">>, <<"0">>}
, {<<"float">>, <<"float">>, <<"0">>}
, {<<"double">>, <<"double">>, <<"0">>}
, {<<"integer">>, <<"integer">>, <<"0">>}
, {<<"number">>, <<"number">>, <<"0">>}
, {<<"string">>, <<"string">>, <<"\"\"">>}
]).

+ 1
- 1
src/writeLua/gLuaGen.erl 查看文件

@ -13,7 +13,7 @@ spellMember(FieldList) ->
spellEncode(FieldList) ->
EnHead = <<"\n\ttb.encode = function(byteArray)\n">>,
EnBody = <<<<(gLuaField:builtEncodeStr(OneTypeName))/binary>> || OneTypeName <- FieldList>>,
EnEnd = <<"\t\treturn byteArray\n\tend\n\n">>,
EnEnd = <<"\t\treturn byteArray.getBytes()\n\tend\n\n">>,
<<EnHead/binary, EnBody/binary, EnEnd/binary>>.
spellDecode(FieldList) ->

+ 4
- 4
test/erl/protoMsg.erl 查看文件

@ -14,10 +14,10 @@
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(minF32, 1.175494351E-38).
-define(maxF32, 3.402823466E+38).
-define(minF64, 2.2250738585072014E-308).
-define(maxF64, 1.7976931348623158E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).

+ 284
- 248
test/lua/ByteArray.lua 查看文件

@ -1,251 +1,287 @@
function ByteArray()
local mRadix = {[8]="%03o", [10]="%03u", [16]="%02X"} -- 进制
local mBuf = {} -- 二进制字节流
local mPos = 1 -- 读写位置
-- 验证读写位置
local function checkAvailable()
assert(#mBuf >= mPos, string.format("End of file was encountered. pos: %d, length: %d.", mPos, #mBuf))
end
-- 获取字符码
local function getLetterCode(fmt)
fmt = fmt or ""
return ">"..fmt
end
-- 读单个字节
local function readRawByte()
checkAvailable()
local rawByte = mBuf[mPos]
mPos = mPos + 1
return rawByte
end
-- 写单个字节
local function writeRawByte(rawByte)
if mPos > #mBuf + 1 then
for i=#mBuf + 1, mPos - 1 do
mBuf[i] = string.char(0)
end
end
mBuf[mPos] = rawByte
mPos = mPos + 1
end
-- 读字节流
local function readBuf(length)
checkAvailable()
local buf = table.concat(mBuf, "", mPos, mPos + length - 1)
mPos = mPos + length
return buf
end
-- 写字节流
local function writeBuf(buf)
for i=1, #buf do
writeRawByte(buf:sub(i, i))
end
end
-- 读字符串
local function read_string_bytes(length)
if 0 == length then
return ""
end
local tmp, value = string.unpack(readBuf(length), getLetterCode("A"..length))
return value
end
-- 写字符串
local function write_string_bytes(value)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
----------------------------------------------------------------------
-- public method
----------------------------------------------------------------------
local ba = {}
-- 设置字节流
ba.setBytes = function(buf)
if #mBuf > 0 then
return
end
writeBuf(buf)
mPos = 1 -- 这里必须重置读写位置为1,方能保证接下去的读操作正确
end
-- 获取字节流
ba.getBytes = function()
local bytes = {}
for i=1, #mBuf do
bytes[#bytes+1] = string.byte(mBuf[i])
end
local packRes = string.pack(getLetterCode("b"..#bytes), unpack(bytes))
return packRes
end
-- 获取字节流长度
ba.getLength = function()
return #mBuf
end
-- 字节流转为字符串,radix-8,10,16
ba.toString = function(radix, separator)
radix = radix or 16
radix = mRadix[radix] or "%02X"
separator = separator or " "
local bytes = {}
for i=1, #mBuf do
bytes[i] = string.format(radix..separator, string.byte(mBuf[i]))
end
return table.concat(bytes)
end
----------------------------------------------------------------------
-- 读16位整型
ba.read_int16 = function()
local tmp, value = string.unpack(readBuf(2), getLetterCode("h"))
return value
end
-- 写16位整型
ba.write_int16 = function(value)
local buf = string.pack(getLetterCode("h"), value)
writeBuf(buf)
end
-- 读16位无符号整型
ba.read_uint16 = function()
local tmp, value = string.unpack(readBuf(2), getLetterCode("H"))
return value
end
-- 写16位无符号整型
ba.write_uint16 = function(value)
local sstr = getLetterCode("H")
local buf = string.pack(sstr, value)
writeBuf(buf)
end
-- 读32位整型
ba.read_int32 = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("i"))
return value
end
-- 写32位整型
ba.write_int32 = function(value)
local buf = string.pack(getLetterCode("i"), value)
writeBuf(buf)
end
-- 读32位无符号整型
ba.read_uint32 = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("I"))
return value
end
-- 写32位无符号整型
ba.write_uint32 = function(value)
local buf = string.pack(getLetterCode("I"), value)
writeBuf(buf)
end
-- 读长整型
ba.read_long = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("l"))
return value
end
-- 写长整型
ba.write_long = function(value)
local buf = string.pack(getLetterCode("l"), value)
writeBuf(buf)
end
-- 读无符号长整型
ba.read_ulong = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("L"))
return value
end
-- 写无符号长整型
ba.write_ulong = function(value)
local buf = string.pack(getLetterCode("L"), value)
writeBuf(buf)
end
-- 读64位整型
ba.read_int64 = function()
-- local tmp, value = string.unpack(readBuf(8), getLetterCode("m"))
-- return value
return read_string_bytes(8)
end
-- 写64位整型
ba.write_int64 = function(value)
-- local buf = string.pack(getLetterCode("m"), value)
-- writeBuf(buf)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
-- 读64位无符号整型
ba.read_uint64 = function()
-- local tmp, value = string.unpack(readBuf(8), getLetterCode("M"))
-- return value
return read_string_bytes(8)
end
-- 写64位无符号整型
ba.write_uint64 = function(value)
-- local buf = string.pack(getLetterCode("M"), value)
-- writeBuf(buf)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
-- 读单精度浮点型
ba.read_float = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("f"))
return value
end
-- 写单精度浮点型
ba.write_float = function(value)
local buf = string.pack(getLetterCode("f"), value)
writeBuf(buf)
end
-- 读双精度浮点型
ba.read_double = function()
local tmp, value = string.unpack(readBuf(8), getLetterCode("d"))
return value
end
-- 写双精度浮点型
ba.write_double = function(value)
local buf = string.pack(getLetterCode("d"), value)
writeBuf(buf)
end
-- 读布尔型
ba.read_bool = function()
return 1 == read_char()
end
-- 写布尔型
ba.write_bool = function(value)
if value then
ba.write_char(1)
else
ba.write_char(0)
end
end
-- 读字符型
ba.read_int8 = function()
local tmp, value = string.unpack(readRawByte(), "c")
return value
end
-- 写字符型
ba.write_int8 = function(value)
writeRawByte(string.pack("c", value))
end
-- 读单字节
ba.read_uint8 = function()
-- 方法1
-- return string.byte(readRawByte())
-- 方法2
local tmp, value = string.unpack(readRawByte(), "b")
return value
end
-- 写单字节
ba.write_uint8 = function(value)
-- 方法1
-- writeRawByte(string.char(value))
-- 方法2
writeRawByte(string.pack("b", value))
end
-- 读字符串
ba.read_string = function()
local length = ba.read_uint16()
return read_string_bytes(length)
end
-- 写字符串
ba.write_string = function(value)
local buf = string.pack(getLetterCode("A"), value)
ba.write_uint16(#buf)
writeBuf(buf)
end
----------------------------------------------------------------------
return ba
local mBuf = {} -- 二进制字节流
local mPos = 1 -- 读写位置
----------------------------------------------------------------------
-- public method
----------------------------------------------------------------------
local ba = {}
-- 设置字节流 解码先设置从tcp获取得来的字节数据
ba.setBytes = function(buf)
mBuf = buf
mPos = 1 -- 这里必须重置读写位置为1,方能保证接下去的读操作正确
end
-- 获取字节流 编码完成之后 通过该接口获取编码之后的字节数据
ba.getBytes = function()
return table.concat(mBuf)
end
-- 字节流转为字符串,radix-8,10,16
ba.toString = function(buff, radix, separator)
radix = radix or 16
if radix == 8 then
radix = "%03o"
elseif radix == 16 then
radix = "%02X"
else
radix = "%03u"
end
separator = separator or " "
local bytes = {}
for i = 1, buff do
bytes[i] = string.format(radix .. separator, string.byte(buff[i]))
end
return table.concat(bytes)
end
-- 读布尔型
ba.read_bool = function()
return 1 == read_int8()
end
-- 写布尔型
ba.write_bool = function(value)
if value then
ba.write_int8(1)
else
ba.write_int8(0)
end
end
-- 读字符型
ba.read_int8 = function()
local value, tPos = string.unpack(">i1", mBuf, mPos)
mPos = tPos
return value
end
-- 写字符型
ba.write_int8 = function(value)
local value = string.pack(">i1", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读单字节
ba.read_uint8 = function()
local value, tPos = string.unpack(">I1", mBuf, mPos)
mPos = tPos
return value
end
-- 写单字节
ba.write_uint8 = function(value)
local value = string.pack(">I1", value)
mBuf[mPos] = value
mPos = mPos + 1
end
----------------------------------------------------------------------
-- 读16位整型
ba.read_int16 = function()
local value, tPos = string.unpack(">i2", mBuf, mPos)
mPos = tPos
return value
end
-- 写16位整型
ba.write_int16 = function(value)
local value = string.pack(">i2", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读16位无符号整型
ba.read_uint16 = function()
local value, tPos = string.unpack(">I2", mBuf, mPos)
mPos = tPos
return value
end
-- 写16位无符号整型
ba.write_uint16 = function(value)
local value = string.pack(">I2", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读32位整型
ba.read_int32 = function()
local value, tPos = string.unpack(">i4", mBuf, mPos)
mPos = tPos
return value
end
-- 写32位整型
ba.write_int32 = function(value)
local value = string.pack(">i4", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读32位无符号整型
ba.read_uint32 = function()
local value, tPos = string.unpack(">I4", mBuf, mPos)
mPos = tPos
return value
end
-- 写32位无符号整型
ba.write_uint32 = function(value)
local value = string.pack(">I4", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读64位符号整型
ba.read_int64 = function()
local value, tPos = string.unpack(">i8", mBuf, mPos)
mPos = tPos
return value
end
-- 写64位整型
ba.write_int64 = function(value)
local value = string.pack(">i8", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读64位无符号整型
ba.read_uint64 = function()
local value, tPos = string.unpack(">I8", mBuf, mPos)
mPos = tPos
return value
end
-- 写64位无符号整型
ba.write_uint64 = function(value)
local value = string.pack(">I8", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读单精度浮点型
ba.read_float = function()
local value, tPos = string.unpack(">f", mBuf, mPos)
mPos = tPos
return value
end
-- 写单精度浮点型
ba.write_float = function(value)
local value = string.pack(">f", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读双精度浮点型
ba.read_double = function()
local value, tPos = string.unpack(">d", mBuf, mPos)
mPos = tPos
return value
end
-- 写双精度浮点型
ba.write_double = function(value)
local value = string.pack(">d", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读字符串
ba.read_string = function()
local value, tPos = string.unpack(">s2", mBuf, mPos)
mPos = tPos
return value
end
-- 写字符串
ba.write_string = function(value)
local value = string.pack(">s2", value)
mBuf[mPos] = value
mPos = mPos + 1
end
-- 读字整数
ba.read_integer = function()
local tag = read_int8()
if tag == 8 then
return read_int8()
elseif tag == 16 then
return read_int16()
elseif tag == 32 then
return read_int32()
elseif tag == 64 then
return read_int64()
end
end
-- 写整数
ba.write_integer = function(value)
if value >= -128 and value <= 127 then
write_int8(8)
write_int8(value)
elseif value >= -32768 and value <= 32767 then
write_int8(16)
write_int16(value)
elseif value >= -2147483648 and value <= 2147483647 then
write_int8(32)
write_int32(value)
elseif value >= -9223372036854775808 and value <= 9223372036854775807 then
write_int8(64)
write_int64(value)
end
end
-- 读字数字
ba.read_integer = function()
local tag = read_int8()
if tag == 8 then
return read_int8()
elseif tag == 16 then
return read_int16()
elseif tag == 32 then
return read_int32()
elseif tag == 64 then
return read_int64()
elseif tag == 33 then
return read_float()
elseif tag == 65 then
return read_double()
end
-- 写数字
ba.write_integer = function(value)
local valueType = math.type(value)
if valueType == 'integer' then
if value >= -128 and value <= 127 then
write_int8(8)
write_int8(value)
elseif value >= -32768 and value <= 32767 then
write_int8(16)
write_int16(value)
elseif value >= -2147483648 and value <= 2147483647 then
write_int8(32)
write_int32(value)
elseif value >= -9223372036854775808 and value <= 9223372036854775807 then
write_int8(64)
write_int64(value)
end
elseif valueType == 'float' then
if value >= 1.175494351e-38 and value <= 3.402823466e+38 then
write_int8(33)
write_float(value)
elseif value >= 2.2250738585072014e-308 and value <= 1.7976931348623158e+308 then
write_int8(65)
write_float(value)
end
end
end
end
----------------------------------------------------------------------
return ba
end

+ 0
- 271
test/lua/ByteArray_back.lua 查看文件

@ -1,271 +0,0 @@
BA_ENDIAN_BIG = "ENDIAN_BIG" -- 大端
BA_ENDIAN_LITTLE = "ENDIAN_LITTLE" -- 小端
function ByteArray(endian)
----------------------------------------------------------------------
-- private member variable
----------------------------------------------------------------------
local mRadix = {[8]="%03o", [10]="%03u", [16]="%02X"} -- 进制
local mEndian = endian or "" -- 大小端标识
local mBuf = {} -- 二进制字节流
local mPos = 1 -- 读写位置
----------------------------------------------------------------------
-- private method
----------------------------------------------------------------------
-- 验证读写位置
local function checkAvailable()
assert(#mBuf >= mPos, string.format("End of file was encountered. pos: %d, length: %d.", mPos, #mBuf))
end
-- 获取字符码
local function getLetterCode(fmt)
fmt = fmt or ""
if BA_ENDIAN_LITTLE == mEndian then
return "<"..fmt
elseif BA_ENDIAN_BIG == mEndian then
return ">"..fmt
else
return "="..fmt
end
end
-- 读单个字节
local function readRawByte()
checkAvailable()
local rawByte = mBuf[mPos]
mPos = mPos + 1
return rawByte
end
-- 写单个字节
local function writeRawByte(rawByte)
if mPos > #mBuf + 1 then
for i=#mBuf + 1, mPos - 1 do
mBuf[i] = string.char(0)
end
end
mBuf[mPos] = rawByte
mPos = mPos + 1
end
-- 读字节流
local function readBuf(length)
checkAvailable()
local buf = table.concat(mBuf, "", mPos, mPos + length - 1)
mPos = mPos + length
return buf
end
-- 写字节流
local function writeBuf(buf)
for i=1, #buf do
writeRawByte(buf:sub(i, i))
end
end
-- 读字符串
local function read_string_bytes(length)
if 0 == length then
return ""
end
local tmp, value = string.unpack(readBuf(length), getLetterCode("A"..length))
return value
end
-- 写字符串
local function write_string_bytes(value)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
----------------------------------------------------------------------
-- public method
----------------------------------------------------------------------
local ba = {}
-- 设置大小端
ba.setEndian = function(endian)
mEndian = endian or ""
end
-- 设置字节流
ba.setBytes = function(buf)
if #mBuf > 0 then
return
end
writeBuf(buf)
mPos = 1 -- 这里必须重置读写位置为1,方能保证接下去的读操作正确
end
-- 获取字节流
ba.getBytes = function()
local bytes = {}
for i=1, #mBuf do
bytes[#bytes+1] = string.byte(mBuf[i])
end
local packRes = string.pack(getLetterCode("b"..#bytes), unpack(bytes))
return packRes
end
-- 获取字节流长度
ba.getLength = function()
return #mBuf
end
-- 字节流转为字符串,radix-8,10,16
ba.toString = function(radix, separator)
radix = radix or 16
radix = mRadix[radix] or "%02X"
separator = separator or " "
local bytes = {}
for i=1, #mBuf do
bytes[i] = string.format(radix..separator, string.byte(mBuf[i]))
end
return table.concat(bytes)
end
----------------------------------------------------------------------
-- 读16位整型
ba.read_int16 = function()
local tmp, value = string.unpack(readBuf(2), getLetterCode("h"))
return value
end
-- 写16位整型
ba.write_int16 = function(value)
local buf = string.pack(getLetterCode("h"), value)
writeBuf(buf)
end
-- 读16位无符号整型
ba.read_uint16 = function()
local tmp, value = string.unpack(readBuf(2), getLetterCode("H"))
return value
end
-- 写16位无符号整型
ba.write_uint16 = function(value)
local sstr = getLetterCode("H")
local buf = string.pack(sstr, value)
writeBuf(buf)
end
-- 读32位整型
ba.read_int32 = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("i"))
return value
end
-- 写32位整型
ba.write_int32 = function(value)
local buf = string.pack(getLetterCode("i"), value)
writeBuf(buf)
end
-- 读32位无符号整型
ba.read_uint32 = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("I"))
return value
end
-- 写32位无符号整型
ba.write_uint32 = function(value)
local buf = string.pack(getLetterCode("I"), value)
writeBuf(buf)
end
-- 读长整型
ba.read_long = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("l"))
return value
end
-- 写长整型
ba.write_long = function(value)
local buf = string.pack(getLetterCode("l"), value)
writeBuf(buf)
end
-- 读无符号长整型
ba.read_ulong = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("L"))
return value
end
-- 写无符号长整型
ba.write_ulong = function(value)
local buf = string.pack(getLetterCode("L"), value)
writeBuf(buf)
end
-- 读64位整型
ba.read_int64 = function()
-- local tmp, value = string.unpack(readBuf(8), getLetterCode("m"))
-- return value
return read_string_bytes(8)
end
-- 写64位整型
ba.write_int64 = function(value)
-- local buf = string.pack(getLetterCode("m"), value)
-- writeBuf(buf)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
-- 读64位无符号整型
ba.read_uint64 = function()
-- local tmp, value = string.unpack(readBuf(8), getLetterCode("M"))
-- return value
return read_string_bytes(8)
end
-- 写64位无符号整型
ba.write_uint64 = function(value)
-- local buf = string.pack(getLetterCode("M"), value)
-- writeBuf(buf)
local buf = string.pack(getLetterCode("A"), value)
writeBuf(buf)
end
-- 读单精度浮点型
ba.read_float = function()
local tmp, value = string.unpack(readBuf(4), getLetterCode("f"))
return value
end
-- 写单精度浮点型
ba.write_float = function(value)
local buf = string.pack(getLetterCode("f"), value)
writeBuf(buf)
end
-- 读双精度浮点型
ba.read_double = function()
local tmp, value = string.unpack(readBuf(8), getLetterCode("d"))
return value
end
-- 写双精度浮点型
ba.write_double = function(value)
local buf = string.pack(getLetterCode("d"), value)
writeBuf(buf)
end
-- 读布尔型
ba.read_bool = function()
return 1 == read_char()
end
-- 写布尔型
ba.write_bool = function(value)
if value then
ba.write_char(1)
else
ba.write_char(0)
end
end
-- 读字符型
ba.read_int8 = function()
local tmp, value = string.unpack(readRawByte(), "c")
return value
end
-- 写字符型
ba.write_int8 = function(value)
writeRawByte(string.pack("c", value))
end
-- 读单字节
ba.read_uint8 = function()
-- 方法1
-- return string.byte(readRawByte())
-- 方法2
local tmp, value = string.unpack(readRawByte(), "b")
return value
end
-- 写单字节
ba.write_uint8 = function(value)
-- 方法1
-- writeRawByte(string.char(value))
-- 方法2
writeRawByte(string.pack("b", value))
end
-- 读字符串
ba.read_string = function()
local length = ba.read_uint16()
return read_string_bytes(length)
end
-- 写字符串
ba.write_string = function(value)
local buf = string.pack(getLetterCode("A"), value)
ba.write_uint16(#buf)
writeBuf(buf)
end
----------------------------------------------------------------------
return ba
end

+ 181
- 601
test/lua/protoMsg.lua
文件差異過大導致無法顯示
查看文件


Loading…
取消
儲存