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 org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Set; 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) { default void sendPacketsToViewers(@NotNull Collection<SendablePacket> packets) {
packets.forEach(this::sendPacketToViewers); 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. * Sends a packet to all viewers and the viewable element if it is a player.
* <p> * <p>

View File

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

View File

@ -196,6 +196,14 @@ public class PlayerSocketConnection extends PlayerConnection {
this.worker.queue().offer(() -> writePacketSync(packet, compressed)); 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 @ApiStatus.Internal
public void write(@NotNull ByteBuffer buffer, int index, int length) { public void write(@NotNull ByteBuffer buffer, int index, int length) {
this.worker.queue().offer(() -> writeBufferSync(buffer, index, length)); this.worker.queue().offer(() -> writeBufferSync(buffer, index, length));