Skip to content

Commit 3e9968f

Browse files
committed
ref[handler]: create session when channel is activate
1 parent 09f1c9f commit 3e9968f

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

net/src/main/java/com/zfoo/net/core/proxy/handler/TunnelClientRouteHandler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,37 @@
1717
import com.zfoo.net.core.proxy.TunnelClient;
1818
import com.zfoo.net.core.proxy.TunnelProtocolClient2Server;
1919
import com.zfoo.net.core.proxy.TunnelProtocolServer2Client;
20-
import com.zfoo.net.handler.ClientRouteHandler;
20+
import com.zfoo.net.handler.BaseRouteHandler;
2121
import com.zfoo.net.session.Session;
2222
import com.zfoo.net.util.SessionUtils;
2323
import io.netty.channel.ChannelHandler;
2424
import io.netty.channel.ChannelHandlerContext;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527

2628
/**
2729
* @author jaysunxiao
2830
*/
2931
@ChannelHandler.Sharable
30-
public class TunnelClientRouteHandler extends ClientRouteHandler {
32+
public class TunnelClientRouteHandler extends BaseRouteHandler {
33+
34+
private static final Logger logger = LoggerFactory.getLogger(TunnelClientRouteHandler.class);
3135

3236
@Override
3337
public void channelActive(ChannelHandlerContext ctx) throws Exception {
3438
super.channelActive(ctx);
3539
TunnelClient.tunnels.add(ctx.channel());
3640
var session = SessionUtils.getSession(ctx);
3741
ctx.channel().writeAndFlush(new TunnelProtocolClient2Server.TunnelRegister(session.getSid()));
42+
logger.info("tunnel client activate in sid:[{}]", session.getSid());
3843
}
3944

4045
@Override
4146
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
4247
super.channelInactive(ctx);
48+
var session = SessionUtils.getSession(ctx);
4349
TunnelClient.tunnels.remove(ctx.channel());
50+
logger.info("tunnel client inactivate in sid:[{}]", session.getSid());
4451
}
4552

4653
@Override

net/src/main/java/com/zfoo/net/core/proxy/handler/TunnelServerRouteHandler.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,34 @@
1414
package com.zfoo.net.core.proxy.handler;
1515

1616
import com.zfoo.net.core.proxy.TunnelServer;
17+
import com.zfoo.net.handler.BaseRouteHandler;
1718
import com.zfoo.net.handler.ServerRouteHandler;
19+
import com.zfoo.net.util.SessionUtils;
1820
import io.netty.channel.ChannelHandler;
1921
import io.netty.channel.ChannelHandlerContext;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2024

2125
/**
2226
* @author jaysunxiao
2327
*/
2428
@ChannelHandler.Sharable
25-
public class TunnelServerRouteHandler extends ServerRouteHandler {
29+
public class TunnelServerRouteHandler extends BaseRouteHandler {
30+
31+
private static final Logger logger = LoggerFactory.getLogger(TunnelServerRouteHandler.class);
32+
33+
34+
@Override
35+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
36+
super.channelActive(ctx);
37+
38+
logger.info("tunnel server channel is active {}", SessionUtils.sessionInfo(ctx));
39+
}
2640

2741
@Override
2842
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
2943
super.channelInactive(ctx);
3044
TunnelServer.tunnels.remove(ctx.channel());
45+
logger.warn("tunnel server channel is inactive {}", SessionUtils.sessionSimpleInfo(ctx));
3146
}
3247
}

net/src/main/java/com/zfoo/net/handler/BaseRouteHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public static Session initChannel(Channel channel) {
4848
return session;
4949
}
5050

51+
@Override
52+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
53+
super.channelActive(ctx);
54+
initChannel(ctx.channel());
55+
}
5156

5257
@Override
5358
public void channelRead(ChannelHandlerContext ctx, Object msg) {

net/src/main/java/com/zfoo/net/handler/ClientRouteHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class ClientRouteHandler extends BaseRouteHandler {
3535
@Override
3636
public void channelActive(ChannelHandlerContext ctx) throws Exception {
3737
super.channelActive(ctx);
38-
var session = initChannel(ctx.channel());
38+
39+
var session = SessionUtils.getSession(ctx);
3940
NetContext.getSessionManager().addClientSession(session);
4041
logger.info("client channel is active {}", SessionUtils.sessionInfo(ctx));
4142
EventBus.post(ClientSessionActiveEvent.valueOf(session));
@@ -46,7 +47,6 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
4647
super.channelInactive(ctx);
4748

4849
var session = SessionUtils.getSession(ctx);
49-
5050
if (session == null) {
5151
return;
5252
}

net/src/main/java/com/zfoo/net/handler/ServerRouteHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ServerRouteHandler extends BaseRouteHandler {
3434
@Override
3535
public void channelActive(ChannelHandlerContext ctx) throws Exception {
3636
super.channelActive(ctx);
37-
var session = initChannel(ctx.channel());
37+
var session = SessionUtils.getSession(ctx);
3838
NetContext.getSessionManager().addServerSession(session);
3939
logger.info("server channel is active {}", SessionUtils.sessionInfo(ctx));
4040
EventBus.post(ServerSessionActiveEvent.valueOf(session));

0 commit comments

Comments
 (0)