ref[handler]: create session when channel is activate

This commit is contained in:
jaysunxiao committed 2025-07-05 13:42:10 +08:00
1 parent 09f1c9fd70
commit 3e9968f69f
5 files changed
+33 -6

No files matched your search

@@ -17,17 +17,21 @@ import com.zfoo.net.NetContext;
import com.zfoo.net.core.proxy.TunnelClient;
import com.zfoo.net.core.proxy.TunnelProtocolClient2Server;
import com.zfoo.net.core.proxy.TunnelProtocolServer2Client;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.session.Session;
import com.zfoo.net.util.SessionUtils;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author jaysunxiao
*/
@ChannelHandler.Sharable
public class TunnelClientRouteHandler extends ClientRouteHandler {
public class TunnelClientRouteHandler extends BaseRouteHandler {
private static final Logger logger = LoggerFactory.getLogger(TunnelClientRouteHandler.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -35,12 +39,15 @@ public class TunnelClientRouteHandler extends ClientRouteHandler {
TunnelClient.tunnels.add(ctx.channel());
var session = SessionUtils.getSession(ctx);
ctx.channel().writeAndFlush(new TunnelProtocolClient2Server.TunnelRegister(session.getSid()));
logger.info("tunnel client activate in sid:[{}]", session.getSid());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
var session = SessionUtils.getSession(ctx);
TunnelClient.tunnels.remove(ctx.channel());
logger.info("tunnel client inactivate in sid:[{}]", session.getSid());
}
@Override
@@ -14,19 +14,34 @@
package com.zfoo.net.core.proxy.handler;
import com.zfoo.net.core.proxy.TunnelServer;
import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.util.SessionUtils;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author jaysunxiao
*/
@ChannelHandler.Sharable
public class TunnelServerRouteHandler extends ServerRouteHandler {
public class TunnelServerRouteHandler extends BaseRouteHandler {
private static final Logger logger = LoggerFactory.getLogger(TunnelServerRouteHandler.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
logger.info("tunnel server channel is active {}", SessionUtils.sessionInfo(ctx));
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
TunnelServer.tunnels.remove(ctx.channel());
logger.warn("tunnel server channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
}
}
@@ -48,6 +48,11 @@ public abstract class BaseRouteHandler extends ChannelInboundHandlerAdapter {
return session;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
initChannel(ctx.channel());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
@@ -35,7 +35,8 @@ public class ClientRouteHandler extends BaseRouteHandler {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
var session = initChannel(ctx.channel());
var session = SessionUtils.getSession(ctx);
NetContext.getSessionManager().addClientSession(session);
logger.info("client channel is active {}", SessionUtils.sessionInfo(ctx));
EventBus.post(ClientSessionActiveEvent.valueOf(session));
@@ -46,7 +47,6 @@ public class ClientRouteHandler extends BaseRouteHandler {
super.channelInactive(ctx);
var session = SessionUtils.getSession(ctx);
if (session == null) {
return;
}
@@ -34,7 +34,7 @@ public class ServerRouteHandler extends BaseRouteHandler {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
var session = initChannel(ctx.channel());
var session = SessionUtils.getSession(ctx);
NetContext.getSessionManager().addServerSession(session);
logger.info("server channel is active {}", SessionUtils.sessionInfo(ctx));
EventBus.post(ServerSessionActiveEvent.valueOf(session));