1. ByteToMessageDecoder
这个类是解码器的基类,其中描述了解码器基本的工作方式和实现原理;;还定义了一个解码的抽象方法decode,这个方法由业务实现,负责将一段字节数据解码为具体的消息对象。
// 存储接收到的数据 ByteBuf cumulation; private boolean first; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof ByteBuf) { CodecOutputList out = CodecOutputList.newInstance(); try { ByteBuf data = (ByteBuf) msg; first = cumulation == null; if (first) { // 如果 ByteBuf 为空,则将 ByteBuf 指向接收到的消息 cumulation = data; } else { // 如果 ByteBuf 不为空,则将接收到的数据追加到 ByteBuf cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data); } // 解码 ByteBuf 中的数据 callDecode(ctx, cumulation, out); } catch (DecoderException e) { throw e; } catch (Throwable t) { throw new DecoderException(t); } finally { // ByteBuf 中的数据被消费完后,重置消费次数,释放内存,ByteBuf置为null if (cumulation != null && !cumulation.isReadable()) { numReads = 0; cumulation.release(); cumulation = null; // ByteBuf 被消费16后压缩消息 } else if (++ numReads >= discardAfterReads) { numReads = 0; discardSomeReadBytes(); } // 将解码得到的报文对象全部分发下去 int size = out.size(); decodeWasNull = !out.insertSinceRecycled(); fireChannelRead(ctx, out, size); out.recycle(); } } else { ctx.fireChannelRead(msg); } } // 解码 ByteBuf protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List