| | |
| | | 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; |
| | |
| | | /** |
| | | * Created by vincent on 2019-04-10 |
| | | */ |
| | | @Slf4j |
| | | public class ProtocolDecoder extends ByteToMessageDecoder { |
| | | |
| | | private final SnowflakeIdWorker snowflakeIdWorker; |
| | |
| | | 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); |
| | | |