【问题标题】:How to custom LengthFieldBasedFrameDecoder in netty 4.0.25 final如何在 netty 4.0.25 final 中自定义 LengthFieldBasedFrameDecoder
【发布时间】:2015-01-27 18:57:41
【问题描述】:

我是新手网民。 我有消息

  • (Header)(Length)(Type)(MessageBody) 大小

  • <...> - 长度包括类型和 消息正文。

我想获取消息的标题和类型,如何在 netty 4.0.25 final 中自定义 LengthFieldBasedFrameDecoder? 我参考链接:Buffer Returned from LengthFieldBasedFrameDecoder too small 但它是 netty 3.xx。

【问题讨论】:

    标签: java netty


    【解决方案1】:

    为什么要自定义 LengthFieldBasedFrameDecoder?设置

    • maxFrameLength 到您将接受的最大消息
    • lengthFieldOffset = 4
    • lengthFieldLength = 4
    • 长度调整 = 0
    • initialBytesToStrip = 0

    然后上游的下一个解码器将收到一个包含整个消息的 ChannelBuffer,您可以根据需要对其进行解析和处理。

    【讨论】:

    • 为什么是 4 个字节?这实际上取决于他的消息大小。根据他的最大数据包大小,4 字节可能非常浪费。
    • @Underbalanced 因为 OP 说它在问题中是 4 个字节
    猜你喜欢
    • 2015-11-15
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多