#
tqs
2022-09-26 7d8b1dba7cf7a77b7955a0ed10d1bd7fa8947db6
src/main/java/com/zy/core/netty/handle/ProtocolDecoder.java
@@ -7,6 +7,7 @@
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
@@ -15,6 +16,7 @@
/**
 * Created by vincent on 2019-04-10
 */
@Slf4j
public class ProtocolDecoder extends ByteToMessageDecoder {
    private final SnowflakeIdWorker snowflakeIdWorker;
@@ -23,20 +25,10 @@
        this.snowflakeIdWorker = snowflakeIdWorker;
    }
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
        int startMark = indexOfStartMark(in);
        if (startMark == -1){
            return;
        }
        // 去除无用前缀报文
        if (startMark != 0){
            in.readerIndex(startMark);
            in.discardReadBytes();
        }
        // 生成和初始化消息包装类
        // 生成和初始化消息包装类
        String ip = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
        ChPackage pac = ChPackage.valueOfEmpty(String.valueOf(snowflakeIdWorker.nextId()), ip);