mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-23 12:24:33 +00:00
perf[protocol]: 让javascript的协议自动初始化
This commit is contained in:
@@ -103,10 +103,10 @@ public abstract class GenerateJsUtils {
|
||||
importBuilder.append(StringUtils.format("import {} from './{}/{}.js';", protocolName, path, protocolName)).append(LS);
|
||||
}
|
||||
|
||||
initProtocolBuilder.append(TAB).append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName));
|
||||
initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName)).append(LS);
|
||||
}
|
||||
|
||||
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), initProtocolBuilder.toString().trim());
|
||||
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), StringUtils.EMPTY_JSON, initProtocolBuilder.toString().trim());
|
||||
FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.js")), protocolManagerTemplate);
|
||||
}
|
||||
|
||||
@@ -185,9 +185,9 @@ public abstract class GenerateJsUtils {
|
||||
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).append("if (!buffer.isReadable()) {").append(LS);
|
||||
jsBuilder.append(TAB + TAB).append("return packet;").append(LS);
|
||||
jsBuilder.append(TAB).append("}").append(LS);
|
||||
}
|
||||
var readObject = jsSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration);
|
||||
jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
|
||||
|
||||
@@ -4,6 +4,9 @@ const protocols = new Map();
|
||||
|
||||
const ProtocolManager = {};
|
||||
|
||||
// initProtocol
|
||||
{}
|
||||
|
||||
ProtocolManager.getProtocol = function getProtocol(protocolId) {
|
||||
const protocol = protocols.get(protocolId);
|
||||
if (protocol === null) {
|
||||
@@ -26,8 +29,4 @@ ProtocolManager.read = function read(buffer) {
|
||||
return packet;
|
||||
};
|
||||
|
||||
ProtocolManager.initProtocol = function initProtocol() {
|
||||
{}
|
||||
};
|
||||
|
||||
export default ProtocolManager;
|
||||
|
||||
@@ -7,8 +7,6 @@ describe('jsProtocolTest', () => {
|
||||
it('complexObjectTest', () => {
|
||||
const data = fs.readFileSync('../resources/ComplexObject.bytes');
|
||||
|
||||
ProtocolManager.initProtocol();
|
||||
|
||||
const arrayBytes = new Uint8Array(data.length);
|
||||
data.copy(arrayBytes, 0, 0, data.length);
|
||||
|
||||
|
||||
@@ -6,7 +6,14 @@ import SimpleObject from './packet/SimpleObject.js';
|
||||
|
||||
const protocols = new Map();
|
||||
|
||||
const ProtocolManager = protocols.set(100, ComplexObject); protocols.set(101, NormalObject); protocols.set(102, ObjectA); protocols.set(103, ObjectB); protocols.set(104, SimpleObject);
|
||||
const ProtocolManager = {};
|
||||
|
||||
// initProtocol
|
||||
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);
|
||||
@@ -30,8 +37,4 @@ ProtocolManager.read = function read(buffer) {
|
||||
return packet;
|
||||
};
|
||||
|
||||
ProtocolManager.initProtocol = function initProtocol() {
|
||||
{}
|
||||
};
|
||||
|
||||
export default ProtocolManager;
|
||||
|
||||
Reference in New Issue
Block a user