package cn.org.hentai.jtt1078.server.backup; import cn.org.hentai.jtt1078.entity.Jt1078Message; import cn.org.hentai.jtt1078.publisher.PublishManager; import cn.org.hentai.jtt1078.server.SessionManager; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by matrixy on 2019/4/9. */ public class Jt1078Handler extends SimpleChannelInboundHandler { static Logger logger = LoggerFactory.getLogger(Jt1078Handler.class); @Override protected void channelRead0(ChannelHandlerContext ctx, Jt1078Message msg) { String sim = msg.getSim(); int channel = msg.getChannel(); String tag = sim + "-" + channel; int sequence = msg.getSequence(); long timestamp = msg.getTimestampLong(); int pt = msg.getPt(); byte[] data = msg.getData(); if(data==null) { return; } int dataType = msg.getDataType(); int spm = msg.getSpm(); if (dataType == 0x00 || dataType == 0x01 || dataType == 0x02) { PublishManager.getInstance().publishVideo(tag, sequence, timestamp, pt, data); } else if (dataType == 0x03) { PublishManager.getInstance().publishAudio(tag, sequence, timestamp, pt, data); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); release(ctx.channel()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // super.exceptionCaught(ctx, cause); cause.printStackTrace(); release(ctx.channel()); ctx.close(); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) { String tag = SessionManager.get(ctx.channel(), "tag"); logger.info("read timeout: {}", tag); release(ctx.channel()); } } } private void release(io.netty.channel.Channel channel) { String tag = SessionManager.get(channel, "tag"); if (tag != null) { logger.info("close netty channel: {}", tag); PublishManager.getInstance().close(tag); } } }