• 【Netty】ByteToMessageDecoder源码解析


    目录

    1.协议说明

    2.类的实现

    3.Decoder工作流程

    4.源码解析

    4.1 累加器Cumulator(数据累加缓冲区)

    4.2 状态码说明

    4.3 ByteToMessageDecoder#channelRead

    4.4 Decoder实现举例

    5. 如何开发自己的Decoder


    1.协议说明

    Netty框架是基于Java NIO框架,性能彪悍,支持的协议丰富,广受Java爱好者亲莱,支持如下协议

    • TCP/UDP:Netty提供了基于NIO的TCP和UDP编程框架,可以用来构建高性能、高可用性的网络应用。
    • HTTP/HTTPS:Netty提供了HTTP/HTTPS编程框架,可以用来开发Web服务器和客户端。
    • WebSocket:Netty提供了WebSocket编程框架,可以用来实现双向通信应用程序,如聊天室等。
    • SPDY/HTTP2:Netty提供了SPDY和HTTP2编程框架,可以用来实现高效的Web应用程序。
    • MQTT/CoAP:Netty提供了MQTT和CoAP编程框架,可以用来构建IoT应用程序。
       

    我们在基于Netty框架开发过程中往往需要自定义私有协议,如端到端的通信协议,端到平台数据通信协议,我们需要根据业务的特点自定义数据报文格式,举例如下:

    数据报文格式定义(TCP)
    帧头版本命令标识符序列号设备编码帧长正文校验码
    1byte1byte1byte2byte4byte       4byteN个byte2byte

    假如我们定义了上述私有协议的TCP报文,通过netty框架发送和解析

    发送端:某类通信设备(client)

    接收端:Java应用服务(Server)

    本节我主要分析一下server端解析报文的一个过程,client当然也很重要,尤其在建立TCP连接和关闭连接需要严格控制,否则服务端会发现大量的CLOSE_WAIT(被动关闭连接),甚至大量TIME_WAIT(主动关闭连接),关于这个处理之前的文章有讲解。

    本节Server端是基于Netty版本:netty-all-4.1.30.Final

    本节源码分析需求就是要解析一个自定义TCP协议的数据报文进行解码,关于编码解码熟悉网络编程的同学都明白,不清楚的可以稍微查阅一下资料有助于学习为什么要解码以及如何解码。本节不会对具体报文的解析做具体讲解,只对Netty提供的解码器基类ByteToMessageDecoder做一下源码分解,以及如何使用ByteToMessageDecoder开发属于自己的Decoder,接下来我们看看ByteToMessageDecoder的定义。

    1. /*继承ChannelInboundHandlerAdapter
    2. * 字节到消息的编码器,是Inbound操作,这类解码器处理器都不是共享的,因为需要存没有
    3. * 解码完的数据,还有各种状态,是独立的,不能进行共享。
    4. * 所以在pipeline上需要每次创建新的对象,不是能够进行对象复用。
    5. */
    6. public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
    7. }

    2.类的实现

    解码器的ByteToMessageDecoder ,该类继承了ChannelInboundHandlerAdapter ,ChannelInboundHandlerAdapter继承ChannelHandlerAdapter,

    ChannelInboundHandlerAdapter实现ChannelInboundHandler接口,也就是说ChannelInboundHandler定义了解码器需要处理的工作(方法)

    ChannelInboundHandlerAdapter是一个适配器模式,负责Decoder的扩展。它的实现有很多,简单列举一下:
    • HeartBeatHandler
    • MessageToMessageDecoder
    • SimpleChannelInboundHandler(实现抽象了channelRead方法,提供抽象方法channelRead0)
    •  ByteToMessageDecoder
    •  。。。。。。

    以上都是比较常用的Decoder或Handler,基于这些基类还定义了很多handler,有兴趣的同学可以跟代码查阅。

    3.Decoder工作流程

    每当数据到达Server端时,SocketServer通过Reactor模型分配具体的worker线程进行处理数据,处理数据就需要我们的事先定义好的Decoder以及handler,假如我们定义了以下两个对象:

    • MyDecoder extends ByteToMessageDecoder{} 作为解码器
    • MyHandler extends SimpleChannelInboundHandler{} 作为解码后的业务处理器

    worker线程——〉MyDecoder#channelRead实际就是调用ByteToMessageDecoder#channelRead——〉Cumulator累加器处理——〉

    解码器decode处理(MyDecoder需要实现decode方法)——〉Myhandler#channelRead0处理具体的数据(msg),msg是通过MyDecoder#decode方法解码后的数据对象。

    4.源码解析

    4.1 累加器Cumulator(数据累加缓冲区)

    累加器Cumulator的作用是数据累加缓冲区,解决tcp数据包中出现半包和粘包问题。

    半包:接收到的byte字节不足一个完整的数据包,

    半包处理办法:不足一个完整的数据包先放入累加器不做解码,等待续传的数据包;

    粘包:接收到的byte字节数据包中包括其他数据包的数据(靠数据包协议中定义的帧头帧尾标识来识别,多于1个以上的帧头或帧尾数据包为粘包数据),

    粘包处理办法:按照数据包帧结构定义去解析,需要结合累加器,解析完一个数据包交给handler去处理,剩下的不足一个数据包长度的字节保存在累加器等待续传的数据包收到之后继续解码。

    ByteToMessageDecoder内部定义了Cumulator接口

    1. /**
    2. * Cumulate {@link ByteBuf}s.
    3. */
    4. public interface Cumulator {
    5. /**
    6. * Cumulate the given {@link ByteBuf}s and return the {@link ByteBuf} that holds the cumulated bytes.
    7. * The implementation is responsible to correctly handle the life-cycle of the given {@link ByteBuf}s and so
    8. * call {@link ByteBuf#release()} if a {@link ByteBuf} is fully consumed.
    9. */
    10. ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in);
    11. }

    其中在类最开始的时候构建了两个对象,分别是MERGE_CUMULATOR,COMPOSITE_CUMULATOR,代码如下

    1. /**
    2. * Cumulate {@link ByteBuf}s by merge them into one {@link ByteBuf}'s, using memory copies.
    3. */
    4. public static final Cumulator MERGE_CUMULATOR = new Cumulator() {
    5. /**
    6. * 主要是做一般缓冲区的合并,直接将新的缓冲区拷贝到累加缓冲区中。
    7. * cumulation 累加的缓冲区
    8. * in 新读取的数据
    9. */
    10. @Override
    11. public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
    12. try {
    13. final ByteBuf buffer;
    14. //1.如果累加器ByteBuf 剩余可写的capacity不满足当前需要写入的ByteBuf(in)长度,则进行扩容累加器ByteBuf容量,执行expandCumulation方法
    15. if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes()
    16. || cumulation.refCnt() > 1 || cumulation.isReadOnly()) {
    17. buffer = expandCumulation(alloc, cumulation, in.readableBytes());
    18. } else {
    19. buffer = cumulation;
    20. }
    21. //2.写入累加器并返回更新后的cumulation
    22. buffer.writeBytes(in);
    23. return buffer;
    24. } finally {
    25. // We must release in in all cases as otherwise it may produce a leak if writeBytes(...) throw
    26. // for whatever release (for example because of OutOfMemoryError)
    27. //3.由于是对in的拷贝,in不需要在进行往下的传递,所以应该直接释放,需要release
    28. in.release();
    29. }
    30. }
    31. };
    32. //通过对CompositeByteBuf的累加器的实现,CompositeByteBuf内部使用ComponentList
    33. //实现对ByteBuf进行追加
    34. //ComponentList是ArrayList的实现,所以每次Add操作都是一次内存拷贝。
    35. public static final Cumulator COMPOSITE_CUMULATOR = new Cumulator() {
    36. @Override
    37. public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
    38. ByteBuf buffer;
    39. try {
    40. if (cumulation.refCnt() > 1) {
    41. buffer = expandCumulation(alloc, cumulation, in.readableBytes());
    42. buffer.writeBytes(in);
    43. } else {
    44. CompositeByteBuf composite;
    45. if (cumulation instanceof CompositeByteBuf) {
    46. composite = (CompositeByteBuf) cumulation;
    47. } else {
    48. composite = alloc.compositeBuffer(Integer.MAX_VALUE);
    49. composite.addComponent(true, cumulation);
    50. }
    51. composite.addComponent(true, in);
    52. in = null;
    53. buffer = composite;
    54. }
    55. return buffer;
    56. } finally {
    57. if (in != null) {
    58. //因为是对ByteBuf in的拷贝,所以需要释放
    59. in.release();
    60. }
    61. }
    62. }
    63. };

    4.2 状态码说明

    1. //状态码
    2. private static final byte STATE_INIT = 0; //表示初始化状态
    3. private static final byte STATE_CALLING_CHILD_DECODE = 1; //表示正在调用子类解码器
    4. private static final byte STATE_HANDLER_REMOVED_PENDING = 2; //表示handler正在删除
    5. ByteBuf cumulation; //核心重点 累加的缓冲区(累加器)
    6. private Cumulator cumulator = MERGE_CUMULATOR; //累加器的默认状(默认是合并的累加器)
    7. private boolean singleDecode; //是否解码一次
    8. private boolean decodeWasNull; //out对象有被添加或者设置,表是有读过了
    9. private boolean first; //是否是第一次累加缓冲区,true表示第一次累加缓存区,false表示不是第一次累加缓冲区
    10. /**
    11. * A bitmask where the bits are defined as
    12. *
      • *
      • {@link #STATE_INIT}
    13. *
    14. {@link #STATE_CALLING_CHILD_DECODE}
  • *
  • {@link #STATE_HANDLER_REMOVED_PENDING}
  • *
  • */
  • private byte decodeState = STATE_INIT; //解码初始状态
  • private int discardAfterReads = 16; //读取16个字节后丢弃已读的
  • private int numReads; //累加器(cumulation)读取数据的次数
  • 4.3 ByteToMessageDecoder#channelRead

    1. /**
    2. * 接收数据并读取
    3. * 只是在业务的处理前进行解码和缓存的操作。主要将读取的数据(msg)叠加到累加缓冲区(cumulation)中,并且调用解码方法(callDecode)
    4. */
    5. @Override
    6. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    7. //如果是设置在ServerBootstrap的childHandler那么msg的对象类型就是ByteBuf,否则就执行else
    8. if (msg instanceof ByteBuf) {
    9. //CodecOutputList对象可以查阅文档https://www.freesion.com/article/4800509769/
    10. //这个out对象随着callDecode方法进行传递,解码后的数据保存在out中
    11. CodecOutputList out = CodecOutputList.newInstance();
    12. try {
    13. ByteBuf data = (ByteBuf) msg;
    14. //1.第一次读取数据时cumulation是null,所以first是ture
    15. first = cumulation == null;
    16. if (first) {
    17. //2.第一次收到数据累加器为null,将数据赋值给cumulation
    18. cumulation = data;
    19. } else {
    20. //3.第二次收到数据累加器需要评估ByteBuf的capacity,够用则追加到cumulation,capacity不够则进行扩容
    21. cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data);
    22. }
    23. //4.调用callDecode进行解码
    24. //5.CodecOutputList out对象保存解码后的数据,它的实现是基于AbstractList,
    25. //重新定义了add(),set(),remove()等方法,其中add()方法实现对Array数组中
    26. //进行insert,没有直接拷贝而是通过对象引用,将对象指向数据索引的index,是性能的一个提升。
    27. callDecode(ctx, cumulation, out);
    28. } catch (DecoderException e) {
    29. throw e;
    30. } catch (Exception e) {
    31. throw new DecoderException(e);
    32. } finally {
    33. //6.如果累加器cumulation中的数据被解码器读完了,则可以完全释放累加器cumulation
    34. if (cumulation != null && !cumulation.isReadable()) {
    35. numReads = 0;
    36. cumulation.release();
    37. cumulation = null;
    38. } else if (++ numReads >= discardAfterReads) {
    39. // We did enough reads already try to discard some bytes so we not risk to see a OOME.
    40. // See https://github.com/netty/netty/issues/4275
    41. //7.释放累加器cumulation里面的已读数据,防止cumulation无限制增长
    42. numReads = 0;
    43. discardSomeReadBytes();
    44. }
    45. int size = out.size();
    46. //8.out对象有被添加或者设置,表示有读过了
    47. decodeWasNull = !out.insertSinceRecycled();
    48. //9.解码完成后需要触发事先定义好的handler的channelRead()方法处理解码后的out数据
    49. fireChannelRead(ctx, out, size);
    50. //10.最终需要回收out对象
    51. out.recycle();
    52. }
    53. } else {
    54. //11.非ByteBuf直接向后触发传递
    55. ctx.fireChannelRead(msg);
    56. }
    57. }

    ByteToMessageDecoder#channelRead看到了将累加器交给callDecoder方法

    注意,这里有个while循环,会循环读取缓冲区字节数据,循环调用我们自己实现的MyDecoder,所以在自己实现的Mydecoder#decode()方法中不需要循环读取字节,只需要按照指定的数据报文帧结构解码即可,解码后的对象需要加入到List out。

    1. //这里ByteBuf in 就是累加器对象cumulaction,从累加器中读取字节交给decode去解码
    2. // List out 是list,存放解码后的对象
    3. protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List out) {
    4. try {
    5. //1. 循环读取累加器对象的byte,循环直到累加缓冲区没有可读数据,或者直接跳出循环为止
    6. while (in.isReadable()) {
    7. int outSize = out.size();
    8. //2.如果解码后out对象中产生数据则触发后边的handler(MyHandler)处理数据
    9. if (outSize > 0) {
    10. fireChannelRead(ctx, out, outSize);
    11. // 处理完清空out
    12. out.clear();
    13. //判断handler的上下文是否被删除,被删除就不在处理,直接跳出循环
    14. if (ctx.isRemoved()) {
    15. break;
    16. }
    17. outSize = 0;
    18. }
    19. //3.继续解析累加器传递过来的byte,获取可读的字节长度
    20. int oldInputLength = in.readableBytes();
    21. //4.注意out对象是从channelRead()方法传递过来,继续传递下去
    22. decodeRemovalReentryProtection(ctx, in, out);
    23. if (ctx.isRemoved()) {
    24. break;
    25. }
    26. //4.判断当前的outSize是否等于list列表的长度,等于表示没有生成新的消息,可能读取的字节长度不够,无法解码出一个消息。
    27. //不相等说明解码后生成了一个新的消息
    28. if (outSize == out.size()) {
    29. //5.如果解码器decode没有消费累加器 in 任何字节,结束循环 if (oldInputLength == in.readableBytes()) {
    30. break;
    31. //6.否则继续循环调用解码器decode
    32. } else {
    33. continue;
    34. }
    35. }
    36. //7.如果已经解码获取到消息,但是累加器ByteBuf in中可读字节数依然没有变化,说明readIndex没有修改,
    37. //说明实现的解码器decode()方法有bug,需要检查自身代码问题(需要readByte(),不能getByte(),getByte()会导致readIndex不会修改)
    38. if (oldInputLength == in.readableBytes()) {
    39. throw new DecoderException(
    40. StringUtil.simpleClassName(getClass()) +
    41. ".decode() did not read anything but decoded a message.");
    42. }
    43. //8.是否设定每次调用解码器一次,如果是,则结束本次解码
    44. if (isSingleDecode()) {
    45. break;
    46. }
    47. }
    48. } catch (DecoderException e) {
    49. } catch (Exception cause) {
    50. }
    51. }
    52. 继续查看ByteToMessageDecoder#decodeRemovalReentryProtection方法

      1. //此方法不允许重写
      2. final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List out)
      3. throws Exception {
      4. //1.切换ByteToMessageDecoder的状态为子类解码状态,表示当前正在解码
      5. decodeState = STATE_CALLING_CHILD_DECODE;
      6. try {
      7. //2.核心方法decode,这是一个抽象方法,没有实现,需要在自定义的Decoder(Mydecoder)进行实现
      8. //3.自定义Decoder需要将解码后的数据放入到out对象中
      9. decode(ctx, in, out);
      10. } finally {
      11. //4.判断decodeState状态是否为待删除,如果不是返回ture,如果是返回false
      12. boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING;
      13. //5.重置decodeState为初始化状态
      14. decodeState = STATE_INIT;
      15. //6.如果当前ByetToMessageDecoder为待删除状态
      16. if (removePending) {
      17. /7.删除handlercontext的上下文
      18. handlerRemoved(ctx);
      19. }
      20. }
      21. }
      22. //解码decode方法需要子类(自定义的实现类)去实现该方法,最终将解码后的消息放入List out
      23. protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception;

        4.4 Decoder实现举例

        基于ByteToMessageDecoder的实现很多,简单列举一下

        • JsonObjectDecoder
        • RedisDecoder
        • XmlDecoder
        • MqttDecoder
        • ReplayingDecoder
        • SslDecoder
        • DelimiterBasedFrameDecoder
        • FixedLengthFrameDecoder
        • LengthFieldBasedFrameDecoder
        • ....

        我们拿JsonObjectDecoder举例如下:

        1. @Override
        2. protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
        3. // 省略代码。。。。。。
        4. int idx = this.idx;
        5. int wrtIdx = in.writerIndex();
        6. //省略代码。。。。。。。
        7. for (/* use current idx */; idx < wrtIdx; idx++) {
        8. byte c = in.getByte(idx);
        9. if (state == ST_DECODING_NORMAL) {
        10. decodeByte(c, in, idx);
        11. if (openBraces == 0) {
        12. ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
        13. //1.解析后的对象加入out中
        14. if (json != null) {
        15. out.add(json);
        16. }
        17. in.readerIndex(idx + 1);
        18. reset();
        19. }
        20. } else if (state == ST_DECODING_ARRAY_STREAM) {
        21. //2.自身实现解析json格式的方法
        22. decodeByte(c, in, idx);
        23. if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) {
        24. for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
        25. in.skipBytes(1);
        26. }
        27. // skip trailing spaces.
        28. int idxNoSpaces = idx - 1;
        29. while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
        30. idxNoSpaces--;
        31. }
        32. ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
        33. //3.解析后的对象加入out中
        34. if (json != null) {
        35. out.add(json);
        36. }
        37. in.readerIndex(idx + 1);
        38. if (c == ']') {
        39. reset();
        40. }
        41. }
        42. }
        43. //省略代码。。。。。。
        44. }
        45. if (in.readableBytes() == 0) {
        46. this.idx = 0;
        47. } else {
        48. this.idx = idx;
        49. }
        50. this.lastReaderIndex = in.readerIndex();
        51. }
        52. 5. 如何开发自己的Decoder

          读了ByteToMessageDecoder的部分源码,以及它的实现JsonObjectDecoder,那么如果我们自己实现一个Decoder该如何实现,这里提供三个思路给大家,有时间再补充代码。

          • 基于ByteToMessageDecoder实现,MyDecoder extends ByteToMessageDecoder{实现decode()方法},可参考RedisDecoder、XmlDecoder等实现。
          • 基于ChannelInboundHandlerAdapter实现,这个时候需要自己负责解决TCP报文半包和粘包问题,重写其中的channelRead()方法。
          • 直接使用已经实现ByteToMessageDecoder的解码器,如FixedLengthFrameDecoder、DelimiterBasedFrameDecoder、LengthFieldBasedFrameDecoder。

          注意事项:

           * Be aware that sub-classes of {@link ByteToMessageDecoder} MUST NOT
           * annotated with {@link @Sharable}.

          ByteToMessageDecoder的子类不能使用@Sharable注解修饰,因为解码器只能单独为一个Channel进行解码,也就是说每个worker线程需要独立的Decoder。


           *


           * Some methods such as {@link ByteBuf#readBytes(int)} will cause a memory leak if the returned buffer
           * is not released or added to the out {@link List}. Use derived buffers like {@link ByteBuf#readSlice(int)}
           * to avoid leaking memory.

          如果基于ChannelInboundHandlerAdapter自己实现Decoder#channelRead()方法时注意内存泄露问题,ByteBuf#readBytes(int)方法会产生一个新的ByteBuf,需要手动释放。

          或者

          基于ByteToMessageDecoder实现decode()方法时将解析后的对象放入out对象中(上面源码分析中有提示)

          或者

          使用派生的ByteBuf,如调用ByteBuf#readSlice(int)方法,返回的ByteBuf与原有ByteBuf共享内存,不会产生新的Reference count,可以避免内存泄露。

          Netty Project官网也有说明:

          Reference counted objects
          ByteBuf.duplicate(), ByteBuf.slice() and ByteBuf.order(ByteOrder) create a derived buffer which shares the memory region of the parent buffer. A derived buffer does not have its own reference count, but shares the reference count of the parent buffer.

        53. 相关阅读:
          3.2 基于vexpress-a9 arm平台 的QEMU仿真的rootfs镜像环境搭建
          java毕业设计校园社区系统mybatis+源码+调试部署+系统+数据库+lw
          领导:谁再用redis过期监听实现关闭订单,立马滚蛋!
          NTFS文件系统.权限
          信钰证券:股票k线图中b和s是什么情况?一文带你了解!
          python学习:break用法详解
          Redis快速上手篇五(持久化)
          Java 将list集合的字符串格式转为Map
          算法竞赛个人注意事项
          L85.linux命令每日一练 -- 第12章 Linux系统常用内置命令(一)
        54. 原文地址:https://blog.csdn.net/smallbirdnq/article/details/133309134