MessageToMessageCodec与MessageToMessageDecoder以及MessageToMessageEncoder在Netty中的区别。

huangapple go评论75阅读模式
英文:

MessageToMessageCodec vs MessageToMessageDecoder and MessageToMessageEncoder in netty

问题

我正在创建一个基于数据包的服务器应用程序,我想知道是使用MessageToMessageCodec同时进行数据包编码和解码更好,还是为其分别使用编码器和解码器更好?

是否有一些特定的事情我需要考虑在使用MessageToMessageCodec时?为什么MessageToMessageCodec不能被标记为@Sharable

每个通道的处理流程如下:

  • LengthFieldPrepender
  • LengthFieldBasedFrameDecoder
  • CryptoHandler(扩展了MessageToMessageCodec,用于解密和加密数据包)
  • PacketDecoder(扩展了MessageToMessageDecoder)
  • PacketEncoder(扩展了MessageToMessageEncoder)
  • NetworkHandler(扩展了SimpleChannelInboundHandler

我应该在编码和解码时都使用MessageToMessageCodec吗?它们各自的优缺点是什么,哪种更高效?

英文:

I'm creating a packet based server application and I wanna know is it better to use a MessageToMessageCodec for both encoding and decoding packets or separate encoder and decoder for which one?

is there something specific that I need to consider for MessageToMessageCodec? why can't a MessageToMessageCodec be annotated as @Sharable?

the pipeline for each channel is something like this:

  • LengthFieldPrepender
  • LengthFieldBasedFrameDecoder
  • CryptoHandler (extends MessageToMessageCodec used for both decrypting and
    encrypting packets
    )
  • PacketDecoder (extends MessageToMessageDecoder)
  • PacketEncoder (extends MessageToMessageEncoder)
  • NetworkHandler (extends SimpleChannelInboundHandler<Packet>)

Should I use a MessageToMessageCodec for both encoding and decoding? what are the pros and cons and which one is more efficient?

答案1

得分: 0

在源代码中,编解码器只是将编码器和解码器组合在一起。它在底层使用了每个实例:https://github.com/netty/netty/blob/00afb19d7a37de21b35ce4f6cb3fa7f74809f2ab/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java#L37-L39

使用编解码器和使用编码器和解码器之间似乎没有区别。提供这三个类允许根据解决方案的需要灵活选择其中之一,如果不需要,还可以使用编解码器的便利性。

例如,ByteToMessageCodec 无法被标注为 @Sharable,因为ByteToMessageDecoder 不能被标注为 @Sharable,而前者在其他情况下由后者组成。

ByteToMessageCodec 最终同时实现了 ChannelInboundHandlerChannelOutboundHandler,因此是的,您会将其用于编码和解码两者。

英文:

In the source, the codec just combines the encoder and decoder. It uses an instance of each under the covers: https://github.com/netty/netty/blob/00afb19d7a37de21b35ce4f6cb3fa7f74809f2ab/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java#L37-L39

There appears to be no difference between using the codec or using the encoder and decoder. Providing all 3 classes does allow the flexibility of one or the other if the solution requires for that and the convenience of the codec if not.

As an example, the ByteToMessageCodec can't be annotated as @Sharable because the ByteToMessageDecoder can't be annotated as @Sharable and the former is composed of the latter among other things.

The ByteToMessageCodec ultimately implements both ChannelInboundHandler and ChannelOutboundHandler so yes, you would use it for both encoding and decoding.

huangapple
  • 本文由 发表于 2020年7月25日 15:59:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定