netmc/src/main/java/io/github/yezhihao/netmc/codec/MessageDecoderWrapper.java

43 lines
1.3 KiB
Java

package io.github.yezhihao.netmc.codec;
import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.DecoderException;
/**
* 基础消息解码
* @author yezhihao
* home https://gitee.com/yezhihao/jt808-server
*/
@ChannelHandler.Sharable
public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter {
private MessageDecoder decoder;
public MessageDecoderWrapper(MessageDecoder decoder) {
this.decoder = decoder;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg;
try {
Object message = decoder.decode(buf, ctx.channel().attr(Session.KEY).get());
if (message != null)
ctx.fireChannelRead(message);
buf.skipBytes(buf.readableBytes());
} catch (Exception e) {
throw new DecoderException(ByteBufUtil.hexDump(buf), e);
} finally {
buf.release();
}
} else {
ctx.fireChannelRead(msg);
}
}
}