public class AsyncListenerHandler
extends java.lang.Object
Use AsyncMarker.incrementProcessingDelay()
to delay a packet until a certain condition has been met.
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancel the handler.
|
void |
enqueuePacket(PacketEvent packet)
Queue a packet for processing.
|
PacketListener |
getAsyncListener()
Retrieve the current asynchronous packet listener.
|
java.lang.String |
getFriendlyWorkerName(int id)
Create a friendly thread name using the following convention:
|
AsyncRunnable |
getListenerLoop()
Create a worker that will initiate the listener loop.
|
org.bukkit.plugin.Plugin |
getPlugin()
Retrieve the plugin associated with this async listener.
|
int |
getWorkers()
Retrieve the current number of registered workers.
|
boolean |
isCancelled()
Determine whether or not this asynchronous handler has been cancelled.
|
void |
setWorkers(int count)
Set the current number of workers.
|
void |
start()
Start a singler worker thread handling the asynchronous listener.
|
void |
start(com.google.common.base.Function<AsyncRunnable,java.lang.Void> executor)
Start a singler worker thread handling the asynchronous listener.
|
void |
start(int count)
Start multiple worker threads for this listener.
|
void |
stop()
Stop a worker thread.
|
void |
stop(int count)
Stop the given amount of worker threads.
|
boolean |
syncStart()
Start processing packets on the main thread.
|
boolean |
syncStart(long time,
java.util.concurrent.TimeUnit unit)
Start processing packets on the main thread.
|
boolean |
syncStop()
Stop processing packets on the main thread.
|
public boolean isCancelled()
public PacketListener getAsyncListener()
public org.bukkit.plugin.Plugin getPlugin()
public void cancel()
public void enqueuePacket(PacketEvent packet)
packet
- - a packet for processing.java.lang.IllegalStateException
- If the underlying packet queue is full.public AsyncRunnable getListenerLoop()
Warning: Never call the run() method in the main thread.
public void start()
public void start(com.google.common.base.Function<AsyncRunnable,java.lang.Void> executor)
This method is intended to allow callers to customize the thread priority before the worker loop is actually called. This is simpler than to schedule the worker threads manually.
listenerHandler.start(new Function<AsyncRunnable, Void>() {
@Override
public Void apply(@Nullable AsyncRunnable workerLoop) {
Thread thread = Thread.currentThread();
int prevPriority = thread.getPriority();
thread.setPriority(Thread.MIN_PRIORITY);
workerLoop.run();
thread.setPriority(prevPriority);
return null;
}
});
}
executor
- - a method that will execute the given listener loop.public java.lang.String getFriendlyWorkerName(int id)
Protocol Worker {id} - {plugin} - [recv: {packets}, send: {packets}]
id
- - the worker ID.public boolean syncStart()
This is useful if you need to synchronize with the main thread in your packet listener, but you're not performing any expensive processing.
Note: Use a asynchronous worker if the packet listener may use more than 0.5 ms of processing time on a single packet. Do as much as possible on the worker thread, and schedule synchronous tasks to use the Bukkit API instead.
java.lang.IllegalStateException
- If we couldn't start the underlying task.public boolean syncStart(long time, java.util.concurrent.TimeUnit unit)
This is useful if you need to synchronize with the main thread in your packet listener, but you're not performing any expensive processing.
The processing time parameter gives the upper bound for the amount of time spent processing pending packets. It should be set to a fairly low number, such as 0.5 ms or 1% of a game tick - to reduce the impact on the main thread. Never go beyond 50 milliseconds.
Note: Use a asynchronous worker if the packet listener may exceed the ideal processing time on a single packet. Do as much as possible on the worker thread, and schedule synchronous tasks to use the Bukkit API instead.
time
- - the amount of processing time alloted per game tick (20 ticks per second).unit
- - the unit of the processingTime argument.java.lang.IllegalStateException
- If we couldn't start the underlying task.public boolean syncStop()
public void start(int count)
count
- - number of worker threads to start.public void stop()
public void stop(int count)
count
- - number of threads to stop.public void setWorkers(int count)
This method can only be called with a count of zero when the listener is closing.
count
- - new number of workers.public int getWorkers()
Note that the returned value may be out of data.