分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

Netty——高级发送和接收数据handler处理器

发布时间:2023-09-06 01:24责任编辑:沈小雨关键词:暂无标签

netty发送和接收数据handler处理器 主要是继承 SimpleChannelInboundHandler 和 ChannelInboundHandlerAdapter

  一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢?

  其实用这两个抽象类是有讲究的,在客户端的业务Handler继承的是SimpleChannelInboundHandler,而在服务器端继承的是ChannelInboundHandlerAdapter

  最主要的区别就是SimpleChannelInboundHandler在接收到数据后会自动release掉数据占用的Bytebuffer资源(自动调用Bytebuffer.release())。而为何服务器端不能用呢,因为我们想让服务器把客户端请求的数据发送回去,而服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release。

handler处理器 内置 方法

channelActive

通道激活时触发,当客户端connect成功后,服务端就会接收到这个事件,从而可以把客户端的Channel记录下来,供后面复用

channelRead

这个必须用啊,当收到对方发来的数据后,就会触发,参数msg就是发来的信息,可以是基础类型,也可以是序列化的复杂对象。

channelReadComplete

channelRead执行后触发

exceptionCaught

出错是会触发,做一些错误处理

继承  ChannelInboundHandlerAdapter  具体的例子

/** * ?netty服务器的监听 处理器 * ?* @author flm 2017年10月27日 */public class IOHandler extends ChannelInboundHandlerAdapter { ???private static Logger log = Logger.getLogger(IOHandler.class); ???//netty AttributeKey 相对于 web session【重要】 ???public static final AttributeKey<DeviceSession> KEY = AttributeKey.valueOf("IO"); ????private Producer producer; ???????????public IOHandler(Producer producer){ ???????this.producer=producer; ???}/** ????* 读取数据 ????*/ ???@Override ???public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ???????????????DeviceSession session = ctx.channel().attr(KEY).get(); ????// 检测是否 自己注册的 客户端 ???????????????ByteBuf buffer=(ByteBuf) msg; ???????????????if (buffer == null||session == null) { ???????????closeConnection(ctx); // 关闭连接 ???????} ???????????????MsgEntity msgEntity = new MsgEntity(buffer); // 解码 ?buffer 封装 ?msgEntity ???????log.info("# Accept Client data :"+msgEntity.toString()); ???????????????if (MsgType.UNKNOW == msgEntity.getMsgType()) { ???????????log.info("# 客户端 发送数据 类型未定义... :"+msgEntity.toString()); ???????????return; ???????} ???????????????if(!session.isActivity()){ ???????????session.setActivity(true); ???????????session.setImei(msgEntity.getImei()); ???????????SessionManager.getSingleton().addClient(session); ???????} ???????producer.putData(msgEntity); ???????????} ???????????/** ????* 客户端 注册 ????*/ ???@Override ???public void channelRegistered(ChannelHandlerContext ctx) throws Exception { ???????super.channelRegistered(ctx); ???????????????log.info(String.format("# client registered...: ??%s ...", ctx.channel())); ???????????????DeviceSession session = new DeviceSession(ctx.channel()); ???????// 绑定客户端到SOCKET ???????ctx.channel().attr(KEY).set(session); ???} ???????/** ????* 客户端 失去连接 ????*/ ???@Override ???public void channelInactive(ChannelHandlerContext ctx) throws Exception ???{ ???????super.channelInactive(ctx); ???????????????log.info(String.format("# client out... : %s", ctx.channel())); ???????DeviceSession session = ctx.channel().attr(KEY).getAndSet(null); ???????????????// 移除 ?session 并删除 该客户端 ???????SessionManager.getSingleton().removeClient(session, true); ???????????????if(session.getDeviceID() != null) ???????{ ?????????// ?producer.onData(new Request(new RootMessage(MessageType.LOGOUT, null, null), session)); ???????} ???????????} ??????/** ????* 心跳机制 ?用户事件触发 ????*/ ???@Override ???public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception ???{ ???????if (evt instanceof IdleStateEvent) ???????{ ???????????IdleStateEvent e = (IdleStateEvent) evt; ???????????????????????//检测 是否 这段时间没有和服务器联系 ???????????if (e.state() == IdleState.ALL_IDLE) ???????????{ ???????????????//检测心跳 ???????????????checkIdle(ctx); ???????????} ???????} ???????????????super.userEventTriggered(ctx, evt); ???} ?????/** ????* 报错 处理事件 ????*/ ???@Override ???public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) ???????????throws Exception { ???????????????log.error("# 客户端连接 ?Netty 出错..."); ???????cause.printStackTrace(); ???????//关闭连接 ???????closeConnection(ctx); ???} ???????

Netty——高级发送和接收数据handler处理器

原文地址:http://www.cnblogs.com/lemon-flm/p/7813914.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved