| 
                        副标题[/!--empirenews.page--]
                         接上篇《TCP 粘包和半包 介绍及解决(上)》 
上一篇介绍了粘包和半包及其通用的解决方案,今天重点来看一下 Netty 是如何实现封装成帧(Framing)方案的。 
  
解码核心流程 
之前介绍过三种解码器FixedLengthFrameDecoder、DelimiterBasedFrameDecoder、LengthFieldBasedFrameDecoder,它们都继承自ByteToMessageDecoder,而ByteToMessageDecoder继承自ChannelInboundHandlerAdapter,其核心方法为channelRead。因此,我们来看看ByteToMessageDecoder的channelRead方法: 
- @Override 
 - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
 -  if (msg instanceof ByteBuf) { 
 -  CodecOutputList out = CodecOutputList.newInstance(); 
 -  try { 
 -  // 将传入的消息转化为data 
 -  ByteBuf data = (ByteBuf) msg; 
 -  // 最终实现的目标是将数据全部放进cumulation中 
 -  first = cumulation == null; 
 -  // 第一笔数据直接放入 
 -  if (first) { 
 -  cumulation = data; 
 -  } else { 
 -  // 不是第一笔数据就进行追加 
 -  cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data); 
 -  } 
 -  // 解码 
 -  callDecode(ctx, cumulation, out); 
 -  } 
 -  // 以下代码省略,因为不属于解码过程 
 -  } 
 
  
再来看看callDecode方法: 
- protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { 
 -  try { 
 -  while (in.isReadable()) { 
 -  int outoutSize = out.size(); 
 -  if (outSize > 0) { 
 -  // 以下代码省略,因为初始状态时,outSize 只可能是0,不可能进入这里 
 -  } 
 -  int oldInputLength = in.readableBytes(); 
 -  // 在进行 decode 时,不执行handler的remove操作。 
 -  // 只有当 decode 执行完之后,开始清理数据。 
 -  decodeRemovalReentryProtection(ctx, in, out); 
 -  // 省略以下代码,因为后面的内容也不是解码的过程 
 
  
再来看看decodeRemovalReentryProtection方法: 
- final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) 
 -  throws Exception { 
 -  // 设置当前状态为正在解码 
 -  decodeState = STATE_CALLING_CHILD_DECODE; 
 -  try { 
 -  // 解码 
 -  decode(ctx, in, out); 
 -  } finally { 
 -  // 执行hander的remove操作 
 -  boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING; 
 -  decodeState = STATE_INIT; 
 -  if (removePending) { 
 -  handlerRemoved(ctx); 
 -  } 
 -  } 
 - } 
 - // 子类都重写了该方法,每种实现都会有自己特殊的解码方式 
 - protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception; 
 
  
从上面的过程可以总结出,在解码之前,需要先将数据写入cumulation,当解码结束后,需要通过 handler  进行移除。 
具体解码过程 
刚刚说到decode方法在子类中都有实现,那针对我们说的三种解码方式,一一看其实现。 
1. FixedLengthFrameDecoder 
其源码为: 
- @Override 
 - protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { 
 -  Object decodedecoded = decode(ctx, in); 
 -  if (decoded != null) { 
 -  out.add(decoded); 
 -  } 
 - } 
 - protected Object decode( 
 -  @SuppressWarnings("UnusedParameters") ChannelHandlerContext ctx, ByteBuf in) throws Exception { 
 -  // 收集到的数据是否小于固定长度,小于就代表无法解析 
 -  if (in.readableBytes() < frameLength) { 
 -  return null; 
 -  } else { 
 -  return in.readRetainedSlice(frameLength); 
 -  } 
 - } 
 
  
就和这个类的名字一样简单,就是固定长度进行解码,因此,在设置该解码器的时候,需要在构造方式里传入frameLength。 
2. DelimiterBasedFrameDecoder 
                                                (编辑:52站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |