Simplify packet writing

This commit is contained in:
TheMode 2021-08-05 03:09:45 +02:00
parent a52d574049
commit 87f141ff50
2 changed files with 13 additions and 14 deletions

View File

@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable;
import javax.crypto.SecretKey;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
@ -211,16 +210,8 @@ public class NettyPlayerConnection extends PlayerConnection {
}
public void write(@NotNull ServerPacket packet) {
synchronized (tickBuffer) {
this.tickBuffer.mark();
try {
PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
} catch (BufferOverflowException e) {
this.tickBuffer.reset();
flush();
PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
}
}
// TODO write directly to the tick buffer
write(PacketUtils.createFramedPacket(packet, compressed));
}
public void writeAndFlush(@NotNull ServerPacket packet) {
@ -231,8 +222,8 @@ public class NettyPlayerConnection extends PlayerConnection {
}
public void flush() {
if (!channel.isOpen()) return;
synchronized (tickBuffer) {
if (!channel.isOpen()) return;
if (tickBuffer.position() == 0) return;
try {
this.channel.write(tickBuffer.flip());

View File

@ -163,8 +163,8 @@ public final class PacketUtils {
}
}
public static ByteBuffer createFramedPacket(@NotNull ByteBuffer initial, @NotNull ServerPacket packet) {
final boolean compression = MinecraftServer.getCompressionThreshold() > 0;
public static ByteBuffer createFramedPacket(@NotNull ByteBuffer initial, @NotNull ServerPacket packet,
boolean compression) {
var buffer = initial;
try {
writeFramedPacket(buffer, packet, compression);
@ -177,10 +177,18 @@ public final class PacketUtils {
return buffer;
}
public static ByteBuffer createFramedPacket(@NotNull ByteBuffer initial, @NotNull ServerPacket packet) {
return createFramedPacket(initial, packet, MinecraftServer.getCompressionThreshold() > 0);
}
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) {
return createFramedPacket(BUFFER.get().clear(), packet);
}
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet, boolean compression) {
return createFramedPacket(BUFFER.get().clear(), packet, compression);
}
public static ByteBuffer allocateTrimmedPacket(@NotNull ServerPacket packet) {
final var temp = PacketUtils.createFramedPacket(packet);
return ByteBuffer.allocate(temp.position()).put(temp.flip());