Added support for closing asynchronous listeners by packet listener.

This commit is contained in:
Kristian S. Stangeland 2014-07-26 17:28:05 +02:00
parent d717ff1586
commit f378b7bbd9
3 changed files with 43 additions and 5 deletions

View File

@ -50,6 +50,12 @@ public interface AsynchronousManager {
*/
public abstract void unregisterAsyncHandler(AsyncListenerHandler handler);
/**
* Unregisters and closes the first asynchronous handler associated with the given listener.
* @param handler - asynchronous handler.
*/
public abstract void unregisterAsyncHandler(PacketListener listener);
/**
* Unregisters every asynchronous handler associated with this plugin.
* @param plugin - the original plugin.

View File

@ -175,12 +175,15 @@ public class AsyncFilterManager implements AsynchronousManager {
ListeningWhitelist sendingWhitelist = listener.getSendingWhitelist();
ListeningWhitelist receivingWhitelist = listener.getReceivingWhitelist();
if (!hasValidWhitelist(sendingWhitelist) && !hasValidWhitelist(receivingWhitelist)) {
throw new IllegalArgumentException("Listener has an empty sending and receiving whitelist.");
}
// Add listener to either or both processing queue
if (hasValidWhitelist(sendingWhitelist)) {
PacketFilterManager.verifyWhitelist(listener, sendingWhitelist);
serverProcessingQueue.addListener(handler, sendingWhitelist);
}
if (hasValidWhitelist(receivingWhitelist)) {
PacketFilterManager.verifyWhitelist(listener, receivingWhitelist);
clientProcessingQueue.addListener(handler, receivingWhitelist);
@ -191,7 +194,6 @@ public class AsyncFilterManager implements AsynchronousManager {
handler.setNullPacketListener(new NullPacketListener(listener));
manager.addPacketListener(handler.getNullPacketListener());
}
return handler;
}
@ -214,6 +216,35 @@ public class AsyncFilterManager implements AsynchronousManager {
}
}
@Override
public void unregisterAsyncHandler(PacketListener listener) {
if (listener == null)
throw new IllegalArgumentException("listener cannot be NULL.");
AsyncListenerHandler handler =
findHandler(serverProcessingQueue, listener.getSendingWhitelist(), listener);
if (handler == null) {
handler = findHandler(clientProcessingQueue, listener.getReceivingWhitelist(), listener);
}
unregisterAsyncHandler(handler);
}
// Search for the first correct handler
private AsyncListenerHandler findHandler(PacketProcessingQueue queue, ListeningWhitelist search, PacketListener target) {
if (ListeningWhitelist.isEmpty(search))
return null;
for (PacketType type : search.getTypes()) {
for (PrioritizedListener<AsyncListenerHandler> element : queue.getListener(type)) {
if (element.getListener().getAsyncListener() == target) {
return element.getListener();
}
}
}
return null;
}
@Override
public void unregisterAsyncHandler(AsyncListenerHandler handler) {
if (handler == null)

View File

@ -218,7 +218,7 @@ public class ListeningWhitelist {
else if (whitelist == null)
return true;
else
return whitelist.getWhitelist().isEmpty();
return whitelist.getTypes().isEmpty();
}
@Override
@ -294,9 +294,10 @@ public class ListeningWhitelist {
* @author Kristian
*/
public static class Builder {
private ListenerPriority priority;
// Default values
private ListenerPriority priority = ListenerPriority.NORMAL;
private Set<PacketType> types = Sets.newHashSet();
private GamePhase gamePhase;
private GamePhase gamePhase = GamePhase.PLAYING;
private Set<ListenerOptions> options = Sets.newHashSet();
/**