Skip to content

Commit

Permalink
🐳 #271 游戏对外服 - 增加 log 相关的 netty Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed May 1, 2024
1 parent 5afab6e commit 5172168
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class ExternalGlobalConfig {
public AccessAuthenticationHook accessAuthenticationHook = new DefaultAccessAuthenticationHook();
/** 游戏对外服路由缓存 */
public ExternalCmdCache externalCmdCache;

/** true 表示开启简单日志打印 netty handler. see SimpleLoggerHandler */
public boolean enableLoggerHandler = true;
/**
* 协议开关,用于一些协议级别的开关控制,比如 安全加密校验等。 : 0 不校验
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.external.core.netty.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
* 简单日志打印,通常是不活跃的连接、触发异常的连接
*
* @author 渔民小镇
* @date 2024-05-01
*/
@Slf4j
@ChannelHandler.Sharable
public final class SimpleLoggerHandler extends ChannelInboundHandlerAdapter {

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("channelInactive channel.remoteAddress() : {}", ctx.channel().remoteAddress());
super.channelInactive(ctx);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error(cause.getMessage(), cause);
super.exceptionCaught(ctx, cause);
}

private SimpleLoggerHandler() {
}

public static SimpleLoggerHandler me() {
return Holder.ME;
}

/** 通过 JVM 的类加载机制, 保证只加载一次 (singleton) */
private static class Holder {
static final SimpleLoggerHandler ME = new SimpleLoggerHandler();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,32 @@ public void setUserSessions(UserSessions<?, ?> userSessions) {
}

@Override
public void channelActive(ChannelHandlerContext ctx) {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
BrokerClientModuleMessage moduleMessage = brokerClient.getBrokerClientModuleMessage();
int idHash = moduleMessage.getIdHash();

// 加入到 session 管理
SocketUserSession userSession = userSessions.add(ctx);
userSession.setExternalClientId(idHash);

ctx.fireChannelActive();
super.channelActive(ctx);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) {
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// 从 session 管理中移除
var userSession = this.userSessions.getUserSession(ctx);
this.userSessions.removeUserSession(userSession);

super.channelInactive(ctx);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 从 session 管理中移除
var userSession = this.userSessions.getUserSession(ctx);
this.userSessions.removeUserSession(userSession);

super.exceptionCaught(ctx, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public void pipelineIdle(PipelineContext context) {

@Override
public void pipelineCustom(PipelineContext context) {
// 日志打印(异常时)
if (ExternalGlobalConfig.enableLoggerHandler) {
context.addLast("SimpleLoggerHandler", SimpleLoggerHandler.me());
}

// 路由存在检测
context.addLast("CmdCheckHandler", CmdCheckHandler.me());
Expand Down

0 comments on commit 5172168

Please sign in to comment.