From 4f4202185c4cac661ca18fa6cf9afc14f87eda47 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 29 Sep 2012 21:18:21 +0200 Subject: [PATCH] Make it even clearer that the listener loop method should be called from a separate thread. --- .../protocol/async/AsyncListenerHandler.java | 18 ++++++++++++++---- .../protocol/async/PacketSendingQueue.java | 6 ++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java b/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java index 4521831d..72c04470 100644 --- a/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java +++ b/ProtocolLib/src/com/comphenix/protocol/async/AsyncListenerHandler.java @@ -86,12 +86,22 @@ public class AsyncListenerHandler { } /** - * Entry point for the background thread that will be processing the packet asynchronously. + * Create a runnable that will initiate the listener loop. *

- * WARNING: - * Never call this method from the main thread. Doing so will block Minecraft. + * Warning: Never call the run() method in the main thread. */ - public void listenerLoop() { + public Runnable getListenerLoop() { + return new Runnable() { + @Override + public void run() { + listenerLoop(); + } + }; + } + + // DO NOT call this method from the main thread + private void listenerLoop() { + // Danger, danger! if (Thread.currentThread().getId() == mainThread.getId()) throw new IllegalStateException("Do not call this method from the main thread."); diff --git a/ProtocolLib/src/com/comphenix/protocol/async/PacketSendingQueue.java b/ProtocolLib/src/com/comphenix/protocol/async/PacketSendingQueue.java index 2c894dbd..9ab9fb7e 100644 --- a/ProtocolLib/src/com/comphenix/protocol/async/PacketSendingQueue.java +++ b/ProtocolLib/src/com/comphenix/protocol/async/PacketSendingQueue.java @@ -27,7 +27,13 @@ class PacketSendingQueue { public synchronized void signalPacketUpdate(PacketEvent packetUpdated) { // Mark this packet as finished packetUpdated.getAsyncMarker().setProcessed(true); + trySendPackets(); + } + /** + * Attempt to send any remaining packets. + */ + public synchronized void trySendPackets() { // Transmit as many packets as we can while (true) { PacketEvent current = sendingQueue.peek();