liusuyi
2025-10-17 f9ee3fa619d478e5e592dfaa99a5b993feb6c67a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.ard.work.component.netty.handler;
 
import com.ard.work.component.netty.command.BaseCmd;
import com.ard.work.component.netty.command.register.HeartbeatCmd;
import com.ard.work.component.netty.cache.TyTcpServerCache;
import com.ard.work.service.ITyService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
 
 
@Component
@Slf4j
@ChannelHandler.Sharable
public class ClientHandler extends SimpleChannelInboundHandler<BaseCmd> {
 
    @Resource
    private ITyService tyService;
 
    @Override
    public void channelRead0(ChannelHandlerContext ctx, BaseCmd command) {
        Channel channel = ctx.channel();
        String serverId = (String) channel.attr(AttributeKey.valueOf("serverId")).get();
        tyService.processReplyMessage(serverId, command);
    }
 
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //当客户端connect成功后,可以在最后一个handler等待用户输入
        Channel channel = ctx.channel();
        String serverId = (String) channel.attr(AttributeKey.valueOf("serverId")).get();
 
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        String host = remoteAddress.getHostString();
        int port = remoteAddress.getPort();
 
        log.info("与边缘服务器 [{}] 连接成功 ({}:{})", serverId, host, port);
 
        // 针对当前连接的服务器进行注册
        tyService.registerMessage(serverId);
        super.channelActive(ctx);
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("发生异常: ", cause); // 使用日志记录异常
       // ctx.close();
    }
 
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            long timestamp = System.currentTimeMillis();
 
            switch (event.state()) {
                case READER_IDLE:
                    log.warn("读空闲超时 - 可能服务器未响应");
                    break;
                case WRITER_IDLE:
                    // 写空闲时主动发心跳
                    HeartbeatCmd heartbeat = new HeartbeatCmd(timestamp);
                    ctx.writeAndFlush(heartbeat);
                    log.debug("写空闲 -> 发送心跳包 时间戳={}", timestamp);
                    break;
                case ALL_IDLE:
                    log.info("读写都空闲 - 可考虑关闭连接或重连");
                    break;
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
 
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        String serverId = (String) channel.attr(AttributeKey.valueOf("serverId")).get();
        InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
        String host = remoteAddress.getHostString();
        int port = remoteAddress.getPort();
        log.error("与六合服务{}:{}连接断开!", host, port);
        TyTcpServerCache.putTcpServerCache(serverId, false);
        super.channelInactive(ctx);
    }
 
}