Do not flip the buffer inside #write(ByteBuffer)

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-04 23:56:27 +02:00
parent 885be567fa
commit 7b8277b6be
3 changed files with 7 additions and 11 deletions

View File

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

View File

@ -230,15 +230,14 @@ public class PlayerSocketConnection extends PlayerConnection {
}
}
@ApiStatus.Internal
public void write(@NotNull ByteBuffer buffer) {
synchronized (bufferLock) {
final int size = buffer.position();
final int size = buffer.remaining();
if (size <= BUFFER_SIZE) {
if (!tickBuffer.canWrite(size)) flush();
this.tickBuffer.write(buffer.flip());
this.tickBuffer.write(buffer);
} else {
final int positionCache = buffer.position();
final int limitCache = buffer.limit();
final int bufferCount = size / BUFFER_SIZE + 1;
for (int i = 0; i < bufferCount; i++) {
buffer.position(i * BUFFER_SIZE);
@ -246,17 +245,16 @@ public class PlayerSocketConnection extends PlayerConnection {
if (!tickBuffer.canWrite(buffer.remaining())) flush();
this.tickBuffer.write(buffer);
}
buffer.position(positionCache).limit(limitCache);
}
}
}
public void write(@NotNull FramedPacket framedPacket) {
write(framedPacket.body());
write(framedPacket.body().flip());
}
public void write(@NotNull ServerPacket packet) {
write(PacketUtils.createFramedPacket(packet, compressed));
write(PacketUtils.createFramedPacket(packet, compressed).flip());
}
public void writeAndFlush(@NotNull ServerPacket packet) {

View File

@ -308,7 +308,6 @@ public final class PacketUtils {
final int start = pair.leftInt();
if (start != lastWrite) {
ByteBuffer slice = buffer.asByteBuffer(lastWrite, start);
slice.position(slice.limit());
writer.accept(slice);
}
lastWrite = pair.rightInt();
@ -318,7 +317,6 @@ public final class PacketUtils {
final int remaining = buffer.writerOffset() - lastWrite;
if (remaining > 0) {
ByteBuffer remainSlice = buffer.asByteBuffer(lastWrite, remaining);
remainSlice.position(remainSlice.limit());
writer.accept(remainSlice);
}
}