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 2fbba6aba..c8ae92419 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -270,7 +270,11 @@ public class PlayerSocketConnection extends PlayerConnection { } this.tickBuffer.writeChannel(channel); } catch (IOException e) { - MinecraftServer.getExceptionManager().handleException(e); + final String message = e.getMessage(); + if (message == null || + (!message.equals("Broken pipe") && !message.equals("Connection reset by peer"))) { + MinecraftServer.getExceptionManager().handleException(e); + } } catch (ShortBufferException e) { e.printStackTrace(); } finally { diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 10886cb65..bbfdb2aae 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -39,6 +39,9 @@ public final class PacketUtils { private static final LocalCache PACKET_BUFFER = LocalCache.get("packet-buffer", Server.SOCKET_BUFFER_SIZE); private static final LocalCache COMPRESSION_CACHE = LocalCache.get("compression-buffer", Server.SOCKET_BUFFER_SIZE); + private static final Object VIEWABLE_PACKET_LOCK = new Object(); + private static final Map VIEWABLE_STORAGE_MAP = new WeakHashMap<>(); + private PacketUtils() { } @@ -134,6 +137,35 @@ public final class PacketUtils { sendGroupedPacket(MinecraftServer.getConnectionManager().getOnlinePlayers(), packet); } + @ApiStatus.Experimental + public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket, + @Nullable Player player) { + if (player != null && !player.isAutoViewable()) { + // Operation cannot be optimized + player.sendPacketToViewers(serverPacket); + return; + } + ViewableStorage viewableStorage; + synchronized (VIEWABLE_PACKET_LOCK) { + viewableStorage = VIEWABLE_STORAGE_MAP.computeIfAbsent(viewable, ViewableStorage::new); + } + viewableStorage.append(serverPacket, player != null ? player.getPlayerConnection() : null); + } + + @ApiStatus.Experimental + public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) { + prepareViewablePacket(viewable, serverPacket, null); + } + + @ApiStatus.Internal + public static void flush() { + synchronized (VIEWABLE_PACKET_LOCK) { + for (ViewableStorage viewableStorage : VIEWABLE_STORAGE_MAP.values()) { + viewableStorage.process(); + } + } + } + public static void writeFramedPacket(@NotNull ByteBuffer buffer, @NotNull ServerPacket packet, boolean compression) { @@ -233,8 +265,6 @@ public final class PacketUtils { } } - private static final Map VIEWABLE_STORAGE_MAP = new WeakHashMap<>(); - private static final class ViewableStorage { private final Viewable viewable; private final Map> entityIdMap = new HashMap<>(); @@ -244,14 +274,14 @@ public final class PacketUtils { this.viewable = viewable; } - private synchronized void append(PlayerConnection playerConnection, ServerPacket serverPacket) { + private synchronized void append(ServerPacket serverPacket, PlayerConnection connection) { final ByteBuffer framedPacket = createFramedPacket(serverPacket).flip(); if (!buffer.canWrite(framedPacket.limit())) process(); final int start = buffer.writerOffset(); this.buffer.write(framedPacket); final int end = buffer.writerOffset(); - if (playerConnection != null) { - List list = entityIdMap.computeIfAbsent(playerConnection, con -> new ArrayList<>()); + if (connection != null) { + List list = entityIdMap.computeIfAbsent(connection, con -> new ArrayList<>()); list.add(IntIntPair.of(start, end)); } } @@ -291,32 +321,4 @@ public final class PacketUtils { this.buffer.clear(); } } - - private static final Object VIEWABLE_PACKET_LOCK = new Object(); - - public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket, - @Nullable Player player) { - if (player != null && !player.isAutoViewable()) { - // Operation cannot be optimized - player.sendPacketToViewers(serverPacket); - return; - } - final PlayerConnection playerConnection = player != null ? player.getPlayerConnection() : null; - synchronized (VIEWABLE_PACKET_LOCK) { - ViewableStorage viewableStorage = VIEWABLE_STORAGE_MAP.computeIfAbsent(viewable, ViewableStorage::new); - viewableStorage.append(playerConnection, serverPacket); - } - } - - public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) { - prepareViewablePacket(viewable, serverPacket, null); - } - - public static void flush() { - synchronized (VIEWABLE_PACKET_LOCK) { - for (ViewableStorage viewableStorage : VIEWABLE_STORAGE_MAP.values()) { - viewableStorage.process(); - } - } - } }