mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-20 03:26:57 +00:00
perf[protocol]: 使用模板生成协议
This commit is contained in:
@@ -0,0 +1,448 @@
|
||||
--默认为大端模式
|
||||
--支持的lua版本为>=5.3
|
||||
--支持标准的Lua是使用64-bit的int以及64-bit的双精度float
|
||||
--当lua只能支持32位的整数类型时,可以考虑用Long来替代,需要修改原代码
|
||||
|
||||
--local Long = require("Long")
|
||||
|
||||
local maxInt = 2147483647
|
||||
local minInt = -2147483648
|
||||
local initSize = 128
|
||||
local zeroByte = string.char(0)
|
||||
|
||||
local ByteBuffer = {}
|
||||
|
||||
local trueBooleanStrValue = string.char(1)
|
||||
local falseBooleanStrValue = string.char(0)
|
||||
|
||||
-------------------------------------构造器-------------------------------------
|
||||
function ByteBuffer:new()
|
||||
--buffer里的每一个元素为一个长度为1的字符串
|
||||
local obj = {
|
||||
buffer = {},
|
||||
writeOffset = 1,
|
||||
readOffset = 1
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
for i = 1, initSize do
|
||||
table.insert(obj.buffer, zeroByte)
|
||||
end
|
||||
return obj
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------UTF8-------------------------------------
|
||||
-- 判断utf8字符byte长度
|
||||
-- 0xxxxxxx - 1 byte
|
||||
-- 110yxxxx - 192, 2 byte
|
||||
-- 1110yyyy - 225, 3 byte
|
||||
-- 11110zzz - 240, 4 byte
|
||||
local function chsize(char)
|
||||
if not char then
|
||||
print("not char")
|
||||
return 0
|
||||
elseif char > 240 then
|
||||
return 4
|
||||
elseif char > 225 then
|
||||
return 3
|
||||
elseif char > 192 then
|
||||
return 2
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- 截取utf8 字符串
|
||||
-- str: 要截取的字符串
|
||||
-- startChar: 开始字符下标,从1开始
|
||||
-- numChars: 要截取的字符长度
|
||||
local function utf8sub(str, startChar, numChars)
|
||||
local startIndex = 1
|
||||
while startChar > 1 do
|
||||
local char = string.byte(str, startIndex)
|
||||
startIndex = startIndex + chsize(char)
|
||||
startChar = startChar - 1
|
||||
end
|
||||
|
||||
local currentIndex = startIndex
|
||||
|
||||
while numChars > 0 and currentIndex <= #str do
|
||||
local char = string.byte(str, currentIndex)
|
||||
currentIndex = currentIndex + chsize(char)
|
||||
numChars = numChars - 1
|
||||
end
|
||||
return str:sub(startIndex, currentIndex - 1)
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------get和set-------------------------------------
|
||||
function ByteBuffer:getWriteOffset()
|
||||
return self.writeOffset
|
||||
end
|
||||
|
||||
function ByteBuffer:setWriteOffset(writeOffset)
|
||||
if writeOffset > #self.buffer then
|
||||
error("index out of bounds exception: readerIndex: " + self.readOffset
|
||||
+ ", writerIndex: " + self.writeOffset
|
||||
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
|
||||
end
|
||||
self.writeOffset = writeOffset
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:getReadOffset()
|
||||
return self.readOffset
|
||||
end
|
||||
|
||||
function ByteBuffer:setReadOffset(readOffset)
|
||||
if readOffset > self.writeOffset then
|
||||
error("index out of bounds exception: readerIndex: " + self.readOffset
|
||||
+ ", writerIndex: " + this.writeOffset
|
||||
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
|
||||
end
|
||||
self.readOffset = readOffset
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:getLen()
|
||||
return #self.buffer
|
||||
end
|
||||
|
||||
function ByteBuffer:getAvailable()
|
||||
return #self.buffer - self.writeOffset + 1
|
||||
end
|
||||
|
||||
-------------------------------------write和read-------------------------------------
|
||||
|
||||
--bool
|
||||
function ByteBuffer:writeBoolean(boolValue)
|
||||
if boolValue then
|
||||
self:writeRawByteStr(trueBooleanStrValue)
|
||||
else
|
||||
self:writeRawByteStr(falseBooleanStrValue)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readBoolean()
|
||||
-- When char > 256, the readUByte method will show an error.
|
||||
-- So, we have to use readChar
|
||||
return self:readRawByteStr() == trueBooleanStrValue
|
||||
end
|
||||
|
||||
|
||||
--- byte
|
||||
-- The byte is a number between -128 and 127, otherwise, the lua will get an error.
|
||||
function ByteBuffer:writeByte(byteValue)
|
||||
local str = string.pack("b", byteValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readByte()
|
||||
local result = string.unpack("b", self:readRawByteStr())
|
||||
return result
|
||||
end
|
||||
|
||||
-- The byte is a number between 0 and 255, otherwise, the lua will get an error.
|
||||
function ByteBuffer:writeUByte(ubyteValue)
|
||||
self:writeRawByteStr(string.char(ubyteValue))
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readUByte()
|
||||
return string.byte(self:readRawByteStr())
|
||||
end
|
||||
|
||||
|
||||
-- short
|
||||
function ByteBuffer:writeShort(shortValue)
|
||||
local str = string.pack(">h", shortValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readShort()
|
||||
local byteStrArray = self:readBuffer(2)
|
||||
local result = string.unpack(">h", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
-- int
|
||||
function ByteBuffer:writeInt(intValue)
|
||||
if (math.type(intValue) ~= "integer") then
|
||||
error("intValue must be integer")
|
||||
end
|
||||
if ((minInt > intValue) or (intValue > maxInt)) then
|
||||
error("intValue must range between minInt:-2147483648 and maxInt:2147483647")
|
||||
end
|
||||
|
||||
return self:writeLong(intValue)
|
||||
end
|
||||
|
||||
function ByteBuffer:readInt()
|
||||
return self:readLong()
|
||||
end
|
||||
|
||||
-- int
|
||||
function ByteBuffer:writeRawInt(intValue)
|
||||
local str = string.pack(">i", intValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readRawInt()
|
||||
local byteStrArray = self:readBuffer(4)
|
||||
local result = string.unpack(">i", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
--long
|
||||
function ByteBuffer:writeLong(longValue)
|
||||
--Long:writeLong(self, longValue)
|
||||
|
||||
if (math.type(longValue) ~= "integer") then
|
||||
error("longValue must be integer")
|
||||
end
|
||||
|
||||
--lua中的右移为无符号右移,要特殊处理
|
||||
local mask = longValue >> 63
|
||||
local value = longValue << 1
|
||||
if (mask == 1) then
|
||||
value = value ~ 0xFFFFFFFFFFFFFFFF
|
||||
end
|
||||
|
||||
if (value >> 7) == 0 then
|
||||
self:writeUByte(value)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 14) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte((value >> 7) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 21) == 0 then
|
||||
self:writeUByte((value & 0x7F) | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 14) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 28) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 21) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 35) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 28) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 42) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 35) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 49) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 42) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 56) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 42) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 49) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 42) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 49) & 0x7F | 0x80))
|
||||
self:writeUByte(value >> 56)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readLong()
|
||||
--return Long:readLong(self):toString()
|
||||
local b = self:readUByte()
|
||||
local value = b & 0x7F
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 7)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 14)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 21)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 28)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 35)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 42)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 49)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | (b << 56)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return (value >> 1) ~ -(value & 1)
|
||||
end
|
||||
|
||||
--固定8位的lua数字类型
|
||||
function ByteBuffer:writeLuaNumber(luaNumberValue)
|
||||
local str = string.pack(">n", luaNumberValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readLuaNumber()
|
||||
local result = string.unpack(">n", self:readBuffer(8))
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
--float
|
||||
function ByteBuffer:writeFloat(floatValue)
|
||||
local str = string.pack(">f", floatValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readFloat()
|
||||
local byteStrArray = self:readBuffer(4)
|
||||
local result = string.unpack(">f", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
--double
|
||||
function ByteBuffer:writeDouble(doubleValue)
|
||||
local str = string.pack(">d", doubleValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readDouble()
|
||||
local byteStrArray = self:readBuffer(8)
|
||||
local result = string.unpack(">d", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
--string
|
||||
function ByteBuffer:writeString(str)
|
||||
if str == nil or #str == 0 then
|
||||
self:writeInt(0)
|
||||
return
|
||||
end
|
||||
self:writeInt(#str)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readString()
|
||||
local length = self:readInt()
|
||||
return self:readBuffer(length)
|
||||
end
|
||||
|
||||
--char
|
||||
function ByteBuffer:writeChar(charValue)
|
||||
local str = utf8sub(charValue, 1, 1)
|
||||
self:writeString(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readChar()
|
||||
return self:readString()
|
||||
end
|
||||
|
||||
--- Write a encoded char array into buf
|
||||
function ByteBuffer:writeBuffer(str)
|
||||
for i = 1, #str do
|
||||
self:writeRawByteStr(string.sub(str, i, i))
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Read a byte array as string from current position, then update the position.
|
||||
function ByteBuffer:readBuffer(length)
|
||||
local byteStrArray = self:getBytes(self.readOffset, self.readOffset + length - 1)
|
||||
self.readOffset = self.readOffset + length
|
||||
return byteStrArray
|
||||
end
|
||||
|
||||
function ByteBuffer:writeRawByteStr(byteStrValue)
|
||||
if self.writeOffset > #self.buffer + 1 then
|
||||
for i = #self.buffer + 1, self.writeOffset - 1 do
|
||||
table.insert(self.buffer, zeroByte)
|
||||
end
|
||||
end
|
||||
self.buffer[self.writeOffset] = string.sub(byteStrValue, 1, 1)
|
||||
self.writeOffset = self.writeOffset + 1
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readRawByteStr()
|
||||
local byteStrValue = self.buffer[self.readOffset]
|
||||
self.readOffset = self.readOffset + 1
|
||||
return byteStrValue
|
||||
end
|
||||
|
||||
--- Get all byte array as a lua string.
|
||||
-- Do not update position.
|
||||
function ByteBuffer:getBytes(startIndex, endIndex)
|
||||
startIndex = startIndex or 1
|
||||
endIndex = endIndex or #self.buffer
|
||||
return table.concat(self.buffer, "", startIndex, endIndex)
|
||||
end
|
||||
|
||||
return ByteBuffer
|
||||
@@ -0,0 +1,542 @@
|
||||
al MAX_LONG_4BYTE = 1 << 32
|
||||
local MIN_INT = -2147483648
|
||||
local MAX_INT = 2147483647
|
||||
local MIN_LONG = 0x8000000000000000
|
||||
local MAX_LONG = 0x7fffffffffffffff
|
||||
local MIN_LONG_STRING = "-9223372036854775808"
|
||||
--The natural logarithm of 2.
|
||||
local LN2 = 0.6931471805599453
|
||||
|
||||
Long = {}
|
||||
|
||||
function Long:new(low, high)
|
||||
local obj = {
|
||||
low = low & 0xFFFFFFFF,
|
||||
high = high & 0xFFFFFFFF
|
||||
}
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
local function clone(value)
|
||||
return Long:new(value.low, value.high)
|
||||
end
|
||||
|
||||
local function fromBits(lowBits, highBits)
|
||||
return Long:new(lowBits, highBits)
|
||||
end
|
||||
|
||||
local function fromInt(value)
|
||||
value = math.tointeger(value)
|
||||
local param = 0
|
||||
if value < 0 then
|
||||
param = -1
|
||||
end
|
||||
return fromBits(value, param)
|
||||
end
|
||||
|
||||
local ZERO = fromInt(0)
|
||||
local ONE = fromInt(1)
|
||||
local NEG_ONE = fromInt(-1)
|
||||
local MAX_VALUE = fromBits(0xFFFFFFFF, 0x7FFFFFFF)
|
||||
local MIN_VALUE = fromBits(0, 0x80000000)
|
||||
|
||||
local function fromNumber(value)
|
||||
if (value <= -MIN_LONG) then
|
||||
return clone(MIN_VALUE)
|
||||
|
||||
end
|
||||
|
||||
if (value + 1 >= MAX_LONG) then
|
||||
return clone(MAX_VALUE)
|
||||
end
|
||||
|
||||
if (value < 0) then
|
||||
return fromNumber(-value):negate()
|
||||
end
|
||||
return fromBits(math.floor((value % MAX_LONG_4BYTE)) | 0, math.floor(value / MAX_LONG_4BYTE) | 0)
|
||||
end
|
||||
|
||||
function Long:fromString(str, radix)
|
||||
if type(radix) == "nil" then
|
||||
radix = 10
|
||||
end
|
||||
|
||||
if (type(str) ~= "string") then
|
||||
error("str不是string类型参数")
|
||||
end
|
||||
|
||||
--进制必须在2到36
|
||||
if radix < 2 or 36 < radix then
|
||||
error("range radix error")
|
||||
end
|
||||
|
||||
local p = string.find(str, "-")
|
||||
if p ~= nil then
|
||||
if (p > 1) then
|
||||
error("interior hyphen")
|
||||
end
|
||||
|
||||
if (p == 1) then
|
||||
return Long:fromString(string.sub(str, 2), radix):negate()
|
||||
end
|
||||
end
|
||||
|
||||
local radixToPower = fromNumber(radix ^ 8)
|
||||
local result = clone(ZERO)
|
||||
str = tostring(str)
|
||||
for i = 1, #str, 8 do
|
||||
local size = math.min(8, #str - i + 1)
|
||||
if (size < 8) then
|
||||
local value = tonumber(string.sub(str, i), radix)
|
||||
local power = fromNumber(radix ^ size)
|
||||
result = result:multiply(power):add(fromNumber(value))
|
||||
else
|
||||
local value = tonumber(string.sub(str, i, i + 7), radix)
|
||||
result = result:multiply(radixToPower):add(fromNumber(value))
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--转为10进制的string符号的long
|
||||
function Long:toString()
|
||||
local radix = 10
|
||||
if (Long:isZero()) then
|
||||
return "0"
|
||||
end
|
||||
|
||||
if (self:isNegative()) then
|
||||
if (self:equals(MIN_VALUE)) then
|
||||
return MIN_LONG_STRING
|
||||
else
|
||||
return '-' .. self:negate():toString(radix)
|
||||
end
|
||||
end
|
||||
|
||||
local radixToPower = fromNumber(radix ^ 6)
|
||||
local rem = self
|
||||
local result = ''
|
||||
while (true) do
|
||||
local remDiv = rem:divide(radixToPower)
|
||||
local digits = tostring(rem:subtract(remDiv:multiply(radixToPower)):toInt() & 0xFFFFFFFF)
|
||||
rem = remDiv
|
||||
if (rem:isZero()) then
|
||||
return digits .. result
|
||||
else
|
||||
while (#digits < 6) do
|
||||
digits = '0' .. digits
|
||||
end
|
||||
result = '' .. digits .. result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
|
||||
function Long:toNumber()
|
||||
return self.high * MAX_LONG_4BYTE + self.low
|
||||
end
|
||||
|
||||
--Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
|
||||
function Long:toInt()
|
||||
return self.low
|
||||
end
|
||||
|
||||
function Long:isNegative()
|
||||
return (self.high & 0x80000000) ~= 0
|
||||
end
|
||||
|
||||
function Long:negate()
|
||||
if self:equals(MIN_VALUE) then
|
||||
return clone(MIN_VALUE)
|
||||
end
|
||||
--正数转为负数的二进制编码,取反加1
|
||||
local notSelf = fromBits(~self.low, ~self.high)
|
||||
return notSelf:add(ONE)
|
||||
end
|
||||
|
||||
function Long:equals(other)
|
||||
return self.high == other.high and self.low == other.low
|
||||
end
|
||||
|
||||
function Long:isZero()
|
||||
return self.high == 0 and self.low == 0
|
||||
end
|
||||
|
||||
function Long:add(addend)
|
||||
local a48 = (self.high >> 16)
|
||||
local a32 = (self.high & 0xFFFF)
|
||||
local a16 = (self.low >> 16)
|
||||
local a00 = (self.low & 0xFFFF)
|
||||
|
||||
local b48 = (addend.high >> 16)
|
||||
local b32 = (addend.high & 0xFFFF)
|
||||
local b16 = (addend.low >> 16)
|
||||
local b00 = (addend.low & 0xFFFF)
|
||||
|
||||
local c48 = 0
|
||||
local c32 = 0
|
||||
local c16 = 0
|
||||
local c00 = 0
|
||||
c00 = c00 + a00 + b00
|
||||
c16 = c16 + (c00 >> 16)
|
||||
c00 = (c00 & 0xFFFF)
|
||||
c16 = c16 + a16 + b16
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = (c16 & 0xFFFF)
|
||||
c32 = c32 + a32 + b32
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = (c32 & 0xFFFF)
|
||||
c48 = c48 + a48 + b48
|
||||
c48 = (c48 & 0xFFFF)
|
||||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32)
|
||||
end
|
||||
|
||||
function Long:subtract(subtrahend)
|
||||
return self:add(subtrahend:negate())
|
||||
end
|
||||
|
||||
function Long:multiply(multiplier)
|
||||
if (self:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
if (multiplier:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
local a48 = (self.high >> 16)
|
||||
local a32 = (self.high & 0xFFFF)
|
||||
local a16 = (self.low >> 16)
|
||||
local a00 = (self.low & 0xFFFF)
|
||||
|
||||
local b48 = (multiplier.high >> 16)
|
||||
local b32 = (multiplier.high & 0xFFFF)
|
||||
local b16 = (multiplier.low >> 16)
|
||||
local b00 = (multiplier.low & 0xFFFF)
|
||||
|
||||
local c48 = 0
|
||||
local c32 = 0
|
||||
local c16 = 0
|
||||
local c00 = 0
|
||||
c00 = c00 + a00 * b00
|
||||
c16 = c16 + (c00 >> 16)
|
||||
c00 = c00 & 0xFFFF
|
||||
c16 = c16 + a16 * b00
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = c16 & 0xFFFF
|
||||
c16 = c16 + a00 * b16
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = c16 & 0xFFFF
|
||||
c32 = c32 + a32 * b00
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c32 = c32 + a16 * b16
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c32 = c32 + a00 * b32
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c48 = c48 + a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48
|
||||
c48 = c48 & 0xFFFF
|
||||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32)
|
||||
end
|
||||
|
||||
function Long:divide(divisor)
|
||||
if (divisor:isZero()) then
|
||||
error('division by zero')
|
||||
end
|
||||
|
||||
if (self:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
local approx
|
||||
local rem
|
||||
local res
|
||||
if (self:equals(MIN_VALUE)) then
|
||||
if (divisor:equals(ONE) or divisor:equals(NEG_ONE)) then
|
||||
return clone(MIN_VALUE)
|
||||
elseif (divisor:equals(MIN_VALUE)) then
|
||||
return clone(ONE)
|
||||
else
|
||||
local halfThis = self:shiftRight(1)
|
||||
approx = halfThis:divide(divisor):shiftLeft(1)
|
||||
if (approx:equals(ZERO)) then
|
||||
if (divisor:isNegative()) then
|
||||
return clone(ONE)
|
||||
else
|
||||
return clone(NEG_ONE)
|
||||
end
|
||||
else
|
||||
rem = self:subtract(divisor:multiply(approx))
|
||||
res = approx:add(rem:divide(divisor))
|
||||
return res
|
||||
end
|
||||
end
|
||||
elseif (divisor:equals(MIN_VALUE)) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
if (self:isNegative()) then
|
||||
if (divisor:isNegative()) then
|
||||
return self:neg():divide(divisor:negate())
|
||||
end
|
||||
return self:negate():divide(divisor):negate()
|
||||
elseif (divisor:isNegative()) then
|
||||
return self:divide(divisor:negate()):negate()
|
||||
end
|
||||
res = clone(ZERO)
|
||||
|
||||
rem = self
|
||||
while (rem:greaterThanOrEqual(divisor)) do
|
||||
approx = math.max(1, math.floor(rem:toNumber() / divisor:toNumber()))
|
||||
|
||||
local log2 = math.ceil(math.log(approx) / LN2)
|
||||
|
||||
local delta = 1
|
||||
if log2 <= 48 then
|
||||
delta = 2 ^ (log2 - 48)
|
||||
end
|
||||
|
||||
local approxRes = fromNumber(approx)
|
||||
local approxRem = approxRes:multiply(divisor)
|
||||
while (approxRem:isNegative() or approxRem:greaterThan(rem)) do
|
||||
approx = approx - delta
|
||||
approxRes = fromNumber(approx)
|
||||
approxRem = approxRes:multiply(divisor)
|
||||
end
|
||||
|
||||
if (approxRes:isZero()) then
|
||||
approxRes = clone(ONE)
|
||||
end
|
||||
|
||||
res = res:add(approxRes)
|
||||
rem = rem:subtract(approxRem)
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
function shiftRight(numBits)
|
||||
numBits = numBits & 63
|
||||
if (numBits == 0) then
|
||||
return self
|
||||
elseif (numBits < 32) then
|
||||
return fromBits((self.low >> numBits) | (self.high << (32 - numBits)), self.high >> numBits)
|
||||
else
|
||||
if (self.high >= 0) then
|
||||
return fromBits(self.high >> (numBits - 32), 0)
|
||||
else
|
||||
return fromBits(self.high >> (numBits - 32), -1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function shiftLeft(numBits)
|
||||
numBits = numBits & 63
|
||||
if (numBits == 0) then
|
||||
return self
|
||||
elseif (numBits < 32) then
|
||||
return fromBits(self.low << numBits, (self.high << numBits) | (self.low >> (32 - numBits)))
|
||||
else
|
||||
return fromBits(0, self.low << (numBits - 32))
|
||||
end
|
||||
end
|
||||
|
||||
function Long:compare(other)
|
||||
if (self:equals(other)) then
|
||||
return 0
|
||||
end
|
||||
local thisNeg = self:isNegative()
|
||||
local otherNeg = other:isNegative()
|
||||
if (thisNeg and not (otherNeg)) then
|
||||
return -1
|
||||
end
|
||||
if (not (thisNeg) and otherNeg) then
|
||||
return 1
|
||||
end
|
||||
if self:subtract(other):isNegative() then
|
||||
return -1
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
function Long:greaterThanOrEqual(other)
|
||||
return self:compare(other) >= 0
|
||||
end
|
||||
|
||||
function Long:greaterThan(other)
|
||||
return self:compare(other) > 0
|
||||
end
|
||||
|
||||
function Long:encodeZigzagLong()
|
||||
local mask = self.high >> 31
|
||||
if mask == 1 then
|
||||
self.high = ((self.high << 1 | self.low >> 31) ~ 0xFFFFFFFF) & 0xFFFFFFFF
|
||||
self.low = ((self.low << 1 | mask) ~ 0xFFFFFFFE) & 0xFFFFFFFF
|
||||
else
|
||||
self.high = (self.high << 1 | self.low >> 31) & 0xFFFFFFFF
|
||||
self.low = (self.low << 1) & 0xFFFFFFFF
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Long:decodeZigzagLong()
|
||||
local mask = self.low & 1
|
||||
if mask == 1 then
|
||||
self.low = (((self.low >> 1) | (self.high << 31)) ~ 0xFFFFFFFF) & 0xFFFFFFFF
|
||||
self.high = ((self.high >> 1 | (0x80000000)) ~ 0x7FFFFFFF) & 0xFFFFFFFF
|
||||
else
|
||||
self.low = ((self.low >> 1) | (self.high << 31)) & 0xFFFFFFFF
|
||||
self.high = (self.high >> 1) & 0xFFFFFFFF
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Long:writeLong(byteBuffer, longValue)
|
||||
if type(longValue) == "string" then
|
||||
local len = #longValue
|
||||
if len <= 11 then
|
||||
local num = tonumber(longValue)
|
||||
if (MIN_INT <= num) and (num <= MAX_INT) then
|
||||
byteBuffer:writeInt(num)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if type(longValue) == number then
|
||||
if (MIN_INT <= longValue) and (longValue <= MAX_INT) then
|
||||
byteBuffer:writeInt(tonumber(longValue))
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
--写入Long
|
||||
local value = Long:fromString(longValue)
|
||||
value:encodeZigzagLong()
|
||||
local count = 0
|
||||
while (value.high ~= 0) do
|
||||
byteBuffer:writeByte(value.low & 127 | 128)
|
||||
value.low = ((value.low >> 7) | (value.high << 25))
|
||||
value.high = (value.high >> 7)
|
||||
count = count + 7
|
||||
end
|
||||
while (value.low > 127) do
|
||||
if count >= 56 then
|
||||
byteBuffer:writeByte(value.low)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeByte(value.low & 127 | 128)
|
||||
value.low = value.low >> 7
|
||||
count = count + 7
|
||||
end
|
||||
byteBuffer:writeByte(value.low)
|
||||
end
|
||||
|
||||
local function fromByteBuffer(byteBuffer)
|
||||
local bits = Long:new(0, 0)
|
||||
local count = #byteBuffer
|
||||
local i = 0
|
||||
local pos = 1
|
||||
if (count > 4) then
|
||||
--先读入1到4位
|
||||
while i < 4 do
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
--读第5位,第5位底位置读到low,高位置读到high
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << 28)) & 0xFFFFFFFF
|
||||
bits.high = (bits.high | ((byteBuffer[pos] & 127) >> 4)) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = 0
|
||||
pos = pos + 1
|
||||
else
|
||||
while i < 3 do
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
return bits
|
||||
end
|
||||
|
||||
--读最后4位
|
||||
while i < 4 do
|
||||
if (pos == 9) then
|
||||
bits.high = (bits.high | (byteBuffer[pos] << (i * 7 + 3))) & 0xFFFFFFFF
|
||||
return bits
|
||||
end
|
||||
bits.high = (bits.high | ((byteBuffer[pos] & 127) << (i * 7 + 3))) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
|
||||
return bits
|
||||
end
|
||||
|
||||
function Long:readLong(buffer)
|
||||
local byteBuffer = {}
|
||||
local b = buffer:readByte()
|
||||
local count = 1
|
||||
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local longValue = fromByteBuffer(byteBuffer)
|
||||
longValue:decodeZigzagLong()
|
||||
return longValue
|
||||
end
|
||||
|
||||
return Long
|
||||
@@ -0,0 +1,980 @@
|
||||
-- 复杂的对象
|
||||
-- 包括了各种复杂的结构,数组,List,Set,Map
|
||||
--
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.14 11:19
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local ComplexObject = {}
|
||||
|
||||
function ComplexObject:new(a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, cccc, d, dd, ddd, dddd, e, ee, eee, eeee, f, ff, fff, ffff, g, gg, ggg, gggg, h, hh, hhh, hhhh, jj, jjj, kk, kkk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss)
|
||||
local obj = {
|
||||
-- byte类型,最简单的整形
|
||||
a = a, -- byte
|
||||
-- byte的包装类型
|
||||
-- 优先使用基础类型,包装类型会有装箱拆箱
|
||||
aa = aa, -- java.lang.Byte
|
||||
-- 数组类型
|
||||
aaa = aaa, -- byte[]
|
||||
aaaa = aaaa, -- java.lang.Byte[]
|
||||
b = b, -- short
|
||||
bb = bb, -- java.lang.Short
|
||||
bbb = bbb, -- short[]
|
||||
bbbb = bbbb, -- java.lang.Short[]
|
||||
c = c, -- int
|
||||
cc = cc, -- java.lang.Integer
|
||||
ccc = ccc, -- int[]
|
||||
cccc = cccc, -- java.lang.Integer[]
|
||||
d = d, -- long
|
||||
dd = dd, -- java.lang.Long
|
||||
ddd = ddd, -- long[]
|
||||
dddd = dddd, -- java.lang.Long[]
|
||||
e = e, -- float
|
||||
ee = ee, -- java.lang.Float
|
||||
eee = eee, -- float[]
|
||||
eeee = eeee, -- java.lang.Float[]
|
||||
f = f, -- double
|
||||
ff = ff, -- java.lang.Double
|
||||
fff = fff, -- double[]
|
||||
ffff = ffff, -- java.lang.Double[]
|
||||
g = g, -- boolean
|
||||
gg = gg, -- java.lang.Boolean
|
||||
ggg = ggg, -- boolean[]
|
||||
gggg = gggg, -- java.lang.Boolean[]
|
||||
h = h, -- char
|
||||
hh = hh, -- java.lang.Character
|
||||
hhh = hhh, -- char[]
|
||||
hhhh = hhhh, -- java.lang.Character[]
|
||||
jj = jj, -- java.lang.String
|
||||
jjj = jjj, -- java.lang.String[]
|
||||
kk = kk, -- com.zfoo.protocol.packet.ObjectA
|
||||
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[]
|
||||
l = l, -- java.util.List<java.lang.Integer>
|
||||
ll = ll, -- java.util.List<java.util.List<java.util.List<java.lang.Integer>>>
|
||||
lll = lll, -- java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>
|
||||
llll = llll, -- java.util.List<java.lang.String>
|
||||
lllll = lllll, -- java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
mmm = mmm, -- java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>>
|
||||
mmmm = mmmm, -- java.util.Map<java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>, java.util.List<java.util.List<java.util.List<java.lang.Integer>>>>
|
||||
mmmmm = mmmmm, -- java.util.Map<java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>, java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>>
|
||||
s = s, -- java.util.Set<java.lang.Integer>
|
||||
ss = ss, -- java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
|
||||
sss = sss, -- java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
|
||||
ssss = ssss, -- java.util.Set<java.lang.String>
|
||||
sssss = sssss -- java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ComplexObject:protocolId()
|
||||
return 1160
|
||||
end
|
||||
|
||||
function ComplexObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeByte(packet.a)
|
||||
byteBuffer:writeByte(packet.aa)
|
||||
if packet.aaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaa);
|
||||
for index0, element1 in pairs(packet.aaa) do
|
||||
byteBuffer:writeByte(element1)
|
||||
end
|
||||
end
|
||||
if packet.aaaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaaa);
|
||||
for index2, element3 in pairs(packet.aaaa) do
|
||||
byteBuffer:writeByte(element3)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeShort(packet.b)
|
||||
byteBuffer:writeShort(packet.bb)
|
||||
if packet.bbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbb);
|
||||
for index4, element5 in pairs(packet.bbb) do
|
||||
byteBuffer:writeShort(element5)
|
||||
end
|
||||
end
|
||||
if packet.bbbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbbb);
|
||||
for index6, element7 in pairs(packet.bbbb) do
|
||||
byteBuffer:writeShort(element7)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeInt(packet.c)
|
||||
byteBuffer:writeInt(packet.cc)
|
||||
if packet.ccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ccc);
|
||||
for index8, element9 in pairs(packet.ccc) do
|
||||
byteBuffer:writeInt(element9)
|
||||
end
|
||||
end
|
||||
if packet.cccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.cccc);
|
||||
for index10, element11 in pairs(packet.cccc) do
|
||||
byteBuffer:writeInt(element11)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeLong(packet.d)
|
||||
byteBuffer:writeLong(packet.dd)
|
||||
if packet.ddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ddd);
|
||||
for index12, element13 in pairs(packet.ddd) do
|
||||
byteBuffer:writeLong(element13)
|
||||
end
|
||||
end
|
||||
if packet.dddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.dddd);
|
||||
for index14, element15 in pairs(packet.dddd) do
|
||||
byteBuffer:writeLong(element15)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeFloat(packet.e)
|
||||
byteBuffer:writeFloat(packet.ee)
|
||||
if packet.eee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eee);
|
||||
for index16, element17 in pairs(packet.eee) do
|
||||
byteBuffer:writeFloat(element17)
|
||||
end
|
||||
end
|
||||
if packet.eeee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eeee);
|
||||
for index18, element19 in pairs(packet.eeee) do
|
||||
byteBuffer:writeFloat(element19)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeDouble(packet.f)
|
||||
byteBuffer:writeDouble(packet.ff)
|
||||
if packet.fff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.fff);
|
||||
for index20, element21 in pairs(packet.fff) do
|
||||
byteBuffer:writeDouble(element21)
|
||||
end
|
||||
end
|
||||
if packet.ffff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ffff);
|
||||
for index22, element23 in pairs(packet.ffff) do
|
||||
byteBuffer:writeDouble(element23)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
byteBuffer:writeBoolean(packet.gg)
|
||||
if packet.ggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ggg);
|
||||
for index24, element25 in pairs(packet.ggg) do
|
||||
byteBuffer:writeBoolean(element25)
|
||||
end
|
||||
end
|
||||
if packet.gggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.gggg);
|
||||
for index26, element27 in pairs(packet.gggg) do
|
||||
byteBuffer:writeBoolean(element27)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeChar(packet.h)
|
||||
byteBuffer:writeChar(packet.hh)
|
||||
if packet.hhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhh);
|
||||
for index28, element29 in pairs(packet.hhh) do
|
||||
byteBuffer:writeChar(element29)
|
||||
end
|
||||
end
|
||||
if packet.hhhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhhh);
|
||||
for index30, element31 in pairs(packet.hhhh) do
|
||||
byteBuffer:writeChar(element31)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeString(packet.jj)
|
||||
if packet.jjj == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.jjj);
|
||||
for index32, element33 in pairs(packet.jjj) do
|
||||
byteBuffer:writeString(element33)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, packet.kk)
|
||||
if packet.kkk == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.kkk);
|
||||
for index34, element35 in pairs(packet.kkk) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element35)
|
||||
end
|
||||
end
|
||||
if packet.l == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.l)
|
||||
for index36, element37 in pairs(packet.l) do
|
||||
byteBuffer:writeInt(element37)
|
||||
end
|
||||
end
|
||||
if packet.ll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ll)
|
||||
for index38, element39 in pairs(packet.ll) do
|
||||
if element39 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element39)
|
||||
for index40, element41 in pairs(element39) do
|
||||
if element41 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element41)
|
||||
for index42, element43 in pairs(element41) do
|
||||
byteBuffer:writeInt(element43)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.lll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.lll)
|
||||
for index44, element45 in pairs(packet.lll) do
|
||||
if element45 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element45)
|
||||
for index46, element47 in pairs(element45) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element47)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.llll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.llll)
|
||||
for index48, element49 in pairs(packet.llll) do
|
||||
byteBuffer:writeString(element49)
|
||||
end
|
||||
end
|
||||
if packet.lllll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.lllll)
|
||||
for index50, element51 in pairs(packet.lllll) do
|
||||
if element51 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element51))
|
||||
for key52, value53 in pairs(element51) do
|
||||
byteBuffer:writeInt(key52)
|
||||
byteBuffer:writeString(value53)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key54, value55 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key54)
|
||||
byteBuffer:writeString(value55)
|
||||
end
|
||||
end
|
||||
if packet.mm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mm))
|
||||
for key56, value57 in pairs(packet.mm) do
|
||||
byteBuffer:writeInt(key56)
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, value57)
|
||||
end
|
||||
end
|
||||
if packet.mmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmm))
|
||||
for key58, value59 in pairs(packet.mmm) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, key58)
|
||||
if value59 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#value59)
|
||||
for index60, element61 in pairs(value59) do
|
||||
byteBuffer:writeInt(element61)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.mmmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmmm))
|
||||
for key62, value63 in pairs(packet.mmmm) do
|
||||
if key62 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#key62)
|
||||
for index64, element65 in pairs(key62) do
|
||||
if element65 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element65)
|
||||
for index66, element67 in pairs(element65) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element67)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if value63 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#value63)
|
||||
for index68, element69 in pairs(value63) do
|
||||
if element69 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element69)
|
||||
for index70, element71 in pairs(element69) do
|
||||
if element71 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element71)
|
||||
for index72, element73 in pairs(element71) do
|
||||
byteBuffer:writeInt(element73)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.mmmmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmmmm))
|
||||
for key74, value75 in pairs(packet.mmmmm) do
|
||||
if key74 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#key74)
|
||||
for index76, element77 in pairs(key74) do
|
||||
if element77 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element77))
|
||||
for key78, value79 in pairs(element77) do
|
||||
byteBuffer:writeInt(key78)
|
||||
byteBuffer:writeString(value79)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if value75 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(value75))
|
||||
for index80, element81 in pairs(value75) do
|
||||
if element81 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element81))
|
||||
for key82, value83 in pairs(element81) do
|
||||
byteBuffer:writeInt(key82)
|
||||
byteBuffer:writeString(value83)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.s == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.s))
|
||||
for index84, element85 in pairs(packet.s) do
|
||||
byteBuffer:writeInt(element85)
|
||||
end
|
||||
end
|
||||
if packet.ss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ss))
|
||||
for index86, element87 in pairs(packet.ss) do
|
||||
if element87 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(element87))
|
||||
for index88, element89 in pairs(element87) do
|
||||
if element89 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element89)
|
||||
for index90, element91 in pairs(element89) do
|
||||
byteBuffer:writeInt(element91)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.sss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.sss))
|
||||
for index92, element93 in pairs(packet.sss) do
|
||||
if element93 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(element93))
|
||||
for index94, element95 in pairs(element93) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element95)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.ssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ssss))
|
||||
for index96, element97 in pairs(packet.ssss) do
|
||||
byteBuffer:writeString(element97)
|
||||
end
|
||||
end
|
||||
if packet.sssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.sssss))
|
||||
for index98, element99 in pairs(packet.sssss) do
|
||||
if element99 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element99))
|
||||
for key100, value101 in pairs(element99) do
|
||||
byteBuffer:writeInt(key100)
|
||||
byteBuffer:writeString(value101)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ComplexObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ComplexObject:new()
|
||||
local result102 = byteBuffer:readByte()
|
||||
packet.a = result102
|
||||
local result103 = byteBuffer:readByte()
|
||||
packet.aa = result103
|
||||
local result104 = {}
|
||||
local size106 = byteBuffer:readInt()
|
||||
if size106 > 0 then
|
||||
for index105 = 1, size106 do
|
||||
local result107 = byteBuffer:readByte()
|
||||
table.insert(result104, result107)
|
||||
end
|
||||
end
|
||||
packet.aaa = result104
|
||||
local result108 = {}
|
||||
local size110 = byteBuffer:readInt()
|
||||
if size110 > 0 then
|
||||
for index109 = 1, size110 do
|
||||
local result111 = byteBuffer:readByte()
|
||||
table.insert(result108, result111)
|
||||
end
|
||||
end
|
||||
packet.aaaa = result108
|
||||
local result112 = byteBuffer:readShort()
|
||||
packet.b = result112
|
||||
local result113 = byteBuffer:readShort()
|
||||
packet.bb = result113
|
||||
local result114 = {}
|
||||
local size116 = byteBuffer:readInt()
|
||||
if size116 > 0 then
|
||||
for index115 = 1, size116 do
|
||||
local result117 = byteBuffer:readShort()
|
||||
table.insert(result114, result117)
|
||||
end
|
||||
end
|
||||
packet.bbb = result114
|
||||
local result118 = {}
|
||||
local size120 = byteBuffer:readInt()
|
||||
if size120 > 0 then
|
||||
for index119 = 1, size120 do
|
||||
local result121 = byteBuffer:readShort()
|
||||
table.insert(result118, result121)
|
||||
end
|
||||
end
|
||||
packet.bbbb = result118
|
||||
local result122 = byteBuffer:readInt()
|
||||
packet.c = result122
|
||||
local result123 = byteBuffer:readInt()
|
||||
packet.cc = result123
|
||||
local result124 = {}
|
||||
local size126 = byteBuffer:readInt()
|
||||
if size126 > 0 then
|
||||
for index125 = 1, size126 do
|
||||
local result127 = byteBuffer:readInt()
|
||||
table.insert(result124, result127)
|
||||
end
|
||||
end
|
||||
packet.ccc = result124
|
||||
local result128 = {}
|
||||
local size130 = byteBuffer:readInt()
|
||||
if size130 > 0 then
|
||||
for index129 = 1, size130 do
|
||||
local result131 = byteBuffer:readInt()
|
||||
table.insert(result128, result131)
|
||||
end
|
||||
end
|
||||
packet.cccc = result128
|
||||
local result132 = byteBuffer:readLong()
|
||||
packet.d = result132
|
||||
local result133 = byteBuffer:readLong()
|
||||
packet.dd = result133
|
||||
local result134 = {}
|
||||
local size136 = byteBuffer:readInt()
|
||||
if size136 > 0 then
|
||||
for index135 = 1, size136 do
|
||||
local result137 = byteBuffer:readLong()
|
||||
table.insert(result134, result137)
|
||||
end
|
||||
end
|
||||
packet.ddd = result134
|
||||
local result138 = {}
|
||||
local size140 = byteBuffer:readInt()
|
||||
if size140 > 0 then
|
||||
for index139 = 1, size140 do
|
||||
local result141 = byteBuffer:readLong()
|
||||
table.insert(result138, result141)
|
||||
end
|
||||
end
|
||||
packet.dddd = result138
|
||||
local result142 = byteBuffer:readFloat()
|
||||
packet.e = result142
|
||||
local result143 = byteBuffer:readFloat()
|
||||
packet.ee = result143
|
||||
local result144 = {}
|
||||
local size146 = byteBuffer:readInt()
|
||||
if size146 > 0 then
|
||||
for index145 = 1, size146 do
|
||||
local result147 = byteBuffer:readFloat()
|
||||
table.insert(result144, result147)
|
||||
end
|
||||
end
|
||||
packet.eee = result144
|
||||
local result148 = {}
|
||||
local size150 = byteBuffer:readInt()
|
||||
if size150 > 0 then
|
||||
for index149 = 1, size150 do
|
||||
local result151 = byteBuffer:readFloat()
|
||||
table.insert(result148, result151)
|
||||
end
|
||||
end
|
||||
packet.eeee = result148
|
||||
local result152 = byteBuffer:readDouble()
|
||||
packet.f = result152
|
||||
local result153 = byteBuffer:readDouble()
|
||||
packet.ff = result153
|
||||
local result154 = {}
|
||||
local size156 = byteBuffer:readInt()
|
||||
if size156 > 0 then
|
||||
for index155 = 1, size156 do
|
||||
local result157 = byteBuffer:readDouble()
|
||||
table.insert(result154, result157)
|
||||
end
|
||||
end
|
||||
packet.fff = result154
|
||||
local result158 = {}
|
||||
local size160 = byteBuffer:readInt()
|
||||
if size160 > 0 then
|
||||
for index159 = 1, size160 do
|
||||
local result161 = byteBuffer:readDouble()
|
||||
table.insert(result158, result161)
|
||||
end
|
||||
end
|
||||
packet.ffff = result158
|
||||
local result162 = byteBuffer:readBoolean()
|
||||
packet.g = result162
|
||||
local result163 = byteBuffer:readBoolean()
|
||||
packet.gg = result163
|
||||
local result164 = {}
|
||||
local size166 = byteBuffer:readInt()
|
||||
if size166 > 0 then
|
||||
for index165 = 1, size166 do
|
||||
local result167 = byteBuffer:readBoolean()
|
||||
table.insert(result164, result167)
|
||||
end
|
||||
end
|
||||
packet.ggg = result164
|
||||
local result168 = {}
|
||||
local size170 = byteBuffer:readInt()
|
||||
if size170 > 0 then
|
||||
for index169 = 1, size170 do
|
||||
local result171 = byteBuffer:readBoolean()
|
||||
table.insert(result168, result171)
|
||||
end
|
||||
end
|
||||
packet.gggg = result168
|
||||
local result172 = byteBuffer:readChar()
|
||||
packet.h = result172
|
||||
local result173 = byteBuffer:readChar()
|
||||
packet.hh = result173
|
||||
local result174 = {}
|
||||
local size176 = byteBuffer:readInt()
|
||||
if size176 > 0 then
|
||||
for index175 = 1, size176 do
|
||||
local result177 = byteBuffer:readChar()
|
||||
table.insert(result174, result177)
|
||||
end
|
||||
end
|
||||
packet.hhh = result174
|
||||
local result178 = {}
|
||||
local size180 = byteBuffer:readInt()
|
||||
if size180 > 0 then
|
||||
for index179 = 1, size180 do
|
||||
local result181 = byteBuffer:readChar()
|
||||
table.insert(result178, result181)
|
||||
end
|
||||
end
|
||||
packet.hhhh = result178
|
||||
local result182 = byteBuffer:readString()
|
||||
packet.jj = result182
|
||||
local result183 = {}
|
||||
local size185 = byteBuffer:readInt()
|
||||
if size185 > 0 then
|
||||
for index184 = 1, size185 do
|
||||
local result186 = byteBuffer:readString()
|
||||
table.insert(result183, result186)
|
||||
end
|
||||
end
|
||||
packet.jjj = result183
|
||||
local result187 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
packet.kk = result187
|
||||
local result188 = {}
|
||||
local size190 = byteBuffer:readInt()
|
||||
if size190 > 0 then
|
||||
for index189 = 1, size190 do
|
||||
local result191 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result188, result191)
|
||||
end
|
||||
end
|
||||
packet.kkk = result188
|
||||
local result192 = {}
|
||||
local size193 = byteBuffer:readInt()
|
||||
if size193 > 0 then
|
||||
for index194 = 1, size193 do
|
||||
local result195 = byteBuffer:readInt()
|
||||
table.insert(result192, result195)
|
||||
end
|
||||
end
|
||||
packet.l = result192
|
||||
local result196 = {}
|
||||
local size197 = byteBuffer:readInt()
|
||||
if size197 > 0 then
|
||||
for index198 = 1, size197 do
|
||||
local result199 = {}
|
||||
local size200 = byteBuffer:readInt()
|
||||
if size200 > 0 then
|
||||
for index201 = 1, size200 do
|
||||
local result202 = {}
|
||||
local size203 = byteBuffer:readInt()
|
||||
if size203 > 0 then
|
||||
for index204 = 1, size203 do
|
||||
local result205 = byteBuffer:readInt()
|
||||
table.insert(result202, result205)
|
||||
end
|
||||
end
|
||||
table.insert(result199, result202)
|
||||
end
|
||||
end
|
||||
table.insert(result196, result199)
|
||||
end
|
||||
end
|
||||
packet.ll = result196
|
||||
local result206 = {}
|
||||
local size207 = byteBuffer:readInt()
|
||||
if size207 > 0 then
|
||||
for index208 = 1, size207 do
|
||||
local result209 = {}
|
||||
local size210 = byteBuffer:readInt()
|
||||
if size210 > 0 then
|
||||
for index211 = 1, size210 do
|
||||
local result212 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result209, result212)
|
||||
end
|
||||
end
|
||||
table.insert(result206, result209)
|
||||
end
|
||||
end
|
||||
packet.lll = result206
|
||||
local result213 = {}
|
||||
local size214 = byteBuffer:readInt()
|
||||
if size214 > 0 then
|
||||
for index215 = 1, size214 do
|
||||
local result216 = byteBuffer:readString()
|
||||
table.insert(result213, result216)
|
||||
end
|
||||
end
|
||||
packet.llll = result213
|
||||
local result217 = {}
|
||||
local size218 = byteBuffer:readInt()
|
||||
if size218 > 0 then
|
||||
for index219 = 1, size218 do
|
||||
local result220 = {}
|
||||
local size221 = byteBuffer:readInt()
|
||||
if size221 > 0 then
|
||||
for index222 = 1, size221 do
|
||||
local result223 = byteBuffer:readInt()
|
||||
local result224 = byteBuffer:readString()
|
||||
result220[result223] = result224
|
||||
end
|
||||
end
|
||||
table.insert(result217, result220)
|
||||
end
|
||||
end
|
||||
packet.lllll = result217
|
||||
local result225 = {}
|
||||
local size226 = byteBuffer:readInt()
|
||||
if size226 > 0 then
|
||||
for index227 = 1, size226 do
|
||||
local result228 = byteBuffer:readInt()
|
||||
local result229 = byteBuffer:readString()
|
||||
result225[result228] = result229
|
||||
end
|
||||
end
|
||||
packet.m = result225
|
||||
local result230 = {}
|
||||
local size231 = byteBuffer:readInt()
|
||||
if size231 > 0 then
|
||||
for index232 = 1, size231 do
|
||||
local result233 = byteBuffer:readInt()
|
||||
local result234 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result230[result233] = result234
|
||||
end
|
||||
end
|
||||
packet.mm = result230
|
||||
local result235 = {}
|
||||
local size236 = byteBuffer:readInt()
|
||||
if size236 > 0 then
|
||||
for index237 = 1, size236 do
|
||||
local result238 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
local result239 = {}
|
||||
local size240 = byteBuffer:readInt()
|
||||
if size240 > 0 then
|
||||
for index241 = 1, size240 do
|
||||
local result242 = byteBuffer:readInt()
|
||||
table.insert(result239, result242)
|
||||
end
|
||||
end
|
||||
result235[result238] = result239
|
||||
end
|
||||
end
|
||||
packet.mmm = result235
|
||||
local result243 = {}
|
||||
local size244 = byteBuffer:readInt()
|
||||
if size244 > 0 then
|
||||
for index245 = 1, size244 do
|
||||
local result246 = {}
|
||||
local size247 = byteBuffer:readInt()
|
||||
if size247 > 0 then
|
||||
for index248 = 1, size247 do
|
||||
local result249 = {}
|
||||
local size250 = byteBuffer:readInt()
|
||||
if size250 > 0 then
|
||||
for index251 = 1, size250 do
|
||||
local result252 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result249, result252)
|
||||
end
|
||||
end
|
||||
table.insert(result246, result249)
|
||||
end
|
||||
end
|
||||
local result253 = {}
|
||||
local size254 = byteBuffer:readInt()
|
||||
if size254 > 0 then
|
||||
for index255 = 1, size254 do
|
||||
local result256 = {}
|
||||
local size257 = byteBuffer:readInt()
|
||||
if size257 > 0 then
|
||||
for index258 = 1, size257 do
|
||||
local result259 = {}
|
||||
local size260 = byteBuffer:readInt()
|
||||
if size260 > 0 then
|
||||
for index261 = 1, size260 do
|
||||
local result262 = byteBuffer:readInt()
|
||||
table.insert(result259, result262)
|
||||
end
|
||||
end
|
||||
table.insert(result256, result259)
|
||||
end
|
||||
end
|
||||
table.insert(result253, result256)
|
||||
end
|
||||
end
|
||||
result243[result246] = result253
|
||||
end
|
||||
end
|
||||
packet.mmmm = result243
|
||||
local result263 = {}
|
||||
local size264 = byteBuffer:readInt()
|
||||
if size264 > 0 then
|
||||
for index265 = 1, size264 do
|
||||
local result266 = {}
|
||||
local size267 = byteBuffer:readInt()
|
||||
if size267 > 0 then
|
||||
for index268 = 1, size267 do
|
||||
local result269 = {}
|
||||
local size270 = byteBuffer:readInt()
|
||||
if size270 > 0 then
|
||||
for index271 = 1, size270 do
|
||||
local result272 = byteBuffer:readInt()
|
||||
local result273 = byteBuffer:readString()
|
||||
result269[result272] = result273
|
||||
end
|
||||
end
|
||||
table.insert(result266, result269)
|
||||
end
|
||||
end
|
||||
local result274 = {}
|
||||
local size275 = byteBuffer:readInt()
|
||||
if size275 > 0 then
|
||||
for index276 = 1, size275 do
|
||||
local result277 = {}
|
||||
local size278 = byteBuffer:readInt()
|
||||
if size278 > 0 then
|
||||
for index279 = 1, size278 do
|
||||
local result280 = byteBuffer:readInt()
|
||||
local result281 = byteBuffer:readString()
|
||||
result277[result280] = result281
|
||||
end
|
||||
end
|
||||
result274[result277] = result277
|
||||
end
|
||||
end
|
||||
result263[result266] = result274
|
||||
end
|
||||
end
|
||||
packet.mmmmm = result263
|
||||
local result282 = {}
|
||||
local size283 = byteBuffer:readInt()
|
||||
if size283 > 0 then
|
||||
for index284 = 1, size283 do
|
||||
local result285 = byteBuffer:readInt()
|
||||
result282[result285] = result285
|
||||
end
|
||||
end
|
||||
packet.s = result282
|
||||
local result286 = {}
|
||||
local size287 = byteBuffer:readInt()
|
||||
if size287 > 0 then
|
||||
for index288 = 1, size287 do
|
||||
local result289 = {}
|
||||
local size290 = byteBuffer:readInt()
|
||||
if size290 > 0 then
|
||||
for index291 = 1, size290 do
|
||||
local result292 = {}
|
||||
local size293 = byteBuffer:readInt()
|
||||
if size293 > 0 then
|
||||
for index294 = 1, size293 do
|
||||
local result295 = byteBuffer:readInt()
|
||||
table.insert(result292, result295)
|
||||
end
|
||||
end
|
||||
result289[result292] = result292
|
||||
end
|
||||
end
|
||||
result286[result289] = result289
|
||||
end
|
||||
end
|
||||
packet.ss = result286
|
||||
local result296 = {}
|
||||
local size297 = byteBuffer:readInt()
|
||||
if size297 > 0 then
|
||||
for index298 = 1, size297 do
|
||||
local result299 = {}
|
||||
local size300 = byteBuffer:readInt()
|
||||
if size300 > 0 then
|
||||
for index301 = 1, size300 do
|
||||
local result302 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result299[result302] = result302
|
||||
end
|
||||
end
|
||||
result296[result299] = result299
|
||||
end
|
||||
end
|
||||
packet.sss = result296
|
||||
local result303 = {}
|
||||
local size304 = byteBuffer:readInt()
|
||||
if size304 > 0 then
|
||||
for index305 = 1, size304 do
|
||||
local result306 = byteBuffer:readString()
|
||||
result303[result306] = result306
|
||||
end
|
||||
end
|
||||
packet.ssss = result303
|
||||
local result307 = {}
|
||||
local size308 = byteBuffer:readInt()
|
||||
if size308 > 0 then
|
||||
for index309 = 1, size308 do
|
||||
local result310 = {}
|
||||
local size311 = byteBuffer:readInt()
|
||||
if size311 > 0 then
|
||||
for index312 = 1, size311 do
|
||||
local result313 = byteBuffer:readInt()
|
||||
local result314 = byteBuffer:readString()
|
||||
result310[result313] = result314
|
||||
end
|
||||
end
|
||||
result307[result310] = result310
|
||||
end
|
||||
end
|
||||
packet.sssss = result307
|
||||
return packet
|
||||
end
|
||||
|
||||
return ComplexObject
|
||||
@@ -0,0 +1,369 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2021-02-07 17:18
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local NormalObject = {}
|
||||
|
||||
function NormalObject:new(a, aaa, b, bbb, c, ccc, d, ddd, e, eee, f, fff, g, ggg, h, hhh, jj, jjj, kk, kkk, l, llll, m, mm, s, ssss)
|
||||
local obj = {
|
||||
a = a, -- byte
|
||||
aaa = aaa, -- byte[]
|
||||
b = b, -- short
|
||||
bbb = bbb, -- short[]
|
||||
c = c, -- int
|
||||
ccc = ccc, -- int[]
|
||||
d = d, -- long
|
||||
ddd = ddd, -- long[]
|
||||
e = e, -- float
|
||||
eee = eee, -- float[]
|
||||
f = f, -- double
|
||||
fff = fff, -- double[]
|
||||
g = g, -- boolean
|
||||
ggg = ggg, -- boolean[]
|
||||
h = h, -- char
|
||||
hhh = hhh, -- char[]
|
||||
jj = jj, -- java.lang.String
|
||||
jjj = jjj, -- java.lang.String[]
|
||||
kk = kk, -- com.zfoo.protocol.packet.ObjectA
|
||||
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[]
|
||||
l = l, -- java.util.List<java.lang.Integer>
|
||||
llll = llll, -- java.util.List<java.lang.String>
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
s = s, -- java.util.Set<java.lang.Integer>
|
||||
ssss = ssss -- java.util.Set<java.lang.String>
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function NormalObject:protocolId()
|
||||
return 1161
|
||||
end
|
||||
|
||||
function NormalObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeByte(packet.a)
|
||||
if packet.aaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaa);
|
||||
for index0, element1 in pairs(packet.aaa) do
|
||||
byteBuffer:writeByte(element1)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeShort(packet.b)
|
||||
if packet.bbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbb);
|
||||
for index2, element3 in pairs(packet.bbb) do
|
||||
byteBuffer:writeShort(element3)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeInt(packet.c)
|
||||
if packet.ccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ccc);
|
||||
for index4, element5 in pairs(packet.ccc) do
|
||||
byteBuffer:writeInt(element5)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeLong(packet.d)
|
||||
if packet.ddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ddd);
|
||||
for index6, element7 in pairs(packet.ddd) do
|
||||
byteBuffer:writeLong(element7)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeFloat(packet.e)
|
||||
if packet.eee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eee);
|
||||
for index8, element9 in pairs(packet.eee) do
|
||||
byteBuffer:writeFloat(element9)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeDouble(packet.f)
|
||||
if packet.fff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.fff);
|
||||
for index10, element11 in pairs(packet.fff) do
|
||||
byteBuffer:writeDouble(element11)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
if packet.ggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ggg);
|
||||
for index12, element13 in pairs(packet.ggg) do
|
||||
byteBuffer:writeBoolean(element13)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeChar(packet.h)
|
||||
if packet.hhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhh);
|
||||
for index14, element15 in pairs(packet.hhh) do
|
||||
byteBuffer:writeChar(element15)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeString(packet.jj)
|
||||
if packet.jjj == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.jjj);
|
||||
for index16, element17 in pairs(packet.jjj) do
|
||||
byteBuffer:writeString(element17)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, packet.kk)
|
||||
if packet.kkk == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.kkk);
|
||||
for index18, element19 in pairs(packet.kkk) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element19)
|
||||
end
|
||||
end
|
||||
if packet.l == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.l)
|
||||
for index20, element21 in pairs(packet.l) do
|
||||
byteBuffer:writeInt(element21)
|
||||
end
|
||||
end
|
||||
if packet.llll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.llll)
|
||||
for index22, element23 in pairs(packet.llll) do
|
||||
byteBuffer:writeString(element23)
|
||||
end
|
||||
end
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key24, value25 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key24)
|
||||
byteBuffer:writeString(value25)
|
||||
end
|
||||
end
|
||||
if packet.mm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mm))
|
||||
for key26, value27 in pairs(packet.mm) do
|
||||
byteBuffer:writeInt(key26)
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, value27)
|
||||
end
|
||||
end
|
||||
if packet.s == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.s))
|
||||
for index28, element29 in pairs(packet.s) do
|
||||
byteBuffer:writeInt(element29)
|
||||
end
|
||||
end
|
||||
if packet.ssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ssss))
|
||||
for index30, element31 in pairs(packet.ssss) do
|
||||
byteBuffer:writeString(element31)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function NormalObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = NormalObject:new()
|
||||
local result32 = byteBuffer:readByte()
|
||||
packet.a = result32
|
||||
local result33 = {}
|
||||
local size35 = byteBuffer:readInt()
|
||||
if size35 > 0 then
|
||||
for index34 = 1, size35 do
|
||||
local result36 = byteBuffer:readByte()
|
||||
table.insert(result33, result36)
|
||||
end
|
||||
end
|
||||
packet.aaa = result33
|
||||
local result37 = byteBuffer:readShort()
|
||||
packet.b = result37
|
||||
local result38 = {}
|
||||
local size40 = byteBuffer:readInt()
|
||||
if size40 > 0 then
|
||||
for index39 = 1, size40 do
|
||||
local result41 = byteBuffer:readShort()
|
||||
table.insert(result38, result41)
|
||||
end
|
||||
end
|
||||
packet.bbb = result38
|
||||
local result42 = byteBuffer:readInt()
|
||||
packet.c = result42
|
||||
local result43 = {}
|
||||
local size45 = byteBuffer:readInt()
|
||||
if size45 > 0 then
|
||||
for index44 = 1, size45 do
|
||||
local result46 = byteBuffer:readInt()
|
||||
table.insert(result43, result46)
|
||||
end
|
||||
end
|
||||
packet.ccc = result43
|
||||
local result47 = byteBuffer:readLong()
|
||||
packet.d = result47
|
||||
local result48 = {}
|
||||
local size50 = byteBuffer:readInt()
|
||||
if size50 > 0 then
|
||||
for index49 = 1, size50 do
|
||||
local result51 = byteBuffer:readLong()
|
||||
table.insert(result48, result51)
|
||||
end
|
||||
end
|
||||
packet.ddd = result48
|
||||
local result52 = byteBuffer:readFloat()
|
||||
packet.e = result52
|
||||
local result53 = {}
|
||||
local size55 = byteBuffer:readInt()
|
||||
if size55 > 0 then
|
||||
for index54 = 1, size55 do
|
||||
local result56 = byteBuffer:readFloat()
|
||||
table.insert(result53, result56)
|
||||
end
|
||||
end
|
||||
packet.eee = result53
|
||||
local result57 = byteBuffer:readDouble()
|
||||
packet.f = result57
|
||||
local result58 = {}
|
||||
local size60 = byteBuffer:readInt()
|
||||
if size60 > 0 then
|
||||
for index59 = 1, size60 do
|
||||
local result61 = byteBuffer:readDouble()
|
||||
table.insert(result58, result61)
|
||||
end
|
||||
end
|
||||
packet.fff = result58
|
||||
local result62 = byteBuffer:readBoolean()
|
||||
packet.g = result62
|
||||
local result63 = {}
|
||||
local size65 = byteBuffer:readInt()
|
||||
if size65 > 0 then
|
||||
for index64 = 1, size65 do
|
||||
local result66 = byteBuffer:readBoolean()
|
||||
table.insert(result63, result66)
|
||||
end
|
||||
end
|
||||
packet.ggg = result63
|
||||
local result67 = byteBuffer:readChar()
|
||||
packet.h = result67
|
||||
local result68 = {}
|
||||
local size70 = byteBuffer:readInt()
|
||||
if size70 > 0 then
|
||||
for index69 = 1, size70 do
|
||||
local result71 = byteBuffer:readChar()
|
||||
table.insert(result68, result71)
|
||||
end
|
||||
end
|
||||
packet.hhh = result68
|
||||
local result72 = byteBuffer:readString()
|
||||
packet.jj = result72
|
||||
local result73 = {}
|
||||
local size75 = byteBuffer:readInt()
|
||||
if size75 > 0 then
|
||||
for index74 = 1, size75 do
|
||||
local result76 = byteBuffer:readString()
|
||||
table.insert(result73, result76)
|
||||
end
|
||||
end
|
||||
packet.jjj = result73
|
||||
local result77 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
packet.kk = result77
|
||||
local result78 = {}
|
||||
local size80 = byteBuffer:readInt()
|
||||
if size80 > 0 then
|
||||
for index79 = 1, size80 do
|
||||
local result81 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result78, result81)
|
||||
end
|
||||
end
|
||||
packet.kkk = result78
|
||||
local result82 = {}
|
||||
local size83 = byteBuffer:readInt()
|
||||
if size83 > 0 then
|
||||
for index84 = 1, size83 do
|
||||
local result85 = byteBuffer:readInt()
|
||||
table.insert(result82, result85)
|
||||
end
|
||||
end
|
||||
packet.l = result82
|
||||
local result86 = {}
|
||||
local size87 = byteBuffer:readInt()
|
||||
if size87 > 0 then
|
||||
for index88 = 1, size87 do
|
||||
local result89 = byteBuffer:readString()
|
||||
table.insert(result86, result89)
|
||||
end
|
||||
end
|
||||
packet.llll = result86
|
||||
local result90 = {}
|
||||
local size91 = byteBuffer:readInt()
|
||||
if size91 > 0 then
|
||||
for index92 = 1, size91 do
|
||||
local result93 = byteBuffer:readInt()
|
||||
local result94 = byteBuffer:readString()
|
||||
result90[result93] = result94
|
||||
end
|
||||
end
|
||||
packet.m = result90
|
||||
local result95 = {}
|
||||
local size96 = byteBuffer:readInt()
|
||||
if size96 > 0 then
|
||||
for index97 = 1, size96 do
|
||||
local result98 = byteBuffer:readInt()
|
||||
local result99 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result95[result98] = result99
|
||||
end
|
||||
end
|
||||
packet.mm = result95
|
||||
local result100 = {}
|
||||
local size101 = byteBuffer:readInt()
|
||||
if size101 > 0 then
|
||||
for index102 = 1, size101 do
|
||||
local result103 = byteBuffer:readInt()
|
||||
result100[result103] = result103
|
||||
end
|
||||
end
|
||||
packet.s = result100
|
||||
local result104 = {}
|
||||
local size105 = byteBuffer:readInt()
|
||||
if size105 > 0 then
|
||||
for index106 = 1, size105 do
|
||||
local result107 = byteBuffer:readString()
|
||||
result104[result107] = result107
|
||||
end
|
||||
end
|
||||
packet.ssss = result104
|
||||
return packet
|
||||
end
|
||||
|
||||
return NormalObject
|
||||
@@ -0,0 +1,65 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.12 15:39
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local ObjectA = {}
|
||||
|
||||
function ObjectA:new(a, m, objectB)
|
||||
local obj = {
|
||||
a = a, -- int
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
objectB = objectB -- com.zfoo.protocol.packet.ObjectB
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ObjectA:protocolId()
|
||||
return 1116
|
||||
end
|
||||
|
||||
function ObjectA:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeInt(packet.a)
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key0, value1 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key0)
|
||||
byteBuffer:writeString(value1)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1117):write(byteBuffer, packet.objectB)
|
||||
end
|
||||
|
||||
function ObjectA:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ObjectA:new()
|
||||
local result2 = byteBuffer:readInt()
|
||||
packet.a = result2
|
||||
local result3 = {}
|
||||
local size4 = byteBuffer:readInt()
|
||||
if size4 > 0 then
|
||||
for index5 = 1, size4 do
|
||||
local result6 = byteBuffer:readInt()
|
||||
local result7 = byteBuffer:readString()
|
||||
result3[result6] = result7
|
||||
end
|
||||
end
|
||||
packet.m = result3
|
||||
local result8 = ProtocolManager.getProtocol(1117):read(byteBuffer)
|
||||
packet.objectB = result8
|
||||
return packet
|
||||
end
|
||||
|
||||
return ObjectA
|
||||
@@ -0,0 +1,39 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.12 15:39
|
||||
|
||||
local ObjectB = {}
|
||||
|
||||
function ObjectB:new(flag)
|
||||
local obj = {
|
||||
flag = flag -- boolean
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ObjectB:protocolId()
|
||||
return 1117
|
||||
end
|
||||
|
||||
function ObjectB:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeBoolean(packet.flag)
|
||||
end
|
||||
|
||||
function ObjectB:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ObjectB:new()
|
||||
local result0 = byteBuffer:readBoolean()
|
||||
packet.flag = result0
|
||||
return packet
|
||||
end
|
||||
|
||||
return ObjectB
|
||||
@@ -0,0 +1,43 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2021-03-27 15:18
|
||||
|
||||
local SimpleObject = {}
|
||||
|
||||
function SimpleObject:new(c, g)
|
||||
local obj = {
|
||||
c = c, -- int
|
||||
g = g -- boolean
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function SimpleObject:protocolId()
|
||||
return 1163
|
||||
end
|
||||
|
||||
function SimpleObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeInt(packet.c)
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
end
|
||||
|
||||
function SimpleObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = SimpleObject:new()
|
||||
local result0 = byteBuffer:readInt()
|
||||
packet.c = result0
|
||||
local result1 = byteBuffer:readBoolean()
|
||||
packet.g = result1
|
||||
return packet
|
||||
end
|
||||
|
||||
return SimpleObject
|
||||
@@ -0,0 +1,68 @@
|
||||
local ByteBuffer = require("LuaProtocol.Buffer.ByteBuffer")
|
||||
|
||||
protocols = {}
|
||||
|
||||
ProtocolManager = {}
|
||||
|
||||
-- table扩展方法,后去set和map的大小
|
||||
function table.setSize(set)
|
||||
local size = 0
|
||||
for _,_ in pairs(set) do
|
||||
size = size + 1
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
|
||||
function table.mapSize(map)
|
||||
local size = 0
|
||||
for _,_ in pairs(map) do
|
||||
size = size + 1
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
function ProtocolManager.getProtocol(protocolId)
|
||||
local protocol = protocols[protocolId]
|
||||
if protocol == nil then
|
||||
error("[protocolId:" + protocolId + "]协议不存在")
|
||||
end
|
||||
return protocol
|
||||
end
|
||||
|
||||
function ProtocolManager.write(byteBuffer, packet)
|
||||
local protocolId = packet:protocolId()
|
||||
-- 写入协议号
|
||||
byteBuffer:writeShort(protocolId)
|
||||
-- 写入包体
|
||||
ProtocolManager.getProtocol(protocolId):write(byteBuffer, packet)
|
||||
end
|
||||
|
||||
function ProtocolManager.read(byteBuffer)
|
||||
local protocolId = byteBuffer:readShort()
|
||||
return ProtocolManager.getProtocol(protocolId):read(byteBuffer)
|
||||
end
|
||||
|
||||
-- C#传进来的byte数组到lua里就会变成string
|
||||
function readBytes(bytes)
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
byteBuffer:writeBuffer(bytes)
|
||||
local packet = ProtocolManager.read(byteBuffer)
|
||||
return packet
|
||||
end
|
||||
|
||||
function initProtocol()
|
||||
local ObjectA = require("LuaProtocol.Packet.ObjectA")
|
||||
local ObjectB = require("LuaProtocol.Packet.ObjectB")
|
||||
local ComplexObject = require("LuaProtocol.Packet.ComplexObject")
|
||||
local NormalObject = require("LuaProtocol.Packet.NormalObject")
|
||||
local SimpleObject = require("LuaProtocol.Packet.SimpleObject")
|
||||
protocols[1116] = ObjectA
|
||||
protocols[1117] = ObjectB
|
||||
protocols[1160] = ComplexObject
|
||||
protocols[1161] = NormalObject
|
||||
protocols[1163] = SimpleObject
|
||||
end
|
||||
|
||||
ProtocolManager.initProtocol = initProtocol
|
||||
return ProtocolManager
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Spring.Util;
|
||||
using XLua;
|
||||
|
||||
namespace Test.Editor.LuaTest
|
||||
{
|
||||
public class LuaProtocolTest
|
||||
{
|
||||
public static readonly string TEST_PATH = "Assets/Test/Editor/LuaTest/";
|
||||
|
||||
[Test]
|
||||
public void ComplexObjectTest()
|
||||
{
|
||||
// 获取复杂对象的字节流
|
||||
var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
|
||||
|
||||
var luaEnv = new LuaEnv();
|
||||
var luaDebugBuilder = new StringBuilder();
|
||||
// Rider的断点调试
|
||||
// luaDebugBuilder.Append("package.cpath = package.cpath .. ';C:/Users/jaysunxiao/AppData/Roaming/JetBrains/Rider2021.1/plugins/EmmyLua/classes/debugger/emmy/windows/x64/?.dll'").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("local dbg = require('emmy_core')").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("dbg.tcpListen('localhost', 9966)").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("dbg.waitIDE()").Append(FileUtils.LS);
|
||||
|
||||
luaEnv.DoString(luaDebugBuilder.ToString());
|
||||
|
||||
luaEnv.AddLoader(CustomLoader);
|
||||
|
||||
var luaProtocolTestStr = File.ReadAllText(TEST_PATH + "main.lua");
|
||||
luaEnv.DoString(luaProtocolTestStr, "main");
|
||||
|
||||
LuaFunction byteBufferTestFunction = luaEnv.Global.Get<LuaFunction>("byteBufferTest");
|
||||
byteBufferTestFunction.Call();
|
||||
|
||||
LuaFunction complexObjectTestFuction = luaEnv.Global.Get<LuaFunction>("complexObjectTest");
|
||||
complexObjectTestFuction.Call(complexObjectBytes);
|
||||
}
|
||||
|
||||
public static byte[] CustomLoader(ref string filepath)
|
||||
{
|
||||
filepath = filepath.Replace(".", "/") + ".lua";
|
||||
|
||||
return File.ReadAllBytes(TEST_PATH + filepath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
local ByteBuffer = require("LuaProtocol.Buffer.ByteBuffer")
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
|
||||
-------------------------------------ProtocolManager的测试-------------------------------------
|
||||
function complexObjectTest(bytes)
|
||||
ProtocolManager.initProtocol()
|
||||
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
byteBuffer:writeBuffer(bytes)
|
||||
local packet = ProtocolManager.read(byteBuffer)
|
||||
|
||||
local newByteBuffer = ByteBuffer:new()
|
||||
ProtocolManager.write(newByteBuffer, packet)
|
||||
assert(#byteBuffer.buffer == #newByteBuffer.buffer)
|
||||
|
||||
-- set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
--for i = 1, #byteBuffer.buffer do
|
||||
-- print(i)
|
||||
-- assert(byteBuffer.buffer[i] == newByteBuffer.buffer[i], i)
|
||||
--end
|
||||
|
||||
local newPacket = ProtocolManager.read(newByteBuffer)
|
||||
return packet
|
||||
end
|
||||
|
||||
|
||||
|
||||
-------------------------------------ByteBuffer的测试-------------------------------------
|
||||
function byteBufferTest()
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeBoolean(false)
|
||||
assert(byteBuffer:readBoolean() == true)
|
||||
assert(byteBuffer:readBoolean() == false)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeUByte(99)
|
||||
byteBuffer:writeUByte(128)
|
||||
assert(byteBuffer:readUByte() == 99)
|
||||
assert(byteBuffer:readUByte() == 128)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeByte(127)
|
||||
byteBuffer:writeByte(-128)
|
||||
assert(byteBuffer:readByte() == 127)
|
||||
assert(byteBuffer:readByte() == -128)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeShort(32767)
|
||||
byteBuffer:writeShort(0)
|
||||
byteBuffer:writeShort(-32768)
|
||||
assert(byteBuffer:readShort() == 32767)
|
||||
assert(byteBuffer:readShort() == 0)
|
||||
assert(byteBuffer:readShort() == -32768)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeInt(2147483647)
|
||||
byteBuffer:writeInt(-999999)
|
||||
byteBuffer:writeInt(0)
|
||||
byteBuffer:writeInt(999999)
|
||||
byteBuffer:writeInt(-2147483648)
|
||||
assert(byteBuffer:readInt() == 2147483647)
|
||||
assert(byteBuffer:readInt() == -999999)
|
||||
assert(byteBuffer:readInt() == 0)
|
||||
assert(byteBuffer:readInt() == 999999)
|
||||
assert(byteBuffer:readInt() == -2147483648)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeLuaNumber(1234.5678)
|
||||
byteBuffer:writeLuaNumber(0)
|
||||
byteBuffer:writeLuaNumber(-2147483648)
|
||||
assert(math.abs(byteBuffer:readLuaNumber() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readLuaNumber() == 0)
|
||||
assert(byteBuffer:readLuaNumber() == -2147483648)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeLong(math.mininteger)
|
||||
byteBuffer:writeLong(-9223372036854775807)
|
||||
byteBuffer:writeLong(-9999999999999999)
|
||||
byteBuffer:writeLong(-99999999)
|
||||
byteBuffer:writeLong(0)
|
||||
byteBuffer:writeLong(99999999)
|
||||
byteBuffer:writeLong(9999999999999999)
|
||||
byteBuffer:writeLong(9223372036854775807)
|
||||
assert(byteBuffer:readLong() == math.mininteger)
|
||||
assert(byteBuffer:readLong() == -9223372036854775807)
|
||||
assert(byteBuffer:readLong() == -9999999999999999)
|
||||
assert(byteBuffer:readLong() == -99999999)
|
||||
assert(byteBuffer:readLong() == 0)
|
||||
assert(byteBuffer:readLong() == 99999999)
|
||||
assert(byteBuffer:readLong() == 9999999999999999)
|
||||
assert(byteBuffer:readLong() == 9223372036854775807)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeFloat(0x0.000002P-126)
|
||||
byteBuffer:writeFloat(0)
|
||||
byteBuffer:writeFloat(1234.5678)
|
||||
byteBuffer:writeFloat(0x1.fffffeP+127)
|
||||
assert(byteBuffer:readFloat() == 0x0.000002P-126)
|
||||
assert(byteBuffer:readFloat() == 0)
|
||||
assert(math.abs(byteBuffer:readFloat() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readFloat() == 0x1.fffffeP+127)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeDouble(0x0.0000000000001P-1022)
|
||||
byteBuffer:writeDouble(0)
|
||||
byteBuffer:writeDouble(1234.5678)
|
||||
byteBuffer:writeDouble(0x1.fffffffffffffP+1023)
|
||||
assert(byteBuffer:readDouble() == 0x0.0000000000001P-1022)
|
||||
assert(byteBuffer:readDouble() == 0)
|
||||
assert(math.abs(byteBuffer:readDouble() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readDouble() == 0x1.fffffffffffffP+1023)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
local s = "你好 hello world"
|
||||
byteBuffer:writeString(s)
|
||||
assert(byteBuffer:readString() == s)
|
||||
|
||||
byteBuffer:writeChar(s)
|
||||
assert(byteBuffer:readChar() == "你")
|
||||
byteBuffer:setWriteOffset(0)
|
||||
byteBuffer:setReadOffset(0)
|
||||
|
||||
|
||||
print("----------------------------------------------------")
|
||||
end
|
||||
Reference in New Issue
Block a user