perf[protocol]: 简化lua协议

This commit is contained in:
jaysunxiao
2021-10-24 11:53:32 +08:00
parent c46fe942e5
commit 1cd1779136
20 changed files with 91 additions and 123 deletions
@@ -34,7 +34,7 @@ public class CsObjectProtocolSerializer implements ICsSerializer {
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.Write({}, {});", objectStr, objectProtocolField.getProtocolId()))
builder.append(StringUtils.format("buffer.WritePacket({}, {});", objectStr, objectProtocolField.getProtocolId()))
.append(LS);
}
@@ -46,7 +46,7 @@ public class CsObjectProtocolSerializer implements ICsSerializer {
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("{} {} = buffer.Read<{}>({});", protocolSimpleName, result, protocolSimpleName, objectProtocolField.getProtocolId()))
builder.append(StringUtils.format("{} {} = buffer.ReadPacket<{}>({});", protocolSimpleName, result, protocolSimpleName, objectProtocolField.getProtocolId()))
.append(LS);
return result;
}
@@ -13,12 +13,10 @@
package com.zfoo.protocol.serializer.lua;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.generate.GenerateProtocolDocument;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolAnalysis;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.reflect.*;
@@ -128,7 +126,7 @@ public abstract class GenerateLuaUtils {
var luaBuilder = new StringBuilder();
// document
luaBuilder.append(documentTitleAndImport(registration));
luaBuilder.append(documentTitle(registration));
// new object
luaBuilder.append(newFunction(registration));
@@ -153,7 +151,7 @@ public abstract class GenerateLuaUtils {
FileUtils.writeStringToFile(new File(protocolOutputPath), luaBuilder.toString());
}
private static String documentTitleAndImport(ProtocolRegistration registration) {
private static String documentTitle(ProtocolRegistration registration) {
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var fieldRegistrations = registration.getFieldRegistrations();
@@ -167,13 +165,6 @@ public abstract class GenerateLuaUtils {
luaBuilder.append(LS);
}
// 如果协议包含子协议,则需要导入ProtocolManager
var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId);
if (CollectionUtils.isNotEmpty(subProtocols)) {
luaBuilder.append("local ProtocolManager = require(\"LuaProtocol.ProtocolManager\")").append(LS + LS);
}
return luaBuilder.toString();
}
@@ -245,14 +236,10 @@ public abstract class GenerateLuaUtils {
var luaBuilder = new StringBuilder();
luaBuilder.append(StringUtils.format("function {}:write(buffer, packet)", protocolClazzName)).append(LS);
luaBuilder.append(TAB).append("if packet == null then").append(LS);
luaBuilder.append(TAB + TAB).append("buffer:writeBoolean(false)").append(LS);
luaBuilder.append(TAB).append("if buffer:writePacketFlag(packet) then").append(LS);
luaBuilder.append(TAB + TAB).append("return").append(LS);
luaBuilder.append(TAB).append("end").append(LS);
luaBuilder.append(TAB).append("buffer:writeBoolean(true)").append(LS);
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
@@ -41,12 +41,12 @@ public class LuaArraySerializer implements ILuaSerializer {
ArrayField arrayField = (ArrayField) fieldRegistration;
builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer:writeInt(0)").append(LS);
builder.append("buffer:writeInt(0)").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("else").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("byteBuffer:writeInt(#{});", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeInt(#{});", objectStr)).append(LS);
String index = "index" + GenerateProtocolFile.index.getAndIncrement();
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
@@ -76,7 +76,7 @@ public class LuaArraySerializer implements ILuaSerializer {
var size = "size" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readInt()", size)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readInt()", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0 then", size)).append(LS);
@@ -31,7 +31,7 @@ public class LuaBooleanSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeBoolean({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeBoolean({})", objectStr)).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaBooleanSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readBoolean()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readBoolean()", result)).append(LS);
return result;
}
}
@@ -31,7 +31,7 @@ public class LuaByteSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeByte({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeByte({})", objectStr)).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaByteSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readByte()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readByte()", result)).append(LS);
return result;
}
}
@@ -30,14 +30,14 @@ public class LuaCharSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeChar({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeChar({})", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readChar()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readChar()", result)).append(LS);
return result;
}
}
@@ -31,7 +31,7 @@ public class LuaDoubleSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeDouble({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeDouble({})", objectStr)).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaDoubleSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readDouble()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readDouble()", result)).append(LS);
return result;
}
}
@@ -31,7 +31,7 @@ public class LuaFloatSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeFloat({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeFloat({})", objectStr)).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaFloatSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readFloat()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readFloat()", result)).append(LS);
return result;
}
}
@@ -30,7 +30,7 @@ public class LuaIntSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeInt({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeInt({})", objectStr)).append(LS);
}
@Override
@@ -38,7 +38,7 @@ public class LuaIntSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readInt()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readInt()", result)).append(LS);
return result;
}
}
@@ -40,12 +40,12 @@ public class LuaListSerializer implements ILuaSerializer {
ListField listField = (ListField) fieldRegistration;
builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer:writeInt(0)").append(LS);
builder.append("buffer:writeInt(0)").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("else").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("byteBuffer:writeInt(#{})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeInt(#{})", objectStr)).append(LS);
String index = "index" + GenerateProtocolFile.index.getAndIncrement();
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
@@ -73,7 +73,7 @@ public class LuaListSerializer implements ILuaSerializer {
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("local {} = byteBuffer:readInt()", size)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readInt()", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0 then", size)).append(LS);
@@ -30,7 +30,7 @@ public class LuaLongSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeLong({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeLong({})", objectStr)).append(LS);
}
@Override
@@ -38,7 +38,7 @@ public class LuaLongSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readLong()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readLong()", result)).append(LS);
return result;
}
}
@@ -40,13 +40,13 @@ public class LuaMapSerializer implements ILuaSerializer {
MapField mapField = (MapField) fieldRegistration;
builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer:writeInt(0)").append(LS);
builder.append("buffer:writeInt(0)").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("else").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("byteBuffer:writeInt(table.mapSize({}))", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeInt(table.mapSize({}))", objectStr)).append(LS);
String key = "key" + GenerateProtocolFile.index.getAndIncrement();
String value = "value" + GenerateProtocolFile.index.getAndIncrement();
@@ -77,7 +77,7 @@ public class LuaMapSerializer implements ILuaSerializer {
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("local {} = byteBuffer:readInt()", size)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readInt()", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0 then", size)).append(LS);
@@ -31,7 +31,7 @@ public class LuaObjectProtocolSerializer implements ILuaSerializer {
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("ProtocolManager.getProtocol({}):write(byteBuffer, {})", objectProtocolField.getProtocolId(), objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writePacket({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaObjectProtocolSerializer implements ILuaSerializer {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = ProtocolManager.getProtocol({}):read(byteBuffer)", result, objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("local {} = buffer:readPacket({})", result, objectProtocolField.getProtocolId())).append(LS);
return result;
}
}
@@ -40,13 +40,13 @@ public class LuaSetSerializer implements ILuaSerializer {
SetField setField = (SetField) fieldRegistration;
builder.append(StringUtils.format("if {} == null then", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer:writeInt(0)").append(LS);
builder.append("buffer:writeInt(0)").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("else").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("byteBuffer:writeInt(table.setSize({}))", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeInt(#{})", objectStr)).append(LS);
String index = "index" + GenerateProtocolFile.index.getAndIncrement();
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
@@ -74,7 +74,7 @@ public class LuaSetSerializer implements ILuaSerializer {
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("local {} = byteBuffer:readInt()", size)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readInt()", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if {} > 0 then", size)).append(LS);
@@ -85,7 +85,7 @@ public class LuaSetSerializer implements ILuaSerializer {
String readObject = GenerateLuaUtils.luaSerializer(setField.getSetElementRegistration().serializer())
.readObject(builder, deep + 2, field, setField.getSetElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}[{}] = {}", result, readObject, readObject)).append(LS);
builder.append(StringUtils.format("table.insert({}, {})", result, readObject)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("end").append(LS);
GenerateProtocolFile.addTab(builder, deep);
@@ -31,7 +31,7 @@ public class LuaShortSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeShort({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeShort({})", objectStr)).append(LS);
}
@Override
@@ -39,7 +39,7 @@ public class LuaShortSerializer implements ILuaSerializer {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readShort()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readShort()", result)).append(LS);
return result;
}
}
@@ -30,14 +30,14 @@ public class LuaStringSerializer implements ILuaSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("byteBuffer:writeString({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeString({})", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = byteBuffer:readString()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readString()", result)).append(LS);
return result;
}
}
@@ -575,7 +575,7 @@ namespace CsProtocol.Buffer
public T ReadPacket<T>(short protocolId)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
return protocolRegistration.Read(this);;
return (T) protocolRegistration.Read(this);
}
public void WriteBooleanArray(bool[] array)
@@ -763,7 +763,7 @@ namespace CsProtocol.Buffer
{
for (int index = 0; index < size; index++)
{
array[index] = ReadLong();
array[index] = ReadFloat();
}
}
@@ -795,7 +795,7 @@ namespace CsProtocol.Buffer
{
for (int index = 0; index < size; index++)
{
array[index] = ReadLong();
array[index] = ReadDouble();
}
}
@@ -1920,4 +1920,4 @@ namespace CsProtocol.Buffer
return map;
}
}
}
}
@@ -1,4 +1,4 @@
import {readInt64, writeInt64} from './longbits.js';
import { readInt64, writeInt64 } from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
@@ -344,17 +344,17 @@ const ByteBuffer = function() {
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 +365,7 @@ const ByteBuffer = function() {
}
};
this.readBooleanArray = function () {
this.readBooleanArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -376,7 +376,7 @@ const ByteBuffer = function() {
return array;
};
this.writeByteArray = function (array) {
this.writeByteArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -387,7 +387,7 @@ const ByteBuffer = function() {
}
};
this.readByteArray = function () {
this.readByteArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -398,7 +398,7 @@ const ByteBuffer = function() {
return array;
};
this.writeShortArray = function (array) {
this.writeShortArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -409,7 +409,7 @@ const ByteBuffer = function() {
}
};
this.readShortArray = function () {
this.readShortArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -420,7 +420,7 @@ const ByteBuffer = function() {
return array;
};
this.writeIntArray = function (array) {
this.writeIntArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -431,7 +431,7 @@ const ByteBuffer = function() {
}
};
this.readIntArray = function () {
this.readIntArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -442,7 +442,7 @@ const ByteBuffer = function() {
return array;
};
this.writeLongArray = function (array) {
this.writeLongArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -453,7 +453,7 @@ const ByteBuffer = function() {
}
};
this.readLongArray = function () {
this.readLongArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -464,7 +464,7 @@ const ByteBuffer = function() {
return array;
};
this.writeFloatArray = function (array) {
this.writeFloatArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -475,7 +475,7 @@ const ByteBuffer = function() {
}
};
this.readFloatArray = function () {
this.readFloatArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -486,7 +486,7 @@ const ByteBuffer = function() {
return array;
};
this.writeDoubleArray = function (array) {
this.writeDoubleArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -497,7 +497,7 @@ const ByteBuffer = function() {
}
};
this.readDoubleArray = function () {
this.readDoubleArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -508,7 +508,7 @@ const ByteBuffer = function() {
return array;
};
this.writeStringArray = function (array) {
this.writeStringArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -519,7 +519,7 @@ const ByteBuffer = function() {
}
};
this.readStringArray = function () {
this.readStringArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -530,7 +530,7 @@ const ByteBuffer = function() {
return array;
};
this.writeCharArray = function (array) {
this.writeCharArray = function(array) {
if (array === null) {
this.writeInt(0);
} else {
@@ -541,7 +541,7 @@ const ByteBuffer = function() {
}
};
this.readCharArray = function () {
this.readCharArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -552,7 +552,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 +576,7 @@ const ByteBuffer = function() {
return array;
};
this.writeIntIntMap = function (map) {
this.writeIntIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -601,7 +601,7 @@ const ByteBuffer = function() {
return map;
};
this.writeIntLongMap = function (map) {
this.writeIntLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -626,7 +626,7 @@ const ByteBuffer = function() {
return map;
};
this.writeIntStringMap = function (map) {
this.writeIntStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -651,7 +651,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 +678,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongIntMap = function (map) {
this.writeLongIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -703,7 +703,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongLongMap = function (map) {
this.writeLongLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -728,7 +728,7 @@ const ByteBuffer = function() {
return map;
};
this.writeLongStringMap = function (map) {
this.writeLongStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -753,7 +753,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 +780,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringIntMap = function (map) {
this.writeStringIntMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -805,7 +805,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringLongMap = function (map) {
this.writeStringLongMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -830,7 +830,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringStringMap = function (map) {
this.writeStringStringMap = function(map) {
if (map === null) {
this.writeInt(0);
} else {
@@ -855,7 +855,7 @@ const ByteBuffer = function() {
return map;
};
this.writeStringPacketMap = function (map, protocolId) {
this.writeStringPacketMap = function(map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
@@ -397,7 +397,7 @@ function ByteBuffer:writeString(str)
self:writeInt(#str)
self:writeBuffer(str)
return self
end
end
function ByteBuffer:readString()
local length = self:readInt()
@@ -712,7 +712,7 @@ function ByteBuffer:writeIntIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeInt(value)
@@ -738,7 +738,7 @@ function ByteBuffer:writeIntLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeLong(value)
@@ -764,7 +764,7 @@ function ByteBuffer:writeIntStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
self:writeString(value)
@@ -791,7 +791,7 @@ function ByteBuffer:writeIntPacketMap(map, protocolId)
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeInt(key)
protocolRegistration:write(self, value)
@@ -818,7 +818,7 @@ function ByteBuffer:writeLongIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeInt(value)
@@ -844,7 +844,7 @@ function ByteBuffer:writeLongLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeLong(value)
@@ -870,7 +870,7 @@ function ByteBuffer:writeLongStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
self:writeString(value)
@@ -897,7 +897,7 @@ function ByteBuffer:writeLongPacketMap(map, protocolId)
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeLong(key)
protocolRegistration:write(self, value)
@@ -924,7 +924,7 @@ function ByteBuffer:writeStringIntMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeInt(value)
@@ -950,7 +950,7 @@ function ByteBuffer:writeStringLongMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeLong(value)
@@ -976,7 +976,7 @@ function ByteBuffer:writeStringStringMap(map)
if map == null then
self:writeInt(0)
else
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
self:writeString(value)
@@ -1003,7 +1003,7 @@ function ByteBuffer:writeStringPacketMap(map, protocolId)
self:writeInt(0)
else
local protocolRegistration = ProtocolManager.getProtocol(protocolId)
self:writeInt(#map);
self:writeInt(table.mapSize(map));
for key, value in pairs(map) do
self:writeString(key)
protocolRegistration:write(self, value)
@@ -1026,4 +1026,4 @@ function ByteBuffer:readStringPacketMap(protocolId)
return map
end
return ByteBuffer
return ByteBuffer
@@ -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
@@ -43,11 +32,3 @@ function ProtocolManager.read(buffer)
return ProtocolManager.getProtocol(protocolId):read(buffer)
end
-- C#传进来的byte数组到lua里就会变成string
function readBytes(bytes)
local buffer = ByteBuffer:new()
buffer:writeBuffer(bytes)
local packet = ProtocolManager.read(buffer)
return packet
end