feat[protocol]: 提升协议的兼容性,协议向前兼容

This commit is contained in:
jaysunxiao
2022-05-22 22:26:42 +08:00
parent 389de2b6b3
commit 78cb8bd2fc
28 changed files with 1192 additions and 1315 deletions
@@ -2,9 +2,11 @@
--支持的lua版本为>=5.3
--支持标准的Lua是使用64-bit的int以及64-bit的双精度float
--当lua只能支持32位的整数类型时,可以考虑用Long来替代,需要修改原代码
--右移操作>>是无符号右移
--local Long = require("Long")
local ProtocolManager = require("LuaProtocol.ProtocolManager")
local maxInt = 2147483647
local minInt = -2147483648
local initSize = 128
@@ -33,6 +35,14 @@ function ByteBuffer:new()
end
-- C#传进来的byte数组到lua里就会变成string
function readBytes(bytes)
local buffer = ByteBuffer:new()
buffer:writeBuffer(bytes)
local packet = ProtocolManager.read(buffer)
return packet
end
-------------------------------------UTF8-------------------------------------
-- 判断utf8字符byte长度
-- 0xxxxxxx - 1 byte
@@ -115,6 +125,10 @@ function ByteBuffer:getAvailable()
return #self.buffer - self.writeOffset + 1
end
function ByteBuffer:isReadable()
return self.writeOffset > self.readOffset
end
-------------------------------------write和read-------------------------------------
--bool
@@ -387,7 +401,7 @@ function ByteBuffer:writeString(str)
self:writeInt(#str)
self:writeBuffer(str)
return self
end
end
function ByteBuffer:readString()
local length = self:readInt()
@@ -396,6 +410,10 @@ end
--char
function ByteBuffer:writeChar(charValue)
if charValue == nil or #charValue == 0 then
self:writeString("")
return
end
local str = utf8sub(charValue, 1, 1)
self:writeString(str)
return self
@@ -445,4 +463,571 @@ function ByteBuffer:getBytes(startIndex, endIndex)
return table.concat(self.buffer, "", startIndex, endIndex)
end
return ByteBuffer
function ByteBuffer:writePacketFlag(value)
local flag = (value == null)
self:writeBoolean(not flag)
return flag
end
function ByteBuffer:writePacket(value, protocolId)
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
protocolRegistration:write(self, value)
return self
end
function ByteBuffer:readPacket(protocolId)
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
return protocolRegistration:read(self)
end
function ByteBuffer:writeBooleanArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeBoolean(element)
end
end
return self
end
function ByteBuffer:readBooleanArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readBoolean())
end
end
return array
end
function ByteBuffer:writeByteArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeByte(element)
end
end
return self
end
function ByteBuffer:readByteArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readByte())
end
end
return array
end
function ByteBuffer:writeShortArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeShort(element)
end
end
return self
end
function ByteBuffer:readShortArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readShort())
end
end
return array
end
function ByteBuffer:writeIntArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeInt(element)
end
end
return self
end
function ByteBuffer:readIntArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readInt())
end
end
return array
end
function ByteBuffer:writeLongArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeLong(element)
end
end
return self
end
function ByteBuffer:readLongArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readLong())
end
end
return array
end
function ByteBuffer:writeFloatArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeFloat(element)
end
end
return self
end
function ByteBuffer:readFloatArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readFloat())
end
end
return array
end
function ByteBuffer:writeDoubleArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeDouble(element)
end
end
return self
end
function ByteBuffer:readDoubleArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readDouble())
end
end
return array
end
function ByteBuffer:writeCharArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeChar(element)
end
end
return self
end
function ByteBuffer:readCharArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readChar())
end
end
return array
end
function ByteBuffer:writeStringArray(array)
if array == null then
self:writeInt(0)
else
self:writeInt(#array);
for index, element in pairs(array) do
self:writeString(element)
end
end
return self
end
function ByteBuffer:readStringArray()
local array = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
table.insert(array, self:readString())
end
end
return array
end
function ByteBuffer:writePacketArray(array, protocolId)
if array == null then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#array);
for index, element in pairs(array) do
protocolRegistration:write(self, element)
end
end
return self
end
function ByteBuffer:readPacketArray(protocolId)
local array = {}
local size = self:readInt()
if size > 0 then
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
for index = 1, size do
table.insert(array, protocolRegistration:read(self))
end
end
return array
end
function ByteBuffer:writeIntIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeInt(value)
end
end
return self
end
function ByteBuffer:readIntIntMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readInt()
local value = self:readInt()
map[key] = value
end
end
return map
end
function ByteBuffer:writeIntLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeLong(value)
end
end
return self
end
function ByteBuffer:readIntLongMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readInt()
local value = self:readLong()
map[key] = value
end
end
return map
end
function ByteBuffer:writeIntStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeString(value)
end
end
return self
end
function ByteBuffer:readIntStringMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readInt()
local value = self:readString()
map[key] = value
end
end
return map
end
function ByteBuffer:writeIntPacketMap(map, protocolId)
if map == null then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
protocolRegistration:write(self, value)
end
end
return self
end
function ByteBuffer:readIntPacketMap(protocolId)
local map = {}
local size = self:readInt()
if size > 0 then
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
for index = 1, size do
local key = self:readInt()
local value = protocolRegistration:read(self)
map[key] = value
end
end
return map
end
function ByteBuffer:writeLongIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeInt(value)
end
end
return self
end
function ByteBuffer:readLongIntMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readLong()
local value = self:readInt()
map[key] = value
end
end
return map
end
function ByteBuffer:writeLongLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeLong(value)
end
end
return self
end
function ByteBuffer:readLongLongMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readLong()
local value = self:readLong()
map[key] = value
end
end
return map
end
function ByteBuffer:writeLongStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeString(value)
end
end
return self
end
function ByteBuffer:readLongStringMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readLong()
local value = self:readString()
map[key] = value
end
end
return map
end
function ByteBuffer:writeLongPacketMap(map, protocolId)
if map == null then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
protocolRegistration:write(self, value)
end
end
return self
end
function ByteBuffer:readLongPacketMap(protocolId)
local map = {}
local size = self:readInt()
if size > 0 then
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
for index = 1, size do
local key = self:readLong()
local value = protocolRegistration:read(self)
map[key] = value
end
end
return map
end
function ByteBuffer:writeStringIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeInt(value)
end
end
return self
end
function ByteBuffer:readStringIntMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readString()
local value = self:readInt()
map[key] = value
end
end
return map
end
function ByteBuffer:writeStringLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeLong(value)
end
end
return self
end
function ByteBuffer:readStringLongMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readString()
local value = self:readLong()
map[key] = value
end
end
return map
end
function ByteBuffer:writeStringStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeString(value)
end
end
return self
end
function ByteBuffer:readStringStringMap()
local map = {}
local size = self:readInt()
if size > 0 then
for index = 1, size do
local key = self:readString()
local value = self:readString()
map[key] = value
end
end
return map
end
function ByteBuffer:writeStringPacketMap(map, protocolId)
if map == null then
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
protocolRegistration:write(self, value)
end
end
return self
end
function ByteBuffer:readStringPacketMap(protocolId)
local map = {}
local size = self:readInt()
if size > 0 then
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
for index = 1, size do
local key = self:readString()
local value = protocolRegistration:read(self)
map[key] = value
end
end
return map
end
return ByteBuffer
@@ -1,4 +1,4 @@
al MAX_LONG_4BYTE = 1 << 32
local MAX_LONG_4BYTE = 1 << 32
local MIN_INT = -2147483648
local MAX_INT = 2147483647
local MIN_LONG = 0x8000000000000000
File diff suppressed because it is too large Load Diff
@@ -1,34 +1,23 @@
-- @author jaysunxiao
-- @version 1.0
-- @since 2021-02-07 17:18
local ProtocolManager = require("LuaProtocol.ProtocolManager")
-- @version 3.0
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)
function NormalObject:new(a, aaa, b, c, d, e, f, g, jj, kk, l, ll, lll, 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>
ll = ll, -- java.util.List<java.lang.Long>
lll = lll, -- java.util.List<com.zfoo.protocol.packet.ObjectA>
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>
@@ -41,328 +30,74 @@ function NormalObject:new(a, aaa, b, bbb, c, ccc, d, ddd, e, eee, f, fff, g, ggg
end
function NormalObject:protocolId()
return 1161
return 101
end
function NormalObject:write(byteBuffer, packet)
if packet == null then
byteBuffer:writeBoolean(false)
function NormalObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then
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
buffer:writeByte(packet.a)
buffer:writeByteArray(packet.aaa)
buffer:writeShort(packet.b)
buffer:writeInt(packet.c)
buffer:writeLong(packet.d)
buffer:writeFloat(packet.e)
buffer:writeDouble(packet.f)
buffer:writeBoolean(packet.g)
buffer:writeString(packet.jj)
buffer:writePacket(packet.kk, 102)
buffer:writeIntArray(packet.l)
buffer:writeLongArray(packet.ll)
buffer:writePacketArray(packet.lll, 102)
buffer:writeStringArray(packet.llll)
buffer:writeIntStringMap(packet.m)
buffer:writeIntPacketMap(packet.mm, 102)
buffer:writeIntArray(packet.s)
buffer:writeStringArray(packet.ssss)
end
function NormalObject:read(byteBuffer)
if not(byteBuffer:readBoolean()) then
function NormalObject:read(buffer)
if not(buffer: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
local result0 = buffer:readByte()
packet.a = result0
local array1 = buffer:readByteArray()
packet.aaa = array1
local result2 = buffer:readShort()
packet.b = result2
local result3 = buffer:readInt()
packet.c = result3
local result4 = buffer:readLong()
packet.d = result4
local result5 = buffer:readFloat()
packet.e = result5
local result6 = buffer:readDouble()
packet.f = result6
local result7 = buffer:readBoolean()
packet.g = result7
local result8 = buffer:readString()
packet.jj = result8
local result9 = buffer:readPacket(102)
packet.kk = result9
local list10 = buffer:readIntArray()
packet.l = list10
local list11 = buffer:readLongArray()
packet.ll = list11
local list12 = buffer:readPacketArray(102)
packet.lll = list12
local list13 = buffer:readStringArray()
packet.llll = list13
local map14 = buffer:readIntStringMap()
packet.m = map14
local map15 = buffer:readIntPacketMap(102)
packet.mm = map15
local set16 = buffer:readIntArray()
packet.s = set16
local set17 = buffer:readStringArray()
packet.ssss = set17
return packet
end
@@ -1,8 +1,5 @@
-- @author jaysunxiao
-- @version 1.0
-- @since 2017 10.12 15:39
local ProtocolManager = require("LuaProtocol.ProtocolManager")
-- @version 3.0
local ObjectA = {}
@@ -18,47 +15,29 @@ function ObjectA:new(a, m, objectB)
end
function ObjectA:protocolId()
return 1116
return 102
end
function ObjectA:write(byteBuffer, packet)
if packet == null then
byteBuffer:writeBoolean(false)
function ObjectA:write(buffer, packet)
if buffer:writePacketFlag(packet) then
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)
buffer:writeInt(packet.a)
buffer:writeIntStringMap(packet.m)
buffer:writePacket(packet.objectB, 103)
end
function ObjectA:read(byteBuffer)
if not(byteBuffer:readBoolean()) then
function ObjectA:read(buffer)
if not(buffer: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
local result0 = buffer:readInt()
packet.a = result0
local map1 = buffer:readIntStringMap()
packet.m = map1
local result2 = buffer:readPacket(103)
packet.objectB = result2
return packet
end
@@ -1,6 +1,5 @@
-- @author jaysunxiao
-- @version 1.0
-- @since 2017 10.12 15:39
-- @version 3.0
local ObjectB = {}
@@ -14,24 +13,22 @@ function ObjectB:new(flag)
end
function ObjectB:protocolId()
return 1117
return 103
end
function ObjectB:write(byteBuffer, packet)
if packet == null then
byteBuffer:writeBoolean(false)
function ObjectB:write(buffer, packet)
if buffer:writePacketFlag(packet) then
return
end
byteBuffer:writeBoolean(true)
byteBuffer:writeBoolean(packet.flag)
buffer:writeBoolean(packet.flag)
end
function ObjectB:read(byteBuffer)
if not(byteBuffer:readBoolean()) then
function ObjectB:read(buffer)
if not(buffer:readBoolean()) then
return nil
end
local packet = ObjectB:new()
local result0 = byteBuffer:readBoolean()
local result0 = buffer:readBoolean()
packet.flag = result0
return packet
end
@@ -1,6 +1,5 @@
-- @author jaysunxiao
-- @version 1.0
-- @since 2021-03-27 15:18
-- @version 3.0
local SimpleObject = {}
@@ -15,27 +14,25 @@ function SimpleObject:new(c, g)
end
function SimpleObject:protocolId()
return 1163
return 104
end
function SimpleObject:write(byteBuffer, packet)
if packet == null then
byteBuffer:writeBoolean(false)
function SimpleObject:write(buffer, packet)
if buffer:writePacketFlag(packet) then
return
end
byteBuffer:writeBoolean(true)
byteBuffer:writeInt(packet.c)
byteBuffer:writeBoolean(packet.g)
buffer:writeInt(packet.c)
buffer:writeBoolean(packet.g)
end
function SimpleObject:read(byteBuffer)
if not(byteBuffer:readBoolean()) then
function SimpleObject:read(buffer)
if not(buffer:readBoolean()) then
return nil
end
local packet = SimpleObject:new()
local result0 = byteBuffer:readInt()
local result0 = buffer:readInt()
packet.c = result0
local result1 = byteBuffer:readBoolean()
local result1 = buffer:readBoolean()
packet.g = result1
return packet
end
@@ -1,19 +1,8 @@
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
-- table扩展方法,map的大小
function table.mapSize(map)
local size = 0
for _,_ in pairs(map) do
@@ -30,38 +19,30 @@ function ProtocolManager.getProtocol(protocolId)
return protocol
end
function ProtocolManager.write(byteBuffer, packet)
function ProtocolManager.write(buffer, packet)
local protocolId = packet:protocolId()
-- 写入协议号
byteBuffer:writeShort(protocolId)
buffer:writeShort(protocolId)
-- 写入包体
ProtocolManager.getProtocol(protocolId):write(byteBuffer, packet)
ProtocolManager.getProtocol(protocolId):write(buffer, 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
function ProtocolManager.read(buffer)
local protocolId = buffer:readShort()
return ProtocolManager.getProtocol(protocolId):read(buffer)
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 ObjectA = require("LuaProtocol.Packet.ObjectA")
local ObjectB = require("LuaProtocol.Packet.ObjectB")
local SimpleObject = require("LuaProtocol.Packet.SimpleObject")
protocols[1116] = ObjectA
protocols[1117] = ObjectB
protocols[1160] = ComplexObject
protocols[1161] = NormalObject
protocols[1163] = SimpleObject
protocols[100] = ComplexObject
protocols[101] = NormalObject
protocols[102] = ObjectA
protocols[103] = ObjectB
protocols[104] = SimpleObject
end
ProtocolManager.initProtocol = initProtocol
+4 -1
View File
@@ -10,9 +10,11 @@ function complexObjectTest(bytes)
byteBuffer:writeBuffer(bytes)
local packet = ProtocolManager.read(byteBuffer)
-- complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
packet.myCompatible = 0
local newByteBuffer = ByteBuffer:new()
ProtocolManager.write(newByteBuffer, packet)
assert(#byteBuffer.buffer == #newByteBuffer.buffer)
assert(#byteBuffer.buffer <= #newByteBuffer.buffer)
-- set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
--for i = 1, #byteBuffer.buffer do
@@ -134,4 +136,5 @@ function byteBufferTest()
print("----------------------------------------------------")
print("Hello")
end