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
@@ -235,7 +235,7 @@ public abstract class EnhanceUtils {
var readObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
builder.append(StringUtils.format("if(!$1.isReadable()){ return packet; }"));
builder.append("if(!$1.isReadable()){ return packet; }");
}
if (Modifier.isPublic(field.getModifiers())) {
@@ -19,6 +19,7 @@ import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.anno.Compatible;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
@@ -178,7 +179,7 @@ public abstract class GenerateCsUtils {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var csBuilder = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
csSerializer(fieldRegistration.serializer()).writeObject(csBuilder, "message." + field.getName(), 3, field, fieldRegistration);
@@ -191,13 +192,17 @@ public abstract class GenerateCsUtils {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var csBuilder = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
String readObject = csSerializer(fieldRegistration.serializer()).readObject(csBuilder, 3, field, fieldRegistration);
csBuilder.append(TAB + TAB + TAB)
.append(StringUtils.format("packet.{} = {};", field.getName(), readObject))
.append(LS);
if (field.isAnnotationPresent(Compatible.class)) {
csBuilder.append(TAB + TAB + TAB).append("if (!buffer.IsReadable())").append(LS);
csBuilder.append(TAB + TAB + TAB).append("{").append(LS);
csBuilder.append(TAB + TAB + TAB + TAB).append("return packet;").append(LS);
csBuilder.append(TAB + TAB + TAB).append("}").append(LS);
}
var readObject = csSerializer(fieldRegistration.serializer()).readObject(csBuilder, 3, field, fieldRegistration);
csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
return csBuilder.toString();
}
@@ -19,6 +19,7 @@ import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.anno.Compatible;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
@@ -33,6 +34,7 @@ import java.util.List;
import java.util.Map;
import static com.zfoo.protocol.util.FileUtils.LS;
import static com.zfoo.protocol.util.StringUtils.TAB;
import static com.zfoo.protocol.util.StringUtils.TAB_ASCII;
/**
@@ -169,6 +171,10 @@ public abstract class GenerateGdUtils {
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
gdBuilder.append(TAB_ASCII).append("if (!buffer.isReadable()):").append(LS);
gdBuilder.append(TAB_ASCII + TAB_ASCII).append("return packet").append(LS);
}
var readObject = gdSerializer(fieldRegistration.serializer()).readObject(gdBuilder, 1, field, fieldRegistration);
gdBuilder.append(TAB_ASCII).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS);
}
@@ -20,6 +20,7 @@ import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.anno.Compatible;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
@@ -183,7 +184,11 @@ public abstract class GenerateJsUtils {
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
jsBuilder.append( TAB).append("if (!buffer.isReadable()) {").append(LS);
jsBuilder.append( TAB + TAB).append("return packet;").append(LS);
jsBuilder.append( TAB).append("}").append(LS);
}
var readObject = jsSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration);
jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
@@ -20,6 +20,7 @@ import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.anno.Compatible;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
@@ -193,6 +194,11 @@ public abstract class GenerateLuaUtils {
for (int i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
luaBuilder.append(TAB).append("if not(buffer:isReadable()) then").append(LS);
luaBuilder.append(TAB + TAB).append("return packet").append(LS);
luaBuilder.append(TAB).append("end").append(LS);
}
var readObject = luaSerializer(fieldRegistration.serializer()).readObject(luaBuilder, 1, field, fieldRegistration);
luaBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS);
}
@@ -88,6 +88,10 @@ namespace CsProtocol.Buffer
return bytes;
}
public bool IsReadable()
{
return writeOffset > readOffset;
}
// -------------------------------------------------write/read-------------------------------------------------
public void WriteBool(bool value)
@@ -1907,7 +1911,7 @@ namespace CsProtocol.Buffer
return map;
}
public void WritePacket<T>(T packet, short protocolId)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
@@ -1920,4 +1924,4 @@ namespace CsProtocol.Buffer
return (T) protocolRegistration.Read(this);
}
}
}
}
@@ -31,6 +31,9 @@ func setReadOffset(readIndex: int) -> void:
func getReadOffset() -> int:
return readOffset
func isReadable() -> bool:
return writeOffset > readOffset
# -------------------------------------------------write/read-------------------------------------------------
func writePoolByteArray(value: PoolByteArray):
var length = value.size()
@@ -82,6 +82,10 @@ const ByteBuffer = function() {
}
};
this.isReadable = function() {
return this.writeOffset > this.readOffset;
};
this.writeBoolean = function(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
@@ -339,7 +343,7 @@ const ByteBuffer = function() {
};
this.writePacketFlag = function(value) {
const flag = value === null;
const flag = (value === null) || (value === undefined);
this.writeBoolean(!flag);
return flag;
};
@@ -125,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
@@ -88,6 +88,10 @@ namespace CsProtocol.Buffer
return bytes;
}
public bool IsReadable()
{
return writeOffset > readOffset;
}
// -------------------------------------------------write/read-------------------------------------------------
public void WriteBool(bool value)
@@ -1907,7 +1911,7 @@ namespace CsProtocol.Buffer
return map;
}
public void WritePacket<T>(T packet, short protocolId)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
@@ -1920,4 +1924,4 @@ namespace CsProtocol.Buffer
return (T) protocolRegistration.Read(this);
}
}
}
}
@@ -66,8 +66,11 @@ namespace CsProtocol
public HashSet<HashSet<ObjectA>> sss;
public HashSet<string> ssss;
public HashSet<Dictionary<int, string>> sssss;
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
public int myCompatible;
public ObjectA myObject;
public static ComplexObject ValueOf(byte a, byte aa, byte[] aaa, byte[] aaaa, short b, short bb, short[] bbb, short[] bbbb, int c, int cc, int[] ccc, int[] cccc, long d, long dd, long[] ddd, long[] dddd, float e, float ee, float[] eee, float[] eeee, double f, double ff, double[] fff, double[] ffff, bool g, bool gg, bool[] ggg, bool[] gggg, char h, char hh, char[] hhh, char[] hhhh, string jj, string[] jjj, ObjectA kk, ObjectA[] kkk, List<int> l, List<List<List<int>>> ll, List<List<ObjectA>> lll, List<string> llll, List<Dictionary<int, string>> lllll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, Dictionary<ObjectA, List<int>> mmm, Dictionary<List<List<ObjectA>>, List<List<List<int>>>> mmmm, Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>> mmmmm, HashSet<int> s, HashSet<HashSet<List<int>>> ss, HashSet<HashSet<ObjectA>> sss, HashSet<string> ssss, HashSet<Dictionary<int, string>> sssss)
public static ComplexObject ValueOf(byte a, byte aa, byte[] aaa, byte[] aaaa, short b, short bb, short[] bbb, short[] bbbb, int c, int cc, int[] ccc, int[] cccc, long d, long dd, long[] ddd, long[] dddd, float e, float ee, float[] eee, float[] eeee, double f, double ff, double[] fff, double[] ffff, bool g, bool gg, bool[] ggg, bool[] gggg, char h, char hh, char[] hhh, char[] hhhh, string jj, string[] jjj, ObjectA kk, ObjectA[] kkk, List<int> l, List<List<List<int>>> ll, List<List<ObjectA>> lll, List<string> llll, List<Dictionary<int, string>> lllll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, Dictionary<ObjectA, List<int>> mmm, Dictionary<List<List<ObjectA>>, List<List<List<int>>>> mmmm, Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>> mmmmm, HashSet<int> s, HashSet<HashSet<List<int>>> ss, HashSet<HashSet<ObjectA>> sss, HashSet<string> ssss, HashSet<Dictionary<int, string>> sssss, int myCompatible, ObjectA myObject)
{
var packet = new ComplexObject();
packet.a = a;
@@ -121,6 +124,8 @@ namespace CsProtocol
packet.sss = sss;
packet.ssss = ssss;
packet.sssss = sssss;
packet.myCompatible = myCompatible;
packet.myObject = myObject;
return packet;
}
@@ -398,6 +403,8 @@ namespace CsProtocol
buffer.WriteIntStringMap(i37);
}
}
buffer.WriteInt(message.myCompatible);
buffer.WritePacket(message.myObject, 102);
}
public IPacket Read(ByteBuffer buffer)
@@ -656,6 +663,18 @@ namespace CsProtocol
}
}
packet.sssss = result136;
if (!buffer.IsReadable())
{
return packet;
}
int result140 = buffer.ReadInt();
packet.myCompatible = result140;
if (!buffer.IsReadable())
{
return packet;
}
ObjectA result141 = buffer.ReadPacket<ObjectA>(102);
packet.myObject = result141;
return packet;
}
}
+1 -1
View File
@@ -89,7 +89,7 @@ func test():
var packet = ProtocolManager.read(buffer)
print(packet)
packet.myCompatible = 0
var newByteBuffer = ByteBuffer.new()
ProtocolManager.write(newByteBuffer, packet);
print(newByteBuffer.getWriteOffset())
@@ -10,8 +10,8 @@ static func getProtocol(protocolId: int):
return protocols[protocolId]
static func newInstance(protocolId: int):
var protocol = protocols[protocolId]
return protocol.new()
var protocol = protocols[protocolId]
return protocol.new()
static func write(buffer, packet):
var protocolId: int = packet.PROTOCOL_ID
@@ -20,10 +20,10 @@ static func write(buffer, packet):
protocol.write(buffer, packet)
static func read(buffer):
var protocolId = buffer.readShort();
var protocol = protocols[protocolId]
var packet = protocol.read(buffer);
return packet;
var protocolId = buffer.readShort();
var protocol = protocols[protocolId]
var packet = protocol.read(buffer);
return packet;
static func initProtocol():
protocols[100] = ComplexObject
@@ -31,6 +31,9 @@ func setReadOffset(readIndex: int) -> void:
func getReadOffset() -> int:
return readOffset
func isReadable() -> bool:
return writeOffset > readOffset
# -------------------------------------------------write/read-------------------------------------------------
func writePoolByteArray(value: PoolByteArray):
var length = value.size()
@@ -59,6 +59,9 @@ var ss # java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
var sss # java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
var ssss # java.util.Set<java.lang.String>
var sssss # java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
# 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
var myCompatible # int
var myObject # com.zfoo.protocol.packet.ObjectA
const PROTOCOL_ID = 100
@@ -202,6 +205,8 @@ static func write(buffer, packet):
buffer.writeInt(packet.sssss.size())
for element18 in packet.sssss:
buffer.writeIntStringMap(element18)
buffer.writeInt(packet.myCompatible)
buffer.writePacket(packet.myObject, 102)
static func read(buffer):
@@ -393,4 +398,12 @@ static func read(buffer):
var map120 = buffer.readIntStringMap()
result117.append(map120)
packet.sssss = result117
if (!buffer.isReadable()):
return packet
var result121 = buffer.readInt()
packet.myCompatible = result121
if (!buffer.isReadable()):
return packet
var result122 = buffer.readPacket(102)
packet.myObject = result122
return packet
+3 -1
View File
@@ -16,6 +16,8 @@ describe('jsProtocolTest', () => {
byteBuffer.writeBytes(arrayBytes);
const packet = ProtocolManager.read(byteBuffer);
// complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
packet.myCompatible = 0
console.log(packet);
const newByteBuffer = new ByteBuffer();
@@ -24,7 +26,7 @@ describe('jsProtocolTest', () => {
const newPacket = ProtocolManager.read(newByteBuffer);
console.log(newPacket);
expect(byteBuffer.readOffset).toBe(newByteBuffer.writeOffset);
expect(byteBuffer.readOffset).toBeLessThan(newByteBuffer.writeOffset);
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
const length = newByteBuffer.writeOffset;
@@ -6,11 +6,7 @@ import SimpleObject from './packet/SimpleObject.js';
const protocols = new Map();
const ProtocolManager = protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
const ProtocolManager = protocols.set(100, ComplexObject); protocols.set(101, NormalObject); protocols.set(102, ObjectA); protocols.set(103, ObjectB); protocols.set(104, SimpleObject);
ProtocolManager.getProtocol = function getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
@@ -35,8 +31,7 @@ ProtocolManager.read = function read(buffer) {
};
ProtocolManager.initProtocol = function initProtocol() {
{
}
{}
};
export default ProtocolManager;
@@ -1,4 +1,4 @@
import {readInt64, writeInt64} from './longbits.js';
import { readInt64, writeInt64 } from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
@@ -82,6 +82,10 @@ const ByteBuffer = function() {
}
};
this.isReadable = function() {
return this.writeOffset > this.readOffset;
};
this.writeBoolean = function(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
@@ -339,22 +343,22 @@ const ByteBuffer = function() {
};
this.writePacketFlag = function(value) {
const flag = value === null;
const flag = (value === null) || (value === undefined);
this.writeBoolean(!flag);
return flag;
};
this.writePacket = function (packet, protocolId) {
this.writePacket = function(packet, protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, packet);
};
this.readPacket = function (protocolId) {
this.readPacket = function(protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
this.writeBooleanArray = function (array) {
this.writeBooleanArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -365,7 +369,7 @@ const ByteBuffer = function() {
}
};
this.readBooleanArray = function () {
this.readBooleanArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -376,7 +380,7 @@ const ByteBuffer = function() {
return array;
};
this.writeByteArray = function (array) {
this.writeByteArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -387,7 +391,7 @@ const ByteBuffer = function() {
}
};
this.readByteArray = function () {
this.readByteArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -398,7 +402,7 @@ const ByteBuffer = function() {
return array;
};
this.writeShortArray = function (array) {
this.writeShortArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -409,7 +413,7 @@ const ByteBuffer = function() {
}
};
this.readShortArray = function () {
this.readShortArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -420,7 +424,7 @@ const ByteBuffer = function() {
return array;
};
this.writeIntArray = function (array) {
this.writeIntArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -431,7 +435,7 @@ const ByteBuffer = function() {
}
};
this.readIntArray = function () {
this.readIntArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -442,7 +446,7 @@ const ByteBuffer = function() {
return array;
};
this.writeLongArray = function (array) {
this.writeLongArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -453,7 +457,7 @@ const ByteBuffer = function() {
}
};
this.readLongArray = function () {
this.readLongArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -464,7 +468,7 @@ const ByteBuffer = function() {
return array;
};
this.writeFloatArray = function (array) {
this.writeFloatArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -475,7 +479,7 @@ const ByteBuffer = function() {
}
};
this.readFloatArray = function () {
this.readFloatArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -486,7 +490,7 @@ const ByteBuffer = function() {
return array;
};
this.writeDoubleArray = function (array) {
this.writeDoubleArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -497,7 +501,7 @@ const ByteBuffer = function() {
}
};
this.readDoubleArray = function () {
this.readDoubleArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -508,7 +512,7 @@ const ByteBuffer = function() {
return array;
};
this.writeStringArray = function (array) {
this.writeStringArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -519,7 +523,7 @@ const ByteBuffer = function() {
}
};
this.readStringArray = function () {
this.readStringArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -530,7 +534,7 @@ const ByteBuffer = function() {
return array;
};
this.writeCharArray = function (array) {
this.writeCharArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -541,7 +545,7 @@ const ByteBuffer = function() {
}
};
this.readCharArray = function () {
this.readCharArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -552,7 +556,7 @@ const ByteBuffer = function() {
return array;
};
this.writePacketArray = function (array, protocolId) {
this.writePacketArray = function(array, protocolId) {
if (array === null) {
this.writeInt(0);
} else {
@@ -576,7 +580,7 @@ const ByteBuffer = function() {
return array;
};
this.writeIntIntMap = function (map) {
this.writeIntIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -601,7 +605,7 @@ const ByteBuffer = function() {
return map;
};
this.writeIntLongMap = function (map) {
this.writeIntLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -626,7 +630,7 @@ const ByteBuffer = function() {
return map;
};
this.writeIntStringMap = function (map) {
this.writeIntStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -651,7 +655,7 @@ const ByteBuffer = function() {
return map;
};
this.writeIntPacketMap = function (map, protocolId) {
this.writeIntPacketMap = function(map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
@@ -678,7 +682,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongIntMap = function (map) {
this.writeLongIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -703,7 +707,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongLongMap = function (map) {
this.writeLongLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -728,7 +732,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongStringMap = function (map) {
this.writeLongStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -753,7 +757,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongPacketMap = function (map, protocolId) {
this.writeLongPacketMap = function(map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
@@ -780,7 +784,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringIntMap = function (map) {
this.writeStringIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -805,7 +809,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringLongMap = function (map) {
this.writeStringLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -830,7 +834,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringStringMap = function (map) {
this.writeStringStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -855,7 +859,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringPacketMap = function (map, protocolId) {
this.writeStringPacketMap = function(map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
@@ -3,7 +3,7 @@
//
// @author jaysunxiao
// @version 3.0
const ComplexObject = function(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) {
const ComplexObject = function(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, myCompatible, myObject) {
// byte类型,最简单的整形
this.a = a; // byte
// byte的包装类型
@@ -59,6 +59,9 @@ const ComplexObject = function(a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, c
this.sss = sss; // java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
this.ssss = ssss; // java.util.Set<java.lang.String>
this.sssss = sssss; // java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
this.myCompatible = myCompatible; // int
this.myObject = myObject; // com.zfoo.protocol.packet.ObjectA
};
ComplexObject.prototype.protocolId = function() {
@@ -235,6 +238,8 @@ ComplexObject.write = function(buffer, packet) {
buffer.writeIntStringMap(element18);
});
}
buffer.writeInt(packet.myCompatible);
buffer.writePacket(packet.myObject, 102);
};
ComplexObject.read = function(buffer) {
@@ -459,6 +464,16 @@ ComplexObject.read = function(buffer) {
}
}
packet.sssss = result117;
if (!buffer.isReadable()) {
return packet;
}
const result121 = buffer.readInt();
packet.myCompatible = result121;
if (!buffer.isReadable()) {
return packet;
}
const result122 = buffer.readPacket(102);
packet.myObject = result122;
return packet;
};
@@ -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