perf[protocol]: 支持TypeScript

This commit is contained in:
jaysunxiao
2022-06-06 17:55:43 +08:00
parent e4ac24a40e
commit 1fa4ff032e
37 changed files with 4386 additions and 112 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
安装好以后,在git bash中输入:git --version,有结果返回则表示安装成功
```
- git下载代码慢的解决方法|无法下载代码的解决方法
- git下载代码慢的解决方法|无法下载代码的解决方法,取消git配置使用git config --global --unset http.proxy
```
使用vpn
@@ -57,13 +57,13 @@ public abstract class GenerateProtocolPath {
var protocolPath = protocolPathMap.get(protocolId);
var relativePath = protocolPathMap.get(relativeProtocolId);
if (relativePath.startsWith(protocolPath)) {
return StringUtils.substringAfterFirst(relativePath, protocolPath).replaceAll(StringUtils.PERIOD_REGEX, StringUtils.SLASH);
return StringUtils.format(".{}", StringUtils.substringAfterFirst(relativePath, protocolPath).replaceAll(StringUtils.PERIOD_REGEX, StringUtils.SLASH));
}
var splits = protocolPath.split(StringUtils.PERIOD_REGEX);
var builder = new StringBuilder();
for (var i = splits.length - 1; i > 0; i--) {
for (var i = splits.length; i > 0; i--) {
builder.append("../");
var path = StringUtils.joinWith(StringUtils.PERIOD, Arrays.stream(splits).limit(i).collect(Collectors.toList()).toArray());
if (relativePath.startsWith(path)) {
@@ -35,8 +35,7 @@ public class CppListSerializer implements ICppSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString());
var name = StringUtils.format("{}", field.getName());
return new Pair<>(type, name);
return new Pair<>(type, field.getName());
}
@Override
@@ -35,8 +35,7 @@ public class CppMapSerializer implements ICppSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString());
var name = StringUtils.format("{}", field.getName());
return new Pair<>(type, name);
return new Pair<>(type, field.getName());
}
@Override
@@ -37,8 +37,7 @@ public class CppObjectProtocolSerializer implements ICppSerializer {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
var type = StringUtils.format("{}", protocolSimpleName);
var name = field.getName();
return new Pair<>(type, name);
return new Pair<>(type, field.getName());
}
@Override
@@ -35,8 +35,7 @@ public class CppSetSerializer implements ICppSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString());
var name = StringUtils.format("{}", field.getName());
return new Pair<>(type, name);
return new Pair<>(type, field.getName());
}
@Override
@@ -408,6 +408,4 @@ public abstract class GenerateCppUtils {
return typeName;
}
}
@@ -122,13 +122,11 @@ public abstract class GenerateTsUtils {
var importSubProtocol = importSubProtocol(registration);
var docTitle = docTitle(registration);
var fieldDefinition = fieldDefinition(registration);
var valueOfMethod = valueOfMethod(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
protocolTemplate = StringUtils.format(protocolTemplate, importSubProtocol, docTitle, protocolClazzName, fieldDefinition.trim()
, valueOfMethod.getKey().trim(), valueOfMethod.getValue().trim(), protocolId, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
, protocolId, protocolClazzName, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
var protocolOutputPath = StringUtils.format("{}/{}/{}.ts", protocolOutputRootPath
, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate);
@@ -144,8 +142,12 @@ public abstract class GenerateTsUtils {
var importBuilder = new StringBuilder();
for (var subProtocolId : subProtocols) {
var protocolClassName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId);
var path = GenerateProtocolPath.getProtocolPath(protocolId);
importBuilder.append(StringUtils.format("import {} from './{}';", protocolClassName, protocolClassName)).append(LS);
var path = GenerateProtocolPath.getRelativePath(protocolId, subProtocolId);
if (StringUtils.isBlank(path)) {
importBuilder.append(StringUtils.format("import {} from './{}';", protocolClassName, protocolClassName)).append(LS);
} else {
importBuilder.append(StringUtils.format("import {} from '{}/{}';", protocolClassName, path, protocolClassName)).append(LS);
}
}
return importBuilder.toString();
}
@@ -178,35 +180,12 @@ public abstract class GenerateTsUtils {
Arrays.stream(doc.split(LS)).forEach(it -> fieldDefinitionBuilder.append(TAB).append(it).append(LS));
}
var propertyTypeAndName = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(TAB).append(StringUtils.format("{}: {};", propertyTypeAndName.getValue(), propertyTypeAndName.getKey())).append(LS);
var triple = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(TAB).append(StringUtils.format("{}{} = {};", triple.getMiddle(), triple.getLeft(), triple.getRight())).append(LS);
}
return fieldDefinitionBuilder.toString();
}
private static Pair<String, String> valueOfMethod(ProtocolRegistration registration) {
var protocolId = registration.getId();
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var filedList = new ArrayList<Pair<String, String>>();
for (int i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
var propertyTypeAndName = tsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
filedList.add(propertyTypeAndName);
}
var valueOfParams = filedList.stream()
.map(it -> StringUtils.format("{}: {}", it.getValue(), it.getKey()))
.collect(Collectors.toList());
var valueOfParamsStr = StringUtils.joinWith(StringUtils.COMMA + " ", valueOfParams.toArray());
var cppBuilder = new StringBuilder();
filedList.forEach(it -> cppBuilder.append(TAB + TAB).append(StringUtils.format("this.{} = {};", it.getValue(), it.getValue())).append(LS));
return new Pair<>(valueOfParamsStr, cppBuilder.toString());
}
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
@@ -227,13 +206,98 @@ public abstract class GenerateTsUtils {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
jsBuilder.append(TAB).append("if (!buffer.isReadable()) {").append(LS);
jsBuilder.append(TAB + TAB).append("return packet;").append(LS);
jsBuilder.append(TAB).append("}").append(LS);
jsBuilder.append(TAB + TAB).append("if (!buffer.isReadable()) {").append(LS);
jsBuilder.append(TAB + TAB + TAB).append("return packet;").append(LS);
jsBuilder.append(TAB + TAB).append("}").append(LS);
}
var readObject = tsSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 2, field, fieldRegistration);
jsBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
return jsBuilder.toString();
}
public static String toTsClassName(String typeName) {
typeName = typeName.replaceAll("java.util.|java.lang.", StringUtils.EMPTY);
typeName = typeName.replaceAll("com\\.[a-zA-Z0-9_.]*\\.", StringUtils.EMPTY);
// CSharp不适用基础类型的泛型,会影响性能
switch (typeName) {
case "boolean":
case "Boolean":
typeName = "boolean";
return typeName;
case "byte":
case "Byte":
case "short":
case "Short":
case "int":
case "Integer":
case "long":
case "Long":
case "float":
case "Float":
case "double":
case "Double":
typeName = "number";
return typeName;
case "char":
case "Character":
case "String":
typeName = "string";
return typeName;
default:
}
// 将boolean转为bool
typeName = typeName.replaceAll("[B|b]oolean\\[", "boolean");
typeName = typeName.replace("<Boolean", "<boolean");
typeName = typeName.replace("Boolean>", "boolean>");
// 将Byte转为byte
typeName = typeName.replace("Byte[", "number");
typeName = typeName.replace("Byte>", "number>");
typeName = typeName.replace("<Byte", "<number");
// 将Short转为short
typeName = typeName.replace("Short[", "number");
typeName = typeName.replace("Short>", "number>");
typeName = typeName.replace("<Short", "<number");
// 将Integer转为int
typeName = typeName.replace("Integer[", "number");
typeName = typeName.replace("Integer>", "number>");
typeName = typeName.replace("<Integer", "<number");
// 将Long转为long
typeName = typeName.replace("Long[", "number");
typeName = typeName.replace("Long>", "number>");
typeName = typeName.replace("<Long", "<number");
// 将Float转为float
typeName = typeName.replace("Float[", "number");
typeName = typeName.replace("Float>", "number>");
typeName = typeName.replace("<Float", "<number");
// 将Double转为double
typeName = typeName.replace("Double[", "number");
typeName = typeName.replace("Double>", "number>");
typeName = typeName.replace("<Double", "<number");
// 将Character转为Char
typeName = typeName.replace("Character[", "string");
typeName = typeName.replace("Character>", "string>");
typeName = typeName.replace("<Character", "<string");
// 将String转为string
typeName = typeName.replace("String[", "string");
typeName = typeName.replace("String>", "string>");
typeName = typeName.replace("<String", "<string");
typeName = typeName.replace("Map<", "Map<");
typeName = typeName.replace("Set<", "Set<");
typeName = typeName.replace("List<", "Array<");
return typeName;
}
}
@@ -14,6 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import java.lang.reflect.Field;
@@ -25,9 +26,9 @@ import java.lang.reflect.Field;
public interface ITsSerializer {
/**
* 获取属性的类型名称
* 获取属性的类型名称,默认值
*/
Pair<String, String> field(Field field, IFieldRegistration fieldRegistration);
Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration);
void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration);
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
@@ -32,8 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsArraySerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("Array", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": Array<{}> | null", GenerateTsUtils.toTsClassName(field.getType().getComponentType().getSimpleName()));
return new Triple<>(type, field.getName(), "null");
}
@Override
@@ -75,8 +76,8 @@ public class TsArraySerializer implements ITsSerializer {
ArrayField arrayField = (ArrayField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
var typeName = GenerateTsUtils.toTsClassName(arrayField.getType().getSimpleName());
builder.append(StringUtils.format("const {} = new {};", result, typeName)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsBooleanSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("boolean", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": boolean", field.getName(), "false");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsByteSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsCharSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("string", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": string", field.getName(), "''");
}
@Override
@@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +30,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsDoubleSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsFloatSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsIntSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.serializer.CodeLanguage;
@@ -32,8 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsListSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("Array", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {} | null", GenerateTsUtils.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "null");
}
@Override
@@ -75,8 +76,8 @@ public class TsListSerializer implements ITsSerializer {
ListField listField = (ListField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
var typeName = GenerateTsUtils.toTsClassName(listField.getType().toString());
builder.append(StringUtils.format("const {} = new {}();", result, typeName)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsLongSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
@@ -32,8 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsMapSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("Map", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {} | null", GenerateTsUtils.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "null");
}
@Override
@@ -79,8 +80,8 @@ public class TsMapSerializer implements ITsSerializer {
MapField mapField = (MapField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = new Map();", result)).append(LS);
var typeName = GenerateTsUtils.toTsClassName(mapField.getType().toString());
builder.append(StringUtils.format("const {} = new {}();", result, typeName)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
@@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
@@ -31,11 +32,11 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsObjectProtocolSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
var type = StringUtils.format("{}", protocolSimpleName);
return new Pair<>(type, field.getName());
var type = StringUtils.format(": {} | null", protocolSimpleName);
return new Triple<>(type, field.getName(), "null");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.CodeLanguage;
@@ -32,8 +32,9 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsSetSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("Set", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format(": {} | null", GenerateTsUtils.toTsClassName(field.getGenericType().toString()));
return new Triple<>(type, field.getName(), "null");
}
@Override
@@ -76,8 +77,8 @@ public class TsSetSerializer implements ITsSerializer {
SetField setField = (SetField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = new Set();", result)).append(LS);
var typeName = GenerateTsUtils.toTsClassName(setField.getType().toString());
builder.append(StringUtils.format("const {} = new {}();", result, typeName)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
@@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +30,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsShortSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("number", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": number", field.getName(), "0");
}
@Override
@@ -14,7 +14,7 @@
package com.zfoo.protocol.serializer.typescript;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
@@ -29,8 +29,8 @@ import static com.zfoo.protocol.util.FileUtils.LS;
public class TsStringSerializer implements ITsSerializer {
@Override
public Pair<String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Pair<>("string", field.getName());
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(": string", field.getName(), "''");
}
@Override
@@ -723,8 +723,8 @@ const ByteBuffer = function() {
};
this.writeLongSet = function(set) {
if (array === null) {
set.writeInt(0);
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
@@ -1,12 +1,12 @@
{}
const protocols = new Map();
const protocols = new Map<number, any>();
// initProtocol
{}
class ProtocolManager {
static getProtocol(): Any {
static getProtocol(protocolId: number): any {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
@@ -14,14 +14,14 @@ class ProtocolManager {
return protocol;
}
static write(buffer: Any, packet: Any): void {
static write(buffer: any, packet: any): void {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = getProtocol(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
}
static read(buffer: Any): Any {
static read(buffer: any): any {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
@@ -4,19 +4,22 @@ class {} {
{}
constructor({}) {
{}
}
protocolId(): number {
return {};
}
static write(buffer: Any, packet: {}) {
static write(buffer: any, packet: {} | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
{}
}
static read(buffer: Any): {} {
static read(buffer: any): {} | null {
if (!buffer.readBoolean()) {
return null;
}
@@ -1,3 +1,5 @@
import ProtocolManager from "../ProtocolManager";
const Longbits = require('./longbits.js');
const empty_str = '';
@@ -23,14 +25,14 @@ const decoder = new util.TextDecoder('utf-8');
// const maxLong = 9007199254740992;
// const minLong = -9007199254740992;
const copy = function copy(original: ArrayBuffer, newLength: number) {
function copy(original: ArrayBuffer, newLength: number) {
if (original.byteLength > newLength) {
throw new Error('newLength is too small');
}
const dst = new ArrayBuffer(newLength);
new Uint8Array(dst).set(new Uint8Array(original));
return dst;
};
}
function encodeZigzagInt(n: number) {
// 有效位左移一位+符号位右移31位
@@ -347,6 +349,784 @@ class ByteBuffer {
this.readOffset += length;
return value;
}
writePacketFlag(value: any): boolean {
const flag = (value === null) || (value === undefined);
this.writeBoolean(!flag);
return flag;
};
writePacket(packet: any, protocolId: number) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, packet);
};
readPacket(protocolId: number): any {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
writeBooleanArray(array: Array<boolean> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
});
}
};
readBooleanArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
}
}
return array;
};
writeByteArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeByte(element);
});
}
};
readByteArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readByte());
}
}
return array;
};
writeShortArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeShort(element);
});
}
};
readShortArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readShort());
}
}
return array;
};
writeIntArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeInt(element);
});
}
};
readIntArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readInt());
}
}
return array;
};
writeLongArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeLong(element);
});
}
};
readLongArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readLong());
}
}
return array;
};
writeFloatArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeFloat(element);
});
}
};
readFloatArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readFloat());
}
}
return array;
};
writeDoubleArray(array: Array<number> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeDouble(element);
});
}
};
readDoubleArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readDouble());
}
}
return array;
};
writeStringArray(array: Array<string> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeString(element);
});
}
};
readStringArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readString());
}
}
return array;
};
writeCharArray(array: Array<string> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeChar(element);
});
}
};
readCharArray(): any {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readChar());
}
}
return array;
};
writePacketArray(array: Array<any> | null, protocolId: number) {
if (array === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(array.length);
array.forEach(element => {
protocolRegistration.write(this, element);
});
}
};
readPacketArray(protocolId: number): any {
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;
};
// ---------------------------------------------list-------------------------------------------
writeBooleanList(list: any) {
this.writeBooleanArray(list);
};
readBooleanList(): any {
return this.readBooleanArray();
};
writeByteList(list: any) {
this.writeByteArray(list);
};
readByteList(): any {
return this.readByteArray();
};
writeShortList(list: any) {
this.writeShortArray(list);
};
readShortList(): any {
return this.readShortArray();
};
writeIntList(list: any) {
this.writeIntArray(list);
};
readIntList(): any {
return this.readIntArray();
};
writeLongList(list: any) {
this.writeLongArray(list);
};
readLongList(): any {
return this.readLongArray();
};
writeFloatList(list: any) {
this.writeFloatArray(list);
};
readFloatList(): any {
return this.readFloatArray();
};
writeDoubleList(list: any) {
this.writeDoubleArray(list);
};
readDoubleList(): any {
return this.readDoubleArray();
};
writeStringList(list: any) {
this.writeStringArray(list);
};
readStringList(): any {
return this.readStringArray();
};
writeCharList(list: any) {
this.writeCharArray(list);
};
readCharList(): any {
return this.readCharArray();
};
writePacketList(list: any, protocolId: number) {
this.writePacketArray(list, protocolId);
};
readPacketList(protocolId: number): any {
return this.readPacketArray(protocolId);
};
// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set: Set<boolean> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeBoolean(element);
});
}
};
readBooleanSet(): any {
return new Set(this.readBooleanArray());
};
writeByteSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeByte(element);
});
}
};
readByteSet(): any {
return new Set(this.readByteArray());
};
writeShortSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeShort(element);
});
}
};
readShortSet(): any {
return new Set(this.readShortArray());
};
writeIntSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeInt(element);
});
}
};
readIntSet(): any {
return new Set(this.readIntArray());
};
writeLongSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeLong(element);
});
}
};
readLongSet(): any {
return new Set(this.readLongArray());
};
writeFloatSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeFloat(element);
});
}
};
readFloatSet(): any {
return new Set(this.readFloatArray());
};
writeDoubleSet(set: Set<number> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeDouble(element);
});
}
};
readDoubleSet(): any {
return new Set(this.readDoubleArray());
};
writeStringSet(set: Set<string> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeString(element);
});
}
};
readStringSet(): any {
return new Set(this.readStringArray());
};
writeCharSet(set: Set<string> | null) {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeChar(element);
});
}
};
readCharSet(): any {
return new Set(this.readCharArray());
};
writePacketSet(set: Set<any> | null, protocolId: number) {
if (set === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(set.size);
set.forEach(element => {
protocolRegistration.write(this, element);
});
}
};
readPacketSet(protocolId: number): any {
return new Set(this.readPacketArray(protocolId));
};
// ---------------------------------------------map-------------------------------------------
writeIntIntMap(map: Map<number, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeInt(value);
});
}
};
readIntIntMap(): any {
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;
};
writeIntLongMap(map: Map<number, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeLong(value);
});
}
};
readIntLongMap(): any {
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;
};
writeIntStringMap(map: Map<number, string> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeString(value);
});
}
};
readIntStringMap(): any {
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;
};
writeIntPacketMap(map: Map<number, any> | null, protocolId: number) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
protocolRegistration.write(this, value);
});
}
};
readIntPacketMap(protocolId: number): any {
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;
};
writeLongIntMap(map: Map<number, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeInt(value);
});
}
};
readLongIntMap(): any {
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;
};
writeLongLongMap(map: Map<number, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeLong(value);
});
}
};
readLongLongMap(): any {
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;
};
writeLongStringMap(map: Map<number, string> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeString(value);
});
}
};
readLongStringMap(): any {
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;
};
writeLongPacketMap(map: Map<number, any> | null, protocolId: number): any {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
protocolRegistration.write(this, value);
});
}
};
readLongPacketMap(protocolId: number): any {
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;
};
writeStringIntMap(map: Map<string, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeInt(value);
});
}
};
readStringIntMap(): any {
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;
};
writeStringLongMap(map: Map<string, number> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeLong(value);
});
}
};
readStringLongMap(): any {
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;
};
writeStringStringMap(map: Map<string, string> | null) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeString(value);
});
}
};
readStringStringMap(): any {
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;
};
writeStringPacketMap(map: Map<string, any> | null, protocolId: number) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
protocolRegistration.write(this, value);
});
}
};
readStringPacketMap(protocolId: number): any {
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;
+33 -1
View File
@@ -1,4 +1,5 @@
import ByteBuffer from './zfoojs/buffer/ByteBuffer';
import ByteBuffer from './zfoo/buffer/ByteBuffer';
import ProtocolManager from './zfoo/ProtocolManager';
function assert(flag: boolean): void {
if (!flag) {
@@ -10,6 +11,37 @@ function assert(flag: boolean): void {
console.log("Hello world");
const data = fs.readFileSync('C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
const arrayBytes = new Uint8Array(data.length);
data.copy(arrayBytes, 0, 0, data.length);
const byteBuffer = new ByteBuffer();
byteBuffer.writeBytes(arrayBytes);
const packet = ProtocolManager.read(byteBuffer);
// complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
packet.myCompatible = 0
console.log(packet);
const newByteBuffer = new ByteBuffer();
ProtocolManager.write(newByteBuffer, packet);
const newPacket = ProtocolManager.read(newByteBuffer);
console.log(newPacket);
assert(byteBuffer.readOffset <= newByteBuffer.writeOffset);
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
const length = newByteBuffer.writeOffset;
byteBuffer.setReadOffset(0);
newByteBuffer.setReadOffset(0);
for (let i = 0; i < length; i++) {
assert(byteBuffer.readByte() == newByteBuffer.readByte());
}
// ByteBuffer test
let buffer = new ByteBuffer();
@@ -0,0 +1,40 @@
import ComplexObject from './packet/ComplexObject';
import NormalObject from './packet/NormalObject';
import ObjectA from './packet/ObjectA';
import ObjectB from './packet/ObjectB';
import SimpleObject from './packet/SimpleObject';
const protocols = new Map<number, any>();
// initProtocol
protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
class ProtocolManager {
static getProtocol(protocolId: number): any {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
}
return protocol;
}
static write(buffer: any, packet: any): void {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
}
static read(buffer: any): any {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
}
}
export default ProtocolManager;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,189 @@
// from protobuf
const Long = require('./long.js')
module.exports = LongBits;
/**
* Constructs new long bits.
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @constructor
* @param {number} lo Low 32 bits, unsigned
* @param {number} hi High 32 bits, unsigned
*/
function LongBits(lo, hi) {
// note that the casts below are theoretically unnecessary as of today, but older statically
// generated converter code might still call the ctor with signed 32bits. kept for compat.
/**
* Low bits.
* @type {number}
*/
this.lo = lo >>> 0;
/**
* High bits.
* @type {number}
*/
this.hi = hi >>> 0;
}
/**
* Zig-zag encodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBits.prototype.zzEncode = function zzEncode() {
const mask = this.hi >> 31;
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
this.lo = (this.lo << 1 ^ mask) >>> 0;
return this;
};
/**
* Zig-zag decodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBits.prototype.zzDecode = function zzDecode() {
const mask = -(this.lo & 1);
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
return this;
};
/**
* Converts this long bits to a long.
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long} Long
*/
LongBits.prototype.toLong = function toLong(unsigned) {
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned));
};
/**
* Zero bits.
* @memberof util.LongBits
* @type {util.LongBits}
*/
const zero = LongBits.zero = new LongBits(0, 0);
function from(value) {
if (typeof value === 'number') {
return fromNumber(value);
}
if (typeof value === 'string' || value instanceof String) {
value = Long.fromString(value);
}
return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
}
/**
* Constructs new long bits from the specified number.
* @param {number} value Value
* @returns {util.LongBits} Instance
*/
function fromNumber(value) {
if (value === 0) {
return zero;
}
const sign = value < 0;
if (sign) {
value = -value;
}
let lo = value >>> 0;
let hi = (value - lo) / 4294967296 >>> 0;
if (sign) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (++lo > 4294967295) {
lo = 0;
if (++hi > 4294967295) {
hi = 0;
}
}
}
return new LongBits(lo, hi);
}
function writeVarint64(byteBuffer, value) {
let count = 0;
while (value.hi) {
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = (value.lo >>> 7 | value.hi << 25) >>> 0;
value.hi >>>= 7;
count = count + 7;
}
while (value.lo > 127) {
if (count >= 56) {
byteBuffer.writeByte(value.lo);
return;
}
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = value.lo >>> 7;
count = count + 7;
}
byteBuffer.writeByte(value.lo);
}
function readLongVarint(buffer) {
// tends to deopt with local vars for octet etc.
const bits = new LongBits(0, 0);
let i = 0;
const len = buffer.length;
let pos = 0;
if (len - pos > 4) { // fast route (lo)
for (; i < 4; ++i) {
// 1st..4th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 5th
bits.lo = (bits.lo | (buffer[pos] & 127) << 28) >>> 0;
bits.hi = (bits.hi | (buffer[pos] & 127) >> 4) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
i = 0;
} else {
for (; i < 3; ++i) {
// 1st..3th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 4th
bits.lo = (bits.lo | (buffer[pos++] & 127) << i * 7) >>> 0;
return bits;
}
// 6th..9th
for (; i < 4; ++i) {
// 最后一位直接写入
if (pos === 8) {
bits.hi = (bits.hi | buffer[pos] << i * 7 + 3) >>> 0;
return bits;
}
bits.hi = (bits.hi | (buffer[pos] & 127) << i * 7 + 3) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
return bits;
}
function writeInt64(byteBuffer, value) {
const bits = from(value).zzEncode();
writeVarint64(byteBuffer, bits);
}
function readInt64(buffer) {
return readLongVarint(buffer).zzDecode().toLong(false);
}
LongBits.writeInt64 = writeInt64;
LongBits.readInt64 = readInt64;
@@ -0,0 +1,488 @@
import ObjectA from './ObjectA';
import ObjectB from './ObjectB';
// 复杂的对象
// 包括了各种复杂的结构,数组,List,Set,Map
//
// @author jaysunxiao
// @version 3.0
class ComplexObject {
// byte类型,最简单的整形
a: number = 0;
// byte的包装类型
// 优先使用基础类型,包装类型会有装箱拆箱
aa: number = 0;
// 数组类型
aaa: Array<number> | null = null;
aaaa: Array<number> | null = null;
b: number = 0;
bb: number = 0;
bbb: Array<number> | null = null;
bbbb: Array<number> | null = null;
c: number = 0;
cc: number = 0;
ccc: Array<number> | null = null;
cccc: Array<number> | null = null;
d: number = 0;
dd: number = 0;
ddd: Array<number> | null = null;
dddd: Array<number> | null = null;
e: number = 0;
ee: number = 0;
eee: Array<number> | null = null;
eeee: Array<number> | null = null;
f: number = 0;
ff: number = 0;
fff: Array<number> | null = null;
ffff: Array<number> | null = null;
g: boolean = false;
gg: boolean = false;
ggg: Array<boolean> | null = null;
gggg: Array<boolean> | null = null;
h: string = '';
hh: string = '';
hhh: Array<string> | null = null;
hhhh: Array<string> | null = null;
jj: string = '';
jjj: Array<string> | null = null;
kk: ObjectA | null = null;
kkk: Array<ObjectA> | null = null;
l: Array<number> | null = null;
ll: Array<Array<Array<number>>> | null = null;
lll: Array<Array<ObjectA>> | null = null;
llll: Array<string> | null = null;
lllll: Array<Map<number, string>> | null = null;
m: Map<number, string> | null = null;
mm: Map<number, ObjectA> | null = null;
mmm: Map<ObjectA, Array<number>> | null = null;
mmmm: Map<Array<Array<ObjectA>>, Array<Array<Array<number>>>> | null = null;
mmmmm: Map<Array<Map<number, string>>, Set<Map<number, string>>> | null = null;
s: Set<number> | null = null;
ss: Set<Set<Array<number>>> | null = null;
sss: Set<Set<ObjectA>> | null = null;
ssss: Set<string> | null = null;
sssss: Set<Map<number, string>> | null = null;
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
myCompatible: number = 0;
myObject: ObjectA | null = null;
protocolId(): number {
return 100;
}
static write(buffer: any, packet: ComplexObject | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
buffer.writeByte(packet.a);
buffer.writeByte(packet.aa);
buffer.writeByteArray(packet.aaa);
buffer.writeByteArray(packet.aaaa);
buffer.writeShort(packet.b);
buffer.writeShort(packet.bb);
buffer.writeShortArray(packet.bbb);
buffer.writeShortArray(packet.bbbb);
buffer.writeInt(packet.c);
buffer.writeInt(packet.cc);
buffer.writeIntArray(packet.ccc);
buffer.writeIntArray(packet.cccc);
buffer.writeLong(packet.d);
buffer.writeLong(packet.dd);
buffer.writeLongArray(packet.ddd);
buffer.writeLongArray(packet.dddd);
buffer.writeFloat(packet.e);
buffer.writeFloat(packet.ee);
buffer.writeFloatArray(packet.eee);
buffer.writeFloatArray(packet.eeee);
buffer.writeDouble(packet.f);
buffer.writeDouble(packet.ff);
buffer.writeDoubleArray(packet.fff);
buffer.writeDoubleArray(packet.ffff);
buffer.writeBoolean(packet.g);
buffer.writeBoolean(packet.gg);
buffer.writeBooleanArray(packet.ggg);
buffer.writeBooleanArray(packet.gggg);
buffer.writeChar(packet.h);
buffer.writeChar(packet.hh);
buffer.writeCharArray(packet.hhh);
buffer.writeCharArray(packet.hhhh);
buffer.writeString(packet.jj);
buffer.writeStringArray(packet.jjj);
buffer.writePacket(packet.kk, 102);
buffer.writePacketArray(packet.kkk, 102);
buffer.writeIntList(packet.l);
if (packet.ll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.ll.length);
packet.ll.forEach(element0 => {
if (element0 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element0.length);
element0.forEach(element1 => {
buffer.writeIntList(element1);
});
}
});
}
if (packet.lll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.lll.length);
packet.lll.forEach(element2 => {
buffer.writePacketList(element2, 102);
});
}
buffer.writeStringList(packet.llll);
if (packet.lllll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.lllll.length);
packet.lllll.forEach(element3 => {
buffer.writeIntStringMap(element3);
});
}
buffer.writeIntStringMap(packet.m);
buffer.writeIntPacketMap(packet.mm, 102);
if (packet.mmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmm.size);
packet.mmm.forEach((value5, key4) => {
buffer.writePacket(key4, 102);
buffer.writeIntList(value5);
});
}
if (packet.mmmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmmm.size);
packet.mmmm.forEach((value7, key6) => {
if (key6 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(key6.length);
key6.forEach(element8 => {
buffer.writePacketList(element8, 102);
});
}
if (value7 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(value7.length);
value7.forEach(element9 => {
if (element9 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element9.length);
element9.forEach(element10 => {
buffer.writeIntList(element10);
});
}
});
}
});
}
if (packet.mmmmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmmmm.size);
packet.mmmmm.forEach((value12, key11) => {
if (key11 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(key11.length);
key11.forEach(element13 => {
buffer.writeIntStringMap(element13);
});
}
if (value12 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(value12.size);
value12.forEach(element14 => {
buffer.writeIntStringMap(element14);
});
}
});
}
buffer.writeIntSet(packet.s);
if (packet.ss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.ss.size);
packet.ss.forEach(element15 => {
if (element15 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element15.size);
element15.forEach(element16 => {
buffer.writeIntList(element16);
});
}
});
}
if (packet.sss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.sss.size);
packet.sss.forEach(element17 => {
buffer.writePacketSet(element17, 102);
});
}
buffer.writeStringSet(packet.ssss);
if (packet.sssss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.sssss.size);
packet.sssss.forEach(element18 => {
buffer.writeIntStringMap(element18);
});
}
buffer.writeInt(packet.myCompatible);
buffer.writePacket(packet.myObject, 102);
}
static read(buffer: any): ComplexObject | null {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ComplexObject();
const result19 = buffer.readByte();
packet.a = result19;
const result20 = buffer.readByte();
packet.aa = result20;
const array21 = buffer.readByteArray();
packet.aaa = array21;
const array22 = buffer.readByteArray();
packet.aaaa = array22;
const result23 = buffer.readShort();
packet.b = result23;
const result24 = buffer.readShort();
packet.bb = result24;
const array25 = buffer.readShortArray();
packet.bbb = array25;
const array26 = buffer.readShortArray();
packet.bbbb = array26;
const result27 = buffer.readInt();
packet.c = result27;
const result28 = buffer.readInt();
packet.cc = result28;
const array29 = buffer.readIntArray();
packet.ccc = array29;
const array30 = buffer.readIntArray();
packet.cccc = array30;
const result31 = buffer.readLong();
packet.d = result31;
const result32 = buffer.readLong();
packet.dd = result32;
const array33 = buffer.readLongArray();
packet.ddd = array33;
const array34 = buffer.readLongArray();
packet.dddd = array34;
const result35 = buffer.readFloat();
packet.e = result35;
const result36 = buffer.readFloat();
packet.ee = result36;
const array37 = buffer.readFloatArray();
packet.eee = array37;
const array38 = buffer.readFloatArray();
packet.eeee = array38;
const result39 = buffer.readDouble();
packet.f = result39;
const result40 = buffer.readDouble();
packet.ff = result40;
const array41 = buffer.readDoubleArray();
packet.fff = array41;
const array42 = buffer.readDoubleArray();
packet.ffff = array42;
const result43 = buffer.readBoolean();
packet.g = result43;
const result44 = buffer.readBoolean();
packet.gg = result44;
const array45 = buffer.readBooleanArray();
packet.ggg = array45;
const array46 = buffer.readBooleanArray();
packet.gggg = array46;
const result47 = buffer.readChar();
packet.h = result47;
const result48 = buffer.readChar();
packet.hh = result48;
const array49 = buffer.readCharArray();
packet.hhh = array49;
const array50 = buffer.readCharArray();
packet.hhhh = array50;
const result51 = buffer.readString();
packet.jj = result51;
const array52 = buffer.readStringArray();
packet.jjj = array52;
const result53 = buffer.readPacket(102);
packet.kk = result53;
const array54 = buffer.readPacketArray(102);
packet.kkk = array54;
const list55 = buffer.readIntList();
packet.l = list55;
const result56 = new Array<Array<Array<number>>>();
const size57 = buffer.readInt();
if (size57 > 0) {
for (let index58 = 0; index58 < size57; index58++) {
const result59 = new Array<Array<number>>();
const size60 = buffer.readInt();
if (size60 > 0) {
for (let index61 = 0; index61 < size60; index61++) {
const list62 = buffer.readIntList();
result59.push(list62);
}
}
result56.push(result59);
}
}
packet.ll = result56;
const result63 = new Array<Array<ObjectA>>();
const size64 = buffer.readInt();
if (size64 > 0) {
for (let index65 = 0; index65 < size64; index65++) {
const list66 = buffer.readPacketList(102);
result63.push(list66);
}
}
packet.lll = result63;
const list67 = buffer.readStringList();
packet.llll = list67;
const result68 = new Array<Map<number, string>>();
const size69 = buffer.readInt();
if (size69 > 0) {
for (let index70 = 0; index70 < size69; index70++) {
const map71 = buffer.readIntStringMap();
result68.push(map71);
}
}
packet.lllll = result68;
const map72 = buffer.readIntStringMap();
packet.m = map72;
const map73 = buffer.readIntPacketMap(102);
packet.mm = map73;
const result74 = new Map<ObjectA, Array<number>>();
const size75 = buffer.readInt();
if (size75 > 0) {
for (let index76 = 0; index76 < size75; index76++) {
const result77 = buffer.readPacket(102);
const list78 = buffer.readIntList();
result74.set(result77, list78);
}
}
packet.mmm = result74;
const result79 = new Map<Array<Array<ObjectA>>, Array<Array<Array<number>>>>();
const size80 = buffer.readInt();
if (size80 > 0) {
for (let index81 = 0; index81 < size80; index81++) {
const result82 = new Array<Array<ObjectA>>();
const size83 = buffer.readInt();
if (size83 > 0) {
for (let index84 = 0; index84 < size83; index84++) {
const list85 = buffer.readPacketList(102);
result82.push(list85);
}
}
const result86 = new Array<Array<Array<number>>>();
const size87 = buffer.readInt();
if (size87 > 0) {
for (let index88 = 0; index88 < size87; index88++) {
const result89 = new Array<Array<number>>();
const size90 = buffer.readInt();
if (size90 > 0) {
for (let index91 = 0; index91 < size90; index91++) {
const list92 = buffer.readIntList();
result89.push(list92);
}
}
result86.push(result89);
}
}
result79.set(result82, result86);
}
}
packet.mmmm = result79;
const result93 = new Map<Array<Map<number, string>>, Set<Map<number, string>>>();
const size94 = buffer.readInt();
if (size94 > 0) {
for (let index95 = 0; index95 < size94; index95++) {
const result96 = new Array<Map<number, string>>();
const size97 = buffer.readInt();
if (size97 > 0) {
for (let index98 = 0; index98 < size97; index98++) {
const map99 = buffer.readIntStringMap();
result96.push(map99);
}
}
const result100 = new Set<Map<number, string>>();
const size101 = buffer.readInt();
if (size101 > 0) {
for (let index102 = 0; index102 < size101; index102++) {
const map103 = buffer.readIntStringMap();
result100.add(map103);
}
}
result93.set(result96, result100);
}
}
packet.mmmmm = result93;
const set104 = buffer.readIntSet();
packet.s = set104;
const result105 = new Set<Set<Array<number>>>();
const size106 = buffer.readInt();
if (size106 > 0) {
for (let index107 = 0; index107 < size106; index107++) {
const result108 = new Set<Array<number>>();
const size109 = buffer.readInt();
if (size109 > 0) {
for (let index110 = 0; index110 < size109; index110++) {
const list111 = buffer.readIntList();
result108.add(list111);
}
}
result105.add(result108);
}
}
packet.ss = result105;
const result112 = new Set<Set<ObjectA>>();
const size113 = buffer.readInt();
if (size113 > 0) {
for (let index114 = 0; index114 < size113; index114++) {
const set115 = buffer.readPacketSet(102);
result112.add(set115);
}
}
packet.sss = result112;
const set116 = buffer.readStringSet();
packet.ssss = set116;
const result117 = new Set<Map<number, string>>();
const size118 = buffer.readInt();
if (size118 > 0) {
for (let index119 = 0; index119 < size118; index119++) {
const map120 = buffer.readIntStringMap();
result117.add(map120);
}
}
packet.sssss = result117;
if (!buffer.isReadable()) {
return packet;
}
const result121 = buffer.readInt();
packet.myCompatible = result121;
if (!buffer.isReadable()) {
return packet;
}
const result122 = buffer.readPacket(102);
packet.myObject = result122;
return packet;
}
}
export default ComplexObject;
@@ -0,0 +1,104 @@
import ObjectA from './ObjectA';
import ObjectB from './ObjectB';
// @author jaysunxiao
// @version 3.0
class NormalObject {
a: number = 0;
aaa: Array<number> | null = null;
b: number = 0;
c: number = 0;
d: number = 0;
e: number = 0;
f: number = 0;
g: boolean = false;
jj: string = '';
kk: ObjectA | null = null;
l: Array<number> | null = null;
ll: Array<number> | null = null;
lll: Array<ObjectA> | null = null;
llll: Array<string> | null = null;
m: Map<number, string> | null = null;
mm: Map<number, ObjectA> | null = null;
s: Set<number> | null = null;
ssss: Set<string> | null = null;
protocolId(): number {
return 101;
}
static write(buffer: any, packet: NormalObject | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
buffer.writeByte(packet.a);
buffer.writeByteArray(packet.aaa);
buffer.writeShort(packet.b);
buffer.writeInt(packet.c);
buffer.writeLong(packet.d);
buffer.writeFloat(packet.e);
buffer.writeDouble(packet.f);
buffer.writeBoolean(packet.g);
buffer.writeString(packet.jj);
buffer.writePacket(packet.kk, 102);
buffer.writeIntList(packet.l);
buffer.writeLongList(packet.ll);
buffer.writePacketList(packet.lll, 102);
buffer.writeStringList(packet.llll);
buffer.writeIntStringMap(packet.m);
buffer.writeIntPacketMap(packet.mm, 102);
buffer.writeIntSet(packet.s);
buffer.writeStringSet(packet.ssss);
}
static read(buffer: any): NormalObject | null {
if (!buffer.readBoolean()) {
return null;
}
const packet = new NormalObject();
const result0 = buffer.readByte();
packet.a = result0;
const array1 = buffer.readByteArray();
packet.aaa = array1;
const result2 = buffer.readShort();
packet.b = result2;
const result3 = buffer.readInt();
packet.c = result3;
const result4 = buffer.readLong();
packet.d = result4;
const result5 = buffer.readFloat();
packet.e = result5;
const result6 = buffer.readDouble();
packet.f = result6;
const result7 = buffer.readBoolean();
packet.g = result7;
const result8 = buffer.readString();
packet.jj = result8;
const result9 = buffer.readPacket(102);
packet.kk = result9;
const list10 = buffer.readIntList();
packet.l = list10;
const list11 = buffer.readLongList();
packet.ll = list11;
const list12 = buffer.readPacketList(102);
packet.lll = list12;
const list13 = buffer.readStringList();
packet.llll = list13;
const map14 = buffer.readIntStringMap();
packet.m = map14;
const map15 = buffer.readIntPacketMap(102);
packet.mm = map15;
const set16 = buffer.readIntSet();
packet.s = set16;
const set17 = buffer.readStringSet();
packet.ssss = set17;
return packet;
}
}
export default NormalObject;
@@ -0,0 +1,43 @@
import ObjectB from './ObjectB';
// @author jaysunxiao
// @version 3.0
class ObjectA {
a: number = 0;
m: Map<number, string> | null = null;
objectB: ObjectB | null = null;
protocolId(): number {
return 102;
}
static write(buffer: any, packet: ObjectA | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
buffer.writeInt(packet.a);
buffer.writeIntStringMap(packet.m);
buffer.writePacket(packet.objectB, 103);
}
static read(buffer: any): ObjectA | null {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ObjectA();
const result0 = buffer.readInt();
packet.a = result0;
const map1 = buffer.readIntStringMap();
packet.m = map1;
const result2 = buffer.readPacket(103);
packet.objectB = result2;
return packet;
}
}
export default ObjectA;
@@ -0,0 +1,34 @@
// @author jaysunxiao
// @version 3.0
class ObjectB {
flag: boolean = false;
protocolId(): number {
return 103;
}
static write(buffer: any, packet: ObjectB | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
buffer.writeBoolean(packet.flag);
}
static read(buffer: any): ObjectB | null {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ObjectB();
const result0 = buffer.readBoolean();
packet.flag = result0;
return packet;
}
}
export default ObjectB;
@@ -0,0 +1,38 @@
// @author jaysunxiao
// @version 3.0
class SimpleObject {
c: number = 0;
g: boolean = false;
protocolId(): number {
return 104;
}
static write(buffer: any, packet: SimpleObject | null) {
if (buffer.writePacketFlag(packet)) {
return;
}
if (packet === null) {
return;
}
buffer.writeInt(packet.c);
buffer.writeBoolean(packet.g);
}
static read(buffer: any): SimpleObject | null {
if (!buffer.readBoolean()) {
return null;
}
const packet = new SimpleObject();
const result0 = buffer.readInt();
packet.c = result0;
const result1 = buffer.readBoolean();
packet.g = result1;
return packet;
}
}
export default SimpleObject;