Class AsyncListenerHandler
Use AsyncMarker.incrementProcessingDelay() to delay a packet until a certain condition has been met.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancel the handler.voidenqueuePacket(PacketEvent packet) Queue a packet for processing.Retrieve the current asynchronous packet listener.getFriendlyWorkerName(int id) Create a friendly thread name using the following convention:Create a worker that will initiate the listener loop.org.bukkit.plugin.PluginRetrieve the plugin associated with this async listener.intRetrieve the current number of registered workers.booleanDetermine whether or not this asynchronous handler has been cancelled.voidsetWorkers(int count) Set the current number of workers.voidstart()Start a singler worker thread handling the asynchronous listener.voidstart(int count) Start multiple worker threads for this listener.voidstart(com.google.common.base.Function<AsyncRunnable, Void> executor) Start a singler worker thread handling the asynchronous listener.voidstop()Stop a worker thread.voidstop(int count) Stop the given amount of worker threads.booleanStart processing packets on the main thread.booleanStart processing packets on the main thread.booleansyncStop()Stop processing packets on the main thread.
-
Field Details
-
REPORT_HANDLER_NOT_STARTED
-
-
Method Details
-
isCancelled
public boolean isCancelled()Determine whether or not this asynchronous handler has been cancelled.- Returns:
- TRUE if it has been cancelled/stopped, FALSE otherwise.
-
getAsyncListener
Retrieve the current asynchronous packet listener.- Returns:
- Current packet listener.
-
getPlugin
public org.bukkit.plugin.Plugin getPlugin()Retrieve the plugin associated with this async listener.- Returns:
- The plugin.
-
cancel
public void cancel()Cancel the handler. -
enqueuePacket
Queue a packet for processing.- Parameters:
packet- - a packet for processing.- Throws:
IllegalStateException- If the underlying packet queue is full.
-
getListenerLoop
Create a worker that will initiate the listener loop. Note that using stop() to close a specific worker is less efficient than stopping an arbitrary worker.Warning: Never call the run() method in the main thread.
- Returns:
- The listener loop
-
start
public void start()Start a singler worker thread handling the asynchronous listener. -
start
Start a singler worker thread handling the asynchronous listener.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; } }); }- Parameters:
executor- - a method that will execute the given listener loop.
-
getFriendlyWorkerName
Create a friendly thread name using the following convention:Protocol Worker {id} - {plugin} - [recv: {packets}, send: {packets}]- Parameters:
id- - the worker ID.- Returns:
- A friendly thread name.
-
syncStart
public boolean syncStart()Start processing packets on the main thread.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.
- Returns:
- TRUE if the synchronized processing was successfully started, FALSE if it's already running.
- Throws:
IllegalStateException- If we couldn't start the underlying task.
-
syncStart
Start processing packets on the main thread.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.
- Parameters:
time- - the amount of processing time alloted per game tick (20 ticks per second).unit- - the unit of the processingTime argument.- Returns:
- TRUE if the synchronized processing was successfully started, FALSE if it's already running.
- Throws:
IllegalStateException- If we couldn't start the underlying task.
-
syncStop
public boolean syncStop()Stop processing packets on the main thread.- Returns:
- TRUE if we stopped any processing tasks, FALSE if it has already been stopped.
-
start
public void start(int count) Start multiple worker threads for this listener.- Parameters:
count- - number of worker threads to start.
-
stop
public void stop()Stop a worker thread. -
stop
public void stop(int count) Stop the given amount of worker threads.- Parameters:
count- - number of threads to stop.
-
setWorkers
public void setWorkers(int count) Set the current number of workers.This method can only be called with a count of zero when the listener is closing.
- Parameters:
count- - new number of workers.
-
getWorkers
public int getWorkers()Retrieve the current number of registered workers.Note that the returned value may be out of data.
- Returns:
- Number of registered workers.
-