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; 
 | 
} 
 |