Skip to content

Commit

Permalink
perf[es]: use class syntax of es
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Apr 23, 2024
1 parent d2d038f commit 0cd3f44
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static void generate(GenerateOperation generateOperation) throws IOExcept
if (generateLanguages.contains(CodeLanguage.ES)) {
GenerateEsUtils.init(generateOperation);
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateEsUtils.createJsProtocolFile((ProtocolRegistration) protocolRegistration);
GenerateEsUtils.createEsProtocolFile((ProtocolRegistration) protocolRegistration);
}
GenerateEsUtils.createProtocolManager(allSortedGenerateProtocols);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public static void createProtocolManager(List<IProtocolRegistration> protocolLis
initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName)).append(LS);
}

protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), StringUtils.EMPTY_JSON, initProtocolBuilder.toString().trim());
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), initProtocolBuilder.toString().trim());
var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.mjs"));
FileUtils.writeStringToFile(file, protocolManagerTemplate, true);
logger.info("Generated ES protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
}

public static void createJsProtocolFile(ProtocolRegistration registration) throws IOException {
public static void createEsProtocolFile(ProtocolRegistration registration) throws IOException {
// 初始化index
GenerateProtocolFile.index.set(0);

Expand All @@ -124,8 +124,8 @@ public static void createJsProtocolFile(ProtocolRegistration registration) throw
var readObject = readObject(registration);

protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName
, fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
, fieldDefinition.trim(), protocolId, protocolClazzName
, writeObject.trim(), protocolClazzName, readObject.trim(), protocolClazzName);
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, protocolTemplate, true);
Expand All @@ -144,12 +144,12 @@ private static String fieldDefinition(ProtocolRegistration registration) {
var fieldName = field.getName();
// 生成注释
var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.ES);
for(var fieldNote : fieldNotes) {
for (var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS);
}
var triple = esSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(TAB)
.append(StringUtils.format("this.{} = {}; // {}", fieldName, triple.getRight(), triple.getLeft()))
.append(StringUtils.format("{} = {}; // {}", fieldName, triple.getRight(), triple.getLeft()))
.append(LS);

}
Expand All @@ -169,10 +169,10 @@ private static String writeObject(ProtocolRegistration registration) {
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
esSerializer(fieldRegistration.serializer()).writeObject(jsBuilder, "packet." + field.getName(), 1, field, fieldRegistration);
esSerializer(fieldRegistration.serializer()).writeObject(jsBuilder, "packet." + field.getName(), 2, field, fieldRegistration);
}
if (registration.isCompatible()) {
jsBuilder.append(TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS);
jsBuilder.append(TAB + TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS);
}
return jsBuilder.toString();
}
Expand All @@ -185,14 +185,14 @@ private static String readObject(ProtocolRegistration registration) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
jsBuilder.append(TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 2, field, fieldRegistration);
jsBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
jsBuilder.append(TAB).append("}").append(LS);
jsBuilder.append(TAB + TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 3, field, fieldRegistration);
jsBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
jsBuilder.append(TAB + TAB).append("}").append(LS);
continue;
}
var readObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration);
jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
var readObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 2, field, fieldRegistration);
jsBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
return jsBuilder.toString();
}
Expand Down
41 changes: 21 additions & 20 deletions protocol/src/main/resources/es/ProtocolManagerTemplate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

const protocols = new Map();

const ProtocolManager = {};

// initProtocol
{}

ProtocolManager.getProtocol = function getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
class ProtocolManager {
static getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
}
return protocol;
}

static write(buffer, packet) {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
}
return protocol;
};

ProtocolManager.write = function write(buffer, packet) {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
};
static read(buffer) {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
}
}

ProtocolManager.read = function read(buffer) {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
};

export default ProtocolManager;
46 changes: 24 additions & 22 deletions protocol/src/main/resources/es/ProtocolTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
{}
const {} = function() {
class {} {
{}
};

{}.prototype.protocolId = function() {
return {};
};
static PROTOCOL_ID = {};

{}.write = function(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
protocolId() {
return {}.PROTOCOL_ID;
}
{}
};

{}.read = function(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
static write(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
}
{}
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new {}();
{}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);

static read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new {}();
{}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
return packet;
};

}
export default {};
Loading

0 comments on commit 0cd3f44

Please sign in to comment.