netty半包粘包 处理_netty解决粘包半包问题

前言:开发者用到TCP/IP交互时,偶尔会遇到粘包或者半包的数据,这种情况有时会对我们的程序造成严重的影响,netty框架为解决这种问题提供了若干框架

1. LineBasedFrameDecoder:通过在包尾添加回车换行符 \r\n 来区分整包消息。

说明:LineBasedFrameDecoder 是从 ByteBuf 的可读字节中找到 \n 或者 \r\n,找到之后就以此为结束,然后将当前读取到的数据组成一行。

使用方法:ch.pipline().addLast(new LineBasedFrameDecoder(1024));//1024是设置每一行的最大长度,如果读到最大长度还没有结束符,会抛出异常,结束当前读取到的数据

2.DelimiterBasedFrameDecoder:通过特殊字符作为分隔符来区分整包消息

说明:客户端和服务端协议中有一致的特殊字符,来代表每一包的结束

如:客户端发送3条消息:"你好你好你好$_"

"hellohellohello$_"

"赞赞$_"

服务端为了防止分包粘包,使用方法如下:

ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());

那就介绍一种科一解决不定长协议的分包粘包方法:LengthFieldBasedFrameDecoder

先看一下LengthFieldBasedFrameDecoder源码中一个构造函数:

public LengthFieldBasedFrameDecoder(

int maxFrameLength,

int lengthFieldOffset, int lengthFieldLength,

int lengthAdjustment, int initialBytesToStrip) {

this(

maxFrameLength,

lengthFieldOffset, lengthFieldLength, lengthAdjustment,

initialBytesToStrip, true);

}

其中有5个参数的定义我用一个协议数据举例

maxFrameLength:单个包的最大长度,根据实际环境定义

lengthFieldOffset:表示数据长度字段开始的偏移量,上图中,表示数据长度的字段前边还有协议头、版本号、日期等字段,一共占了12字节,所以按照这个协议,这个参数填写12

lengthFieldOffset:数据长度字段所占的字节数,上图中协议长度占用2个字节,这个参数写2

lengthAdjustment:lengthAdjustment +数据长度取值 = 数据长度字段之后剩下包的字节数,上图中,除了协议长度和数据内容,后边还有协议尾和CRC认证,占用6个字节,这个参数写6

initialBytesToStrip:从整个包开始,向后忽略的字节数(我写成0)

按照以上协议,我的使用方法就是:ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024,12,2,6,0));

这句代码加在自定义Hander的前边,否则无效。这样就解决了粘包和分包的问题

但是我现在遇见的问题是:我的协议数据长度字段和数据内容实际长度可能是不对应的

我的协议是如果长度字段不是8的倍数,要在数据后补0 ,例如:

协议长度字段是0046 46不是8的倍数 要补两个0 所以导致以上所有方法都不适合我,因为我不知道长度究竟会是几,究竟会补几个0,于是我自定义LengthFieldBasedFrameDecoder解决这个问题

源码中LengthFieldBasedFrameDecoder继承的是ByteToMessageDecoder(以上所有方法都是继承这个类)

解决方案:自己创建一个类,继承ByteToMessageDecoder,将LengthFieldBasedFrameDecoder源码拷贝到自己新建的类,我的代码:

public class SelfDefineEncodeHandler extends ByteToMessageDecoder {

private final ByteOrder byteOrder;

private final int maxFrameLength;

private final int lengthFieldOffset;

private final int lengthFieldLength;

private final int lengthFieldEndOffset;

private final int lengthAdjustment;

private final int initialBytesToStrip;

private final boolean failFast;

private boolean discardingTooLongFrame;

private long tooLongFrameLength;

private long bytesToDiscard;

/**

* Creates a new instance.

*

* @param maxFrameLength

* the maximum length of the frame. If the length of the frame is

* greater than this value, {@link TooLongFrameException} will be

* thrown.

* @param lengthFieldOffset

* the offset of the length field

* @param lengthFieldLength

* the length of the length field

*/

public SelfDefineEncodeHandler(

int maxFrameLength,

int lengthFieldOffset, int lengthFieldLength) {

this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 0, 0);

}

