ref[protocol]: 重构protocol的JavaScript的解析

This commit is contained in:
jaysunxiao
2021-10-17 18:17:41 +08:00
parent be3596beec
commit a49cfbb902
28 changed files with 2606 additions and 1420 deletions
@@ -726,6 +726,42 @@ public abstract class ByteBufUtils {
return array;
}
public static void writeBooleanCollection(ByteBuf byteBuf, Collection<Boolean> collection) {
if (collection == null) {
byteBuf.writeByte(0);
return;
}
writeInt(byteBuf, collection.size());
for (var value : collection) {
writeBooleanBox(byteBuf, value);
}
}
public static void writeBooleanList(ByteBuf byteBuf, List<Boolean> list) {
writeBooleanCollection(byteBuf, list);
}
public static List<Boolean> readBooleanList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Boolean>) CollectionUtils.newFixedList(length);
for (var i = 0; i < length; i++) {
list.add(readBooleanBox(byteBuf));
}
return list;
}
public static void writeBooleanSet(ByteBuf byteBuf, Set<Boolean> set) {
writeBooleanCollection(byteBuf, set);
}
public static Set<Boolean> readBooleanSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Boolean>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
set.add(readBooleanBox(byteBuf));
}
return set;
}
//---------------------------------byte--------------------------------------
public static void writeByteArray(ByteBuf byteBuf, byte[] array) {
@@ -768,6 +804,42 @@ public abstract class ByteBufUtils {
return bytesBox;
}
public static void writeByteCollection(ByteBuf byteBuf, Collection<Byte> collection) {
if (collection == null) {
byteBuf.writeByte(0);
return;
}
writeInt(byteBuf, collection.size());
for (var value : collection) {
writeByteBox(byteBuf, value);
}
}
public static void writeByteList(ByteBuf byteBuf, List<Byte> list) {
writeByteCollection(byteBuf, list);
}
public static List<Byte> readByteList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Byte>) CollectionUtils.newFixedList(length);
for (var i = 0; i < length; i++) {
list.add(readByteBox(byteBuf));
}
return list;
}
public static void writeByteSet(ByteBuf byteBuf, Set<Byte> set) {
writeByteCollection(byteBuf, set);
}
public static Set<Byte> readByteSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Byte>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
set.add(readByteBox(byteBuf));
}
return set;
}
//---------------------------------short--------------------------------------
public static void writeShortArray(ByteBuf byteBuf, short[] array) {
@@ -818,6 +890,43 @@ public abstract class ByteBufUtils {
return shorts;
}
public static void writeShortCollection(ByteBuf byteBuf, Collection<Short> collection) {
if (collection == null) {
byteBuf.writeByte(0);
return;
}
writeInt(byteBuf, collection.size());
for (var value : collection) {
writeShortBox(byteBuf, value);
}
}
public static void writeShortList(ByteBuf byteBuf, List<Short> list) {
writeShortCollection(byteBuf, list);
}
public static List<Short> readShortList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Short>) CollectionUtils.newFixedList(length);
for (var i = 0; i < length; i++) {
list.add(readShortBox(byteBuf));
}
return list;
}
public static void writeShortSet(ByteBuf byteBuf, Set<Short> set) {
writeShortCollection(byteBuf, set);
}
public static Set<Short> readShortSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Short>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
set.add(readShortBox(byteBuf));
}
return set;
}
//---------------------------------int--------------------------------------
public static void writeIntArray(ByteBuf byteBuf, int[] array) {
@@ -890,11 +999,11 @@ public abstract class ByteBufUtils {
public static Set<Integer> readIntSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (Set<Integer>) CollectionUtils.newFixedSet(length);
var set = (Set<Integer>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
list.add(readIntBox(byteBuf));
set.add(readIntBox(byteBuf));
}
return list;
return set;
}
@@ -1025,6 +1134,42 @@ public abstract class ByteBufUtils {
return floats;
}
public static void writeFloatCollection(ByteBuf byteBuf, Collection<Float> collection) {
if (collection == null) {
byteBuf.writeByte(0);
return;
}
writeInt(byteBuf, collection.size());
for (var value : collection) {
writeFloatBox(byteBuf, value);
}
}
public static void writeFloatList(ByteBuf byteBuf, List<Float> list) {
writeFloatCollection(byteBuf, list);
}
public static List<Float> readFloatList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Float>) CollectionUtils.newFixedList(length);
for (var i = 0; i < length; i++) {
list.add(readFloatBox(byteBuf));
}
return list;
}
public static void writeFloatSet(ByteBuf byteBuf, Set<Float> set) {
writeFloatCollection(byteBuf, set);
}
public static Set<Float> readFloatSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Float>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
set.add(readFloatBox(byteBuf));
}
return set;
}
//---------------------------------double--------------------------------------
public static void writeDoubleArray(ByteBuf byteBuf, double[] array) {
@@ -1075,6 +1220,42 @@ public abstract class ByteBufUtils {
return doubles;
}
public static void writeDoubleCollection(ByteBuf byteBuf, Collection<Double> collection) {
if (collection == null) {
byteBuf.writeByte(0);
return;
}
writeInt(byteBuf, collection.size());
for (var value : collection) {
writeDoubleBox(byteBuf, value);
}
}
public static void writeDoubleList(ByteBuf byteBuf, List<Double> list) {
writeDoubleCollection(byteBuf, list);
}
public static List<Double> readDoubleList(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (List<Double>) CollectionUtils.newFixedList(length);
for (var i = 0; i < length; i++) {
list.add(readDoubleBox(byteBuf));
}
return list;
}
public static void writeDoubleSet(ByteBuf byteBuf, Set<Double> set) {
writeDoubleCollection(byteBuf, set);
}
public static Set<Double> readDoubleSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var set = (Set<Double>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
set.add(readDoubleBox(byteBuf));
}
return set;
}
//---------------------------------string--------------------------------------
public static void writeStringArray(ByteBuf byteBuf, String[] array) {
@@ -1127,11 +1308,11 @@ public abstract class ByteBufUtils {
public static Set<String> readStringSet(ByteBuf byteBuf) {
var length = readInt(byteBuf);
var list = (Set<String>) CollectionUtils.newFixedSet(length);
var set = (Set<String>) CollectionUtils.newFixedSet(length);
for (var i = 0; i < length; i++) {
list.add(readString(byteBuf));
set.add(readString(byteBuf));
}
return list;
return set;
}
@@ -21,6 +21,8 @@ import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -38,6 +40,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
public boolean writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) {
var arrayField = (ArrayField) fieldRegistration;
var arrayName = getArrayClassName(arrayField);
var flag = true;
// 直接在字节码里调用方法是为了减小生成字节码的体积,下面的代码去掉也不会有任何影响
switch (arrayName) {
@@ -45,119 +48,204 @@ public class CutDownArraySerializer implements ICutDownSerializer {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeBooleanArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeBooleanArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Boolean":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeBooleanBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeBooleanArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "byte":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeByteArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeByteArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Byte":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeByteBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeByteArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "short":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeShortArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeShortArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Short":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeShortBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeShortArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "int":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Integer":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "long":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Long":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "float":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeFloatArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeFloatArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Float":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeFloatBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeFloatArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "double":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeDoubleArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeDoubleArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Double":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeDoubleBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeDoubleArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "String":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "char":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeCharArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeCharArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "Character":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeCharBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeCharArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
default:
@@ -166,12 +254,19 @@ public class CutDownArraySerializer implements ICutDownSerializer {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
return false;
return flag;
}
@Override
@@ -181,132 +276,234 @@ public class CutDownArraySerializer implements ICutDownSerializer {
var array = "array" + GenerateProtocolFile.index.getAndIncrement();
var flag = true;
switch (arrayName) {
case "boolean":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readBooleanArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readBooleanArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Boolean":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readBooleanBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readBooleanArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "byte":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readByteArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readByteArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Byte":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readByteBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readByteArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "short":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readShortArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readShortArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Short":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readShortBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readShortArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "int":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readIntArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Integer":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readIntBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "long":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readLongArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Long":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readLongBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "float":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readFloatArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readFloatArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Float":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readFloatBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readFloatArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "double":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readDoubleArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readDoubleArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Double":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readDoubleBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readDoubleArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "String":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readStringArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "char":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readCharArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readCharArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
case "Character":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}[] {} = {}.readCharBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils));
return array;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readCharArray();", array)).append(LS);
break;
default:
flag = false;
}
break;
default:
// Java不支持泛型的数组初始化,这边就不做任何操作
if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) arrayField.getArrayElementRegistration();
switch (language) {
// Java不支持泛型的数组初始化,这边就不做任何操作
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readPacketArray({});", array, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
GenerateProtocolFile.index.getAndDecrement();
return null;
if (flag) {
return array;
} else {
GenerateProtocolFile.index.getAndDecrement();
return null;
}
}
public String getArrayClassName(ArrayField arrayField) {
@@ -21,6 +21,8 @@ import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -36,20 +38,93 @@ public class CutDownListSerializer implements ICutDownSerializer {
@Override
public boolean writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) {
var listField = (ListField) fieldRegistration;
var flag = true;
switch (listField.getType().getTypeName()) {
case "java.util.List<java.lang.Boolean>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeBooleanList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeBooleanArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Byte>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeByteList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeByteArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Short>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeShortList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeShortArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Integer>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Long>": {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
}
case "java.util.List<java.lang.Float>": {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeFloatList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeFloatArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
}
case "java.util.List<java.lang.Double>": {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeDoubleList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeDoubleArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
}
@@ -57,64 +132,163 @@ public class CutDownListSerializer implements ICutDownSerializer {
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
default:
// List<IPacket>
if (listField.getListElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
// List<IPacket>
if (listField.getListElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
return true;
}
}
return false;
return flag;
}
@Override
public String readObject(StringBuilder builder, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) {
var listField = (ListField) fieldRegistration;
var list = "list" + GenerateProtocolFile.index.getAndIncrement();
var flag = true;
switch (listField.getType().getTypeName()) {
case "java.util.List<java.lang.Boolean>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readBooleanList($1);", list, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readBooleanArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Byte>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readByteList($1);", list, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readByteArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Short>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readShortList($1);", list, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readShortArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Integer>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readIntList($1);", list, EnhanceUtils.byteBufUtils));
return list;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Long>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readLongList($1);", list, EnhanceUtils.byteBufUtils));
return list;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Float>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readFloatList($1);", list, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readFloatArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.Double>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readDoubleList($1);", list, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readDoubleArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.List<java.lang.String>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readStringList($1);", list, EnhanceUtils.byteBufUtils));
return list;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringArray();", list)).append(LS);
break;
default:
flag = false;
}
break;
default:
if (listField.getListElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readPacketList($1, {});", list, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readPacketArray({});", list, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
if (listField.getListElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("List {} = {}.readPacketList($1, {});", list, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
return list;
}
}
GenerateProtocolFile.index.getAndDecrement();
return null;
if (flag) {
return list;
} else {
GenerateProtocolFile.index.getAndDecrement();
return null;
}
}
}
@@ -26,6 +26,8 @@ import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -50,50 +52,120 @@ public class CutDownMapSerializer implements ICutDownSerializer {
if (keyRegistration instanceof BaseField) {
if (keySerializer == IntSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeIntIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeIntLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntLongMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeIntStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
} else if (keySerializer == LongSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeLongIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeLongLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeLongStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
} else if (keySerializer == StringSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeStringIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeStringLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringLongMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeStringStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return true;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
}
}
return false;
}
@@ -110,49 +182,120 @@ public class CutDownMapSerializer implements ICutDownSerializer {
if (keyRegistration instanceof BaseField) {
if (keySerializer == IntSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readIntIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readIntIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readIntLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readIntLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readIntStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readIntStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readIntPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readIntPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
}
}
} else if (keySerializer == LongSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readLongIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readLongIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readLongLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readLongLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readLongStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readLongStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
}
}
} else if (keySerializer == StringSerializer.INSTANCE) {
if (valueSerializer == IntSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readStringIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readStringIntMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readStringLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readStringLongMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readStringStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readStringStringMap($1);", map, EnhanceUtils.byteBufUtils));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
return map;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
}
}
}
}
GenerateProtocolFile.index.getAndDecrement();
return null;
}
@@ -21,6 +21,8 @@ import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -35,89 +37,256 @@ public class CutDownSetSerializer implements ICutDownSerializer {
@Override
public boolean writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) {
var setField = (SetField) fieldRegistration;
var flag = true;
// 直接在字节码里调用方法是为了减小生成字节码的体积,下面的代码去掉也不会有任何影响
switch (setField.getType().getTypeName()) {
case "java.util.Set<java.lang.Boolean>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeBooleanSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeBooleanArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Byte>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeByteSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeByteArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Short>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeShortSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeShortArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Integer>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeIntSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeIntArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Long>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeLongSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeLongArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Float>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeFloatSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeFloatArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Double>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeDoubleSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeDoubleArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.String>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writeStringSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr));
return true;
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writeStringArray({});", objectStr)).append(LS);
break;
default:
flag = false;
}
break;
default:
// Set<IPacket>
if (setField.getSetElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writePacketSet($1, (Set){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
break;
case JavaScript:
builder.append(StringUtils.format("byteBuffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
// Set<IPacket>
if (setField.getSetElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("{}.writePacketSet($1, (Set){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
return true;
}
}
return false;
return flag;
}
@Override
public String readObject(StringBuilder builder, Field field, IFieldRegistration fieldRegistration, CodeLanguage language) {
var setField = (SetField) fieldRegistration;
var set = "set" + GenerateProtocolFile.index.getAndIncrement();
var flag = true;
switch (setField.getType().getTypeName()) {
case "java.util.Set<java.lang.Boolean>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readBooleanSet($1);", set, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readBooleanArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Byte>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readByteSet($1);", set, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readByteArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Short>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readShortSet($1);", set, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readShortArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Integer>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readIntSet($1);", set, EnhanceUtils.byteBufUtils));
return set;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readIntArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Long>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readLongSet($1);", set, EnhanceUtils.byteBufUtils));
return set;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readLongArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Float>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readFloatSet($1);", set, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readFloatArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.Double>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readDoubleSet($1);", set, EnhanceUtils.byteBufUtils));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readDoubleArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
case "java.util.Set<java.lang.String>":
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readStringSet($1);", set, EnhanceUtils.byteBufUtils));
return set;
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readStringArray();", set)).append(LS);
break;
default:
flag = false;
}
break;
default:
if (setField.getSetElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readPacketSet($1, {});", set, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
break;
case JavaScript:
builder.append(StringUtils.format("const {} = byteBuffer.readPacketArray({});", set, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
} else {
flag = false;
}
}
if (setField.getSetElementRegistration() instanceof ObjectProtocolField) {
var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration();
switch (language) {
case Enhance:
builder.append(StringUtils.format("Set {} = {}.readPacketSet($1, {});", set, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId())));
return set;
}
if (flag) {
return set;
} else {
GenerateProtocolFile.index.getAndDecrement();
return null;
}
GenerateProtocolFile.index.getAndDecrement();
return null;
}
}
@@ -128,19 +128,6 @@ public abstract class GenerateJsUtils {
var jsBuilder = new StringBuilder();
// 如果协议包含子协议,则需要导入ProtocolManager
var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId);
if (CollectionUtils.isNotEmpty(subProtocols)) {
var path = GenerateProtocolPath.getProtocolPath(protocolId);
if (StringUtils.isBlank(path)) {
jsBuilder.append("import ProtocolManager from './ProtocolManager.js';").append(LS);
} else {
jsBuilder.append("import ProtocolManager from '");
Arrays.stream(path.split(StringUtils.SLASH)).forEach(it -> jsBuilder.append("../"));
jsBuilder.append("ProtocolManager.js';").append(LS);
}
}
// export object
jsBuilder.append(exportFunction(registration));
@@ -223,15 +210,12 @@ public abstract class GenerateJsUtils {
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
jsBuilder.append(StringUtils.format("{}.writeObject = function(byteBuffer, packet) {", protocolClazzName)).append(LS);
jsBuilder.append(StringUtils.format("{}.write = function(byteBuffer, packet) {", protocolClazzName)).append(LS);
jsBuilder.append(TAB).append("if (packet === null) {").append(LS);
jsBuilder.append(TAB + TAB).append("byteBuffer.writeBoolean(false);").append(LS);
jsBuilder.append(TAB).append("if (byteBuffer.writePacketFlag(packet)) {").append(LS);
jsBuilder.append(TAB + TAB).append("return;").append(LS);
jsBuilder.append(TAB).append("}").append(LS);
jsBuilder.append(TAB).append("byteBuffer.writeBoolean(true);").append(LS);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
IFieldRegistration fieldRegistration = fieldRegistrations[i];
@@ -250,7 +234,7 @@ public abstract class GenerateJsUtils {
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
jsBuilder.append(StringUtils.format("{}.readObject = function(byteBuffer) {", protocolClazzName)).append(LS);
jsBuilder.append(StringUtils.format("{}.read = function(byteBuffer) {", protocolClazzName)).append(LS);
jsBuilder.append(TAB).append("if (!byteBuffer.readBoolean()) {").append(LS);
jsBuilder.append(TAB + TAB).append("return null;").append(LS);
jsBuilder.append(TAB).append("}").append(LS);
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.js;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownArraySerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -31,9 +33,13 @@ public class JsArraySerializer implements IJsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.JavaScript)) {
return;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer.writeInt(0);").append(LS);
@@ -56,10 +62,15 @@ public class JsArraySerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.JavaScript);
if (cutDown != null) {
return cutDown;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.js;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownListSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -30,9 +32,13 @@ public class JsListSerializer implements IJsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.JavaScript)) {
return;
}
ListField listField = (ListField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer.writeInt(0);").append(LS);
@@ -55,10 +61,15 @@ public class JsListSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.JavaScript);
if (cutDown != null) {
return cutDown;
}
ListField listField = (ListField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.js;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownMapSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -30,9 +32,12 @@ public class JsMapSerializer implements IJsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
MapField mapField = (MapField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.JavaScript)) {
return;
}
MapField mapField = (MapField) fieldRegistration;
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer.writeInt(0);").append(LS);
@@ -60,10 +65,15 @@ public class JsMapSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.JavaScript);
if (cutDown != null) {
return cutDown;
}
MapField mapField = (MapField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = new Map();", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
@@ -32,7 +32,7 @@ public class JsObjectProtocolSerializer implements IJsSerializer {
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({}).writeObject(byteBuffer, {});", objectProtocolField.getProtocolId(), objectStr)).append(LS);
builder.append(StringUtils.format("byteBuffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
}
@Override
@@ -40,7 +40,7 @@ public class JsObjectProtocolSerializer implements IJsSerializer {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = ProtocolManager.getProtocol({}).readObject(byteBuffer);", result, objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("const {} = byteBuffer.readPacket({});", result, objectProtocolField.getProtocolId())).append(LS);
return result;
}
}
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.js;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownSetSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -30,9 +32,13 @@ public class JsSetSerializer implements IJsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.JavaScript)) {
return;
}
SetField setField = (SetField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("byteBuffer.writeInt(0);").append(LS);
@@ -56,10 +62,15 @@ public class JsSetSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.JavaScript);
if (cutDown != null) {
return cutDown;
}
SetField setField = (SetField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = new Set();", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
@@ -14,13 +14,13 @@ ProtocolManager.write = function write(byteBuffer, packet) {
const protocolId = packet.protocolId();
byteBuffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.writeObject(byteBuffer, packet);
protocol.write(byteBuffer, packet);
};
ProtocolManager.read = function read(byteBuffer) {
const protocolId = byteBuffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.readObject(byteBuffer);
const packet = protocol.read(byteBuffer);
return packet;
};
@@ -1,5 +1,7 @@
import {readInt64, writeInt64} from './longbits.js';
import { readInt64, writeInt64 } from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
const initSize = 128;
const maxSize = 655537;
@@ -41,22 +43,22 @@ function decodeZigzagInt(n) {
}
const ByteBuffer = function () {
const ByteBuffer = function() {
this.writeOffset = 0;
this.readOffset = 0;
this.buffer = new ArrayBuffer(initSize);
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
this.setWriteOffset = function (writeOffset) {
this.setWriteOffset = function(writeOffset) {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
}
this.writeOffset = writeOffset;
};
this.setReadOffset = function (readOffset) {
this.setReadOffset = function(readOffset) {
if (readOffset > this.writeOffset) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
@@ -65,11 +67,11 @@ const ByteBuffer = function () {
this.readOffset = readOffset;
};
this.getCapacity = function () {
this.getCapacity = function() {
return this.buffer.byteLength - this.writeOffset;
};
this.ensureCapacity = function (minCapacity) {
this.ensureCapacity = function(minCapacity) {
while (minCapacity - this.getCapacity() > 0) {
const newSize = this.buffer.byteLength * 2;
if (newSize > maxSize) {
@@ -80,7 +82,7 @@ const ByteBuffer = function () {
}
};
this.writeBoolean = function (value) {
this.writeBoolean = function(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
@@ -93,32 +95,32 @@ const ByteBuffer = function () {
this.writeOffset++;
};
this.readBoolean = function () {
this.readBoolean = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
};
this.writeBytes = function (byteArray) {
this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};
this.writeByte = function (value) {
this.writeByte = function(value) {
this.ensureCapacity(1);
this.bufferView.setInt8(this.writeOffset, value);
this.writeOffset++;
};
this.readByte = function () {
this.readByte = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return value;
};
this.writeShort = function (value) {
this.writeShort = function(value) {
if (!(minShort <= value && value <= maxShort)) {
throw new Error('value must range between minShort:-32768 and maxShort:32767');
}
@@ -127,13 +129,13 @@ const ByteBuffer = function () {
this.writeOffset += 2;
};
this.readShort = function () {
this.readShort = function() {
const value = this.bufferView.getInt16(this.readOffset);
this.readOffset += 2;
return value;
};
this.writeRawInt = function (value) {
this.writeRawInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
@@ -142,13 +144,13 @@ const ByteBuffer = function () {
this.writeOffset += 4;
};
this.readRawInt = function () {
this.readRawInt = function() {
const value = this.bufferView.getInt32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeInt = function (value) {
this.writeInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
@@ -189,7 +191,7 @@ const ByteBuffer = function () {
this.writeByte(value >>> 28);
};
this.readInt = function () {
this.readInt = function() {
let b = this.readByte();
let value = b & 0x7F;
if ((b & 0x80) !== 0) {
@@ -212,7 +214,7 @@ const ByteBuffer = function () {
return decodeZigzagInt(value);
};
this.writeLong = function (value) {
this.writeLong = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -221,7 +223,7 @@ const ByteBuffer = function () {
writeInt64(this, value);
};
this.readLong = function () {
this.readLong = function() {
const buffer = new ArrayBuffer(9);
const bufferView = new DataView(buffer, 0, buffer.byteLength);
@@ -263,7 +265,7 @@ const ByteBuffer = function () {
return readInt64(new Uint8Array(buffer.slice(0, count))).toString();
};
this.writeFloat = function (value) {
this.writeFloat = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -272,13 +274,13 @@ const ByteBuffer = function () {
this.writeOffset += 4;
};
this.readFloat = function () {
this.readFloat = function() {
const value = this.bufferView.getFloat32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeDouble = function (value) {
this.writeDouble = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -287,25 +289,25 @@ const ByteBuffer = function () {
this.writeOffset += 8;
};
this.readDouble = function () {
this.readDouble = function() {
const value = this.bufferView.getFloat64(this.readOffset);
this.readOffset += 8;
return value;
};
this.writeChar = function (value) {
this.writeChar = function(value) {
if (value === null || value === undefined || value.length === 0) {
this.writeInt(0);
this.writeString(empty_str);
return;
}
this.writeString(value.charAt(0));
};
this.readChar = function () {
this.readChar = function() {
return this.readString();
};
this.writeString = function (value) {
this.writeString = function(value) {
if (value === null || value === undefined || value.trim().length === 0) {
this.writeInt(0);
return;
@@ -319,10 +321,10 @@ const ByteBuffer = function () {
uint8Array.forEach((value) => this.writeByte(value));
};
this.readString = function () {
this.readString = function() {
const length = this.readInt();
if (length <= 0) {
return '';
return empty_str;
}
const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length));
const value = decoder.decode(uint8Array);
@@ -330,11 +332,555 @@ const ByteBuffer = function () {
return value;
};
this.toBytes = function () {
this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};
this.writePacketFlag = function(value) {
const flag = value === null;
this.writeBoolean(!flag);
return flag;
};
this.writePacket = function(value, protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, value);
};
this.readPacket = function(protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
this.writeBooleanArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeBoolean(element);
});
}
};
this.readBooleanArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
}
}
return array;
};
this.writeByteArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeByte(element);
});
}
};
this.readByteArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readByte());
}
}
return array;
};
this.writeShortArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeShort(element);
});
}
};
this.readShortArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readShort());
}
}
return array;
};
this.writeIntArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(element);
});
}
};
this.readIntArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readInt());
}
}
return array;
};
this.writeLongArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeLong(element);
});
}
};
this.readLongArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readLong());
}
}
return array;
};
this.writeFloatArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeFloat(element);
});
}
};
this.readFloatArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readFloat());
}
}
return array;
};
this.writeDoubleArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeDouble(element);
});
}
};
this.readDoubleArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readDouble());
}
}
return array;
};
this.writeStringArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeString(element);
});
}
};
this.readStringArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readString());
}
}
return array;
};
this.writeCharArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeChar(element);
});
}
};
this.readCharArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readChar());
}
}
return array;
};
this.writePacketArray = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.length);
value.forEach(element => {
protocolRegistration.write(this, element);
});
}
};
this.readPacketArray = function(protocolId) {
const array = [];
const length = this.readInt();
if (length > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < length; index++) {
array.push(protocolRegistration.read(this));
}
}
return array;
};
this.writeIntIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeInt(value);
});
}
};
this.readIntIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeIntLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeLong(value);
});
}
};
this.readIntLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeIntStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeString(value);
});
}
};
this.readIntStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeIntPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
protocolRegistration.write(this, value);
});
}
};
this.readIntPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeLongIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeInt(value);
});
}
};
this.readLongIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeLongLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeLong(value);
});
}
};
this.readLongLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeLongStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeString(value);
});
}
};
this.readLongStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeLongPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
protocolRegistration.write(this, value);
});
}
};
this.readLongPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeStringIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeInt(value);
});
}
};
this.readStringIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeStringLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeLong(value);
});
}
};
this.readStringLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeStringStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeString(value);
});
}
};
this.readStringStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeStringPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
protocolRegistration.write(this, value);
});
}
};
this.readStringPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
};
export default ByteBuffer;
@@ -22,6 +22,7 @@ import com.google.protobuf.CodedOutputStream;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.packet.*;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
@@ -323,10 +324,8 @@ public class SpeedTest {
static {
var op = GenerateOperation.NO_OPERATION;
// op.setGenerateLuaProtocol(true);
// op.setGenerateCsharpProtocol(true);
// op.setGenerateJsProtocol(true);
// op.setFoldProtocol(true);
op.getGenerateLanguages().add(CodeLanguage.JavaScript);
op.setFoldProtocol(true);
// zfoo协议注册
ProtocolManager.initProtocol(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, ObjectA.class, ObjectB.class), op);
@@ -26,7 +26,7 @@ import java.util.*;
*/
public class ComplexObject implements IPacket {
public static final transient short PROTOCOL_ID = 1160;
public static final transient short PROTOCOL_ID = 100;
// byte类型,最简单的整形
private byte a;
@@ -25,7 +25,7 @@ import java.util.Set;
*/
public class NormalObject implements IPacket {
public static final transient short PROTOCOL_ID = 1161;
public static final transient short PROTOCOL_ID = 101;
private byte a;
private byte[] aaa;
@@ -24,7 +24,7 @@ import java.util.Objects;
*/
public class ObjectA implements IPacket {
public static final transient short PROTOCOL_ID = 1116;
public static final transient short PROTOCOL_ID = 102;
private int a;
@@ -23,7 +23,7 @@ import java.util.Objects;
*/
public class ObjectB implements IPacket {
public static final transient short PROTOCOL_ID = 1117;
public static final transient short PROTOCOL_ID = 103;
private boolean flag;
@@ -21,7 +21,7 @@ import com.zfoo.protocol.IPacket;
*/
public class SimpleObject implements IPacket {
public static final transient short PROTOCOL_ID = 1163;
public static final transient short PROTOCOL_ID = 104;
private int c;
Binary file not shown.
@@ -1,7 +1,7 @@
import ObjectA from './packet/ObjectA.js';
import ObjectB from './packet/ObjectB.js';
import ComplexObject from './packet/ComplexObject.js';
import NormalObject from './packet/NormalObject.js';
import ObjectA from './packet/ObjectA.js';
import ObjectB from './packet/ObjectB.js';
import SimpleObject from './packet/SimpleObject.js';
@@ -21,22 +21,22 @@ ProtocolManager.write = function write(byteBuffer, packet) {
const protocolId = packet.protocolId();
byteBuffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.writeObject(byteBuffer, packet);
protocol.write(byteBuffer, packet);
};
ProtocolManager.read = function read(byteBuffer) {
const protocolId = byteBuffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.readObject(byteBuffer);
const packet = protocol.read(byteBuffer);
return packet;
};
ProtocolManager.initProtocol = function initProtocol() {
protocols.set(1116, ObjectA);
protocols.set(1117, ObjectB);
protocols.set(1160, ComplexObject);
protocols.set(1161, NormalObject);
protocols.set(1163, SimpleObject);
protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
};
export default ProtocolManager;
@@ -1,5 +1,7 @@
import {readInt64, writeInt64} from './longbits.js';
import { readInt64, writeInt64 } from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
const initSize = 128;
const maxSize = 655537;
@@ -41,22 +43,22 @@ function decodeZigzagInt(n) {
}
const ByteBuffer = function () {
const ByteBuffer = function() {
this.writeOffset = 0;
this.readOffset = 0;
this.buffer = new ArrayBuffer(initSize);
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
this.setWriteOffset = function (writeOffset) {
this.setWriteOffset = function(writeOffset) {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
}
this.writeOffset = writeOffset;
};
this.setReadOffset = function (readOffset) {
this.setReadOffset = function(readOffset) {
if (readOffset > this.writeOffset) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
@@ -65,11 +67,11 @@ const ByteBuffer = function () {
this.readOffset = readOffset;
};
this.getCapacity = function () {
this.getCapacity = function() {
return this.buffer.byteLength - this.writeOffset;
};
this.ensureCapacity = function (minCapacity) {
this.ensureCapacity = function(minCapacity) {
while (minCapacity - this.getCapacity() > 0) {
const newSize = this.buffer.byteLength * 2;
if (newSize > maxSize) {
@@ -80,7 +82,7 @@ const ByteBuffer = function () {
}
};
this.writeBoolean = function (value) {
this.writeBoolean = function(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
@@ -93,32 +95,32 @@ const ByteBuffer = function () {
this.writeOffset++;
};
this.readBoolean = function () {
this.readBoolean = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
};
this.writeBytes = function (byteArray) {
this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};
this.writeByte = function (value) {
this.writeByte = function(value) {
this.ensureCapacity(1);
this.bufferView.setInt8(this.writeOffset, value);
this.writeOffset++;
};
this.readByte = function () {
this.readByte = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return value;
};
this.writeShort = function (value) {
this.writeShort = function(value) {
if (!(minShort <= value && value <= maxShort)) {
throw new Error('value must range between minShort:-32768 and maxShort:32767');
}
@@ -127,13 +129,13 @@ const ByteBuffer = function () {
this.writeOffset += 2;
};
this.readShort = function () {
this.readShort = function() {
const value = this.bufferView.getInt16(this.readOffset);
this.readOffset += 2;
return value;
};
this.writeRawInt = function (value) {
this.writeRawInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
@@ -142,13 +144,13 @@ const ByteBuffer = function () {
this.writeOffset += 4;
};
this.readRawInt = function () {
this.readRawInt = function() {
const value = this.bufferView.getInt32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeInt = function (value) {
this.writeInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
@@ -189,7 +191,7 @@ const ByteBuffer = function () {
this.writeByte(value >>> 28);
};
this.readInt = function () {
this.readInt = function() {
let b = this.readByte();
let value = b & 0x7F;
if ((b & 0x80) !== 0) {
@@ -212,7 +214,7 @@ const ByteBuffer = function () {
return decodeZigzagInt(value);
};
this.writeLong = function (value) {
this.writeLong = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -221,7 +223,7 @@ const ByteBuffer = function () {
writeInt64(this, value);
};
this.readLong = function () {
this.readLong = function() {
const buffer = new ArrayBuffer(9);
const bufferView = new DataView(buffer, 0, buffer.byteLength);
@@ -263,7 +265,7 @@ const ByteBuffer = function () {
return readInt64(new Uint8Array(buffer.slice(0, count))).toString();
};
this.writeFloat = function (value) {
this.writeFloat = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -272,13 +274,13 @@ const ByteBuffer = function () {
this.writeOffset += 4;
};
this.readFloat = function () {
this.readFloat = function() {
const value = this.bufferView.getFloat32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeDouble = function (value) {
this.writeDouble = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
@@ -287,25 +289,25 @@ const ByteBuffer = function () {
this.writeOffset += 8;
};
this.readDouble = function () {
this.readDouble = function() {
const value = this.bufferView.getFloat64(this.readOffset);
this.readOffset += 8;
return value;
};
this.writeChar = function (value) {
this.writeChar = function(value) {
if (value === null || value === undefined || value.length === 0) {
this.writeInt(0);
this.writeString(empty_str);
return;
}
this.writeString(value.charAt(0));
};
this.readChar = function () {
this.readChar = function() {
return this.readString();
};
this.writeString = function (value) {
this.writeString = function(value) {
if (value === null || value === undefined || value.trim().length === 0) {
this.writeInt(0);
return;
@@ -319,10 +321,10 @@ const ByteBuffer = function () {
uint8Array.forEach((value) => this.writeByte(value));
};
this.readString = function () {
this.readString = function() {
const length = this.readInt();
if (length <= 0) {
return '';
return empty_str;
}
const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length));
const value = decoder.decode(uint8Array);
@@ -330,11 +332,555 @@ const ByteBuffer = function () {
return value;
};
this.toBytes = function () {
this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};
this.writePacketFlag = function(value) {
const flag = value === null;
this.writeBoolean(!flag);
return flag;
};
this.writePacket = function(value, protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, value);
};
this.readPacket = function(protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
this.writeBooleanArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeBoolean(element);
});
}
};
this.readBooleanArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
}
}
return array;
};
this.writeByteArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeByte(element);
});
}
};
this.readByteArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readByte());
}
}
return array;
};
this.writeShortArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeShort(element);
});
}
};
this.readShortArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readShort());
}
}
return array;
};
this.writeIntArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(element);
});
}
};
this.readIntArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readInt());
}
}
return array;
};
this.writeLongArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeLong(element);
});
}
};
this.readLongArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readLong());
}
}
return array;
};
this.writeFloatArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeFloat(element);
});
}
};
this.readFloatArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readFloat());
}
}
return array;
};
this.writeDoubleArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeDouble(element);
});
}
};
this.readDoubleArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readDouble());
}
}
return array;
};
this.writeStringArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeString(element);
});
}
};
this.readStringArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readString());
}
}
return array;
};
this.writeCharArray = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeChar(element);
});
}
};
this.readCharArray = function() {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readChar());
}
}
return array;
};
this.writePacketArray = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.length);
value.forEach(element => {
protocolRegistration.write(this, element);
});
}
};
this.readPacketArray = function(protocolId) {
const array = [];
const length = this.readInt();
if (length > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < length; index++) {
array.push(protocolRegistration.read(this));
}
}
return array;
};
this.writeIntIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeInt(value);
});
}
};
this.readIntIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeIntLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeLong(value);
});
}
};
this.readIntLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeIntStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
this.writeString(value);
});
}
};
this.readIntStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeIntPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(key);
protocolRegistration.write(this, value);
});
}
};
this.readIntPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeLongIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeInt(value);
});
}
};
this.readLongIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeLongLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeLong(value);
});
}
};
this.readLongLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeLongStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
this.writeString(value);
});
}
};
this.readLongStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeLongPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeLong(key);
protocolRegistration.write(this, value);
});
}
};
this.readLongPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeStringIntMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeInt(value);
});
}
};
this.readStringIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeStringLongMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeLong(value);
});
}
};
this.readStringLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeStringStringMap = function(value) {
if (value === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
this.writeString(value);
});
}
};
this.readStringStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeStringPacketMap = function(value, protocolId) {
if (value === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeString(key);
protocolRegistration.write(this, value);
});
}
};
this.readStringPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
};
export default ByteBuffer;
File diff suppressed because it is too large Load Diff
@@ -1,29 +1,19 @@
import ProtocolManager from '../ProtocolManager.js';
// @author jaysunxiao
// @version 1.0
// @since 2021-02-07 17:18
const NormalObject = function (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) {
// @version 3.0
const NormalObject = function(a, aaa, b, c, d, e, f, g, jj, kk, l, ll, lll, llll, m, mm, s, ssss) {
this.a = a; // byte
this.aaa = aaa; // byte[]
this.b = b; // short
this.bbb = bbb; // short[]
this.c = c; // int
this.ccc = ccc; // int[]
this.d = d; // long
this.ddd = ddd; // long[]
this.e = e; // float
this.eee = eee; // float[]
this.f = f; // double
this.fff = fff; // double[]
this.g = g; // boolean
this.ggg = ggg; // boolean[]
this.h = h; // char
this.hhh = hhh; // char[]
this.jj = jj; // java.lang.String
this.jjj = jjj; // java.lang.String[]
this.kk = kk; // com.zfoo.protocol.packet.ObjectA
this.kkk = kkk; // com.zfoo.protocol.packet.ObjectA[]
this.l = l; // java.util.List<java.lang.Integer>
this.ll = ll; // java.util.List<java.lang.Long>
this.lll = lll; // java.util.List<com.zfoo.protocol.packet.ObjectA>
this.llll = llll; // java.util.List<java.lang.String>
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
this.mm = mm; // java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
@@ -31,329 +21,75 @@ const NormalObject = function (a, aaa, b, bbb, c, ccc, d, ddd, e, eee, f, fff, g
this.ssss = ssss; // java.util.Set<java.lang.String>
};
NormalObject.prototype.protocolId = function () {
return 1161;
NormalObject.prototype.protocolId = function() {
return 101;
};
NormalObject.writeObject = function (byteBuffer, packet) {
if (packet === null) {
byteBuffer.writeBoolean(false);
NormalObject.write = function(byteBuffer, packet) {
if (byteBuffer.writePacketFlag(packet)) {
return;
}
byteBuffer.writeBoolean(true);
byteBuffer.writeByte(packet.a);
if (packet.aaa === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.aaa.length);
packet.aaa.forEach(element0 => {
byteBuffer.writeByte(element0);
});
}
byteBuffer.writeByteArray(packet.aaa);
byteBuffer.writeShort(packet.b);
if (packet.bbb === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.bbb.length);
packet.bbb.forEach(element1 => {
byteBuffer.writeShort(element1);
});
}
byteBuffer.writeInt(packet.c);
if (packet.ccc === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.ccc.length);
packet.ccc.forEach(element2 => {
byteBuffer.writeInt(element2);
});
}
byteBuffer.writeLong(packet.d);
if (packet.ddd === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.ddd.length);
packet.ddd.forEach(element3 => {
byteBuffer.writeLong(element3);
});
}
byteBuffer.writeFloat(packet.e);
if (packet.eee === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.eee.length);
packet.eee.forEach(element4 => {
byteBuffer.writeFloat(element4);
});
}
byteBuffer.writeDouble(packet.f);
if (packet.fff === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.fff.length);
packet.fff.forEach(element5 => {
byteBuffer.writeDouble(element5);
});
}
byteBuffer.writeBoolean(packet.g);
if (packet.ggg === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.ggg.length);
packet.ggg.forEach(element6 => {
byteBuffer.writeBoolean(element6);
});
}
byteBuffer.writeChar(packet.h);
if (packet.hhh === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.hhh.length);
packet.hhh.forEach(element7 => {
byteBuffer.writeChar(element7);
});
}
byteBuffer.writeString(packet.jj);
if (packet.jjj === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.jjj.length);
packet.jjj.forEach(element8 => {
byteBuffer.writeString(element8);
});
}
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, packet.kk);
if (packet.kkk === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.kkk.length);
packet.kkk.forEach(element9 => {
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element9);
});
}
if (packet.l === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.l.length);
packet.l.forEach(element10 => {
byteBuffer.writeInt(element10);
});
}
if (packet.llll === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.llll.length);
packet.llll.forEach(element11 => {
byteBuffer.writeString(element11);
});
}
if (packet.m === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.m.size);
packet.m.forEach((value13, key12) => {
byteBuffer.writeInt(key12);
byteBuffer.writeString(value13);
});
}
if (packet.mm === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.mm.size);
packet.mm.forEach((value15, key14) => {
byteBuffer.writeInt(key14);
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, value15);
});
}
if (packet.s === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.s.size);
packet.s.forEach(element16 => {
byteBuffer.writeInt(element16);
});
}
if (packet.ssss === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.ssss.size);
packet.ssss.forEach(element17 => {
byteBuffer.writeString(element17);
});
}
byteBuffer.writePacket(packet.kk, 102);
byteBuffer.writeIntArray(packet.l);
byteBuffer.writeLongArray(packet.ll);
byteBuffer.writePacketArray(packet.lll, 102);
byteBuffer.writeStringArray(packet.llll);
byteBuffer.writeIntStringMap(packet.m);
byteBuffer.writeIntPacketMap(packet.mm, 102);
byteBuffer.writeIntArray(packet.s);
byteBuffer.writeStringArray(packet.ssss);
};
NormalObject.readObject = function (byteBuffer) {
NormalObject.read = function(byteBuffer) {
if (!byteBuffer.readBoolean()) {
return null;
}
const packet = new NormalObject();
const result18 = byteBuffer.readByte();
packet.a = result18;
const result19 = [];
const size21 = byteBuffer.readInt();
if (size21 > 0) {
for (let index20 = 0; index20 < size21; index20++) {
const result22 = byteBuffer.readByte();
result19.push(result22);
}
}
packet.aaa = result19;
const result23 = byteBuffer.readShort();
packet.b = result23;
const result24 = [];
const size26 = byteBuffer.readInt();
if (size26 > 0) {
for (let index25 = 0; index25 < size26; index25++) {
const result27 = byteBuffer.readShort();
result24.push(result27);
}
}
packet.bbb = result24;
const result28 = byteBuffer.readInt();
packet.c = result28;
const result29 = [];
const size31 = byteBuffer.readInt();
if (size31 > 0) {
for (let index30 = 0; index30 < size31; index30++) {
const result32 = byteBuffer.readInt();
result29.push(result32);
}
}
packet.ccc = result29;
const result33 = byteBuffer.readLong();
packet.d = result33;
const result34 = [];
const size36 = byteBuffer.readInt();
if (size36 > 0) {
for (let index35 = 0; index35 < size36; index35++) {
const result37 = byteBuffer.readLong();
result34.push(result37);
}
}
packet.ddd = result34;
const result38 = byteBuffer.readFloat();
packet.e = result38;
const result39 = [];
const size41 = byteBuffer.readInt();
if (size41 > 0) {
for (let index40 = 0; index40 < size41; index40++) {
const result42 = byteBuffer.readFloat();
result39.push(result42);
}
}
packet.eee = result39;
const result43 = byteBuffer.readDouble();
packet.f = result43;
const result44 = [];
const size46 = byteBuffer.readInt();
if (size46 > 0) {
for (let index45 = 0; index45 < size46; index45++) {
const result47 = byteBuffer.readDouble();
result44.push(result47);
}
}
packet.fff = result44;
const result48 = byteBuffer.readBoolean();
packet.g = result48;
const result49 = [];
const size51 = byteBuffer.readInt();
if (size51 > 0) {
for (let index50 = 0; index50 < size51; index50++) {
const result52 = byteBuffer.readBoolean();
result49.push(result52);
}
}
packet.ggg = result49;
const result53 = byteBuffer.readChar();
packet.h = result53;
const result54 = [];
const size56 = byteBuffer.readInt();
if (size56 > 0) {
for (let index55 = 0; index55 < size56; index55++) {
const result57 = byteBuffer.readChar();
result54.push(result57);
}
}
packet.hhh = result54;
const result58 = byteBuffer.readString();
packet.jj = result58;
const result59 = [];
const size61 = byteBuffer.readInt();
if (size61 > 0) {
for (let index60 = 0; index60 < size61; index60++) {
const result62 = byteBuffer.readString();
result59.push(result62);
}
}
packet.jjj = result59;
const result63 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
packet.kk = result63;
const result64 = [];
const size66 = byteBuffer.readInt();
if (size66 > 0) {
for (let index65 = 0; index65 < size66; index65++) {
const result67 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
result64.push(result67);
}
}
packet.kkk = result64;
const result68 = [];
const size69 = byteBuffer.readInt();
if (size69 > 0) {
for (let index70 = 0; index70 < size69; index70++) {
const result71 = byteBuffer.readInt();
result68.push(result71);
}
}
packet.l = result68;
const result72 = [];
const size73 = byteBuffer.readInt();
if (size73 > 0) {
for (let index74 = 0; index74 < size73; index74++) {
const result75 = byteBuffer.readString();
result72.push(result75);
}
}
packet.llll = result72;
const result76 = new Map();
const size77 = byteBuffer.readInt();
if (size77 > 0) {
for (let index78 = 0; index78 < size77; index78++) {
const result79 = byteBuffer.readInt();
const result80 = byteBuffer.readString();
result76.set(result79, result80);
}
}
packet.m = result76;
const result81 = new Map();
const size82 = byteBuffer.readInt();
if (size82 > 0) {
for (let index83 = 0; index83 < size82; index83++) {
const result84 = byteBuffer.readInt();
const result85 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
result81.set(result84, result85);
}
}
packet.mm = result81;
const result86 = new Set();
const size87 = byteBuffer.readInt();
if (size87 > 0) {
for (let index88 = 0; index88 < size87; index88++) {
const result89 = byteBuffer.readInt();
result86.add(result89);
}
}
packet.s = result86;
const result90 = new Set();
const size91 = byteBuffer.readInt();
if (size91 > 0) {
for (let index92 = 0; index92 < size91; index92++) {
const result93 = byteBuffer.readString();
result90.add(result93);
}
}
packet.ssss = result90;
const result0 = byteBuffer.readByte();
packet.a = result0;
const array1 = byteBuffer.readByteArray();
packet.aaa = array1;
const result2 = byteBuffer.readShort();
packet.b = result2;
const result3 = byteBuffer.readInt();
packet.c = result3;
const result4 = byteBuffer.readLong();
packet.d = result4;
const result5 = byteBuffer.readFloat();
packet.e = result5;
const result6 = byteBuffer.readDouble();
packet.f = result6;
const result7 = byteBuffer.readBoolean();
packet.g = result7;
const result8 = byteBuffer.readString();
packet.jj = result8;
const result9 = byteBuffer.readPacket(102);
packet.kk = result9;
const list10 = byteBuffer.readIntArray();
packet.l = list10;
const list11 = byteBuffer.readLongArray();
packet.ll = list11;
const list12 = byteBuffer.readPacketArray(102);
packet.lll = list12;
const list13 = byteBuffer.readStringArray();
packet.llll = list13;
const map14 = byteBuffer.readIntStringMap();
packet.m = map14;
const map15 = byteBuffer.readIntPacketMap(102);
packet.mm = map15;
const set16 = byteBuffer.readIntArray();
packet.s = set16;
const set17 = byteBuffer.readStringArray();
packet.ssss = set17;
return packet;
};
@@ -1,55 +1,35 @@
import ProtocolManager from '../ProtocolManager.js';
// @author jaysunxiao
// @version 1.0
// @since 2017 10.12 15:39
const ObjectA = function (a, m, objectB) {
// @version 3.0
const ObjectA = function(a, m, objectB) {
this.a = a; // int
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
this.objectB = objectB; // com.zfoo.protocol.packet.ObjectB
};
ObjectA.prototype.protocolId = function () {
return 1116;
ObjectA.prototype.protocolId = function() {
return 102;
};
ObjectA.writeObject = function (byteBuffer, packet) {
if (packet === null) {
byteBuffer.writeBoolean(false);
ObjectA.write = function(byteBuffer, packet) {
if (byteBuffer.writePacketFlag(packet)) {
return;
}
byteBuffer.writeBoolean(true);
byteBuffer.writeInt(packet.a);
if (packet.m === null) {
byteBuffer.writeInt(0);
} else {
byteBuffer.writeInt(packet.m.size);
packet.m.forEach((value1, key0) => {
byteBuffer.writeInt(key0);
byteBuffer.writeString(value1);
});
}
ProtocolManager.getProtocol(1117).writeObject(byteBuffer, packet.objectB);
byteBuffer.writeIntStringMap(packet.m);
byteBuffer.writePacket(packet.objectB, 103);
};
ObjectA.readObject = function (byteBuffer) {
ObjectA.read = function(byteBuffer) {
if (!byteBuffer.readBoolean()) {
return null;
}
const packet = new ObjectA();
const result2 = byteBuffer.readInt();
packet.a = result2;
const result3 = new Map();
const size4 = byteBuffer.readInt();
if (size4 > 0) {
for (let index5 = 0; index5 < size4; index5++) {
const result6 = byteBuffer.readInt();
const result7 = byteBuffer.readString();
result3.set(result6, result7);
}
}
packet.m = result3;
const result8 = ProtocolManager.getProtocol(1117).readObject(byteBuffer);
packet.objectB = result8;
const result0 = byteBuffer.readInt();
packet.a = result0;
const map1 = byteBuffer.readIntStringMap();
packet.m = map1;
const result2 = byteBuffer.readPacket(103);
packet.objectB = result2;
return packet;
};
@@ -1,29 +1,26 @@
// @author jaysunxiao
// @version 1.0
// @since 2017 10.12 15:39
const ObjectB = function (flag) {
// @version 3.0
const ObjectB = function(flag) {
this.flag = flag; // boolean
};
ObjectB.prototype.protocolId = function () {
return 1117;
ObjectB.prototype.protocolId = function() {
return 103;
};
ObjectB.writeObject = function (byteBuffer, packet) {
if (packet === null) {
byteBuffer.writeBoolean(false);
ObjectB.write = function(byteBuffer, packet) {
if (byteBuffer.writePacketFlag(packet)) {
return;
}
byteBuffer.writeBoolean(true);
byteBuffer.writeBoolean(packet.flag);
};
ObjectB.readObject = function (byteBuffer) {
ObjectB.read = function(byteBuffer) {
if (!byteBuffer.readBoolean()) {
return null;
}
const packet = new ObjectB();
const result0 = byteBuffer.readBoolean();
const result0 = byteBuffer.readBoolean();
packet.flag = result0;
return packet;
};
@@ -1,33 +1,30 @@
// @author jaysunxiao
// @version 1.0
// @since 2021-03-27 15:18
const SimpleObject = function (c, g) {
// @version 3.0
const SimpleObject = function(c, g) {
this.c = c; // int
this.g = g; // boolean
};
SimpleObject.prototype.protocolId = function () {
return 1163;
SimpleObject.prototype.protocolId = function() {
return 104;
};
SimpleObject.writeObject = function (byteBuffer, packet) {
if (packet === null) {
byteBuffer.writeBoolean(false);
SimpleObject.write = function(byteBuffer, packet) {
if (byteBuffer.writePacketFlag(packet)) {
return;
}
byteBuffer.writeBoolean(true);
byteBuffer.writeInt(packet.c);
byteBuffer.writeBoolean(packet.g);
};
SimpleObject.readObject = function (byteBuffer) {
SimpleObject.read = function(byteBuffer) {
if (!byteBuffer.readBoolean()) {
return null;
}
const packet = new SimpleObject();
const result0 = byteBuffer.readInt();
packet.c = result0;
const result1 = byteBuffer.readBoolean();
const result1 = byteBuffer.readBoolean();
packet.g = result1;
return packet;
};
@@ -4,16 +4,16 @@ import ProtocolManager from './jsProtocol/ProtocolManager.js';
const fs = require('fs');
describe('jsProtocolTest', () => {
it('complexObjectTest', () => {
const data = fs.readFileSync('D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
console.log(data.buffer);
const data = fs.readFileSync('C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
ProtocolManager.initProtocol();
const arrayBytes = new Uint8Array(data.length);
data.copy(arrayBytes, 0, 0, data.length);
const byteBuffer = new ByteBuffer();
byteBuffer.writeBytes(data.buffer);
byteBuffer.writeBytes(arrayBytes);
const packet = ProtocolManager.read(byteBuffer);
console.log(packet);