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


    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);
        }
        
        
  • 相关阅读:
    [开源]用MQL4实现MD5加密
    如何转换WMV到MP3,WMV到MP3播放器
    C# Winform TreeView 的一些基本用法
    WinServer 2008 远程桌面连接设置
    存储数据类型的转化总结
    EF中执行sql语句,以及事务
    C#(委托a)
    LINQ绑定List到GridView
    循环遍历DataTable绑定到Table
    要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
  • 原文地址:https://www.cnblogs.com/lemon-flm/p/7813914.html
Copyright © 2020-2023  润新知