ProtocolLib

3M Downloads

How to continue asynchronous packets processing?

molor opened this issue ยท 4 comments

commented

Describe the question
In title.

for (Player onlinePlayer : server.getOnlinePlayers()) {
	for (int i = 1; i <= 256; i ++) {
		onlinePlayer.sendMessage("");
	}
}

e.g. some plugin tries to clear chat for every player. ~32 first messages/packets are sent with an interval of 1 second (Thread.sleep() in Code is only for debug), after which the server waits for something and can wait forever, until another message isn't sent to chat via Player#sendMessage(String). In that case, player will receive a next 32 messages/packets and wait again..

API method(s) used
See Code

ProtocolLib version is v4.6.1-SNAPSHOT-b503
Server version is git-Paper-753 (MC: 1.16.5)

Expected behavior
All packets will be processed and sent instantly

Code

ProblemPlugin.java:

ProtocolLibrary.getProtocolManager().getAsynchronousManager().registerAsyncHandler(new ChatEventHandler(thePlugin)).start(2);

ChatEventHandler.java:

public ChatEventHandler(ReplacerPlugin thePlugin) {
	super(thePlugin, ListenerPriority.HIGHEST, PacketType.Play.Server.CHAT);
}

public void onPacketSending(PacketEvent theEvent) {
	if (theEvent.isCancelled()) {
		return;
	}

	PacketContainer theContainer = theEvent.getPacket();

	switch (theContainer.getChatTypes().read(0)) {
		case CHAT:
		case SYSTEM: {
			break;
		}

		default: {
			return;
		}
	}

	StructureModifier<WrappedChatComponent> chatComponents = theContainer.getChatComponents();
	WrappedChatComponent chatComponent = chatComponents.read(0);

	System.out.println("data: " + chatComponent);
	Thread.sleep(1000);
}
commented

I'm a little confused as to what the question is

commented

@dmulloy2, hello. In Code above, even with Thread.sleep() or without it, if any plugin or server sends >32 chat messages to player, only first 32 messages will be processed by onPacketSending. Remaining messages will be processed after another message from plugin or server. Just try to reproduce this using the code above. It looks like there is some kind of buffer of processed onPacketSending results that not yet sent to client. If so, how to provoke it to be sent and cleaned up?

Initial question is how to avoid such behavior and make the processing of all messages without waiting?

commented

Performing a Semaphore#release(int) with a some large number as argument on com.comphenix.protocol.async.PacketProcessingQueue#concurrentProcessing during my JavaPlugin#onEnable() was solved this issue for me, now any amount of chat messages are processed instantly.

commented

Not sure if you're aware, but 1.17 just came out so I'd been working on that