| | |
| | | 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; |
| | |
| | | @Override |
| | | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception { |
| | | |
| | | byte[] bytes = new byte[in.readableBytes()]; |
| | | in.readBytes(bytes); |
| | | |
| | | // 生成和初始化消息包装类 |
| | | |
| | | String ip = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress(); |
| | | ChPackage pac = ChPackage.valueOfEmpty(String.valueOf(snowflakeIdWorker.nextId()), ip); |
| | | |
| | | pac.setSourceBuff(in); |
| | | |
| | | pac.setBytes(bytes); |
| | | |
| | | list.add(pac); |
| | | |
| | | // 解析 |
| | | // list.add(analyzeProtocol(pac)); |
| | | list.add(analyzeProtocol(pac)); |
| | | } |
| | | |
| | | public ChPackage analyzeProtocol(ChPackage pac){ |