Netty 何时使用帧解码

Netty 何时使用帧解码,netty,Netty,我不清楚在基于MessageToMessageDecoder构建自定义POJO解码器时是否需要使用像LineBasedFrameDecoder这样的帧解码器 它在StringDecoder的Netty源代码中声明 我想是的,买为什么?我问自己这是否应该是一个答案;您可以使用任何您想要的实现。JavaDocs提供了一个关于如何对字符串数据进行编码/解码的示例。您可以实现自己的处理程序并确定字符串的开始和停止 例如,我实现了自己的数据包协议,它为在线视频游戏混合了大量数据。所以我通过读取一个变量然后

我不清楚在基于MessageToMessageDecoder构建自定义POJO解码器时是否需要使用像LineBasedFrameDecoder这样的帧解码器

它在StringDecoder的Netty源代码中声明


我想是的,买为什么?

我问自己这是否应该是一个答案;您可以使用任何您想要的实现。JavaDocs提供了一个关于如何对字符串数据进行编码/解码的示例。您可以实现自己的处理程序并确定字符串的开始和停止

例如,我实现了自己的数据包协议,它为在线视频游戏混合了大量数据。所以我通过读取一个变量然后读取字符串来解码我的字符串。我可以采用相同的逻辑,在StringDecoder处理程序之前创建自己的处理程序来解码字符串长度

编码器

public static ByteBuf encodeString(ByteBuf out, String value, Charset charset) {
    byte[] bytes = value.getBytes(charset);
    encodeArray(out, bytes);
    return out;
}

public static ByteBuf encodeArray(ByteBuf out, byte[] bytes) {
    int length = bytes.length;
    encodeInteger(out, length);
    out.writeBytes(bytes);
    return out;
}

public static ByteBuf encodeInteger(ByteBuf out, int number) {
    return encodeInt(out, number, computeUInt32Size(number));
}

public static ByteBuf encodeInt(ByteBuf out, int number, int numBytes) {
    int originalIndex = out.writerIndex();
    int adjustedI = originalIndex + numBytes;
    final int capacity = out.capacity();
    if (adjustedI > capacity) {
        out.capacity(adjustedI);
    }
    out.writerIndex(out.writerIndex() + numBytes);
    for (int i = adjustedI - 1; i >= originalIndex; i--) {
        int curByte = (number & 0x7F);
        if (i != (adjustedI - 1)) {
            curByte |= 0x80;
        }
        out.setByte(i, curByte);
        number >>>= 7;
    }
    return out;
}

public static int computeUInt32Size(int value) {
    if ((value & (~0 <<  7)) == 0) {
        return 1;
    }
    if ((value & (~0 << 14)) == 0) {
        return 2;
    }
    if ((value & (~0 << 21)) == 0) {
        return 3;
    }
    if ((value & (~0 << 28)) == 0) {
        return 4;
    }
    return 5;
}
public static String unsafeDecodeString(ByteBuf in, Charset standardCharsets) {
    byte[] bytes = unsafeDecodeArray(in);
    return new String(bytes, standardCharsets);
}

public static byte[] unsafeDecodeArray(ByteBuf in) {
    int len = unsafeDecodeInteger(in);
    return unsafeDecodeArray(in, len);
}

public static byte[] unsafeDecodeArray(ByteBuf in, int len) {
    byte[] bytes = new byte[len];
    in.readBytes(bytes, 0, len);
    return bytes;
}

// This is unsafe as we do not check readable bytes, is safe if apart of a packet read all at // once
 public static int unsafeDecodeInteger(ByteBuf in) {
    int n = 0;
    for (int i = 0; i <= 8; i++) {
        int curByte = in.readByte();
        n = (n << 7) | (curByte & 0x7f);
        if ((curByte & 0x80) == 0) {
            break;
        }
    }
    return n;
}
公共静态ByteBuf encodeString(ByteBuf out,字符串值,字符集){
字节[]字节=value.getBytes(字符集);
编码数组(输出,字节);
返回;
}
公共静态ByteBuf encodeArray(ByteBuf out,字节[]字节){
int length=bytes.length;
encodeInteger(输出,长度);
out.writeBytes(字节);
返回;
}
公共静态ByteBuf encodeInteger(ByteBuf out,整数){
返回encodeInt(out,number,computeUInt32Size(number));
}
公共静态ByteBuf encodeInt(ByteBuf out,int number,int numBytes){
int originalIndex=out.writerIndex();
int adjustedI=原始索引+单位数;
最终整数容量=out.capacity();
如果(调整EDI>容量){
输出容量(调整后的EDI);
}
out.writerIndex(out.writerIndex()+numBytes);
对于(int i=adjustedI-1;i>=originalIndex;i--){
int curByte=(数字&0x7F);
如果(i!=(adjustedI-1)){
路缘石|=0x80;
}
out.setByte(i,curByte);
数量>>=7;
}
返回;
}
公共静态整型计算机Int32Size(整型值){

如果((value&(~0I)是一个奇怪的问题,它是由Netty源代码中的一条评论引发的

回答我自己的问题:视情况而定! 如果没有固定长度的数据帧,则客户端和服务器都需要某种方式来商定数据帧的结束时间-完成时间。可以是一个\r\n(回车+换行符)或另一个商定的字符