/**

* Creates a new instance.

*

* @param maxFrameLength

* the maximum length of the frame. If the length of the frame is

* greater than this value, {@link TooLongFrameException} will be

* thrown.

* @param lengthFieldOffset

* the offset of the length field

* @param lengthFieldLength

* the length of the length field

* @param lengthAdjustment

* the compensation value to add to the value of the length field

* @param initialBytesToStrip

* the number of first bytes to strip out from the decoded frame

*/

public SelfDefineEncodeHandler(

int maxFrameLength,

int lengthFieldOffset, int lengthFieldLength,

int lengthAdjustment, int initialBytesToStrip) {

this(

maxFrameLength,

lengthFieldOffset, lengthFieldLength, lengthAdjustment,

initialBytesToStrip, true);

}

/**

* Creates a new instance.

*

* @param maxFrameLength

* the maximum length of the frame. If the length of the frame is

* greater than this value, {@link TooLongFrameException} will be

* thrown.

* @param lengthFieldOffset

* the offset of the length field

* @param lengthFieldLength

* the length of the length field

* @param lengthAdjustment

* the compensation value to add to the value of the length field

* @param initialBytesToStrip

* the number of first bytes to strip out from the decoded frame

* @param failFast

* If true, a {@link TooLongFrameException} is thrown as

* soon as the decoder notices the length of the frame will exceed

* maxFrameLength regardless of whether the entire frame

* has been read. If false, a {@link TooLongFrameException}

* is thrown after the entire frame that exceeds maxFrameLength

* has been read.

*/

public SelfDefineEncodeHandler(

int maxFrameLength, int lengthFieldOffset, int lengthFieldLength,

int lengthAdjustment, int initialBytesToStrip, boolean failFast) {

this(

ByteOrder.BIG_ENDIAN, maxFrameLength, lengthFieldOffset, lengthFieldLength,

lengthAdjustment, initialBytesToStrip, failFast);

}

/**

* Creates a new instance.

*

* @param byteOrder

* the {@link ByteOrder} of the length field

* @param maxFrameLength

* the maximum length of the frame. If the length of the frame is

* greater than this value, {@link TooLongFrameException} will be

* thrown.

* @param lengthFieldOffset

* the offset of the length field

* @param lengthFieldLength

* the length of the length field

* @param lengthAdjustment

* the compensation value to add to the value of the length field

* @param initialBytesToStrip

* the number of first bytes to strip out from the decoded frame

* @param failFast

* If true, a {@link TooLongFrameException} is thrown as

* soon as the decoder notices the length of the frame will exceed

* maxFrameLength regardless of whether the entire frame

* has been read. If false, a {@link TooLongFrameException}

* is thrown after the entire frame that exceeds maxFrameLength

* has been read.

*/

public SelfDefineEncodeHandler(

ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength,

int lengthAdjustment, int initialBytesToStrip, boolean failFast) {

if (byteOrder == null) {

throw new NullPointerException("byteOrder");

}

if (maxFrameLength <= 0) {

throw new IllegalArgumentException(

"maxFrameLength must be a positive integer: " +

maxFrameLength);

}

if (lengthFieldOffset < 0) {

throw new IllegalArgumentException(

"lengthFieldOffset must be a non-negative integer: " +

lengthFieldOffset);

}

if (initialBytesToStrip < 0) {

throw new IllegalArgumentException(

"initialBytesToStrip must be a non-negative integer: " +

initialBytesToStrip);

}

if (lengthFieldOffset > maxFrameLength - lengthFieldLength) {

throw new IllegalArgumentException(

"maxFrameLength (" + maxFrameLength + ") " +

"must be equal to or greater than " +

"lengthFieldOffset (" + lengthFieldOffset + ") + " +

"lengthFieldLength (" + lengthFieldLength + ").");

}

this.byteOrder = byteOrder;

this.maxFrameLength = maxFrameLength;

this.lengthFieldOffset = lengthFieldOffset;

this.lengthFieldLength = lengthFieldLength;

this.lengthAdjustment = lengthAdjustment;

lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;

this.initialBytesToStrip = initialBytesToStrip;

this.failFast = failFast;

}

