MessageToMessageDecoder粘包

java 复制代码
import java.time.LocalDateTime;
import java.util.*;
import org.apache.commons.lang.time.DateUtils;

import com.liangxs.commom.HexUtils;
import com.liangxs.commom.SpringUtils;
import io.netty.buffer.Unpooled;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import com.liangxs.logger.Logger;
import com.liangxs.logger.LoggerFactory;
import io.netty.util.ReferenceCountUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

 
public class DianciCustomDecoder extends MessageToMessageDecoder<ByteBuf>{
  
  private byte[] remainingBytes;//剩下的bytes
  private ByteBuf currBB = null;//组合后的新ByteBuf 
  Logger log = LoggerFactory.getLogger(LoggerFactory.MY_INFO_LOG);

  // 超时
  @Override
  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    super.userEventTriggered(ctx, evt);
    if (evt instanceof IdleStateEvent) {
      IdleStateEvent e = (IdleStateEvent) evt;
      if (e.state() == IdleState.WRITER_IDLE) {
      } else if (e.state() == IdleState.READER_IDLE) {
   			ctx.channel().close();
      }
    }

  }

  // 连接成功
  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    String channelId = ctx.channel().id().asLongText();
  
  }

  // 断开
  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    state = false;
    SpringUtils.getBean(com.liangxs.dbapi.javaDyAdmin.DianchiDataParseService.class).setChannel(null);
    String channelId = ctx.channel().id().asLongText();
    ctx.close();
    super.channelInactive(ctx);
  
  }
 
 protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
 /***
 每次有数据到来时产生新数据的ByteBuf容易粘包。经发现最大接收数据一次在2000多字节,大于这个数在下一次接收,但它们又需要是一个整体来解析。
 我们用一个全局BUF变量来组装每次请求的进行重新组合成一个buf


*/
         
        if(remainingBytes == null) {
            currBB = msg;
        }else {
        	//根据remainingBytes余下的长度+msg中可读取的长度构建一个新的tb数据,并用余下的字节填充tb
            byte[] tb = new byte[remainingBytes.length + msg.readableBytes()];
            //将remainingBytes值复制给tb
            System.arraycopy(remainingBytes, 0, tb, 0, remainingBytes.length);
            /***
			将msg新数据读取到vb变量中将填充到tb中,将tb转为buffer对象给currBB 
			***/
            byte[] vb = new byte[msg.readableBytes()];
            msg.readBytes(vb);
            System.arraycopy(vb, 0, tb, remainingBytes.length, vb.length);
            currBB = Unpooled.copiedBuffer(tb);
        }
         
        while(currBB.readableBytes() > 0) {
           //循环判断是否还有可读取的
            if(!dodecode(ctx, currBB, out)) {//没有可读取的数据了则退出
                break;
            }
        }
       
        if(currBB.readableBytes() > 0) {
        	//currBB余下的字节长度
            remainingBytes = new byte[currBB.readableBytes()];
            //将currBB读取到remainingBytes中
            currBB.readBytes(remainingBytes);
        }else {
            remainingBytes = null;
        }

 }
  protected boolean  dodecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

     if (in.readableBytes() > 3) {//可读取大于3个字节,因为协议头是由一个字节标识+2个字节的数据长度标识组成,
        in.markReaderIndex();//打上读取标记
        byte head =  in.readByte();
        int length = in.readShort();
        /****因为协议中的数据长度是包含头的长度,所以判断可读取数据长度数据减3**/
        if ( in.readableBytes() < length - 3) {
          // 数据长度不够重置readerIndex到上面标记的位置
          in.resetReaderIndex();
          return false;
        }  
         if(in.readableBytes() > 0)//还有可读的可能在下一个包中所以返回TRUE
            return true;
        return false;
       } 
     return false;
    //ReferenceCountUtil.release(in);
  }
}
相关推荐
Otaku love travel35 分钟前
老系统改造增加初始化,自动化数据源配置(tomcat+jsp+springmvc)
java·tomcat·初始化·动态数据源
DKPT1 小时前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star1 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
晨岳1 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆1 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
YuTaoShao1 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
程序员的世界你不懂2 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
AI360labs_atyun2 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
不像程序员的程序媛3 小时前
redis的一些疑问
java·redis·mybatis
知其然亦知其所以然3 小时前
Java 面试高频题:GC 到底回收了什么、怎么回收、啥时候回收?
java·后端·面试