|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ProtocolDecoder(SnowflakeIdWorker snowflakeIdWorker){ | 
|---|
|  |  |  | public ProtocolDecoder(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){ | 
|---|
|  |  |  | if (startMark == -1) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 去除无用前缀报文 | 
|---|
|  |  |  | if (startMark != 0){ | 
|---|
|  |  |  | if (startMark != 0) { | 
|---|
|  |  |  | in.readerIndex(startMark); | 
|---|
|  |  |  | in.discardReadBytes(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | list.add(analyzeProtocol(pac)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ChPackage analyzeProtocol(ChPackage pac){ | 
|---|
|  |  |  | public ChPackage analyzeProtocol(ChPackage pac) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ByteBuf byteBuf = pac.getSourceBuff(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取标识位下标 | 
|---|
|  |  |  | private int indexOfStartMark(ByteBuf inputBuffer){ | 
|---|
|  |  |  | private int indexOfStartMark(ByteBuf inputBuffer) { | 
|---|
|  |  |  | int length = inputBuffer.writerIndex(); | 
|---|
|  |  |  | // 报文长度至少大于2 | 
|---|
|  |  |  | if (length < 2) { | 
|---|
|  |  |  | return -1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int readerIndex = inputBuffer.readerIndex(); | 
|---|
|  |  |  | for(int i = readerIndex; i < length - 1; i ++) { | 
|---|
|  |  |  | for (int i = readerIndex; i < length - 1; i++) { | 
|---|
|  |  |  | byte b1 = inputBuffer.getByte(i); | 
|---|
|  |  |  | // "#" = b1 | 
|---|
|  |  |  | if (0x23 == b1) { | 
|---|