@Override

protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {

Object decoded = decode(ctx, in);

if (decoded != null) {

out.add(decoded);

}

}

/**

* Create a frame out of the {@link ByteBuf} and return it.

*

* @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to

* @param in the {@link ByteBuf} from which to read data

* @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could

* be created.

*/

protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

if (discardingTooLongFrame) {

long bytesToDiscard = this.bytesToDiscard;

int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes());

in.skipBytes(localBytesToDiscard);

bytesToDiscard -= localBytesToDiscard;

this.bytesToDiscard = bytesToDiscard;

failIfNecessary(false);

}

if (in.readableBytes() < lengthFieldEndOffset) {

return null;

}

int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset;

long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder);

System.out.println("---------------"+frameLength);

if (frameLength < 0) {

in.skipBytes(lengthFieldEndOffset);

throw new CorruptedFrameException(

"negative pre-adjustment length field: " + frameLength);

}

frameLength += lengthAdjustment + lengthFieldEndOffset;

if (frameLength < lengthFieldEndOffset) {

in.skipBytes(lengthFieldEndOffset);

throw new CorruptedFrameException(

"Adjusted frame length (" + frameLength + ") is less " +

"than lengthFieldEndOffset: " + lengthFieldEndOffset);

}

if (frameLength > maxFrameLength) {

long discard = frameLength - in.readableBytes();

tooLongFrameLength = frameLength;

if (discard < 0) {

// buffer contains more bytes then the frameLength so we can discard all now

in.skipBytes((int) frameLength);

} else {

// Enter the discard mode and discard everything received so far.

discardingTooLongFrame = true;

bytesToDiscard = discard;

in.skipBytes(in.readableBytes());

}

failIfNecessary(true);

return null;

}

// never overflows because it's less than maxFrameLength

int frameLengthInt = (int) frameLength;

if (in.readableBytes() < frameLengthInt) {

return null;

}

if (initialBytesToStrip > frameLengthInt) {

in.skipBytes(frameLengthInt);

throw new CorruptedFrameException(

"Adjusted frame length (" + frameLength + ") is less " +

"than initialBytesToStrip: " + initialBytesToStrip);

}

in.skipBytes(initialBytesToStrip);

// extract frame

int readerIndex = in.readerIndex();

int actualFrameLength = frameLengthInt - initialBytesToStrip;

ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength);

in.readerIndex(readerIndex + actualFrameLength);

return frame;

}

/**

* Decodes the specified region of the buffer into an unadjusted frame length. The default implementation is

* capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer. Override this method to

* decode the length field encoded differently. Note that this method must not modify the state of the specified

* buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.)

*

* @throws DecoderException if failed to decode the specified region

*/

protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {

buf = buf.order(order);

long frameLength;

switch (length) {

case 1:

frameLength = buf.getUnsignedByte(offset);

break;

case 2:

frameLength = buf.getUnsignedShort(offset);

break;

case 3:

frameLength = buf.getUnsignedMedium(offset);

break;

case 4:

frameLength = buf.getUnsignedInt(offset);

break;

case 8:

frameLength = buf.getLong(offset);

break;

default:

throw new DecoderException(

"unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");

}

return frameLength+8-(frameLength%8);

}

private void failIfNecessary(boolean firstDetectionOfTooLongFrame) {

if (bytesToDiscard == 0) {

// Reset to the initial state and tell the handlers that

// the frame was too large.

long tooLongFrameLength = this.tooLongFrameLength;

this.tooLongFrameLength = 0;

discardingTooLongFrame = false;

if (!failFast ||

failFast && firstDetectionOfTooLongFrame) {

fail(tooLongFrameLength);

}

} else {

// Keep discarding and notify handlers if necessary.

if (failFast && firstDetectionOfTooLongFrame) {

fail(tooLongFrameLength);

}

}

}

