diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index 49d478f2a..c2fac0361 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -89,7 +89,8 @@ public class PlayerSocketConnection extends PlayerConnection { try { cipher.update(bytes, 0, remainingBytes, output, 0); } catch (ShortBufferException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); + return; } readBuffer.clear(); readBuffer.writeBytes(output); @@ -134,14 +135,14 @@ public class PlayerSocketConnection extends PlayerConnection { // Process packet final int packetId = content.readVarInt(); try { - var finalBuffer = content.asByteBuffer(content.readerOffset(), packetEnd); - packetProcessor.process(this, packetId, finalBuffer); + final ByteBuffer payload = content.asByteBuffer(content.readerOffset(), packetEnd); + packetProcessor.process(this, packetId, payload); } catch (Exception e) { // Error while reading the packet MinecraftServer.getExceptionManager().handleException(e); break; } - // Return to original state (before writing) + // Position buffer to read the next packet readBuffer.reset(packetEnd, limit); } catch (BufferUnderflowException e) { readBuffer.reset(beginMark); diff --git a/src/main/java/net/minestom/server/network/socket/Worker.java b/src/main/java/net/minestom/server/network/socket/Worker.java index 80487897a..54b94d61d 100644 --- a/src/main/java/net/minestom/server/network/socket/Worker.java +++ b/src/main/java/net/minestom/server/network/socket/Worker.java @@ -61,17 +61,6 @@ public final class Worker extends Thread { } } - public void receiveConnection(SocketChannel channel) throws IOException { - this.connectionMap.put(channel, new PlayerSocketConnection(this, channel, channel.getRemoteAddress())); - channel.configureBlocking(false); - channel.register(selector, SelectionKey.OP_READ); - var socket = channel.socket(); - socket.setSendBufferSize(Server.SOCKET_BUFFER_SIZE); - socket.setReceiveBufferSize(Server.SOCKET_BUFFER_SIZE); - socket.setTcpNoDelay(Server.NO_DELAY); - this.selector.wakeup(); - } - public void disconnect(PlayerSocketConnection connection, SocketChannel channel) { try { channel.close(); @@ -87,6 +76,17 @@ public final class Worker extends Thread { } } + void receiveConnection(SocketChannel channel) throws IOException { + this.connectionMap.put(channel, new PlayerSocketConnection(this, channel, channel.getRemoteAddress())); + channel.configureBlocking(false); + channel.register(selector, SelectionKey.OP_READ); + var socket = channel.socket(); + socket.setSendBufferSize(Server.SOCKET_BUFFER_SIZE); + socket.setReceiveBufferSize(Server.SOCKET_BUFFER_SIZE); + socket.setTcpNoDelay(Server.NO_DELAY); + this.selector.wakeup(); + } + /** * Contains objects that we can be shared across all the connection of a {@link Worker worker}. */ @@ -95,7 +95,7 @@ public final class Worker extends Thread { public final BinaryBuffer contentBuffer = BinaryBuffer.ofSize(Server.MAX_PACKET_SIZE); public final Inflater inflater = new Inflater(); - public void clearBuffers() { + void clearBuffers() { this.readBuffer.clear(); this.contentBuffer.clear(); }