3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Clear bytes instead of reading them, close streams when done. (May fix a few memory leaks)

Dieser Commit ist enthalten in:
Myles 2016-03-07 18:51:18 +00:00
Ursprung 99eadbe343
Commit fc7cdb4244
6 geänderte Dateien mit 21 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -1,7 +1,7 @@
package us.myles.ViaVersion;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

Datei anzeigen

@ -32,10 +32,10 @@ public class ViaDecodeHandler extends ByteToMessageDecoder {
ByteBuf newPacket = ctx.alloc().buffer();
try {
incomingTransformer.transform(id, bytebuf, newPacket);
bytebuf.readBytes(bytebuf.readableBytes());
bytebuf.clear();
bytebuf = newPacket;
} catch (CancelException e) {
bytebuf.readBytes(bytebuf.readableBytes());
bytebuf.clear();
throw e;
}
}

Datei anzeigen

@ -24,7 +24,6 @@ public class ViaEncodeHandler extends MessageToByteEncoder {
}
@Override
protected void encode(final ChannelHandlerContext ctx, Object o, final ByteBuf bytebuf) throws Exception {
// handle the packet type
@ -57,13 +56,8 @@ public class ViaEncodeHandler extends MessageToByteEncoder {
}
}
};
// Synced allows timings to work properly.
// if (ViaVersion.getInstance().isSyncedChunks()) {
// ((ViaVersionPlugin) ViaVersion.getInstance()).run(chunks, false);
// } else {
chunks.run();
// }
bytebuf.readBytes(bytebuf.readableBytes());
bytebuf.clear();
throw new CancelException();
}
// call minecraft encoder
@ -80,7 +74,7 @@ public class ViaEncodeHandler extends MessageToByteEncoder {
try {
outgoingTransformer.transform(id, oldPacket, bytebuf);
} catch (CancelException e) {
bytebuf.readBytes(bytebuf.readableBytes());
bytebuf.clear();
throw e;
} finally {
oldPacket.release();

Datei anzeigen

@ -193,7 +193,8 @@ public class IncomingTransformer {
ByteBuf in = Unpooled.wrappedBuffer(b);
try {
ItemSlotRewriter.ItemStack stack = ItemSlotRewriter.readItemStack(in);
stack.id = (short) Material.WRITTEN_BOOK.getId();
if (stack != null)
stack.id = (short) Material.WRITTEN_BOOK.getId();
// write
ItemSlotRewriter.writeItemStack(stack, output);
} catch (IOException e) {
@ -234,7 +235,7 @@ public class IncomingTransformer {
output.writeLong(position);
int face = PacketUtil.readVarInt(input);
output.writeByte(face);
int hand = PacketUtil.readVarInt(input);
PacketUtil.readVarInt(input);
ItemStack inHand = ViaVersionPlugin.getHandItem(info);
try {

Datei anzeigen

@ -42,7 +42,6 @@ public class OutgoingTransformer {
private final Map<Integer, UUID> uuidMap = new HashMap<>();
private final Map<Integer, EntityType> clientEntityTypes = new HashMap<>();
private final Map<Integer, Integer> vehicleMap = new HashMap<>();
private boolean cancel = false;
private boolean autoTeam = false;
public OutgoingTransformer(ConnectionInfo info) {
@ -72,10 +71,6 @@ public class OutgoingTransformer {
}
public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException {
if (cancel) {
throw new CancelException();
}
PacketType packet = PacketType.getOutgoingPacket(info.getState(), packetID);
int original = packetID;
if (packet == null) {

Datei anzeigen

@ -48,7 +48,13 @@ public class PacketUtil {
return null;
} else {
input.readerIndex(readerIndex);
return (CompoundTag) NBTIO.readTag(new DataInputStream(new ByteBufInputStream(input)));
ByteBufInputStream bytebufStream = new ByteBufInputStream(input);
DataInputStream dataInputStream = new DataInputStream(bytebufStream);
try {
return (CompoundTag) NBTIO.readTag(dataInputStream);
} finally {
dataInputStream.close();
}
}
}
@ -56,7 +62,12 @@ public class PacketUtil {
if (tag == null) {
output.writeByte(0);
} else {
NBTIO.writeTag(new DataOutputStream(new ByteBufOutputStream(output)), tag);
ByteBufOutputStream bytebufStream = new ByteBufOutputStream(output);
DataOutputStream dataOutputStream = new DataOutputStream(bytebufStream);
NBTIO.writeTag(dataOutputStream, tag);
dataOutputStream.close();
}
}