Skip to content

Commit

Permalink
🐳
Browse files Browse the repository at this point in the history
cmdInfo 新增 of 系列方法,用于代替 getCmdInfo 系列方法

异常机制接口 MsgExceptionInfo,新增方法
// 断言为 true, 就抛出异常,可自定义消息
void assertTrueThrows(boolean v1, String msg)
// 断言值 value 不能为 null, 否则就抛出异常,可自定义消息
void assertNonNull(Object value, String msg)
  • Loading branch information
iohao committed Aug 1, 2023
1 parent b35be20 commit 7c13268
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,58 @@ public final class CmdInfo {
* 获取 cmdInfo
* <pre>
* 内部使用享元工厂来获取 cmdInfo
*
* 请使用 of 系列方法来代替此方法
* </pre>
*
* @param cmd 主路由
* @param subCmd 子路由
* @return 路由信息
*/
public static CmdInfo getCmdInfo(int cmd, int subCmd) {
return CmdInfoFlyweightFactory.getCmdInfo(cmd, subCmd);
return of(cmd, subCmd);
}

/**
* 获取 cmdInfo
* <pre>
* 内部使用享元工厂来获取 cmdInfo
*
* 请使用 of 系列方法来代替此方法
* </pre>
*
* @param cmdMerge cmd-subCmd {@link CmdKit#merge(int, int)}
* @return 路由信息
*/
public static CmdInfo getCmdInfo(int cmdMerge) {
return CmdInfoFlyweightFactory.getCmdInfo(cmdMerge);
return of(cmdMerge);
}

/**
* 获取 cmdInfo
* <pre>
* 内部使用享元工厂来获取 cmdInfo
* </pre>
*
* @param cmd 主路由
* @param subCmd 子路由
* @return 路由信息
*/
public static CmdInfo of(int cmd, int subCmd) {
return CmdInfoFlyweightFactory.of(cmd, subCmd);
}

/**
* 获取 cmdInfo
* <pre>
* 内部使用享元工厂来获取 cmdInfo
* </pre>
*
* @param cmdMerge cmd-subCmd {@link CmdKit#merge(int, int)}
* @return 路由信息
*/
public static CmdInfo of(int cmdMerge) {
return CmdInfoFlyweightFactory.of(cmdMerge);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,59 @@ public final class CmdInfoFlyweightFactory {

/**
* 获取路由信息
* <pre>
* 将在下个版本移除,请使用 of 系列代替
* </pre>
*
* @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);
}

/**
* 获取路由信息
* <pre>
* 将在下个版本移除,请使用 of 系列代替
* </pre>
*
* @param cmdMerge 主路由(高16) + 子路由(低16)
* @return 路由信息
*/
@Deprecated
public static CmdInfo getCmdInfo(int cmdMerge) {
return of(cmdMerge);
}


/**
* 获取路由信息
* <pre>
* 如果不存在,就新建
* </pre>
*
* @param cmd 主路由
* @param subCmd 子路由
* @return 路由信息
*/
public static CmdInfo of(int cmd, int subCmd) {
int cmdMerge = CmdKit.merge(cmd, subCmd);
return of(cmdMerge);
}

/**
* 获取路由信息
* <pre>
* 如果不存在,就新建
* </pre>
*
* @param cmdMerge 主路由(高16) + 子路由(低16)
* @return 路由信息
*/
public static CmdInfo of(int cmdMerge) {

CmdInfo cmdInfo = cmdInfoMap.get(cmdMerge);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ public MsgException(int msgCode, String message) {
public MsgException(MsgExceptionInfo msgExceptionInfo) {
this(msgExceptionInfo.getCode(), msgExceptionInfo.getMsg());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, 否则抛出异常
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +34,10 @@
*/
@UtilityClass
public class FlowContextKit {

/** rpc oneway request */
static final byte REQUEST_ONEWAY = (byte) 0x02;

/**
* FlowContext 自身属性赋值
*
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7c13268

Please sign in to comment.