diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java index 89837112f..e9b5603cb 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java @@ -98,7 +98,7 @@ public final class ActionCommand { private ActionCommand(Builder builder) { // -------------- 路由相关 -------------- - this.cmdInfo = CmdInfoFlyweightFactory.getCmdInfo(builder.cmd, builder.subCmd); + this.cmdInfo = CmdInfoFlyweightFactory.of(builder.cmd, builder.subCmd); // -------------- 控制器相关 -------------- this.actionControllerClazz = builder.actionControllerClazz; diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfo.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfo.java index f6a49ab89..82b9d3ef3 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfo.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfo.java @@ -67,6 +67,8 @@ public final class CmdInfo { * 获取 cmdInfo *
      *     内部使用享元工厂来获取 cmdInfo
+     *
+     *     请使用 of 系列方法来代替此方法
      * 
* * @param cmd 主路由 @@ -74,20 +76,49 @@ public final class CmdInfo { * @return 路由信息 */ public static CmdInfo getCmdInfo(int cmd, int subCmd) { - return CmdInfoFlyweightFactory.getCmdInfo(cmd, subCmd); + return of(cmd, subCmd); } /** * 获取 cmdInfo *
      *     内部使用享元工厂来获取 cmdInfo
+     *
+     *     请使用 of 系列方法来代替此方法
      * 
* * @param cmdMerge cmd-subCmd {@link CmdKit#merge(int, int)} * @return 路由信息 */ public static CmdInfo getCmdInfo(int cmdMerge) { - return CmdInfoFlyweightFactory.getCmdInfo(cmdMerge); + return of(cmdMerge); + } + + /** + * 获取 cmdInfo + *
+     *     内部使用享元工厂来获取 cmdInfo
+     * 
+ * + * @param cmd 主路由 + * @param subCmd 子路由 + * @return 路由信息 + */ + public static CmdInfo of(int cmd, int subCmd) { + return CmdInfoFlyweightFactory.of(cmd, subCmd); + } + + /** + * 获取 cmdInfo + *
+     *     内部使用享元工厂来获取 cmdInfo
+     * 
+ * + * @param cmdMerge cmd-subCmd {@link CmdKit#merge(int, int)} + * @return 路由信息 + */ + public static CmdInfo of(int cmdMerge) { + return CmdInfoFlyweightFactory.of(cmdMerge); } @Override diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfoFlyweightFactory.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfoFlyweightFactory.java index 22028fa72..551c20f8e 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfoFlyweightFactory.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/CmdInfoFlyweightFactory.java @@ -40,23 +40,59 @@ public final class CmdInfoFlyweightFactory { /** * 获取路由信息 + *
+     *     将在下个版本移除,请使用 of 系列代替
+     * 
* * @param cmd 主路由 * @param subCmd 子路由 * @return 路由信息 */ + @Deprecated public static CmdInfo getCmdInfo(int cmd, int subCmd) { - int cmdMerge = CmdKit.merge(cmd, subCmd); - return getCmdInfo(cmdMerge); + return of(cmd, subCmd); } /** * 获取路由信息 + *
+     *     将在下个版本移除,请使用 of 系列代替
+     * 
* * @param cmdMerge 主路由(高16) + 子路由(低16) * @return 路由信息 */ + @Deprecated public static CmdInfo getCmdInfo(int cmdMerge) { + return of(cmdMerge); + } + + + /** + * 获取路由信息 + *
+     *     如果不存在,就新建
+     * 
+ * + * @param cmd 主路由 + * @param subCmd 子路由 + * @return 路由信息 + */ + public static CmdInfo of(int cmd, int subCmd) { + int cmdMerge = CmdKit.merge(cmd, subCmd); + return of(cmdMerge); + } + + /** + * 获取路由信息 + *
+     *     如果不存在,就新建
+     * 
+ * + * @param cmdMerge 主路由(高16) + 子路由(低16) + * @return 路由信息 + */ + public static CmdInfo of(int cmdMerge) { CmdInfo cmdInfo = cmdInfoMap.get(cmdMerge); diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgException.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgException.java index 98ff98801..a18935f27 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgException.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgException.java @@ -50,5 +50,4 @@ public MsgException(int msgCode, String message) { public MsgException(MsgExceptionInfo msgExceptionInfo) { this(msgExceptionInfo.getCode(), msgExceptionInfo.getMsg()); } - } diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgExceptionInfo.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgExceptionInfo.java index f137e53c3..df93edccf 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgExceptionInfo.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/exception/MsgExceptionInfo.java @@ -58,21 +58,32 @@ default void assertTrueThrows(boolean v1) throws MsgException { } /** - * 断言必须是 true, 否则抛出异常 + * 断言为 true, 就抛出异常 * - * @param v1 断言值 + * @param v1 断言值 + * @param msg 自定义消息 * @throws MsgException e */ - default void assertTrue(boolean v1) throws MsgException { + default void assertTrueThrows(boolean v1, String msg) throws MsgException { if (v1) { - return; + int code = this.getCode(); + throw new MsgException(code, msg); } + } - throw new MsgException(this); + /** + * 断言值 value 不能为 null, 否则就抛出异常 + * + * @param value 断言值 + * @param msg 自定义消息 + * @throws MsgException e + */ + default void assertNonNull(Object value, String msg) throws MsgException { + assertTrue(Objects.nonNull(value), msg); } /** - * 断言必须是 非null, 否则抛出异常 + * 断言值 value 不能为 null, 否则就抛出异常 * * @param value 断言值 * @throws MsgException e @@ -81,6 +92,21 @@ default void assertNonNull(Object value) throws MsgException { assertTrue(Objects.nonNull(value)); } + /** + * 断言必须是 true, 否则抛出异常 + * + * @param v1 断言值 + * @throws MsgException e + */ + default void assertTrue(boolean v1) throws MsgException { + if (v1) { + return; + } + + throw new MsgException(this); + } + + /** * 断言必须是 false, 否则抛出异常 * diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/FlowContextKit.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/FlowContextKit.java index 23f8626fd..47aee0901 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/FlowContextKit.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/FlowContextKit.java @@ -19,8 +19,11 @@ package com.iohao.game.action.skeleton.core.flow; import com.iohao.game.action.skeleton.core.BarSkeleton; +import com.iohao.game.action.skeleton.core.commumication.ChannelContext; +import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr; import com.iohao.game.action.skeleton.protocol.HeadMetadata; import com.iohao.game.action.skeleton.protocol.RequestMessage; +import com.iohao.game.action.skeleton.protocol.ResponseMessage; import lombok.experimental.UtilityClass; import java.util.Objects; @@ -31,6 +34,10 @@ */ @UtilityClass public class FlowContextKit { + + /** rpc oneway request */ + static final byte REQUEST_ONEWAY = (byte) 0x02; + /** * FlowContext 自身属性赋值 * @@ -72,4 +79,17 @@ public void employ(FlowContext flowContext) { flowContext.setResponse(responseMessage); } } + + public ChannelContext getChannelContext(FlowContext flowContext) { + ResponseMessage response = flowContext.getResponse(); + HeadMetadata headMetadata = response.getHeadMetadata(); + + byte rpcCommandType = headMetadata.getRpcCommandType(); + + if (rpcCommandType == REQUEST_ONEWAY) { + return flowContext.option(FlowAttr.brokerClientContext); + } else { + return flowContext.option(FlowAttr.channelContext); + } + } } diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/interal/DefaultActionAfter.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/interal/DefaultActionAfter.java index ce4f117f5..b4da7f30b 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/interal/DefaultActionAfter.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/flow/interal/DefaultActionAfter.java @@ -22,27 +22,23 @@ import com.iohao.game.action.skeleton.core.commumication.ChannelContext; import com.iohao.game.action.skeleton.core.flow.ActionAfter; import com.iohao.game.action.skeleton.core.flow.FlowContext; -import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr; -import com.iohao.game.action.skeleton.protocol.HeadMetadata; +import com.iohao.game.action.skeleton.core.flow.FlowContextKit; import com.iohao.game.action.skeleton.protocol.ResponseMessage; /** - * 默认的ActionAfter + * 默认的 ActionAfter * * @author 渔民小镇 * @date 2021-12-20 */ public final class DefaultActionAfter implements ActionAfter { - /** rpc oneway request */ - static final byte REQUEST_ONEWAY = (byte) 0x02; - @Override public void execute(final FlowContext flowContext) { - final ResponseMessage response = flowContext.getResponse(); - ChannelContext channelContext = getChannelContext(flowContext); + ChannelContext channelContext = FlowContextKit.getChannelContext(flowContext); // 有错误就响应给调用方 + final ResponseMessage response = flowContext.getResponse(); if (response.hasError()) { channelContext.sendResponse(response); return; @@ -57,17 +53,4 @@ public void execute(final FlowContext flowContext) { // 将数据回传给调用方 channelContext.sendResponse(response); } - - private ChannelContext getChannelContext(FlowContext flowContext) { - ResponseMessage response = flowContext.getResponse(); - HeadMetadata headMetadata = response.getHeadMetadata(); - - byte rpcCommandType = headMetadata.getRpcCommandType(); - - if (rpcCommandType == REQUEST_ONEWAY) { - return flowContext.option(FlowAttr.brokerClientContext); - } else { - return flowContext.option(FlowAttr.channelContext); - } - } } diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/protocol/HeadMetadata.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/protocol/HeadMetadata.java index 09c90ef28..01d49b0cb 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/protocol/HeadMetadata.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/protocol/HeadMetadata.java @@ -170,7 +170,7 @@ public HeadMetadata setCmdInfo(CmdInfo cmdInfo) { * @return cmdInfo */ public CmdInfo getCmdInfo() { - return CmdInfoFlyweightFactory.getCmdInfo(this.cmdMerge); + return CmdInfoFlyweightFactory.of(this.cmdMerge); } public HeadMetadata setCmdMerge(int cmdMerge) { diff --git a/widget/light-client/src/main/java/com/iohao/game/external/client/user/ClientUserChannel.java b/widget/light-client/src/main/java/com/iohao/game/external/client/user/ClientUserChannel.java index 7f4c5370f..55f4fca5c 100644 --- a/widget/light-client/src/main/java/com/iohao/game/external/client/user/ClientUserChannel.java +++ b/widget/light-client/src/main/java/com/iohao/game/external/client/user/ClientUserChannel.java @@ -215,7 +215,7 @@ public void read(ExternalMessage externalMessage, BarSkeleton barSkeleton) { CmdInfo cmdInfo = CmdInfo.getCmdInfo(cmdMerge); if (responseStatus != 0) { - log.error("错误码:{} {} {}", responseStatus, externalMessage.getValidMsg(), cmdInfo); + log.error("[错误码:{}] - [消息:{}] - {}", responseStatus, externalMessage.getValidMsg(), cmdInfo); return; }