热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

NettyinAction笔记(Chapter8)ProvidedChannelHandlersandcodecs

【8.1SecuringNettyapplicationswithSSLTLS】【8.3Handlingidleconnectionsandtimeouts】Youmayfindt

【8.1 Securing Netty applications with SSL/TLS】

【8.3 Handling idle connections and timeouts】
You may find times when you want to handle idle connections and timeouts. Typically youd send a message, also called a "heartbeat," to the remote peer if it idles too long in order to detect if its still alive. Another approach is to disconnect the remote peer if it idles too long.
Dealing with idle connections is such a core part of many applications that Netty ships a solution for it. Three different ChannelHandlers handle idle and timeout, as shown in table 8.4.
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

IdleStateHandler
Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.

Listing 8.9 shows how you can use the IdleStateHandler to get notified if you havent received or sent data for 60 seconds. If this is the case, a heartbeat will be written to the remote peer, and if this fails the connection is closed.
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

【8.4 Decoding delimiter- and length-based protocols】
【8.4.1 Delimiter-based protocols】
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

Imagine you have a protocol that handles only commands. Those commands are formed out of a name and arguments. The name and the arguments are separated by a whitespace.
Writing a decoder for this is a piece of cake if you extend the LineBasedFrameDecoder.
Listing 8.11 shows how to do it.
命令1 [参数...]\r\n
命令2 [参数...]\r\n
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

【8.4.2 Length-based protocols】
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

More often you find the case where the size of the frame is encoded in the header. For this purpose you can use the LengthFieldBasedFrameDecoder, which will read the length out of the header and extract the frame for the length.
Figure 8.7 shows how it works.
Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs
If the length field is part of the header frame extracted, this can be configured using the constructor of the LengthFieldBasedFrameDecoder. Also, it lets you specify where exactly in the frame the length field is and how long it is. Its very flexible, so for more information, please refer to the API docs.

Netty in Action笔记(Chapter 8)Provided ChannelHandlers and codecs

【8.5 Writing big data】

【8.6 Serializing data】


推荐阅读
  • Fixes#3560Itriedtodowhatproposedintheissue(inthisbranchhttps://gith ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Python中的PyInputPlus模块原文:https ... [详细]
  • 本文整理了Java中javax.swing.Action.addPropertyChangeListener()方法的一些代码示例,展示了Action.ad ... [详细]
  • MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找。好记性不如烂笔头& ... [详细]
  • 做好了项上,其中包含有一个上传的功能。在开发环境和测试环境运行、测试都没什么问题。也许是由于本地的局域网的问题,一切都运行的比较快,但把它发布到外网的服务器上去时。就特别的慢。上传小的文件还算比 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • switch语句的一些用法及注意事项
    本文介绍了使用switch语句时的一些用法和注意事项,包括如何实现"fall through"、default语句的作用、在case语句中定义变量时可能出现的问题以及解决方法。同时也提到了C#严格控制switch分支不允许贯穿的规定。通过本文的介绍,读者可以更好地理解和使用switch语句。 ... [详细]
  • 标题: ... [详细]
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • 【爬虫】关于企业信用信息公示系统加速乐最新反爬虫机制
    ( ̄▽ ̄)~又得半夜修仙了,作为一个爬虫小白,花了3天时间写好的程序,才跑了一个月目标网站就更新了,是有点悲催,还是要只有一天的时间重构。升级后网站的层次结构并没有太多变化,表面上 ... [详细]
author-avatar
香福武燕_205
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有