From 1e3f3654d41b4d0f751413da1ab180a864afab85 Mon Sep 17 00:00:00 2001 From: meiwei <309921330@qq.com> Date: Mon, 1 Aug 2022 19:53:22 +0800 Subject: [PATCH] =?UTF-8?q?perf[net]:=20=E4=BC=98=E5=8C=96=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E4=B8=BA=E6=8A=9B=E5=87=BA=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tcp/model/ServerExceptionEvent.java | 72 ++++++++++++++++ .../com/zfoo/net/packet/common/ErrorCode.java | 86 ------------------- .../main/java/com/zfoo/net/router/Router.java | 18 +--- .../exception/ErrorRequestException.java | 45 ---------- .../com/zfoo/net/router/route/PacketBus.java | 5 +- 5 files changed, 76 insertions(+), 150 deletions(-) create mode 100644 net/src/main/java/com/zfoo/net/core/tcp/model/ServerExceptionEvent.java delete mode 100644 net/src/main/java/com/zfoo/net/packet/common/ErrorCode.java delete mode 100644 net/src/main/java/com/zfoo/net/router/exception/ErrorRequestException.java diff --git a/net/src/main/java/com/zfoo/net/core/tcp/model/ServerExceptionEvent.java b/net/src/main/java/com/zfoo/net/core/tcp/model/ServerExceptionEvent.java new file mode 100644 index 00000000..89e5e2ac --- /dev/null +++ b/net/src/main/java/com/zfoo/net/core/tcp/model/ServerExceptionEvent.java @@ -0,0 +1,72 @@ +/* + * 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.core.tcp.model; + +import com.zfoo.event.model.event.IEvent; +import com.zfoo.net.router.attachment.IAttachment; +import com.zfoo.net.session.model.Session; +import com.zfoo.protocol.IPacket; + +/** + * @author meiw + * @version 3.0 + */ +public class ServerExceptionEvent implements IEvent { + + private Session session; + private IPacket packet; + private IAttachment attachment; + private Exception exception; + + public static ServerExceptionEvent valueOf(Session session, IPacket packet, IAttachment attachment, Exception exception) { + var event = new ServerExceptionEvent(); + event.session = session; + event.packet = packet; + event.attachment = attachment; + event.exception = exception; + return event; + } + + public Session getSession() { + return session; + } + + public void setSession(Session session) { + this.session = session; + } + + public IPacket getPacket() { + return packet; + } + + public void setPacket(IPacket packet) { + this.packet = packet; + } + + public IAttachment getAttachment() { + return attachment; + } + + public void setAttachment(IAttachment attachment) { + this.attachment = attachment; + } + + public Exception getException() { + return exception; + } + + public void setException(Exception exception) { + this.exception = exception; + } +} diff --git a/net/src/main/java/com/zfoo/net/packet/common/ErrorCode.java b/net/src/main/java/com/zfoo/net/packet/common/ErrorCode.java deleted file mode 100644 index a332e328..00000000 --- a/net/src/main/java/com/zfoo/net/packet/common/ErrorCode.java +++ /dev/null @@ -1,86 +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.packet.common; - -import com.baidu.bjf.remoting.protobuf.annotation.Ignore; -import com.baidu.bjf.remoting.protobuf.annotation.Protobuf; -import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass; -import com.zfoo.protocol.IPacket; - -/** - * @author meiw - * @version 3.0 - */ -@ProtobufClass -public class ErrorCode implements IPacket { - - @Ignore - public static final transient short PROTOCOL_ID = 105; - - @Protobuf(order = 1) - private int protocolId; - @Protobuf(order = 2) - private int errorCode; - @Protobuf(order = 3) - private String errorMessage; - - @Override - public short protocolId() { - return PROTOCOL_ID; - } - - public static ErrorCode valueOf(int protocolId, int errorCode, String errorMessage) { - ErrorCode response = new ErrorCode(); - response.protocolId = protocolId; - response.errorCode = errorCode; - response.errorMessage = errorMessage; - return response; - } - - public static ErrorCode valueOf(IPacket packet, int errorCode, String errorMessage) { - ErrorCode response = new ErrorCode(); - response.protocolId = packet.protocolId(); - response.errorCode = errorCode; - response.errorMessage = errorMessage; - return response; - } - - public static ErrorCode valueOf(IPacket packet, int errorCode) { - return valueOf(packet, errorCode, ""); - } - - public int getProtocolId() { - return protocolId; - } - - public void setProtocolId(int protocolId) { - this.protocolId = protocolId; - } - - public int getErrorCode() { - return errorCode; - } - - public void setErrorCode(int errorCode) { - this.errorCode = errorCode; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } -} diff --git a/net/src/main/java/com/zfoo/net/router/Router.java b/net/src/main/java/com/zfoo/net/router/Router.java index b21e3eaa..43de544a 100644 --- a/net/src/main/java/com/zfoo/net/router/Router.java +++ b/net/src/main/java/com/zfoo/net/router/Router.java @@ -18,8 +18,8 @@ import com.zfoo.net.NetContext; import com.zfoo.net.core.gateway.model.AuthUidToGatewayCheck; import com.zfoo.net.core.gateway.model.AuthUidToGatewayConfirm; import com.zfoo.net.core.gateway.model.AuthUidToGatewayEvent; +import com.zfoo.net.core.tcp.model.ServerExceptionEvent; import com.zfoo.net.packet.common.Error; -import com.zfoo.net.packet.common.ErrorCode; import com.zfoo.net.packet.common.Heartbeat; import com.zfoo.net.packet.model.EncodedPacketInfo; import com.zfoo.net.router.answer.AsyncAnswer; @@ -27,7 +27,6 @@ import com.zfoo.net.router.answer.SyncAnswer; import com.zfoo.net.router.attachment.GatewayAttachment; import com.zfoo.net.router.attachment.IAttachment; import com.zfoo.net.router.attachment.SignalAttachment; -import com.zfoo.net.router.exception.ErrorRequestException; import com.zfoo.net.router.exception.ErrorResponseException; import com.zfoo.net.router.exception.NetTimeOutException; import com.zfoo.net.router.exception.UnexpectedProtocolException; @@ -344,7 +343,7 @@ public class Router implements IRouter { // 这个在哪个线程处理取决于:这个上层的PacketReceiverTask被丢到了哪个线程中 PacketBus.submit(session, packet, attachment); } catch (Exception e) { - handleException(session, packet, attachment, e); + EventBus.syncSubmit(ServerExceptionEvent.valueOf(session, packet, attachment, e)); logger.error(StringUtils.format("e[uid:{}][sid:{}]未知exception异常", session.getAttribute(AttributeType.UID), session.getSid(), e.getMessage()), e); } catch (Throwable t) { logger.error(StringUtils.format("e[uid:{}][sid:{}]未知error错误", session.getAttribute(AttributeType.UID), session.getSid(), t.getMessage()), t); @@ -362,17 +361,4 @@ public class Router implements IRouter { } } - private void handleException(Session session, IPacket packet, IAttachment attachment, Exception exception) { - if (exception instanceof ErrorRequestException) { - ErrorRequestException requestException = (ErrorRequestException) exception; - int errorCode = requestException.getErrorCode(); - String msg = requestException.getErrorMsg(); - var resp = ErrorCode.valueOf(packet, errorCode, msg); - NetContext.getRouter().send(session, resp, attachment); - return; - } - var resp = ErrorCode.valueOf(packet, 0); - NetContext.getRouter().send(session, resp, attachment); - } - } diff --git a/net/src/main/java/com/zfoo/net/router/exception/ErrorRequestException.java b/net/src/main/java/com/zfoo/net/router/exception/ErrorRequestException.java deleted file mode 100644 index d5cc12f7..00000000 --- a/net/src/main/java/com/zfoo/net/router/exception/ErrorRequestException.java +++ /dev/null @@ -1,45 +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.router.exception; - -/** - * @author meiw - * @version 3.0 - */ -public class ErrorRequestException extends RuntimeException { - - private final int errorCode; - - private final String errorMsg; - - public ErrorRequestException(int errorCode, String errorMsg) { - super(errorMsg); - this.errorCode = errorCode; - this.errorMsg = errorMsg; - } - - public ErrorRequestException(int errorCode) { - this.errorCode = errorCode; - this.errorMsg = ""; - } - - public int getErrorCode() { - return errorCode; - } - - public String getErrorMsg() { - return errorMsg; - } - -} 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 01382e8c..86d53c3b 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 @@ -29,6 +29,7 @@ import com.zfoo.protocol.ProtocolManager; import com.zfoo.protocol.collection.ArrayUtils; import com.zfoo.protocol.exception.RunException; import com.zfoo.protocol.registration.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.util.AssertionUtils; import com.zfoo.protocol.util.ReflectionUtils; import com.zfoo.protocol.util.StringUtils; @@ -125,9 +126,7 @@ public abstract class PacketBus { } try { - var protocolIdField = packetClazz.getDeclaredField(ProtocolManager.PROTOCOL_ID); - ReflectionUtils.makeAccessible(protocolIdField); - var protocolId = (short) protocolIdField.get(null); + var protocolId = ProtocolAnalysis.getProtocolIdByClass(packetClazz); var receiverDefinition = new PacketReceiverDefinition(bean, method, packetClazz, attachmentClazz); var enhanceReceiverDefinition = EnhanceUtils.createPacketReceiver(receiverDefinition);