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