自动化立体仓库 - WCS系统
pang.jiabao
2024-04-22 32b905ac1f907fd0f58784acd8f609e691b2ea6e
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
package com.zy.core.netty.handle;
 
import com.core.common.Cools;
import com.zy.utils.News;
import com.zy.core.netty.AbstractInboundHandler;
import com.zy.core.netty.cache.ChannelCache;
import com.zy.core.netty.domain.ChPackage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
/**
 * 通道维护handler
 * Created by vincent on 2019-04-11
 */
@Slf4j
@Component("protectorHandler")
@ChannelHandler.Sharable
public class ProtectorHandler extends AbstractInboundHandler<ChPackage> {
 
    @Override
    protected boolean channelRead0(ChannelHandlerContext ctx, ChPackage pac) throws Exception {
        // jvm堆外内存需要手动释放
//        ReferenceCountUtil.release(pac.getSourceBuff());
        return true;
    }
 
    /**
     * 空闲剔除
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (IdleState.READER_IDLE == e.state()) {
                String uuid = ChannelCache.removeChannel(ctx.channel());
                ctx.close();
                if (!Cools.isEmpty(uuid)){
                    News.info("uuid={} 空闲剔除", uuid);
                }
            }
        }
    }
 
    /**
     * 断开连接
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        String uuid = ChannelCache.removeChannel(ctx.channel());
        ctx.close();
        if (!Cools.isEmpty(uuid)){
            News.info("通道 uuid={} 失去连接", uuid);
        }
    }
 
    /**
     * 管道异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ChannelCache.removeChannel(ctx.channel());
        ctx.close();
    }
 
}