Skip to content

Commit

Permalink
🐳 UserSession 接口新增 ofRequestMessage 方法,简化玩家在游戏对外服中创建请求对象。
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Aug 16, 2024
1 parent 1debe72 commit 4b570c5
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
@UtilityClass
public class ByteKit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
public final class FixedNameThreadFactory extends ThreadCreator implements ThreadFactory {
public FixedNameThreadFactory(String threadNamePrefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* @author 渔民小镇
* @date 2024-08-02
* @since 21.14
*/
public class CommonIllegalArgumentException extends IllegalArgumentException {
public CommonIllegalArgumentException(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.common.kit.exception;

/**
* NullPointerException
*
* @author 渔民小镇
* @date 2024-08-16
* @since 21.15
*/
public class CommonNullPointerException extends NullPointerException {
public CommonNullPointerException() {
}

public CommonNullPointerException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* @author 渔民小镇
* @date 2024-08-02
* @since 21.14
*/
public class CommonRuntimeException extends RuntimeException {
public CommonRuntimeException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author 渔民小镇
* @date 2024-08-01
* @since 21.14
*/
@UtilityClass
public class ThrowKit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import com.iohao.game.action.skeleton.protocol.BarMessage;
import com.iohao.game.action.skeleton.protocol.HeadMetadata;
import com.iohao.game.action.skeleton.protocol.RequestMessage;
import com.iohao.game.common.kit.attr.AttrOptionDynamic;
import com.iohao.game.external.core.message.ExternalCodecKit;

/**
* UserSession 接口
Expand Down Expand Up @@ -110,4 +112,23 @@ public interface UserSession extends AttrOptionDynamic {
* @return 玩家 ip
*/
String getIp();

/**
* 创建 RequestMessage,内部会将 User 自身的相关信息设置到 RequestMessage 中。
*
* @param cmdMerge 路由
* @return RequestMessage
* @since 21.15
*/
default RequestMessage ofRequestMessage(int cmdMerge) {
RequestMessage request = ExternalCodecKit.createRequest();

HeadMetadata headMetadata = request.getHeadMetadata();
headMetadata.setCmdMerge(cmdMerge);

// 给请求消息加上一些 user 自身的数据
this.employ(headMetadata);

return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
package com.iohao.game.bolt.broker.core.aware;

import com.iohao.game.bolt.broker.core.common.IoGameGlobalConfig;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;
import lombok.experimental.UtilityClass;

import java.util.Objects;
Expand All @@ -31,6 +27,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
@UtilityClass
public class AwareKit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
public interface UserProcessorExecutorSelectorAware {
void setUserProcessorExecutorSelector(UserProcessorExecutorSelectorStrategy executorSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
@Slf4j
final class DefaultUserProcessorExecutorSelectorStrategy extends DefaultCustomSerializer
Expand All @@ -49,6 +50,7 @@ public <T extends RequestCommand> boolean serializeHeader(T request, InvokeConte
HeadMetadata headMetadata = message.getHeadMetadata();

if (Objects.isNull(headMetadata.getUserProcessorExecutorSelectorBytes())) {
// 做一个简单的优化,避免多次序列化
long executorIndex = ExecutorSelectKit.getExecutorIndex(headMetadata);
headMetadata.setUserProcessorExecutorSelectorBytes(ByteKit.toBytes(executorIndex));
}
Expand Down Expand Up @@ -76,6 +78,10 @@ public <T extends RequestCommand> boolean deserializeHeader(T request) {

@Override
public Executor select(String requestClass, Object requestHeader) {
if (Objects.isNull(requestHeader)) {
return null;
}

// see RpcRequestProcessor.java:105
long executorIndex = (long) requestHeader;
return threadExecutorRegion.getThreadExecutor(executorIndex).executor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
final class ProcessorSelectorThreadExecutorRegion implements ThreadExecutorRegion {
final ThreadExecutor[] threadExecutors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author 渔民小镇
* @date 2024-08-10
* @since 21.15
*/
public interface UserProcessorExecutorSelectorStrategy
extends UserProcessor.ExecutorSelector {
Expand Down

0 comments on commit 4b570c5

Please sign in to comment.