netty源码分析 之十 codec
来源:互联网 发布:va ips 知乎 编辑:程序博客网 时间:2024/06/06 08:59
前面学习了别的handler,都是直接继承ChannelHandlerAdapter 或者间接继承来实现的。
codec也很简单,顾名思义 解遍码器
核心类有以下几个
ByteToMessageCodec
ByteBuf 与Object 之间的转换
重写了ChannelHandlerAdapter 的两个方法 channelRead writer
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { decoder.channelRead(ctx, msg); } @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { encoder.write(ctx, msg, promise); }
然后具体再有定义decoder和encoder来实现
ByteToMessageDecoder
之类来实现下,decode方法
/** * 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 accour */ protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception;
MessageToByteEncoder
关注下encode即可
/** * 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 accour */ protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception;
MessageToMessageCodec
Object与Object 之间进行转换
同上面的道理,这里留给读者自己学习吧。
0 0
- netty源码分析 之十 codec
- netty(十)源码分析之ByteBuf
- netty源码分析之ChannelFuture
- netty源码分析之ChannelPipeline
- netty源码分析之ChannelHandler
- netty源码分析之ChannelPipeline
- Netty 源码分析之ByteToMessageDecoder
- Netty源码分析之DelimiterBasedFrameDecoder
- Netty 源码分析之SimpleChannelInboundHandler
- netty源码分析之LengthFieldBasedFrameDecoder
- netty源码分析之服务端
- netty源码分析之客户端
- Netty学习之旅----源码分析Netty内存泄漏检测
- Netty源码分析之handler decoder
- Netty源码分析之handler decoder
- netty源码分析之FrameDecoder(LengthFieldBasedFrameDecoder)
- netty之AbstractBootstrap及其子类源码分析
- netty源码分析 之二 transport(bootstrap)
- ubuntu中使用终端查看U盘里的内容
- linux内核与用户之间的通信方式——虚拟文件系统、ioctl以及netlink .
- 读书笔记:《大型网站技术架构:核心原理与案例分析》(李智慧)(一)
- android reboot 流程
- 设计模式:单例模式
- netty源码分析 之十 codec
- python过滤unicode控制字符
- 猴子爬山
- Android csdn大神
- VS2010 WDK7.1.0 Win7_64
- minicom的配置与使用
- “error LNK2019: 无法解析的外部符号”之分析
- java冒泡排序算法
- SQLite数据库存储之更新数据库