ChannelInboundHandlerAdapter 和 ChannelOutboundHandlerAdapter 会影响netty4中运行编码器和解码器的顺序吗? [英] Does ChannelInboundHandlerAdapter and ChannelOutboundHandlerAdapter effects the sequence of running encoder and decoder in netty4?

查看:44
本文介绍了ChannelInboundHandlerAdapter 和 ChannelOutboundHandlerAdapter 会影响netty4中运行编码器和解码器的顺序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 netty4 来实现编码器和解码器.

I am using netty4 to implement encoder and decoder.

当我扩展ChannelInboundHandlerAdapter类型的解码器和ChannelOutboundHandlerAdapter类型的编码器时,它的运行顺序是什么?

When I am extending decoder of type ChannelInboundHandlerAdapter and encoder of type ChannelOutboundHandlerAdapter, then what should be its running sequence?

  1. 先编码器,然后解码器.
  2. 首先是解码器,然后是编码器.

还有,ChannelOutboundHandlerAdapter和ChannelInboundHandlerAdapter是如何影响读(I/O)和写(I/O)的,应该用在什么地方?

Also, how do ChannelOutboundHandlerAdapter and ChannelInboundHandlerAdapter effect reading (I/O) and writing (I/O) and where should be used what?

推荐答案

注意以下几点:

ChannelInboundHandler — 处理各种入站数据和状态变化 ChannelOutboundHandler — 处理出站数据并允许拦截所有操作

ChannelInboundHandler —Processes inbound data and state changes of all kinds ChannelOutboundHandler —Processes outbound data and allows interception of all operations

例如,当有消息传入时,即当您从套接字读取数据时 ChannelInboundHandler 将被通知.它还会收到状态更改的通知,例如 writabilityChanged、channelActive 和 Inactive 事件.

For example when a message is incoming that is when you are reading data from a socket the ChannelInboundHandler would be notified. Also it would be notified of state changes like writabilityChanged, channelActive and Inactive events.

当您将数据写入频道时,ChannelOutboundHandler会被通知.在频道上进行连接、断开和读取等调用时,它也会收到通知.

When you write data to a channel the ChannelOutboundHandler would be notified. It also will be notified on calls like connect, disconnect and read made on the channel.

要实现编码器和解码器,您可以在 netty 中扩展预定义的解码器和编码器,例如 ByteToMessageDecoderMessageToMessageDecoderMessageToByteEncoderMessageToMessageEncoder 因为它会简化您的任务.

To implement encoders and decoders you can extend predefined decoders and encoders in netty like ByteToMessageDecoder, MessageToMessageDecoder, MessageToByteEncoder and MessageToMessageEncoder as it would simplify your tasks.

至于解码器和编码器的执行顺序可以这样想:当消息通过套接字到达您的应用程序时,它将以字节流的形式存在,然后您必须对其进行解码以进行处理.当您向外部方发送消息时,您必须在将其写入套接字之前将其编码为字节.编码器和解码器的执行顺序取决于此过程.

As for the order of execution of decoders and encoders think of it like this: When a message reaches your application via the socket it will be in byte streams, then you will have to decode it for processing. When you are sending a message to external parties you will have to encode it into bytes before writing it to the socket. Order of execution of encoders and decoders depend upon this process.

这篇关于ChannelInboundHandlerAdapter 和 ChannelOutboundHandlerAdapter 会影响netty4中运行编码器和解码器的顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