All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.yezhihao.netmc.codec.MessageDecoderWrapper Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
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 final 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);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy