Netty编解码器
Netty是一个高可用的java 网络开源框架,目前的版本是Netty4。本文主要介绍Netty编解码器个人的理解
Netty编解码器基本接口
图片:
ChannelHandler接口
上图是Netty基本接口的关系,如图Netty中所有编解码器均实现ChannelHandler接口。在ChannelPipeline中会将编码器和解码器组成入站和出站队列。该过程使用的是责任链模式。
ChannelInboundHandler接口
ChannelInboundHandler接口处理所有入站操作,所有编码器均需实现该接口。当接收数据时数据在入站队列中处理并传递
ChannelOutboundHandler接口
ChannelOutboundHandler接口处理所有出站操作,所有解码器均需实现该接口。当发送数据时,在出站队列中处理并传递。
Netty常用编解码器
了解如上接口可以帮助理解Netty网络数据的处理流程,但实际使用中,我们并不会直接接触以上接口。下面介绍的是实际使用中常用的编解码类。
Netty中通用编码器基类
ChannelInboundHandlerAdapter类
在Netty中所有的解码器均继承于该类,如上图该类继承了ChannelInboundHandler接口,处理入站操作
ChannelOutboundHandler类
在Netty中所有的编码器均继承于该类,如上图该类继承了ChannelInboundHandler接口,处理出站操作
Netty中主要编码器
ByteToMessageDecoder类
public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter {
/**
* Decode the from one {@link ByteBuf} to an other. This method will be called till either the input
* {@link ByteBuf} has nothing to read when return from this method or till nothing was read from the input
* {@link ByteBuf}.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
* @param in the {@link ByteBuf} from which to read data
* @param out the {@link List} to which decoded messages should be added
* @throws Exception is thrown if an error occurs
*/
protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception;
}
Netty的应用中需要继承ByteToMessageDecoder类,并重写decode方法。该类主要作用是将网络的数据流转换为网络消息。一般使用Netty是均需重写该类或其子类。
MessageToMessageDecoder类
public abstract class MessageToMessageDecoder<I> extends ChannelInboundHandlerAdapter {
/**
* Decode from one message to an other. This method will be called for each written message that can be handled
* by this encoder.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link MessageToMessageDecoder} belongs to
* @param msg the message to decode to an other one
* @param out the {@link List} to which decoded messages should be added
* @throws Exception is thrown if an error occurs
*/
protected abstract void decode(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception;
}
在复杂网路应用中,需要网络信息的转换,在使用ByteToMessageDecoder类后需要使用MessageToMessageDecoder类。同样,在使用时需要重写该类或其子类的decode方法。
Netty中主要解码器
MessageToByteEncoder
public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter {
/**
* Encode a message into a {@link ByteBuf}. This method will be called for each written message that can be handled
* by this encoder.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link MessageToByteEncoder} belongs to
* @param msg the message to encode
* @param out the {@link ByteBuf} into which the encoded message will be written
* @throws Exception is thrown if an error occurs
*/
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception;
}
在Netty应用中需要继承MessageToByteEncoder或其子类,并重写encode方法。
MessageToMessageEncoder
public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerAdapter {
/**
* Encode from one message to an other. This method will be called for each written message that can be handled
* by this encoder.
*
* @param ctx the {@link ChannelHandlerContext} which this {@link MessageToMessageEncoder} belongs to
* @param msg the message to encode to an other one
* @param out the {@link List} into which the encoded msg should be added
* needs to do some kind of aggregation
* @throws Exception is thrown if an error occurs
*/
protected abstract void encode(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception;
}
在复杂网路应用中,需要网络信息的转换,在使用MessageToByteEncoder或子类前需要使用MessageToMessageEncoder进行网络格式转换,使用方法与上面相同,重写上面encode方法。