netty的decoder encoder
- public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
随便找了一个用字符串分割粘包的decoder,继承了ByteToMessageDecoder(netty版本不同,逻辑可能有区别)
- final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
- throws Exception {
- decodeState = STATE_CALLING_CHILD_DECODE;
- try {
- decode(ctx, in, out);
- } finally {
- boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING;
- decodeState = STATE_INIT;
- if (removePending) {
- handlerRemoved(ctx);
- }
- }
- }
- protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
- try {
- while (in.isReadable()) {
- int outSize = out.size();
- if (outSize > 0) {
- fireChannelRead(ctx, out, outSize);
- out.clear();
- if (ctx.isRemoved()) {
- break;
- }
- outSize = 0;
- }
- int oldInputLength = in.readableBytes();
- decodeRemovalReentryProtection(ctx, in, out);
- if (ctx.isRemoved()) {
- break;
- }
- if (outSize == out.size()) {
- if (oldInputLength == in.readableBytes()) {
- break;
- } else {
- continue;
- }
- }
- if (oldInputLength == in.readableBytes()) {
- throw new DecoderException(
- StringUtil.simpleClassName(getClass()) +
- ".decode() did not read anything but decoded a message.");
- }
- if (isSingleDecode()) {
- break;
- }
- }
- } catch (DecoderException e) {
- throw e;
- } catch (Exception cause) {
- throw new DecoderException(cause);
- }
- }
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- if (msg instanceof ByteBuf) {
- CodecOutputList out = CodecOutputList.newInstance();
- try {
- ByteBuf data = (ByteBuf) msg;
- first = cumulation == null;
- if (first) {
- cumulation = data;
- } else {
- cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data);
- }
- callDecode(ctx, cumulation, out);
- } catch (DecoderException e) {
- throw e;
- } catch (Exception e) {
- throw new DecoderException(e);
- } finally {
- if (cumulation != null && !cumulation.isReadable()) {
- numReads = 0;
- cumulation.release();
- cumulation = null;
- } else if (++ numReads >= discardAfterReads) {
- numReads = 0;
- discardSomeReadBytes();
- }
- int size = out.size();
- decodeWasNull = !out.insertSinceRecycled();
- fireChannelRead(ctx, out, size);
- out.recycle();
- }
- } else {
- ctx.fireChannelRead(msg);
- }
- }
可以发现,最后是在channelRead中被调用
netty的decoder encoder的更多相关文章
- netty之decoder
转载自:https://blog.csdn.net/jzft_nuosu/article/details/80341018 netty的handler和decoder中的channelRead和dec ...
- 为什么游戏公司的server不愿意微服务化?
背景介绍 笔者最近去面试了家游戏公司(有上市).我问他,公司有没有做微服务架构的打算及考量?他很惊讶的,我没听说过微服务耶,你可以解释一下吗? 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合 ...
- Netty入门3之----Decoder和Encoder
Netty强大的地方,是他能方便的实现自定义协议的网络传输.在上一篇文章中,通过使用Netty封装好的工具类,实现了简单的http服务器.在接下来的文章中,我们看看怎么使用他来搭建自定义协议的服务 ...
- Netty面试
声明:此文章非本人所 原创,是别人分享所得,如有知道原作者是谁可以联系本人,如有转载请加上此段话 1.BIO.NIO 和 AIO 的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要 ...
- Netty面试题
1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...
- Netty相关面试题
1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...
- 深入理解Netty框架
前言 本文讨论的主题是Netty框架,本着3W原则 (What 是什么?->Why 为什么?->How 如何做?)来一步步探究Netty原理和本质以及运用场景. 了解基本名词 1.BIO. ...
- Netty小结
前言 在实际开发中,netty的开发使用相对较小,why?在企业中涉及网络编程的部分比重较小,在这大环境内,企业会优先使用简单的http,udp等基础的通讯协议工具,如果不能满足需求,会考虑基于rpc ...
- 高性能/并发的保证-Netty在Redisson的应用
前言 Redisson Github: https://github.com/redisson/redisson Redisson 官网:https://redisson.pro/ Redis ...
随机推荐
- 20180226xlVbaGetStockData
Sub LoopGetStockData() Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA.Timer Cells. ...
- p1215
一开始没用数组,没成功.后来确定用深搜后,用数组.出现一个不同的abc状态就记录下来,以免重复.一开始要倒的肯定是c杯,之后出现新状态要递归dfs3次.另外发现algorithm里的copy是原数组在 ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- python记录_day23 正则表达式 re模块
一. 正则表达式 使用python的re模块之前应该对正则表达式有一定的了解 正则表达式是对字符串操作的一种逻辑公式.我们一般使用正则表达式对字符串进行匹配和过滤. 正则的优缺点: 优点:灵活, 功能 ...
- 百度地图API 自定义坐标点及图片
var map = new BMap.Map("allmap");var point = new BMap.Point(105.955754,36.525109);map.cent ...
- Homebrew 安装mysql
在mac上安装软件,无疑安装一个brew是个很好的选择,关于brew是什么,怎么安装建议去brew官网查看, 附上地址:brew官网 还有一篇博文 http://www.cnblogs.com/xd ...
- [luogu P3195] [HNOI2008]玩具装箱TOY
[luogu P3195] [HNOI2008]玩具装箱TOY 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆, ...
- Python日志、序列化、正则模块
使用Python内置模块的目的:拿来别人已经写好的模块功能,直接import内置模块使用,简化程序,避免重复造轮子的过程,提示自己的开发效率: 一. loging日志模块: 1. loging模块可以 ...
- net core 上传并使用EPPlus导入Excel文件
1. cshtml页面 form <form id="form" method="post" action="/SaveValueBatch& ...
- Oracle 如何循环查询结果集,进行新增或修改
Oracle的PL/SQL中怎样循环查询的结果集,然后根据查询结果进行判断,是新增或修改操作 loop循环例子 for item in (select a,b,c from table_a where ...