mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 22:47:49 +01:00
API cleanup
This commit is contained in:
parent
73f125763c
commit
024f8d36bd
@ -270,7 +270,11 @@ public class PlayerSocketConnection extends PlayerConnection {
|
|||||||
}
|
}
|
||||||
this.tickBuffer.writeChannel(channel);
|
this.tickBuffer.writeChannel(channel);
|
||||||
} catch (IOException e) {
|
} 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) {
|
} catch (ShortBufferException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -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 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 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, ViewableStorage> VIEWABLE_STORAGE_MAP = new WeakHashMap<>();
|
||||||
|
|
||||||
private PacketUtils() {
|
private PacketUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,6 +137,35 @@ public final class PacketUtils {
|
|||||||
sendGroupedPacket(MinecraftServer.getConnectionManager().getOnlinePlayers(), packet);
|
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,
|
public static void writeFramedPacket(@NotNull ByteBuffer buffer,
|
||||||
@NotNull ServerPacket packet,
|
@NotNull ServerPacket packet,
|
||||||
boolean compression) {
|
boolean compression) {
|
||||||
@ -233,8 +265,6 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<Viewable, ViewableStorage> VIEWABLE_STORAGE_MAP = new WeakHashMap<>();
|
|
||||||
|
|
||||||
private static final class ViewableStorage {
|
private static final class ViewableStorage {
|
||||||
private final Viewable viewable;
|
private final Viewable viewable;
|
||||||
private final Map<PlayerConnection, List<IntIntPair>> entityIdMap = new HashMap<>();
|
private final Map<PlayerConnection, List<IntIntPair>> entityIdMap = new HashMap<>();
|
||||||
@ -244,14 +274,14 @@ public final class PacketUtils {
|
|||||||
this.viewable = viewable;
|
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();
|
final ByteBuffer framedPacket = createFramedPacket(serverPacket).flip();
|
||||||
if (!buffer.canWrite(framedPacket.limit())) process();
|
if (!buffer.canWrite(framedPacket.limit())) process();
|
||||||
final int start = buffer.writerOffset();
|
final int start = buffer.writerOffset();
|
||||||
this.buffer.write(framedPacket);
|
this.buffer.write(framedPacket);
|
||||||
final int end = buffer.writerOffset();
|
final int end = buffer.writerOffset();
|
||||||
if (playerConnection != null) {
|
if (connection != null) {
|
||||||
List<IntIntPair> list = entityIdMap.computeIfAbsent(playerConnection, con -> new ArrayList<>());
|
List<IntIntPair> list = entityIdMap.computeIfAbsent(connection, con -> new ArrayList<>());
|
||||||
list.add(IntIntPair.of(start, end));
|
list.add(IntIntPair.of(start, end));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,32 +321,4 @@ public final class PacketUtils {
|
|||||||
this.buffer.clear();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user