之前做即時通訊,扒了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就進行通知。