/**

* Extract the sub-region of the specified buffer.

*

* If you are sure that the frame and its content are not accessed after

* the current {@link #decode(ChannelHandlerContext, ByteBuf)}

* call returns, you can even avoid memory copy by returning the sliced

* sub-region (i.e. return buffer.slice(index, length)).

* It's often useful when you convert the extracted frame into an object.

* Refer to the source code of {@link ObjectDecoder} to see how this method

* is overridden to avoid memory copy.

*/

protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {

return buffer.retainedSlice(index, length);

}

private void fail(long frameLength) {

if (frameLength > 0) {

throw new TooLongFrameException(

"Adjusted frame length exceeds " + maxFrameLength +

": " + frameLength + " - discarded");

} else {

throw new TooLongFrameException(

"Adjusted frame length exceeds " + maxFrameLength +

" - discarding");

}

}

}

备忘,参考文章:http://linkedkeeper.com/detail/blog.action?bid=105 里面介绍了更多netty解决粘包分包的方法

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421681.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

switch循环

import java.util.Scanner;/*** author Alina* date 2021年10月10日 10:07 下午* switch (表达式) { case 1:* 语句体1;* break;* case 2:* 语句体2;* …

csgo手机上看demo_csgo看demo所有指令

csgo中有许许多多的指令我们可能都用得上&#xff0c;比如设置准星的指令抑或是看Demo的指令。那么今天小编就带大家看看这些看Demo的指令吧。正确答案以demo文件名为 nip.dem为例&#xff0c;Steam安装目录以我的F:/Steam为例1&#xff1a;复制demo文件&#xff0c;放到 \Stea…

C语言输出格式

1 一般格式printf(格式控制&#xff0c;输出表列&#xff09;例如&#xff1a;printf("i%d,ch%c\n",i,ch);说明:(1)“格式控制”是用双撇号括起来的字符串&#xff0c;也称“转换控制字符串”&#xff0c;它包括两种信息&#xff1a;①格式说明&#xff1a;由“%”和…

(Java)反射与文档注释

package com.reflect.test;/*** author Alina* date 2022年02月01日 6:09 下午*/ public class Demo1 {public void method(){System.out.println("Demo1...method");} } package com.reflect.test;import java.lang.annotation.ElementType; import java.lang.anno…

JQ基础练习---图片划过变暗

简单分享下&#xff0c;划过一张图片其余图片变暗&#xff0c;图片划过变暗的简单效果&#xff0c;JQ实现主要是css写法跟思路变化。 <script src"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type"text/javascript"></sc…

postman启动没反应_Linux 0.12上古源码学习(二)-Bochs模拟Linux 0.12内核的启动与运行...

上篇已经初步实现用Bochs仿真了一个X86程序。今天尝试启动早期的Linux 0.12内核。官网上已经有编译好的Linux 0.12启动镜像及根文件系统镜像。我们可以直接下载下来先使用&#xff0c;后面的文章会详细介始如何编译生成这些镜像文件。Index of /Linux.old/bochs-images/​www.o…

[bbk4343]小布-OCP 042全真试题讲解

参考视频: [bbk4343]小布-OCP 042全真试题讲解 1、You are in the middle of a transaction and very crucial data has been modified.Because of a hardware failure,the instance has shutdown before synchronizing all the database files.Which two statements are true …

vscode 搭建web工程_Dubbo 分布式架构搭建教育 PC 站

架构设计与环境搭建项目架构项目介绍教育 PC 站是提供给学员观看技术视频的网站。学员使用手机号注册登录后&#xff0c;可以选择适合自己的课程&#xff0c;并观看课程视频&#xff0c;当然&#xff0c;有免费的课程&#xff0c;也有 VIP 专属课程。技术选型前端技术选型Vue.j…

mysql 之jdbc idea版

