From 6acd609cbfa3f001847be1e95bbed3ea4d6f0950 Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 12 Sep 2022 09:20:15 +0800 Subject: [PATCH] =?UTF-8?q?perf[net]:=20=E6=A0=A1=E9=AA=8C=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8D=8F=E8=AE=AE=E5=8F=AA=E8=83=BD=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E8=A2=AB@PacketReceiver=E6=A0=87=E6=B3=A8?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zfoo/net/router/route/PacketBus.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/net/src/main/java/com/zfoo/net/router/route/PacketBus.java b/net/src/main/java/com/zfoo/net/router/route/PacketBus.java index 86d53c3b..0c10b24a 100644 --- a/net/src/main/java/com/zfoo/net/router/route/PacketBus.java +++ b/net/src/main/java/com/zfoo/net/router/route/PacketBus.java @@ -48,12 +48,6 @@ public abstract class PacketBus { private static final Logger logger = LoggerFactory.getLogger(PacketBus.class); - /** - * 客户端和服务端都有接受packet的方法,packetReceiverList对应的就是包的接收方法,将receiver注册到IProtocolRegistration - */ - public static final IProtocolRegistration[] packetReceiverList = ProtocolManager.protocols; - - /** * 正常消息的接收 *

@@ -61,7 +55,8 @@ public abstract class PacketBus { * 接收者同时只能处理一个session的一个包,同一个发送者发送过来的包排队处理 */ public static void submit(Session session, IPacket packet, IAttachment attachment) { - var packetReceiver = (IPacketReceiver) packetReceiverList[packet.protocolId()].receiver(); + // 客户端和服务端都有接受packet的方法,packetReceiverList对应的就是包的接收方法,将receiver注册到IProtocolRegistration + var packetReceiver = (IPacketReceiver) ProtocolManager.getProtocol(packet.protocolId()).receiver(); if (packetReceiver == null) { throw new RuntimeException(StringUtils.format("no any packetReceiverDefinition found for this [packet:{}]", packet.getClass().getName())); } @@ -127,12 +122,15 @@ public abstract class PacketBus { try { var protocolId = ProtocolAnalysis.getProtocolIdByClass(packetClazz); - var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz); - var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition); // 将receiver注册到IProtocolRegistration - var protocolRegistration = packetReceiverList[protocolId]; + var protocolRegistration = ProtocolManager.getProtocol(protocolId); AssertionUtils.notNull(protocolRegistration, "协议类[class:{}][protocolId:{}]没有注册", packetClazz.getSimpleName(), protocolId); + AssertionUtils.isNull(protocolRegistration.receiver(), "协议类[class:{}]被重复接收[at{}],一个协议只能对应一个被@PacketReceiver标注的方法" + , packetClazz.getSimpleName(), packetClazz.getSimpleName()); + + var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz); + var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition); var receiverField = ReflectionUtils.getFieldByNameInPOJOClass(protocolRegistration.getClass(), "receiver"); ReflectionUtils.makeAccessible(receiverField);