mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-15 20:16:02 +00:00
perf[net]: 优化协议接收器,共用ProtocolManager来减少初始化的空间
This commit is contained in:
1 parent
702bfe5fec
commit
e7067741a7
7 files changed
+63
-58
No files matched your search
@@ -26,6 +26,7 @@ import com.zfoo.net.session.model.Session;
|
|||||||
import com.zfoo.protocol.IPacket;
|
import com.zfoo.protocol.IPacket;
|
||||||
import com.zfoo.protocol.ProtocolManager;
|
import com.zfoo.protocol.ProtocolManager;
|
||||||
import com.zfoo.protocol.collection.ArrayUtils;
|
import com.zfoo.protocol.collection.ArrayUtils;
|
||||||
|
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||||
import com.zfoo.protocol.util.AssertionUtils;
|
import com.zfoo.protocol.util.AssertionUtils;
|
||||||
import com.zfoo.protocol.util.ReflectionUtils;
|
import com.zfoo.protocol.util.ReflectionUtils;
|
||||||
import com.zfoo.protocol.util.StringUtils;
|
import com.zfoo.protocol.util.StringUtils;
|
||||||
@@ -43,9 +44,10 @@ public abstract class PacketBus {
|
|||||||
private static final Logger logger = LoggerFactory.getLogger(PacketBus.class);
|
private static final Logger logger = LoggerFactory.getLogger(PacketBus.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 客户端和服务端都有接受packet的方法,packetReceiverList对应的就是包的接收方法
|
* 客户端和服务端都有接受packet的方法,packetReceiverList对应的就是包的接收方法,将receiver注册到IProtocolRegistration
|
||||||
*/
|
*/
|
||||||
private static final IPacketReceiver[] packetReceiverList = new IPacketReceiver[ProtocolManager.MAX_PROTOCOL_NUM];
|
public static final IProtocolRegistration[] packetReceiverList = ProtocolManager.protocols;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 正常消息的接收
|
* 正常消息的接收
|
||||||
@@ -54,7 +56,7 @@ public abstract class PacketBus {
|
|||||||
* 接收者同时只能处理一个session的一个包,同一个发送者发送过来的包排队处理
|
* 接收者同时只能处理一个session的一个包,同一个发送者发送过来的包排队处理
|
||||||
*/
|
*/
|
||||||
public static void submit(Session session, IPacket packet, IPacketAttachment packetAttachment) {
|
public static void submit(Session session, IPacket packet, IPacketAttachment packetAttachment) {
|
||||||
var packetReceiver = packetReceiverList[packet.protocolId()];
|
var packetReceiver = (IPacketReceiver) packetReceiverList[packet.protocolId()].receiver();
|
||||||
if (packetReceiver == null) {
|
if (packetReceiver == null) {
|
||||||
throw new RuntimeException(StringUtils.format("no any packetReceiverDefinition found for this [packet:{}]", packet.getClass().getName()));
|
throw new RuntimeException(StringUtils.format("no any packetReceiverDefinition found for this [packet:{}]", packet.getClass().getName()));
|
||||||
}
|
}
|
||||||
@@ -124,7 +126,12 @@ public abstract class PacketBus {
|
|||||||
var protocolId = (short) protocolIdField.get(null);
|
var protocolId = (short) protocolIdField.get(null);
|
||||||
var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz);
|
var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz);
|
||||||
var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition);
|
var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition);
|
||||||
packetReceiverList[protocolId] = enhanceReceiverDefinition;
|
|
||||||
|
// 将receiver注册到IProtocolRegistration
|
||||||
|
var protocolRegistration = packetReceiverList[protocolId];
|
||||||
|
var receiverField = ReflectionUtils.getFieldByNameInPOJOClass(protocolRegistration.getClass(), "receiver");
|
||||||
|
ReflectionUtils.makeAccessible(receiverField);
|
||||||
|
ReflectionUtils.setField(receiverField, protocolRegistration, enhanceReceiverDefinition);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
throw new RuntimeException(t);
|
throw new RuntimeException(t);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
package com.zfoo.net.packet.service;
|
package com.zfoo.net.packet.service;
|
||||||
|
|
||||||
import com.zfoo.net.NetContext;
|
import com.zfoo.net.NetContext;
|
||||||
|
import com.zfoo.net.dispatcher.manager.PacketBus;
|
||||||
import com.zfoo.net.packet.model.DecodedPacketInfo;
|
import com.zfoo.net.packet.model.DecodedPacketInfo;
|
||||||
import com.zfoo.net.packet.model.IPacketAttachment;
|
import com.zfoo.net.packet.model.IPacketAttachment;
|
||||||
import com.zfoo.protocol.IPacket;
|
import com.zfoo.protocol.IPacket;
|
||||||
@@ -104,6 +105,13 @@ public class PacketService implements IPacketService {
|
|||||||
logger.error(ExceptionUtils.getMessage(e));
|
logger.error(ExceptionUtils.getMessage(e));
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册协议接收器
|
||||||
|
var beanNames = applicationContext.getBeanDefinitionNames();
|
||||||
|
for (var beanName : beanNames) {
|
||||||
|
var bean = applicationContext.getBean(beanName);
|
||||||
|
PacketBus.registerPacketReceiverDefinition(bean);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -60,11 +60,6 @@ public class NetDefinitionParser implements BeanDefinitionParser {
|
|||||||
builder.addPropertyReference("localConfig", NetConfig.class.getCanonicalName());
|
builder.addPropertyReference("localConfig", NetConfig.class.getCanonicalName());
|
||||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
||||||
|
|
||||||
// 注册NetProcessor
|
|
||||||
clazz = NetProcessor.class;
|
|
||||||
builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
|
||||||
parserContext.getRegistry().registerBeanDefinition(clazz.getCanonicalName(), builder.getBeanDefinition());
|
|
||||||
|
|
||||||
// 注册ProtocolManager
|
// 注册ProtocolManager
|
||||||
clazz = ProtocolManager.class;
|
clazz = ProtocolManager.class;
|
||||||
builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
builder = BeanDefinitionBuilder.rootBeanDefinition(clazz);
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2020 The zfoo Authors
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
|
||||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.zfoo.net.schema;
|
|
||||||
|
|
||||||
import com.zfoo.net.dispatcher.manager.PacketBus;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author jaysunxiao
|
|
||||||
* @version 3.0
|
|
||||||
*/
|
|
||||||
public class NetProcessor implements BeanPostProcessor {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
||||||
PacketBus.registerPacketReceiverDefinition(bean);
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -134,6 +134,10 @@ public abstract class EnhanceUtils {
|
|||||||
constructorFiled.setModifiers(Modifier.PRIVATE);
|
constructorFiled.setModifiers(Modifier.PRIVATE);
|
||||||
enhanceClazz.addField(constructorFiled);
|
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)
|
var allSubProtocolIds = ProtocolAnalysis.getAllSubProtocolIds(protocolId)
|
||||||
.stream()
|
.stream()
|
||||||
@@ -142,7 +146,7 @@ public abstract class EnhanceUtils {
|
|||||||
|
|
||||||
for (var subProtocolId : allSubProtocolIds) {
|
for (var subProtocolId : allSubProtocolIds) {
|
||||||
var protocolRegistrationField = new CtField(classPool.get(IProtocolRegistration.class.getCanonicalName()), getProtocolRegistrationFieldNameByProtocolId(subProtocolId), enhanceClazz);
|
var protocolRegistrationField = new CtField(classPool.get(IProtocolRegistration.class.getCanonicalName()), getProtocolRegistrationFieldNameByProtocolId(subProtocolId), enhanceClazz);
|
||||||
constructorFiled.setModifiers(Modifier.PRIVATE);
|
protocolRegistrationField.setModifiers(Modifier.PRIVATE);
|
||||||
enhanceClazz.addField(protocolRegistrationField);
|
enhanceClazz.addField(protocolRegistrationField);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,6 +167,11 @@ public abstract class EnhanceUtils {
|
|||||||
protocolConstructorMethod.setBody("{return this.constructor;}");
|
protocolConstructorMethod.setBody("{return this.constructor;}");
|
||||||
enhanceClazz.addMethod(protocolConstructorMethod);
|
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);
|
CtMethod moduleMethod = new CtMethod(classPool.get(byte.class.getCanonicalName()), "module", null, enhanceClazz);
|
||||||
moduleMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
moduleMethod.setModifiers(Modifier.PUBLIC + Modifier.FINAL);
|
||||||
moduleMethod.setBody("{return " + registration.module() + ";}");
|
moduleMethod.setBody("{return " + registration.module() + ";}");
|
||||||
|
|||||||
@@ -30,8 +30,19 @@ public interface IProtocolRegistration {
|
|||||||
|
|
||||||
Constructor<?> protocolConstructor();
|
Constructor<?> protocolConstructor();
|
||||||
|
|
||||||
Object read(ByteBuf buffer);
|
/**
|
||||||
|
* 协议接收器,回调方法,主要是存放一些额外的参数
|
||||||
|
*/
|
||||||
|
Object receiver();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列化
|
||||||
|
*/
|
||||||
void write(ByteBuf buffer, IPacket packet);
|
void write(ByteBuf buffer, IPacket packet);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反序列化
|
||||||
|
*/
|
||||||
|
Object read(ByteBuf buffer);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -37,6 +37,8 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
|||||||
private byte module;
|
private byte module;
|
||||||
private Constructor<?> constructor;
|
private Constructor<?> constructor;
|
||||||
|
|
||||||
|
private Object receiver;
|
||||||
|
|
||||||
private Field[] fields;
|
private Field[] fields;
|
||||||
|
|
||||||
|
|
||||||
@@ -64,24 +66,12 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
|||||||
return constructor;
|
return constructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object read(ByteBuf buffer) {
|
public Object receiver() {
|
||||||
if (!ByteBufUtils.readBoolean(buffer)) {
|
return receiver;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(ByteBuf buffer, IPacket packet) {
|
public void write(ByteBuf buffer, IPacket packet) {
|
||||||
if (packet == null) {
|
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() {
|
public short getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
Reference in new issue
Block a user