io.github.yezhihao.netmc.codec.MessageDecoderWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netmc Show documentation
Show all versions of netmc Show documentation
MVC framework based on netty implementation
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