jiazx0107
2025-09-01 ce4f9b9f72a4269a1f25812dadd59bfb92c7b3cf
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
95
package com.ld.io.netty;
 
import com.ld.io.api.HeartbeatProvider;
import com.ld.io.api.IoMsgConsumer;
import com.ld.io.api.IoSession;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
 
import java.io.IOException;
 
@Sharable
public class ReadMessageHandler extends SimpleChannelInboundHandler<Object> {
    private final InternalLogger logger = InternalLoggerFactory.getInstance(this.getClass());
    private ReceiveMessageThreadPool threadPool;//消息处理线程池
    private NettySessionFactory nettySessionFactory;//会话工厂
    private HeartbeatProvider heartbeatMessageProvider;//心跳数据提供者
 
    public ReadMessageHandler(IoMsgConsumer messageConsumer, NettySessionFactory nettySessionFactory, HeartbeatProvider heartbeatMessageProvider) {
        this.threadPool = new ReceiveMessageThreadPool(messageConsumer);
        this.nettySessionFactory = nettySessionFactory;
        this.heartbeatMessageProvider = heartbeatMessageProvider;
    }
 
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        nettySessionFactory.create(new NettyChannel(ctx));
        super.channelActive(ctx);
    }
 
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        nettySessionFactory.destroy(ctx);
        super.channelInactive(ctx);
    }
 
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        IoSession session = nettySessionFactory.getSession(ctx);
        threadPool.execute(session, (byte[]) msg);
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("happened error!", cause);
        Channel channel = ctx.channel();
        if (cause instanceof IOException && channel.isActive()) {
            nettySessionFactory.destroy(ctx);
            ctx.close();
        }
    }
 
    /**
     * 心跳处理
     * 超时后尝试向客户端发送消息,若发送失败关闭channel
     */
    @Override
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            if (heartbeatMessageProvider == null) {
                return;
            }
 
            logger.info("Health check time out, the event was triggered");
            IoSession session = nettySessionFactory.getSession(ctx);
            if(null == session){
                logger.error("no Session by HeartBeat");
                return;
            }
            byte[] bytes = heartbeatMessageProvider.provide(session);
            
            if(null == bytes){
                logger.error("no msg by HeartBeat");
                return;
            }
 
            ByteBuf buf = Unpooled.copiedBuffer(bytes);
            ctx.writeAndFlush(buf).addListener((ChannelFutureListener) future -> {
                if (!future.isSuccess()) {
                    nettySessionFactory.destroy(ctx);
                    future.channel().close();
                }
            });
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}