Archiviert
13
0

Simplify the act of creating an asynchronous listener.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-09-30 00:13:36 +02:00
Ursprung 63197bbbd5
Commit 711990fa15
4 geänderte Dateien mit 34 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -53,7 +53,8 @@ public class ProtocolLibrary extends JavaPlugin {
@Override @Override
public void onLoad() { public void onLoad() {
logger = getLoggerSafely(); logger = getLoggerSafely();
protocolManager = new PacketFilterManager(getClassLoader(), logger); protocolManager = new PacketFilterManager(
getClassLoader(), getServer().getScheduler(), logger);
} }
@Override @Override

Datei anzeigen

@ -6,6 +6,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.AsynchronousManager;
import com.comphenix.protocol.PacketStream; import com.comphenix.protocol.PacketStream;
@ -35,20 +36,27 @@ public class AsyncFilterManager implements AsynchronousManager {
// The likely main thread // The likely main thread
private Thread mainThread; private Thread mainThread;
// Default scheduler
private BukkitScheduler scheduler;
// Current packet index // Current packet index
private AtomicInteger currentSendingIndex = new AtomicInteger(); private AtomicInteger currentSendingIndex = new AtomicInteger();
// Whether or not we're currently cleaning up // Whether or not we're currently cleaning up
private volatile boolean cleaningUp; private volatile boolean cleaningUp;
public AsyncFilterManager(Logger logger, PacketStream packetStream) { public AsyncFilterManager(Logger logger, BukkitScheduler scheduler, PacketStream packetStream) {
// Server packets are synchronized already
this.serverQueue = new PacketSendingQueue(false); this.serverQueue = new PacketSendingQueue(false);
// Client packets must be synchronized // Client packets must be synchronized
this.clientQueue = new PacketSendingQueue(true); this.clientQueue = new PacketSendingQueue(true);
this.serverProcessingQueue = new PacketProcessingQueue(serverQueue); this.serverProcessingQueue = new PacketProcessingQueue(serverQueue);
this.clientProcessingQueue = new PacketProcessingQueue(clientQueue); this.clientProcessingQueue = new PacketProcessingQueue(clientQueue);
this.packetStream = packetStream; this.packetStream = packetStream;
this.scheduler = scheduler;
this.logger = logger; this.logger = logger;
this.mainThread = Thread.currentThread(); this.mainThread = Thread.currentThread();
@ -158,6 +166,15 @@ public class AsyncFilterManager implements AsynchronousManager {
return clientProcessingQueue.keySet(); return clientProcessingQueue.keySet();
} }
/**
* Used to create a default asynchronous task.
* @param plugin - the calling plugin.
* @param runnable - the runnable.
*/
public void scheduleAsyncTask(Plugin plugin, Runnable runnable) {
scheduler.scheduleAsyncDelayedTask(plugin, runnable);
}
@Override @Override
public boolean hasAsynchronousListeners(PacketEvent packet) { public boolean hasAsynchronousListeners(PacketEvent packet) {
return getProcessingQueue(packet).getListener(packet.getPacketID()).size() > 0; return getProcessingQueue(packet).getListener(packet.getPacketID()).size() > 0;

Datei anzeigen

@ -8,6 +8,7 @@ import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
/** /**
* Represents a handler for an asynchronous event. * Represents a handler for an asynchronous event.
@ -180,4 +181,14 @@ public class AsyncListenerHandler {
public Plugin getPlugin() { public Plugin getPlugin() {
return listener != null ? listener.getPlugin() : null; return listener != null ? listener.getPlugin() : null;
} }
/**
* Start the asynchronous listener using the Bukkit scheduler.
*/
public void start() {
if (listener.getPlugin() == null)
throw new IllegalArgumentException("Cannot start task without a valid plugin.");
filterManager.scheduleAsyncTask(listener.getPlugin(), getListenerLoop());
}
} }

Datei anzeigen

@ -44,6 +44,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.AsynchronousManager;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
@ -121,7 +122,7 @@ public final class PacketFilterManager implements ProtocolManager {
/** /**
* Only create instances of this class if protocol lib is disabled. * Only create instances of this class if protocol lib is disabled.
*/ */
public PacketFilterManager(ClassLoader classLoader, Logger logger) { public PacketFilterManager(ClassLoader classLoader, BukkitScheduler scheduler, Logger logger) {
if (logger == null) if (logger == null)
throw new IllegalArgumentException("logger cannot be NULL."); throw new IllegalArgumentException("logger cannot be NULL.");
if (classLoader == null) if (classLoader == null)
@ -132,7 +133,7 @@ public final class PacketFilterManager implements ProtocolManager {
this.classLoader = classLoader; this.classLoader = classLoader;
this.logger = logger; this.logger = logger;
this.packetInjector = new PacketInjector(classLoader, this, connectionLookup); this.packetInjector = new PacketInjector(classLoader, this, connectionLookup);
this.asyncFilterManager = new AsyncFilterManager(logger, this); this.asyncFilterManager = new AsyncFilterManager(logger, scheduler, this);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
logger.log(Level.SEVERE, "Unable to initialize packet injector.", e); logger.log(Level.SEVERE, "Unable to initialize packet injector.", e);
} }