|
|
@ -1,10 +1,7 @@ |
|
|
|
function ByteArray() |
|
|
|
local mBuf = {} -- 二进制字节流 |
|
|
|
local mBuf = {} -- 二进制字节流 |
|
|
|
local mPos = 1 -- 读写位置 |
|
|
|
|
|
|
|
---------------------------------------------------------------------- |
|
|
|
-- public method |
|
|
|
---------------------------------------------------------------------- |
|
|
|
local ba = {} |
|
|
|
-- 设置字节流 解码先设置从tcp获取得来的字节数据 |
|
|
|
ba.setBytes = function(buf) |
|
|
@ -77,7 +74,6 @@ function ByteArray() |
|
|
|
mPos = mPos + 1 |
|
|
|
end |
|
|
|
|
|
|
|
---------------------------------------------------------------------- |
|
|
|
-- 读16位整型 |
|
|
|
ba.read_int16 = function() |
|
|
|
local value, tPos = string.unpack(">i2", mBuf, mPos) |
|
|
@ -251,32 +247,32 @@ function ByteArray() |
|
|
|
elseif tag == 65 then |
|
|
|
return read_double() |
|
|
|
end |
|
|
|
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 |
|
|
|
-- 写数字 |
|
|
|
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 |
|
|
|