Add experimental PlayerConnection#sendFramedPacket

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-20 18:59:15 +02:00
parent 4e58d61345
commit 17807f35da
4 changed files with 20 additions and 15 deletions

View File

@ -130,8 +130,8 @@ public class DynamicChunk extends Chunk {
final FramedPacket chunkPacket = chunkCache.retrieveFramedPacket(lastChange); final FramedPacket chunkPacket = chunkCache.retrieveFramedPacket(lastChange);
if (connection instanceof PlayerSocketConnection) { if (connection instanceof PlayerSocketConnection) {
PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection; PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection;
socketConnection.write(lightPacket); socketConnection.sendFramedPacket(lightPacket);
socketConnection.write(chunkPacket); socketConnection.sendFramedPacket(chunkPacket);
} else { } else {
connection.sendPacket(lightPacket.packet()); connection.sendPacket(lightPacket.packet());
connection.sendPacket(chunkPacket.packet()); connection.sendPacket(chunkPacket.packet());

View File

@ -8,7 +8,9 @@ import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.listener.manager.ServerPacketConsumer; import net.minestom.server.listener.manager.ServerPacketConsumer;
import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState; import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.FramedPacket;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -100,6 +102,11 @@ public abstract class PlayerConnection {
*/ */
public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating); public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating);
@ApiStatus.Experimental
public void sendFramedPacket(@NotNull FramedPacket framedPacket) {
this.sendPacket(framedPacket.packet());
}
/** /**
* Flush waiting data to the connection. * Flush waiting data to the connection.
* <p> * <p>

View File

@ -210,7 +210,7 @@ public class PlayerSocketConnection extends PlayerConnection {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale))); GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
} }
write(serverPacket); writePacket(serverPacket);
} else { } else {
// Player is probably not logged yet // Player is probably not logged yet
writeAndFlush(serverPacket); writeAndFlush(serverPacket);
@ -218,6 +218,11 @@ public class PlayerSocketConnection extends PlayerConnection {
} }
} }
@Override
public void sendFramedPacket(@NotNull FramedPacket framedPacket) {
write(framedPacket.body().duplicate().position(0));
}
@ApiStatus.Internal @ApiStatus.Internal
public void write(@NotNull ByteBuffer buffer) { public void write(@NotNull ByteBuffer buffer) {
synchronized (bufferLock) { synchronized (bufferLock) {
@ -245,17 +250,13 @@ public class PlayerSocketConnection extends PlayerConnection {
} }
} }
public void write(@NotNull FramedPacket framedPacket) { private void writePacket(@NotNull ServerPacket packet) {
write(framedPacket.body().duplicate().position(0));
}
public void write(@NotNull ServerPacket packet) {
write(PacketUtils.createFramedPacket(packet, compressed).flip()); write(PacketUtils.createFramedPacket(packet, compressed).flip());
} }
public void writeAndFlush(@NotNull ServerPacket packet) { public void writeAndFlush(@NotNull ServerPacket packet) {
synchronized (bufferLock) { synchronized (bufferLock) {
write(packet); writePacket(packet);
flush(); flush();
} }
} }

View File

@ -120,12 +120,7 @@ public final class PacketUtils {
for (Player player : players) { for (Player player : players) {
if (!player.isOnline() || !playerValidator.isValid(player)) if (!player.isOnline() || !playerValidator.isValid(player))
continue; continue;
final PlayerConnection connection = player.getPlayerConnection(); player.getPlayerConnection().sendFramedPacket(framedPacket);
if (connection instanceof PlayerSocketConnection) {
((PlayerSocketConnection) connection).write(framedPacket);
} else {
connection.sendPacket(packet);
}
} }
} else { } else {
// Write the same packet for each individual players // Write the same packet for each individual players
@ -218,12 +213,14 @@ public final class PacketUtils {
Utils.writeVarIntHeader(buffer, uncompressedIndex, compressed ? packetSize : 0); Utils.writeVarIntHeader(buffer, uncompressedIndex, compressed ? packetSize : 0);
} }
@ApiStatus.Internal
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet, boolean compression) { public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet, boolean compression) {
ByteBuffer buffer = PACKET_BUFFER.get(); ByteBuffer buffer = PACKET_BUFFER.get();
writeFramedPacket(buffer, packet, compression); writeFramedPacket(buffer, packet, compression);
return buffer; return buffer;
} }
@ApiStatus.Internal
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) { public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) {
return createFramedPacket(packet, MinecraftServer.getCompressionThreshold() > 0); return createFramedPacket(packet, MinecraftServer.getCompressionThreshold() > 0);
} }