perf[net]: 优化协议接收器,共用ProtocolManager来减少初始化的空间

This commit is contained in:
jaysunxiao
2021-08-08 16:00:32 +08:00
parent 702bfe5fec
commit e7067741a7
7 changed files with 63 additions and 58 deletions
@@ -134,6 +134,10 @@ public abstract class EnhanceUtils {
constructorFiled.setModifiers(Modifier.PRIVATE);
enhanceClazz.addField(constructorFiled);
CtField receiverFiled = new CtField(classPool.get(Object.class.getCanonicalName()), "receiver", enhanceClazz);
receiverFiled.setModifiers(Modifier.PRIVATE);
enhanceClazz.addField(receiverFiled);
// 定义类所包含的所有子协议成员
var allSubProtocolIds = ProtocolAnalysis.getAllSubProtocolIds(protocolId)
.stream()
@@ -142,7 +146,7 @@ public abstract class EnhanceUtils {
for (var subProtocolId : allSubProtocolIds) {
var protocolRegistrationField = new CtField(classPool.get(IProtocolRegistration.class.getCanonicalName()), getProtocolRegistrationFieldNameByProtocolId(subProtocolId), enhanceClazz);
constructorFiled.setModifiers(Modifier.PRIVATE);
protocolRegistrationField.setModifiers(Modifier.PRIVATE);
enhanceClazz.addField(protocolRegistrationField);
}
@@ -163,6 +167,11 @@ public abstract class EnhanceUtils {
protocolConstructorMethod.setBody("{return this.constructor;}");
enhanceClazz.addMethod(protocolConstructorMethod);
CtMethod receiverMethod = new CtMethod(classPool.get(Object.class.getCanonicalName()), "receiver", null, enhanceClazz);
receiverMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
receiverMethod.setBody("{return this.receiver;}");
enhanceClazz.addMethod(receiverMethod);
CtMethod moduleMethod = new CtMethod(classPool.get(byte.class.getCanonicalName()), "module", null, enhanceClazz);
moduleMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
moduleMethod.setBody("{return " + registration.module() + ";}");
@@ -30,8 +30,19 @@ public interface IProtocolRegistration {
Constructor<?> protocolConstructor();
Object read(ByteBuf buffer);
/**
* 协议接收器,回调方法,主要是存放一些额外的参数
*/
Object receiver();
/**
* 序列化
*/
void write(ByteBuf buffer, IPacket packet);
/**
* 反序列化
*/
Object read(ByteBuf buffer);
}
@@ -37,6 +37,8 @@ public class ProtocolRegistration implements IProtocolRegistration {
private byte module;
private Constructor<?> constructor;
private Object receiver;
private Field[] fields;
@@ -64,24 +66,12 @@ public class ProtocolRegistration implements IProtocolRegistration {
return constructor;
}
@Override
public Object read(ByteBuf buffer) {
if (!ByteBufUtils.readBoolean(buffer)) {
return null;
}
Object object = ReflectionUtils.newInstance(constructor);
for (int i = 0, length = fields.length; i < length; i++) {
Field field = fields[i];
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
ISerializer serializer = packetFieldRegistration.serializer();
Object fieldValue = serializer.readObject(buffer, packetFieldRegistration);
ReflectionUtils.setField(field, object, fieldValue);
}
return object;
public Object receiver() {
return receiver;
}
@Override
public void write(ByteBuf buffer, IPacket packet) {
if (packet == null) {
@@ -100,6 +90,23 @@ public class ProtocolRegistration implements IProtocolRegistration {
}
}
@Override
public Object read(ByteBuf buffer) {
if (!ByteBufUtils.readBoolean(buffer)) {
return null;
}
Object object = ReflectionUtils.newInstance(constructor);
for (int i = 0, length = fields.length; i < length; i++) {
Field field = fields[i];
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
ISerializer serializer = packetFieldRegistration.serializer();
Object fieldValue = serializer.readObject(buffer, packetFieldRegistration);
ReflectionUtils.setField(field, object, fieldValue);
}
return object;
}
public short getId() {
return id;