Batch sendPackets list content in a single message

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-17 23:12:54 +01:00
parent 750e9b5324
commit 767c743e01
3 changed files with 18 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
@ -65,24 +66,14 @@ public interface Viewable {
}
}
/**
* Sends multiple packets to all viewers.
* <p>
* It is better than looping through the viewers
* to send a packet since it is here only serialized once.
*
* @param packets the packets to send
*/
default void sendPacketsToViewers(@NotNull SendablePacket... packets) {
for (SendablePacket packet : packets) {
sendPacketToViewers(packet);
}
}
default void sendPacketsToViewers(@NotNull Collection<SendablePacket> packets) {
packets.forEach(this::sendPacketToViewers);
}
default void sendPacketsToViewers(@NotNull SendablePacket... packets) {
sendPacketsToViewers(List.of(packets));
}
/**
* Sends a packet to all viewers and the viewable element if it is a player.
* <p>

View File

@ -17,6 +17,7 @@ import org.jetbrains.annotations.Nullable;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
@ -92,15 +93,13 @@ public abstract class PlayerConnection {
public abstract void sendPacket(@NotNull SendablePacket packet);
@ApiStatus.Experimental
public void sendPackets(@NotNull SendablePacket... packets) {
for (SendablePacket p : packets) {
sendPacket(p);
}
public void sendPackets(@NotNull Collection<SendablePacket> packets) {
packets.forEach(this::sendPacket);
}
@ApiStatus.Experimental
public void sendPackets(@NotNull Collection<SendablePacket> packet) {
packet.forEach(this::sendPacket);
public void sendPackets(@NotNull SendablePacket... packets) {
sendPackets(List.of(packets));
}
/**

View File

@ -196,6 +196,14 @@ public class PlayerSocketConnection extends PlayerConnection {
this.worker.queue().offer(() -> writePacketSync(packet, compressed));
}
@Override
public void sendPackets(@NotNull Collection<SendablePacket> packets) {
final boolean compressed = this.compressed;
this.worker.queue().offer(() -> {
for (SendablePacket packet : packets) writePacketSync(packet, compressed);
});
}
@ApiStatus.Internal
public void write(@NotNull ByteBuffer buffer, int index, int length) {
this.worker.queue().offer(() -> writeBufferSync(buffer, index, length));