1. public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {

随便找了一个用字符串分割粘包的decoder,继承了ByteToMessageDecoder(netty版本不同,逻辑可能有区别)

  1. final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
  2. throws Exception {
  3. decodeState = STATE_CALLING_CHILD_DECODE;
  4. try {
  5. decode(ctx, in, out);
  6. } finally {
  7. boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING;
  8. decodeState = STATE_INIT;
  9. if (removePending) {
  10. handlerRemoved(ctx);
  11. }
  12. }
  13. }
  1. protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
  2. try {
  3. while (in.isReadable()) {
  4. int outSize = out.size();
  5.  
  6. if (outSize > 0) {
  7. fireChannelRead(ctx, out, outSize);
  8. out.clear();
  9.  
  10. if (ctx.isRemoved()) {
  11. break;
  12. }
  13. outSize = 0;
  14. }
  15.  
  16. int oldInputLength = in.readableBytes();
  17. decodeRemovalReentryProtection(ctx, in, out);
  18.  
  19. if (ctx.isRemoved()) {
  20. break;
  21. }
  22.  
  23. if (outSize == out.size()) {
  24. if (oldInputLength == in.readableBytes()) {
  25. break;
  26. } else {
  27. continue;
  28. }
  29. }
  30.  
  31. if (oldInputLength == in.readableBytes()) {
  32. throw new DecoderException(
  33. StringUtil.simpleClassName(getClass()) +
  34. ".decode() did not read anything but decoded a message.");
  35. }
  36.  
  37. if (isSingleDecode()) {
  38. break;
  39. }
  40. }
  41. } catch (DecoderException e) {
  42. throw e;
  43. } catch (Exception cause) {
  44. throw new DecoderException(cause);
  45. }
  46. }
  1. @Override
  2. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  3. if (msg instanceof ByteBuf) {
  4. CodecOutputList out = CodecOutputList.newInstance();
  5. try {
  6. ByteBuf data = (ByteBuf) msg;
  7. first = cumulation == null;
  8. if (first) {
  9. cumulation = data;
  10. } else {
  11. cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data);
  12. }
  13. callDecode(ctx, cumulation, out);
  14. } catch (DecoderException e) {
  15. throw e;
  16. } catch (Exception e) {
  17. throw new DecoderException(e);
  18. } finally {
  19. if (cumulation != null && !cumulation.isReadable()) {
  20. numReads = 0;
  21. cumulation.release();
  22. cumulation = null;
  23. } else if (++ numReads >= discardAfterReads) {
  24. numReads = 0;
  25. discardSomeReadBytes();
  26. }
  27.  
  28. int size = out.size();
  29. decodeWasNull = !out.insertSinceRecycled();
  30. fireChannelRead(ctx, out, size);
  31. out.recycle();
  32. }
  33. } else {
  34. ctx.fireChannelRead(msg);
  35. }
  36. }

可以发现,最后是在channelRead中被调用

netty的decoder encoder的更多相关文章

  1. netty之decoder

    转载自:https://blog.csdn.net/jzft_nuosu/article/details/80341018 netty的handler和decoder中的channelRead和dec ...

  2. 为什么游戏公司的server不愿意微服务化?

    背景介绍 笔者最近去面试了家游戏公司(有上市).我问他,公司有没有做微服务架构的打算及考量?他很惊讶的,我没听说过微服务耶,你可以解释一下吗? 我大概说了,方便测试,方便维护,方便升级,服务之间松耦合 ...

  3. Netty入门3之----Decoder和Encoder

    ​ Netty强大的地方,是他能方便的实现自定义协议的网络传输.在上一篇文章中,通过使用Netty封装好的工具类,实现了简单的http服务器.在接下来的文章中,我们看看怎么使用他来搭建自定义协议的服务 ...

  4. Netty面试

    声明:此文章非本人所 原创,是别人分享所得,如有知道原作者是谁可以联系本人,如有转载请加上此段话  1.BIO.NIO 和 AIO 的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要 ...

  5. Netty面试题

    1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...

  6. Netty相关面试题

    1.BIO.NIO和AIO的区别? BIO:一个连接一个线程,客户端有连接请求时服务器端就需要启动一个线程进行处理.线程开销大. 伪异步IO:将请求连接放入线程池,一对多,但线程还是很宝贵的资源. N ...

  7. 深入理解Netty框架

    前言 本文讨论的主题是Netty框架,本着3W原则 (What 是什么?->Why 为什么?->How 如何做?)来一步步探究Netty原理和本质以及运用场景. 了解基本名词 1.BIO. ...

  8. Netty小结

    前言 在实际开发中,netty的开发使用相对较小,why?在企业中涉及网络编程的部分比重较小,在这大环境内,企业会优先使用简单的http,udp等基础的通讯协议工具,如果不能满足需求,会考虑基于rpc ...

  9. 高性能/并发的保证-Netty在Redisson的应用

    前言 ​ Redisson Github: https://github.com/redisson/redisson ​ Redisson 官网:https://redisson.pro/ Redis ...

随机推荐

  1. 20180226xlVbaGetStockData

    Sub LoopGetStockData() Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA.Timer Cells. ...

  2. p1215

    一开始没用数组,没成功.后来确定用深搜后,用数组.出现一个不同的abc状态就记录下来,以免重复.一开始要倒的肯定是c杯,之后出现新状态要递归dfs3次.另外发现algorithm里的copy是原数组在 ...

  3. SP10707 COT2 - Count on a tree II (树上莫队)

    大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...

  4. python记录_day23 正则表达式 re模块

    一. 正则表达式 使用python的re模块之前应该对正则表达式有一定的了解 正则表达式是对字符串操作的一种逻辑公式.我们一般使用正则表达式对字符串进行匹配和过滤. 正则的优缺点: 优点:灵活, 功能 ...

  5. 百度地图API 自定义坐标点及图片

    var map = new BMap.Map("allmap");var point = new BMap.Point(105.955754,36.525109);map.cent ...

  6. Homebrew 安装mysql

    在mac上安装软件,无疑安装一个brew是个很好的选择,关于brew是什么,怎么安装建议去brew官网查看, 附上地址:brew官网  还有一篇博文 http://www.cnblogs.com/xd ...

  7. [luogu P3195] [HNOI2008]玩具装箱TOY

    [luogu P3195] [HNOI2008]玩具装箱TOY 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆, ...

  8. Python日志、序列化、正则模块

    使用Python内置模块的目的:拿来别人已经写好的模块功能,直接import内置模块使用,简化程序,避免重复造轮子的过程,提示自己的开发效率: 一. loging日志模块: 1. loging模块可以 ...

  9. net core 上传并使用EPPlus导入Excel文件

    1.  cshtml页面 form <form id="form" method="post" action="/SaveValueBatch& ...

  10. Oracle 如何循环查询结果集,进行新增或修改

    Oracle的PL/SQL中怎样循环查询的结果集,然后根据查询结果进行判断,是新增或修改操作 loop循环例子 for item in (select a,b,c from table_a where ...