安装时注意jdk mysql 驱动版本 package com.jdsc;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;/*** author Alina* date 2022年02月04日 11:11 下午*/ public class jdbsdemo2 {public static voi…

使用jdbc执行SQL实现登录查询1-带配置文件和工具类

1.jdbc.properties 配置文件 url jdbc:mysql:///db2 user root password 12345678 driver com.mysql.cj.jdbc.Driver 2.Utils.JDBCUtils抽取的工具类 package Utils;import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; im…

jdk 环境变量_Windows下jdk下载安装与环境变量配置

下载安装jdk百度搜索jdk版本&#xff0c;以1.8版本为例&#xff0c;百度搜索“jdk1.8”&#xff0c;一般是第一个。百度搜索jdk1.8打开jdk下载页面&#xff0c;这里我们下载jdk1.8 x64版本下载地址&#xff1a;jdk1.8下载地址jdk1.8下载页面下载历史版本的jdk自动跳转登录&…

路由器配置的问题

刚刚买了个路由器&#xff0c;配置起来很容易吧&#xff0c;像往常一样&#xff0c;连接路由器&#xff0c;按照配置过程配置完毕&#xff0c;打开网页&#xff0c;bingo~&#xff0c;上不去网&#xff0c;为啥呢&#xff1f; 然后断开路由器&#xff0c;直接连接网线&#xff…

使用jdbc执行SQL实现登录查询2-避免SQL注入版

配置文件及工具类参考1 package com.jdsc;import javax.rmi.CORBA.Util; import java.sql.*; import java.util.Scanner;/*** author Alina* date 2022年02月06日 12:13 上午* 判断用户登录是否成功* 使用prepareStatement 执行SQL谨防SQL注入*/ public class jdbcDemo7 {publ…

一般试卷的纸张大小是多少_考试试卷统一设置标准规格要求

常用纸张按尺寸可分为A和B两类:A类就是我们通常说的大度纸&#xff0c;整张纸的尺寸是889*1194mm&#xff0c;可裁切A1(大对开&#xff0c;570*840mm)、A2(大四开&#xff0c;420*570mm)、A3(大八开&#xff0c;285*420mm)、A4(大十六开&#xff0c;210*285mm)、A5(大三十二开&…

上传文件

java实现文件上传&#xff0c;参看: 用commons-fileupload-1.2 实现文件上传 转载于:https://www.cnblogs.com/chaoguo1234/p/3156700.html

js true false 转0 1_「0-1岁」早产儿何时一段转二段奶粉

早产儿指的是37周之前出生的宝宝&#xff0c;想必足月宝宝来说&#xff0c;早产宝宝各方面发育都不完善&#xff0c;所以家长在照顾的时候要格外细心。当早产儿出生后&#xff0c;应当先喝早产儿奶粉&#xff0c;等身体发育发育到一定程度时&#xff0c;才更换奶粉。那么早产儿…

使用jdbc执行SQL实现登录查询3 -事务查询版

package com.jdsc;import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement;/*** author Alina* date 2022年02月06日 2:51 下午* 开启事务管理数据库&#xff0c;如果事务有异常则回滚到之前的状态* JDBC控…

邮件服务器方案参考

国内私募机构九鼎控股打造APP&#xff0c;来就送 20元现金领取地址&#xff1a;http://jdb.jiudingcapital.com/phone.html内部邀请码&#xff1a;C8E245J &#xff08;不写邀请码&#xff0c;没有现金送&#xff09;国内私募机构九鼎控股打造&#xff0c;九鼎投资是在全国股份…

halcon读取dxf坐标翻转_Halcon二维仿射变换实例探究

二维仿射变换&#xff0c;顾名思义就是在二维平面内&#xff0c;对对象进行平移、旋转、缩放等变换的行为(当然还有其他的变换&#xff0c;这里仅论述这三种最常见的)。Halcon中进行仿射变换的常见步骤如下&#xff1a;① 通过hom_mat2d_identity算子创建一个初始化矩阵(即[1.0…