Netty源码分析-MessageToMessageDecoder
Posted 征服.刘华强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Netty源码分析-MessageToMessageDecoder相关的知识,希望对你有一定的参考价值。
package io.netty.handler.codec;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.TypeParameterMatcher;
import java.util.List;
//消息到消息的解码器
public abstract class MessageToMessageDecoder<I> extends ChannelInboundHandlerAdapter
//类型匹配器
private final TypeParameterMatcher matcher;
/**
* Create a new instance which will try to detect the types to match out of the type parameter of the class.
*/
protected MessageToMessageDecoder()
matcher = TypeParameterMatcher.find(this, MessageToMessageDecoder.class, "I");
/**
* Create a new instance
*
* @param inboundMessageType The type of messages to match and so decode
*/
protected MessageToMessageDecoder(Class<? extends I> inboundMessageType)
matcher = TypeParameterMatcher.get(inboundMessageType);
/**
* Returns @code true if the given message should be handled. If @code false it will be passed to the next
* @link ChannelInboundHandler in the @link ChannelPipeline.
*/
//匹配定义类时指定的数据类型<I>
public boolean acceptInboundMessage(Object msg) throws Exception
return matcher.match(msg);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
CodecOutputList out = CodecOutputList.newInstance();
try
//判断msg是否是《I》类型
if (acceptInboundMessage(msg))
@SuppressWarnings("unchecked")
I cast = (I) msg;
try
//子类解码逻辑
decode(ctx, cast, out);
finally
//释放msg
ReferenceCountUtil.release(cast);
else
//不是I类型则传递msg
out.add(msg);
catch (DecoderException e)
throw e;
catch (Exception e)
throw new DecoderException(e);
finally
//传递给PPLine后续解码器
int size = out.size();
for (int i = 0; i < size; i ++)
ctx.fireChannelRead(out.getUnsafe(i));
out.recycle();
/**
* Decode from one message to an other. This method will be called for each written message that can be handled
* by this decoder.
*
* @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;
技术交流QQ:212320390
关注公众号
以上是关于Netty源码分析-MessageToMessageDecoder的主要内容,如果未能解决你的问题,请参考以下文章