perf[protocol]: 使用模板生成协议

This commit is contained in:
jaysunxiao
2022-05-16 20:54:14 +08:00
parent 6a8aa811a0
commit 29ee2b0d90
30 changed files with 307 additions and 294 deletions
@@ -116,7 +116,9 @@ public abstract class GenerateProtocolFile {
// 生成Javascript协议
if (generateLanguages.contains(CodeLanguage.JavaScript)) {
GenerateJsUtils.init(generateOperation);
allSortedGenerateProtocols.forEach(it -> GenerateJsUtils.createJsProtocolFile((ProtocolRegistration) it));
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateJsUtils.createJsProtocolFile((ProtocolRegistration) protocolRegistration);
}
GenerateJsUtils.createProtocolManager(allSortedGenerateProtocols);
}
@@ -219,12 +219,10 @@ public abstract class GenerateCppUtils {
}
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];
@@ -232,7 +230,6 @@ public abstract class GenerateCppUtils {
filedList.add(propertyTypeAndName);
}
// ValueOf()方法
var valueOfParams = filedList.stream()
.map(it -> StringUtils.format("{} {}", it.getKey(), it.getValue()))
@@ -241,7 +238,6 @@ public abstract class GenerateCppUtils {
var cppBuilder = new StringBuilder();
filedList.forEach(it -> cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", it.getValue(), it.getValue())).append(LS));
return new Pair<>(valueOfParamsStr, cppBuilder.toString());
}
@@ -17,9 +17,9 @@ import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.generate.GenerateProtocolDocument;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.reflect.*;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FileUtils;
@@ -28,8 +28,10 @@ import com.zfoo.protocol.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.zfoo.protocol.util.FileUtils.LS;
@@ -78,172 +80,106 @@ public abstract class GenerateJsUtils {
}
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("js/buffer/ByteBuffer.js"
, "js/buffer/long.js"
, "js/buffer/longbits.js");
var list = List.of("js/buffer/ByteBuffer.js", "js/buffer/long.js", "js/buffer/longbits.js");
for (var fileName : list) {
var fileInputStream = ClassUtils.getFileFromClassPath(fileName);
var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "js/")));
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}
// 生成ProtocolManager.js文件
var jsBuilder = new StringBuilder();
var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("js/ProtocolManagerTemplate.js")));
protocolList.stream()
.filter(it -> Objects.nonNull(it))
.forEach(it -> {
var name = it.protocolConstructor().getDeclaringClass().getSimpleName();
var path = GenerateProtocolPath.getProtocolPath(it.protocolId());
if (StringUtils.isBlank(path)) {
jsBuilder.append(StringUtils.format("import {} from './{}.js';", name, name)).append(LS);
} else {
jsBuilder.append(StringUtils.format("import {} from './{}/{}.js';", name, path, name)).append(LS);
}
});
var importBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
var protocolId = protocol.protocolId();
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
var path = GenerateProtocolPath.getProtocolPath(protocol.protocolId());
if (StringUtils.isBlank(path)) {
importBuilder.append(StringUtils.format("import {} from './{}.js';", protocolName, protocolName)).append(LS);
} else {
importBuilder.append(StringUtils.format("import {} from './{}/{}.js';", protocolName, path, protocolName)).append(LS);
}
jsBuilder.append(LS).append(LS);
initProtocolBuilder.append(TAB).append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName));
}
var protocolManagerStr = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("js/ProtocolManager.js")));
jsBuilder.append(protocolManagerStr);
jsBuilder.append("ProtocolManager.initProtocol = function initProtocol() {").append(LS);
protocolList.stream().filter(it -> Objects.nonNull(it))
.forEach(it -> jsBuilder.append(TAB).append(StringUtils.format("protocols.set({}, {});", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS));
jsBuilder.append("};").append(LS + LS);
jsBuilder.append("export default ProtocolManager;").append(LS);
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.js")), jsBuilder.toString());
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), initProtocolBuilder.toString().trim());
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.js")), protocolManagerTemplate);
}
public static void createJsProtocolFile(ProtocolRegistration registration) {
public static void createJsProtocolFile(ProtocolRegistration registration) throws IOException {
// 初始化index
GenerateProtocolFile.index.set(0);
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("js/ProtocolTemplate.js")));
// export object
jsBuilder.append(exportFunction(registration));
var docTitle = docTitle(registration);
var valueOfMethod = valueOfMethod(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
// protocolId method
jsBuilder.append(protocolIdFunction(registration));
// writeObject method
jsBuilder.append(writeObject(registration));
// readObject method
jsBuilder.append(readObject(registration));
jsBuilder.append(LS).append(StringUtils.format("export default {};", protocolClazzName)).append(LS);
var protocolOutputPath = StringUtils.format("{}/{}/{}.js"
, protocolOutputRootPath
, GenerateProtocolPath.getProtocolPath(protocolId)
, protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), jsBuilder.toString());
protocolTemplate = StringUtils.format(protocolTemplate, docTitle, protocolClazzName
, valueOfMethod.getKey().trim(), valueOfMethod.getValue().trim(), protocolClazzName, protocolId, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
var protocolOutputPath = StringUtils.format("{}/{}/{}.js", protocolOutputRootPath
, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate);
}
private static String exportFunction(ProtocolRegistration registration) {
private static String docTitle(ProtocolRegistration registration) {
var protocolId = registration.getId();
var fields = registration.getFields();
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId);
var docTitle = protocolDocument.getKey();
return docTitle;
}
private static Pair<String, String> valueOfMethod(ProtocolRegistration registration) {
var protocolId = registration.getId();
var fields = registration.getFields();
var fieldValueOf = StringUtils.joinWith(", ", Arrays.stream(fields).map(it -> it.getName()).collect(Collectors.toList()).toArray());
var fieldDefinitionBuilder = new StringBuilder();
var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId);
var docFieldMap = protocolDocument.getValue();
var jsBuilder = new StringBuilder();
if (StringUtils.isNotBlank(docTitle)) {
jsBuilder.append(docTitle).append(LS);
}
jsBuilder.append(StringUtils.format("const {} = function(", protocolClazzName));
jsBuilder.append(StringUtils.joinWith(", ", Arrays.stream(fields).map(it -> it.getName()).collect(Collectors.toList()).toArray()))
.append(") {")
.append(LS);
for (var field : fields) {
var propertyName = field.getName();
// 生成注释
var doc = docFieldMap.get(propertyName);
if (StringUtils.isNotBlank(doc)) {
Arrays.stream(doc.split(LS)).forEach(it -> jsBuilder.append(TAB).append(it).append(LS));
Arrays.stream(doc.split(LS)).forEach(it -> fieldDefinitionBuilder.append(TAB).append(it).append(LS));
}
jsBuilder.append(TAB)
fieldDefinitionBuilder.append(TAB)
.append(StringUtils.format("this.{} = {};", propertyName, propertyName))
.append(" // ").append(field.getGenericType().getTypeName())// 生成类型的注释
.append(LS);
}
jsBuilder.append("};").append(LS).append(LS);
return jsBuilder.toString();
return new Pair<>(fieldValueOf, fieldDefinitionBuilder.toString());
}
private static String protocolIdFunction(ProtocolRegistration registration) {
var protocolId = registration.getId();
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
jsBuilder.append(StringUtils.format("{}.prototype.protocolId = function() {", protocolClazzName)).append(LS);
jsBuilder.append(TAB).append(StringUtils.format("return {};", protocolId)).append(LS);
jsBuilder.append("};").append(LS).append(LS);
return jsBuilder.toString();
}
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
jsBuilder.append(StringUtils.format("{}.write = function(buffer, packet) {", protocolClazzName)).append(LS);
jsBuilder.append(TAB).append("if (buffer.writePacketFlag(packet)) {").append(LS);
jsBuilder.append(TAB + TAB).append("return;").append(LS);
jsBuilder.append(TAB).append("}").append(LS);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
IFieldRegistration fieldRegistration = fieldRegistrations[i];
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
jsSerializer(fieldRegistration.serializer()).writeObject(jsBuilder, "packet." + field.getName(), 1, field, fieldRegistration);
}
jsBuilder.append("};").append(LS).append(LS);
return jsBuilder.toString();
}
private static String readObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName();
var jsBuilder = new StringBuilder();
jsBuilder.append(StringUtils.format("{}.read = function(buffer) {", protocolClazzName)).append(LS);
jsBuilder.append(TAB).append("if (!buffer.readBoolean()) {").append(LS);
jsBuilder.append(TAB + TAB).append("return null;").append(LS);
jsBuilder.append(TAB).append("}").append(LS);
jsBuilder.append(TAB).append(StringUtils.format("const packet = new {}();", protocolClazzName)).append(LS);
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
@@ -251,13 +187,6 @@ public abstract class GenerateJsUtils {
var readObject = jsSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration);
jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
jsBuilder.append(TAB).append("return packet;").append(LS);
jsBuilder.append("};").append(LS);
return jsBuilder.toString();
}
}
@@ -24,7 +24,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -91,8 +90,6 @@ public class JsArraySerializer implements IJsSerializer {
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -37,7 +36,6 @@ public class JsBooleanSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS);
return result;
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -37,7 +36,6 @@ public class JsByteSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readByte();", result)).append(LS);
return result;
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -37,7 +36,6 @@ public class JsDoubleSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readDouble();", result)).append(LS);
return result;
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -37,7 +36,6 @@ public class JsFloatSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readFloat();", result)).append(LS);
return result;
@@ -36,7 +36,6 @@ public class JsIntSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readInt();", result)).append(LS);
return result;
@@ -90,8 +90,6 @@ public class JsListSerializer implements IJsSerializer {
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -36,7 +36,6 @@ public class JsLongSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readLong();", result)).append(LS);
return result;
@@ -100,7 +100,6 @@ public class JsMapSerializer implements IJsSerializer {
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -91,7 +91,6 @@ public class JsSetSerializer implements IJsSerializer {
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -21,7 +21,6 @@ import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author jaysunxiao
* @version 3.0
@@ -37,7 +36,6 @@ public class JsShortSerializer implements IJsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readShort();", result)).append(LS);
return result;
@@ -1,3 +1,6 @@
{
}
const protocols = new Map();
const ProtocolManager = {};
@@ -24,3 +27,9 @@ ProtocolManager.read = function read(buffer) {
return packet;
};
ProtocolManager.initProtocol = function initProtocol() {
{
}
};
export default ProtocolManager;
@@ -0,0 +1,39 @@
{
}
const {} = function ({}) {
{
}
};
{
}
.
prototype.protocolId = function () {
return {};
};
{
}
.
write = function (buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
{
}
};
{
}
.
read = function (buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new {}();
{
}
return packet;
};
export default {};
+30 -30
View File
@@ -417,7 +417,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeBool(value);
}
}
@@ -439,7 +439,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeBool(value);
}
}
@@ -460,7 +460,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeBool(value);
}
}
@@ -482,7 +482,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeByte(value);
}
}
@@ -504,7 +504,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeByte(value);
}
}
@@ -525,7 +525,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeByte(value);
}
}
@@ -547,7 +547,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeShort(value);
}
}
@@ -568,7 +568,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeShort(value);
}
}
@@ -589,7 +589,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeShort(value);
}
}
@@ -611,7 +611,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeInt(value);
}
}
@@ -632,7 +632,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeInt(value);
}
}
@@ -653,7 +653,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeInt(value);
}
}
@@ -675,7 +675,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeLong(value);
}
}
@@ -696,7 +696,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeLong(value);
}
}
@@ -717,7 +717,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeLong(value);
}
}
@@ -739,7 +739,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeFloat(value);
}
}
@@ -760,7 +760,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeFloat(value);
}
}
@@ -781,7 +781,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeFloat(value);
}
}
@@ -803,7 +803,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeDouble(value);
}
}
@@ -824,7 +824,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeDouble(value);
}
}
@@ -845,7 +845,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeDouble(value);
}
}
@@ -867,7 +867,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeChar(value);
}
}
@@ -888,7 +888,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeChar(value);
}
}
@@ -909,7 +909,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeChar(value);
}
}
@@ -931,7 +931,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writeString(value);
}
}
@@ -952,7 +952,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writeString(value);
}
}
@@ -973,7 +973,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writeString(value);
}
}
@@ -1282,7 +1282,7 @@ namespace zfoo {
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
for (const auto &value : array) {
writePacket((IPacket *) &value, protocolId);
}
}
@@ -1307,7 +1307,7 @@ namespace zfoo {
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
for (const auto &value : list) {
writePacket((IPacket *) &value, protocolId);
}
}
@@ -1332,7 +1332,7 @@ namespace zfoo {
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
for (const auto &value : set) {
writePacket((IPacket *) &value, protocolId);
}
}
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/app'
]
};
@@ -1,11 +1,11 @@
import ByteBuffer from './jsProtocol/buffer/ByteBuffer.js';
import ProtocolManager from './jsProtocol/ProtocolManager.js';
import ByteBuffer from './zfoojs/buffer/ByteBuffer.js';
import ProtocolManager from './zfoojs/ProtocolManager.js';
const fs = require('fs');
describe('jsProtocolTest', () => {
it('complexObjectTest', () => {
const data = fs.readFileSync('C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
const data = fs.readFileSync('resources\\ComplexObject.bytes');
ProtocolManager.initProtocol();
+51
View File
@@ -0,0 +1,51 @@
{
"name": "zfoo",
"version": "0.0.0",
"description": "This is a common config for all projects",
"author": "jaysunxiao@gmail.com",
"license": "MIT",
"scripts": {
},
"dependencies": {
"vue": "2.6.11",
"vue-i18n": "8.12.0",
"vue-meta": "^2.2.2",
"vue-router": "3.1.6",
"vuelidate": "^0.7.5",
"vuetify": "^2.3.3",
"vuex": "3.1.3",
"vuex-router-sync": "^5.0.0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.3.0",
"@vue/cli-plugin-eslint": "~4.3.0",
"@vue/cli-plugin-pwa": "^4.3.1",
"@vue/cli-service": "~4.3.0",
"@vue/cli-plugin-unit-jest": "~4.3.0",
"@vue/eslint-config-standard": "^4.0.0",
"@vue/test-utils": "1.0.0-beta.33",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "23.6.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.13.0",
"sass": "^1.19.0",
"sass-loader": "^8.0.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"mockjs": "1.0.0",
"vue-template-compiler": "2.6.11",
"vue-cli-plugin-vuetify": "^2.0.6",
"vuetify-loader": "^1.5.0"
},
"engines": {
"node": ">=8.9",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
@@ -4,10 +4,13 @@ import ObjectA from './packet/ObjectA.js';
import ObjectB from './packet/ObjectB.js';
import SimpleObject from './packet/SimpleObject.js';
const protocols = new Map();
const ProtocolManager = {};
const ProtocolManager = protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
ProtocolManager.getProtocol = function getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
@@ -17,26 +20,23 @@ ProtocolManager.getProtocol = function getProtocol(protocolId) {
return protocol;
};
ProtocolManager.write = function write(byteBuffer, packet) {
ProtocolManager.write = function write(buffer, packet) {
const protocolId = packet.protocolId();
byteBuffer.writeShort(protocolId);
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(byteBuffer, packet);
protocol.write(buffer, packet);
};
ProtocolManager.read = function read(byteBuffer) {
const protocolId = byteBuffer.readShort();
ProtocolManager.read = function read(buffer) {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(byteBuffer);
const packet = protocol.read(buffer);
return packet;
};
ProtocolManager.initProtocol = function initProtocol() {
protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
{
}
};
export default ProtocolManager;
@@ -1,4 +1,4 @@
import { readInt64, writeInt64 } from './longbits.js';
import {readInt64, writeInt64} from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
@@ -19,6 +19,7 @@ const minInt = -2147483648;
const util = require('util');
const encoder = new util.TextEncoder('utf-8');
const decoder = new util.TextDecoder('utf-8');
// 在js中long可以支持的最大值
// const maxLong = 9007199254740992;
// const minLong = -9007199254740992;
@@ -343,28 +344,28 @@ const ByteBuffer = function() {
return flag;
};
this.writePacket = function(value, protocolId) {
this.writePacket = function (packet, protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, value);
protocolRegistration.write(this, packet);
};
this.readPacket = function(protocolId) {
this.readPacket = function (protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
this.writeBooleanArray = function(value) {
if (value === null) {
this.writeBooleanArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
});
}
};
this.readBooleanArray = function() {
this.readBooleanArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -375,18 +376,18 @@ const ByteBuffer = function() {
return array;
};
this.writeByteArray = function(value) {
if (value === null) {
this.writeByteArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeByte(element);
});
}
};
this.readByteArray = function() {
this.readByteArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -397,18 +398,18 @@ const ByteBuffer = function() {
return array;
};
this.writeShortArray = function(value) {
if (value === null) {
this.writeShortArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeShort(element);
});
}
};
this.readShortArray = function() {
this.readShortArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -419,18 +420,18 @@ const ByteBuffer = function() {
return array;
};
this.writeIntArray = function(value) {
if (value === null) {
this.writeIntArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeInt(element);
});
}
};
this.readIntArray = function() {
this.readIntArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -441,18 +442,18 @@ const ByteBuffer = function() {
return array;
};
this.writeLongArray = function(value) {
if (value === null) {
this.writeLongArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeLong(element);
});
}
};
this.readLongArray = function() {
this.readLongArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -463,18 +464,18 @@ const ByteBuffer = function() {
return array;
};
this.writeFloatArray = function(value) {
if (value === null) {
this.writeFloatArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeFloat(element);
});
}
};
this.readFloatArray = function() {
this.readFloatArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -485,18 +486,18 @@ const ByteBuffer = function() {
return array;
};
this.writeDoubleArray = function(value) {
if (value === null) {
this.writeDoubleArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeDouble(element);
});
}
};
this.readDoubleArray = function() {
this.readDoubleArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -507,18 +508,18 @@ const ByteBuffer = function() {
return array;
};
this.writeStringArray = function(value) {
if (value === null) {
this.writeStringArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeString(element);
});
}
};
this.readStringArray = function() {
this.readStringArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -529,18 +530,18 @@ const ByteBuffer = function() {
return array;
};
this.writeCharArray = function(value) {
if (value === null) {
this.writeCharArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
this.writeChar(element);
});
}
};
this.readCharArray = function() {
this.readCharArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
@@ -551,13 +552,13 @@ const ByteBuffer = function() {
return array;
};
this.writePacketArray = function(value, protocolId) {
if (value === null) {
this.writePacketArray = function (array, protocolId) {
if (array === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.length);
value.forEach(element => {
this.writeInt(array.length);
array.forEach(element => {
protocolRegistration.write(this, element);
});
}
@@ -575,12 +576,12 @@ const ByteBuffer = function() {
return array;
};
this.writeIntIntMap = function(value) {
if (value === null) {
this.writeIntIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeInt(value);
});
@@ -600,12 +601,12 @@ const ByteBuffer = function() {
return map;
};
this.writeIntLongMap = function(value) {
if (value === null) {
this.writeIntLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeLong(value);
});
@@ -625,12 +626,12 @@ const ByteBuffer = function() {
return map;
};
this.writeIntStringMap = function(value) {
if (value === null) {
this.writeIntStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeString(value);
});
@@ -650,13 +651,13 @@ const ByteBuffer = function() {
return map;
};
this.writeIntPacketMap = function(value, protocolId) {
if (value === null) {
this.writeIntPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
protocolRegistration.write(this, value);
});
@@ -677,12 +678,12 @@ const ByteBuffer = function() {
return map;
};
this.writeLongIntMap = function(value) {
if (value === null) {
this.writeLongIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeInt(value);
});
@@ -702,12 +703,12 @@ const ByteBuffer = function() {
return map;
};
this.writeLongLongMap = function(value) {
if (value === null) {
this.writeLongLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeLong(value);
});
@@ -727,12 +728,12 @@ const ByteBuffer = function() {
return map;
};
this.writeLongStringMap = function(value) {
if (value === null) {
this.writeLongStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeString(value);
});
@@ -752,13 +753,13 @@ const ByteBuffer = function() {
return map;
};
this.writeLongPacketMap = function(value, protocolId) {
if (value === null) {
this.writeLongPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
protocolRegistration.write(this, value);
});
@@ -779,12 +780,12 @@ const ByteBuffer = function() {
return map;
};
this.writeStringIntMap = function(value) {
if (value === null) {
this.writeStringIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeInt(value);
});
@@ -804,12 +805,12 @@ const ByteBuffer = function() {
return map;
};
this.writeStringLongMap = function(value) {
if (value === null) {
this.writeStringLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeLong(value);
});
@@ -829,12 +830,12 @@ const ByteBuffer = function() {
return map;
};
this.writeStringStringMap = function(value) {
if (value === null) {
this.writeStringStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeString(value);
});
@@ -854,13 +855,13 @@ const ByteBuffer = function() {
return map;
};
this.writeStringPacketMap = function(value, protocolId) {
if (value === null) {
this.writeStringPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(value.size);
value.forEach((value, key) => {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
protocolRegistration.write(this, value);
});