zoukankan      html  css  js  c++  java
  • smack 监听不同packet机制

    之前做即时通讯,扒了smack源码来参考。说下其中解包后进行通知的机制。

    Filter类:accept(Packet packet)函数,传入packet在此函数中进行对比判断,返回true 则通过此filter认证。实现PacketFilter接口即可。

    public interface PacketFilter {
    
        /**
         * Tests whether or not the specified packet should pass the filter.
         *
         * @param packet the packet to test.
         * @return true if and only if <tt>packet</tt> passes the filter.
         */
        public boolean accept(Packet packet);
    }

    ListenerWrapper类:packetListener对应了自己在应用中定义的Listener(实现PacketListener接口),当通过对应的packetFilter之后,则进行回掉;

    /**
         * A wrapper class to associate a packet filter with a listener.
         */
        protected static class ListenerWrapper {
    
            private PacketListener packetListener;
            private PacketFilter packetFilter;
    
            /**
             * Create a class which associates a packet filter with a listener.
             * 
             * @param packetListener the packet listener.
             * @param packetFilter the associated filter or null if it listen for all packets.
             */
            public ListenerWrapper(PacketListener packetListener, PacketFilter packetFilter) {
                this.packetListener = packetListener;
                this.packetFilter = packetFilter;
            }
    
            /**
             * Notify and process the packet listener if the filter matches the packet.
             * 
             * @param packet the packet which was sent or received.
             */
            public void notifyListener(Packet packet) {
                if (packetFilter == null || packetFilter.accept(packet)) {
                    packetListener.processPacket(packet);
                }
            }
        }
    public interface PacketListener {
    
        /**
         * Process the next packet sent to this packet listener.<p>
         *
         * A single thread is responsible for invoking all listeners, so
         * it's very important that implementations of this method not block
         * for any extended period of time.
         *
         * @param packet the packet to process.
         */
        public void processPacket(Packet packet);
    
    }

    接下来就是分包了,PacketReader中listenerExecutor.submit(new ListenerNotification(packet));在ListenerNotification线程里面进行判断,关键代码如下,写在run里面

    for (ListenerWrapper listenerWrapper : connection.recvListeners.values()) {
                    listenerWrapper.notifyListener(packet);
                }

    通过迭代出ListenerWrapper调用notifyListener函数,查找通过验证的filter然后相对应是哪个listener就进行通知。

  • 相关阅读:
    xps插入图片
    xps文件的基本操作
    大家注意:升级 win8.1 火狐浏览器 谷歌浏览器 搜狗五笔输入法 都不能用啦
    CF4C_Registration system 题解
    CF1B_Spreadsheets 题解
    CSP-J/S 初赛知识点整理
    Nodejs在centos下的安装
    sqlserver2012 表分区
    adb unknown host service 这个问题的解决,转载
    char和nchar,varchar和nvarchar的区别(转载)
  • 原文地址:https://www.cnblogs.com/helloandroid/p/3721991.html
Copyright © 2011-2022 走看看