Archiviert
13
0

Always create the NullPacketListener (unless otherwise stated).

This functions like a crude "counter" that automatically unregisters 
the packet injectors when every asynchronous listener has been removed.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-10-01 06:04:31 +02:00
Ursprung 961b34da38
Commit f4f28023fa
2 geänderte Dateien mit 18 neuen und 16 gelöschten Zeilen

Datei anzeigen

@ -20,7 +20,6 @@ public interface AsynchronousManager {
* Registers an asynchronous packet handler. * Registers an asynchronous packet handler.
* <p> * <p>
* To start listening asynchronously, pass the getListenerLoop() runnable to a different thread. * To start listening asynchronously, pass the getListenerLoop() runnable to a different thread.
* @param plugin - the plugin that is registering the handler.
* @param listener - the packet listener that will recieve these asynchronous events. * @param listener - the packet listener that will recieve these asynchronous events.
* @return An asynchrouns handler. * @return An asynchrouns handler.
*/ */

Datei anzeigen

@ -68,28 +68,40 @@ public class AsyncFilterManager implements AsynchronousManager {
@Override @Override
public AsyncListenerHandler registerAsyncHandler(PacketListener listener) { public AsyncListenerHandler registerAsyncHandler(PacketListener listener) {
return registerAsyncHandler(listener, true);
}
/**
* Registers an asynchronous packet handler.
* <p>
* To start listening asynchronously, pass the getListenerLoop() runnable to a different thread.
* <p>
* Asynchronous events will only be executed if a synchronous listener with the same packets is registered.
* If you already have a synchronous event, call this method with autoInject set to FALSE.
*
* @param listener - the packet listener that will recieve these asynchronous events.
* @param autoInject - whether or not to automatically create the corresponding synchronous listener,
* @return An asynchrouns handler.
*/
public AsyncListenerHandler registerAsyncHandler(PacketListener listener, boolean autoInject) {
AsyncListenerHandler handler = new AsyncListenerHandler(mainThread, this, listener); AsyncListenerHandler handler = new AsyncListenerHandler(mainThread, this, listener);
ListeningWhitelist sendingWhitelist = listener.getSendingWhitelist(); ListeningWhitelist sendingWhitelist = listener.getSendingWhitelist();
ListeningWhitelist receivingWhitelist = listener.getReceivingWhitelist(); ListeningWhitelist receivingWhitelist = listener.getReceivingWhitelist();
// We need a synchronized listener to get the ball rolling
boolean hasListener = true;
// Add listener to either or both processing queue // Add listener to either or both processing queue
if (hasValidWhitelist(sendingWhitelist)) { if (hasValidWhitelist(sendingWhitelist)) {
PacketFilterManager.verifyWhitelist(listener, sendingWhitelist); PacketFilterManager.verifyWhitelist(listener, sendingWhitelist);
serverProcessingQueue.addListener(handler, sendingWhitelist); serverProcessingQueue.addListener(handler, sendingWhitelist);
hasListener &= hasPacketListener(sendingWhitelist);
} }
if (hasValidWhitelist(receivingWhitelist)) { if (hasValidWhitelist(receivingWhitelist)) {
PacketFilterManager.verifyWhitelist(listener, receivingWhitelist); PacketFilterManager.verifyWhitelist(listener, receivingWhitelist);
clientProcessingQueue.addListener(handler, receivingWhitelist); clientProcessingQueue.addListener(handler, receivingWhitelist);
hasListener &= hasPacketListener(receivingWhitelist);
} }
if (!hasListener) { // We need a synchronized listener to get the ball rolling
if (autoInject) {
handler.setNullPacketListener(new NullPacketListener(listener)); handler.setNullPacketListener(new NullPacketListener(listener));
manager.addPacketListener(handler.getNullPacketListener()); manager.addPacketListener(handler.getNullPacketListener());
} }
@ -97,15 +109,6 @@ public class AsyncFilterManager implements AsynchronousManager {
return handler; return handler;
} }
/**
* Determine if the given packets are represented.
* @param whitelist - list of packets.
* @return TRUE if they are all registered, FALSE otherwise.
*/
private boolean hasPacketListener(ListeningWhitelist whitelist) {
return manager.getSendingFilters().containsAll(whitelist.getWhitelist());
}
private boolean hasValidWhitelist(ListeningWhitelist whitelist) { private boolean hasValidWhitelist(ListeningWhitelist whitelist) {
return whitelist != null && whitelist.getWhitelist().size() > 0; return whitelist != null && whitelist.getWhitelist().size() > 0;
} }