自动化立体仓库 - WCS系统
zhangc
2025-03-11 d5449236ef0b3adafb3e4cc872f50479efa0ce7b
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
package com.zy.core.netty;
 
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
 
/**
 * netty handler增强器
 * 设计模式: 适配器模式
 * Created by vincent on 2019-04-02
 */
public abstract class AbstractInboundHandler<T> extends ChannelInboundHandlerAdapter {
 
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        @SuppressWarnings("unchecked")
        T t = (T) obj;
        if (channelRead0(ctx, t)) {
            ctx.fireChannelRead(t);
        } else {
            // 管道中断,fireChannelRead未执行,需要手动释放堆外内存
            if (obj instanceof ByteBuf) {
                ReferenceCountUtil.release(obj);
            }
//            if (obj instanceof GBPackage){
//                GBPackage pac = (GBPackage) obj;
//                ReferenceCountUtil.release(pac.getSourceBuff());
//            }
        }
    }
 
    protected abstract boolean channelRead0(ChannelHandlerContext ctx, T t) throws Exception;
}