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

xin.alum.aim.coder.LengthDecoder Maven / Gradle / Ivy

There is a newer version: 1.9.6
Show newest version
package xin.alum.aim.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class LengthDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) {
        int length = in.readInt();
        //发生断包情况,等待接收完成
        if (in.readableBytes() < length) {
            in.resetReaderIndex();
            return;
        } else {
            ByteBuf proto = in.readRetainedSlice(length);
            ctx.fireChannelRead(proto.retain());
        }
    }
}