前言:开发者用到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());
ch.pipline().addLast(
new
DelimiterBasedFrameDecoder(
1024
, delimiter));//1024设置每一行的最大长度
ch.pipline().addLast(
new
FixedLengthFrameDecoder(
1024
));
那就介绍一种科一解决不定长协议的分包粘包方法: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
备忘,参考文章:http://linkedkeeper.com/detail/blog.action?bid=105 里面介绍了更多netty解决粘包分包的方法