• 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就进行通知。

  • 相关阅读:
    linux基础命令篇四
    linux基础命令篇三
    linux基础命令篇二
    msf测试
    msf
    [精品转载] [NoSaFe]KALI下免杀神器TheFatRat使用秘籍
    kali&BT安装好之后无法上网或者无法获得内网IP
    Kali Linux安装之后需要做的一些事
    在Ubuntu下解决E: 无法对目录 /var/lib/apt/lists/ 加锁的问题(转)
    xshell之类的软件第一次连接不上初次安装kali问题(转)
  • 原文地址:https://www.cnblogs.com/helloandroid/p/3721991.html
Copyright © 2020-2023  润新知