Merge pull request #1078 from thatsIch/b-1077-hotfix-packet-size

Fixes #1077 Hotfix: Opening a terminal will not crash as fast anymore in systems with many items.
This commit is contained in:
thatsIch
2015-03-25 12:19:49 +01:00
2 changed files with 118 additions and 105 deletions
@@ -18,6 +18,7 @@
package appeng.core.sync; package appeng.core.sync;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
@@ -50,101 +51,101 @@ import appeng.core.sync.packets.PacketSwitchGuis;
import appeng.core.sync.packets.PacketTransitionEffect; import appeng.core.sync.packets.PacketTransitionEffect;
import appeng.core.sync.packets.PacketValueConfig; import appeng.core.sync.packets.PacketValueConfig;
public class AppEngPacketHandlerBase public class AppEngPacketHandlerBase
{ {
private static final Map<Class<? extends AppEngPacket>, PacketTypes> REVERSE_LOOKUP = new HashMap<Class<? extends AppEngPacket>, AppEngPacketHandlerBase.PacketTypes>();
public static final Map<Class, PacketTypes> REVERSE_LOOKUP = new HashMap<Class, AppEngPacketHandlerBase.PacketTypes>();
public enum PacketTypes public enum PacketTypes
{ {
PACKET_COMPASS_REQUEST(PacketCompassRequest.class), PACKET_COMPASS_REQUEST( PacketCompassRequest.class ),
PACKET_COMPASS_RESPONSE(PacketCompassResponse.class), PACKET_COMPASS_RESPONSE( PacketCompassResponse.class ),
PACKET_INVENTORY_ACTION(PacketInventoryAction.class), PACKET_INVENTORY_ACTION( PacketInventoryAction.class ),
PACKET_ME_INVENTORY_UPDATE(PacketMEInventoryUpdate.class), PACKET_ME_INVENTORY_UPDATE( PacketMEInventoryUpdate.class ),
PACKET_CONFIG_BUTTON(PacketConfigButton.class), PACKET_CONFIG_BUTTON( PacketConfigButton.class ),
PACKET_MULTIPART(PacketMultiPart.class), PACKET_MULTIPART( PacketMultiPart.class ),
PACKET_PART_PLACEMENT(PacketPartPlacement.class), PACKET_PART_PLACEMENT( PacketPartPlacement.class ),
PACKET_LIGHTNING(PacketLightning.class), PACKET_LIGHTNING( PacketLightning.class ),
PACKET_MATTER_CANNON(PacketMatterCannon.class), PACKET_MATTER_CANNON( PacketMatterCannon.class ),
PACKET_MOCK_EXPLOSION(PacketMockExplosion.class), PACKET_MOCK_EXPLOSION( PacketMockExplosion.class ),
PACKET_VALUE_CONFIG(PacketValueConfig.class), PACKET_VALUE_CONFIG( PacketValueConfig.class ),
PACKET_TRANSITION_EFFECT(PacketTransitionEffect.class), PACKET_TRANSITION_EFFECT( PacketTransitionEffect.class ),
PACKET_PROGRESS_VALUE(PacketProgressBar.class), PACKET_PROGRESS_VALUE( PacketProgressBar.class ),
PACKET_CLICK(PacketClick.class), PACKET_CLICK( PacketClick.class ),
PACKET_NEW_STORAGE_DIMENSION(PacketNewStorageDimension.class), PACKET_NEW_STORAGE_DIMENSION( PacketNewStorageDimension.class ),
PACKET_SWITCH_GUIS(PacketSwitchGuis.class), PACKET_SWITCH_GUIS( PacketSwitchGuis.class ),
PACKET_SWAP_SLOTS(PacketSwapSlots.class), PACKET_SWAP_SLOTS( PacketSwapSlots.class ),
PACKET_PATTERN_SLOT(PacketPatternSlot.class), PACKET_PATTERN_SLOT( PacketPatternSlot.class ),
PACKET_RECIPE_NEI(PacketNEIRecipe.class), PACKET_RECIPE_NEI( PacketNEIRecipe.class ),
PACKET_PARTIAL_ITEM(PacketPartialItem.class), PACKET_PARTIAL_ITEM( PacketPartialItem.class ),
PACKET_CRAFTING_REQUEST(PacketCraftRequest.class), PACKET_CRAFTING_REQUEST( PacketCraftRequest.class ),
PACKET_ASSEMBLER_ANIMATION(PacketAssemblerAnimation.class), PACKET_ASSEMBLER_ANIMATION( PacketAssemblerAnimation.class ),
PACKET_COMPRESSED_NBT(PacketCompressedNBT.class), PACKET_COMPRESSED_NBT( PacketCompressedNBT.class ),
PACKET_PAINTED_ENTITY(PacketPaintedEntity.class); PACKET_PAINTED_ENTITY( PacketPaintedEntity.class );
final public Class pc; private final Class<? extends AppEngPacket> packetClass;
final public Constructor con; private final Constructor<? extends AppEngPacket> packetConstructor;
private PacketTypes(Class c) { PacketTypes( Class<? extends AppEngPacket> c )
this.pc = c; {
this.packetClass = c;
Constructor x = null; Constructor<? extends AppEngPacket> x = null;
try try
{ {
x = this.pc.getConstructor( ByteBuf.class ); x = this.packetClass.getConstructor( ByteBuf.class );
} }
catch (NoSuchMethodException ignored) catch( NoSuchMethodException ignored )
{ {
} }
catch (SecurityException ignored) catch( SecurityException ignored )
{ {
} }
this.con = x; this.packetConstructor = x;
AppEngPacketHandlerBase.REVERSE_LOOKUP.put( this.pc, this ); REVERSE_LOOKUP.put( this.packetClass, this );
if ( this.con == null ) if( this.packetConstructor == null )
throw new RuntimeException( "Invalid Packet Class, must be constructable on DataInputStream" ); throw new RuntimeException( "Invalid Packet Class, must be constructable on DataInputStream" );
} }
public AppEngPacket parsePacket(ByteBuf in) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException public static PacketTypes getPacket( int id )
{ {
return (AppEngPacket) this.con.newInstance( in ); return ( values() )[id];
} }
public static PacketTypes getPacket(int id) public static PacketTypes getID( Class<? extends AppEngPacket> c )
{ {
return (values())[id]; return REVERSE_LOOKUP.get( c );
} }
public static PacketTypes getID(Class<? extends AppEngPacket> c) public AppEngPacket parsePacket( ByteBuf in ) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{ {
return AppEngPacketHandlerBase.REVERSE_LOOKUP.get( c ); return this.packetConstructor.newInstance( in );
} }
} }
} }
@@ -18,6 +18,7 @@
package appeng.core.sync.packets; package appeng.core.sync.packets;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@@ -26,6 +27,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@@ -48,23 +50,31 @@ import appeng.core.sync.AppEngPacket;
import appeng.core.sync.network.INetworkInfo; import appeng.core.sync.network.INetworkInfo;
import appeng.util.item.AEItemStack; import appeng.util.item.AEItemStack;
public class PacketMEInventoryUpdate extends AppEngPacket public class PacketMEInventoryUpdate extends AppEngPacket
{ {
private static final int UNCOMPRESSED_PACKET_BYTE_LIMIT = 16 * 1024 * 1024;
// output... private static final int OPERATION_BYTE_LIMIT = 2 * 1024;
final private byte ref; private static final int TEMP_BUFFER_SIZE = 1024;
final private ByteBuf data; private static final int STREAM_MASK = 0xff;
final private GZIPOutputStream compressFrame;
int writtenBytes = 0;
boolean empty = true;
// input. // input.
final List<IAEItemStack> list; @Nullable
private final List<IAEItemStack> list;
// output...
private final byte ref;
@Nullable
private final ByteBuf data;
@Nullable
private final GZIPOutputStream compressFrame;
private int writtenBytes = 0;
private boolean empty = true;
// automatic. // automatic.
public PacketMEInventoryUpdate(final ByteBuf stream) throws IOException { public PacketMEInventoryUpdate( final ByteBuf stream ) throws IOException
{
this.data = null; this.data = null;
this.compressFrame = null; this.compressFrame = null;
this.list = new LinkedList<IAEItemStack>(); this.list = new LinkedList<IAEItemStack>();
@@ -72,25 +82,24 @@ public class PacketMEInventoryUpdate extends AppEngPacket
// int originalBytes = stream.readableBytes(); // int originalBytes = stream.readableBytes();
GZIPInputStream gzReader = new GZIPInputStream( new InputStream() { GZIPInputStream gzReader = new GZIPInputStream( new InputStream()
{
@Override @Override
public int read() throws IOException public int read() throws IOException
{ {
if ( stream.readableBytes() <= 0 ) if( stream.readableBytes() <= 0 )
return -1; return -1;
return stream.readByte() & 0xff; return stream.readByte() & STREAM_MASK;
} }
} ); } );
ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() ); ByteBuf uncompressed = Unpooled.buffer( stream.readableBytes() );
byte[] tmp = new byte[1024]; byte[] tmp = new byte[TEMP_BUFFER_SIZE];
while (gzReader.available() != 0) while( gzReader.available() != 0 )
{ {
int bytes = gzReader.read( tmp ); int bytes = gzReader.read( tmp );
if ( bytes > 0 ) if( bytes > 0 )
uncompressed.writeBytes( tmp, 0, bytes ); uncompressed.writeBytes( tmp, 0, bytes );
} }
gzReader.close(); gzReader.close();
@@ -98,32 +107,58 @@ public class PacketMEInventoryUpdate extends AppEngPacket
// int uncompressedBytes = uncompressed.readableBytes(); // int uncompressedBytes = uncompressed.readableBytes();
// AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes ); // AELog.info( "Receiver: " + originalBytes + " -> " + uncompressedBytes );
while (uncompressed.readableBytes() > 0) while( uncompressed.readableBytes() > 0 )
this.list.add( AEItemStack.loadItemStackFromPacket( uncompressed ) ); this.list.add( AEItemStack.loadItemStackFromPacket( uncompressed ) );
this.empty = this.list.isEmpty(); this.empty = this.list.isEmpty();
} }
// api
public PacketMEInventoryUpdate() throws IOException
{
this( (byte) 0 );
}
// api
public PacketMEInventoryUpdate( byte ref ) throws IOException
{
this.ref = ref;
this.data = Unpooled.buffer( OPERATION_BYTE_LIMIT );
this.data.writeInt( this.getPacketID() );
this.data.writeByte( this.ref );
this.compressFrame = new GZIPOutputStream( new OutputStream()
{
@Override
public void write( int value ) throws IOException
{
PacketMEInventoryUpdate.this.data.writeByte( value );
}
} );
this.list = null;
}
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly( Side.CLIENT )
public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player) public void clientPacketData( INetworkInfo network, AppEngPacket packet, EntityPlayer player )
{ {
GuiScreen gs = Minecraft.getMinecraft().currentScreen; GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if ( gs instanceof GuiCraftConfirm ) if( gs instanceof GuiCraftConfirm )
((GuiCraftConfirm) gs).postUpdate( this.list, this.ref ); ( (GuiCraftConfirm) gs ).postUpdate( this.list, this.ref );
if ( gs instanceof GuiCraftingCPU ) if( gs instanceof GuiCraftingCPU )
((GuiCraftingCPU) gs).postUpdate( this.list, this.ref ); ( (GuiCraftingCPU) gs ).postUpdate( this.list, this.ref );
if ( gs instanceof GuiMEMonitorable ) if( gs instanceof GuiMEMonitorable )
((GuiMEMonitorable) gs).postUpdate( this.list ); ( (GuiMEMonitorable) gs ).postUpdate( this.list );
if ( gs instanceof GuiNetworkStatus )
((GuiNetworkStatus) gs).postUpdate( this.list );
if( gs instanceof GuiNetworkStatus )
( (GuiNetworkStatus) gs ).postUpdate( this.list );
} }
@Nullable
@Override @Override
public FMLProxyPacket getProxy() public FMLProxyPacket getProxy()
{ {
@@ -134,46 +169,24 @@ public class PacketMEInventoryUpdate extends AppEngPacket
this.configureWrite( this.data ); this.configureWrite( this.data );
return super.getProxy(); return super.getProxy();
} }
catch (IOException e) catch( IOException e )
{ {
AELog.error( e ); AELog.error( e );
} }
return null; return null;
} }
// api public void appendItem( IAEItemStack is ) throws IOException, BufferOverflowException
public PacketMEInventoryUpdate() throws IOException {
this( (byte) 0 );
}
// api
public PacketMEInventoryUpdate(byte ref) throws IOException {
this.data = Unpooled.buffer( 2048 );
this.data.writeInt( this.getPacketID() );
this.data.writeByte( this.ref = ref );
this.compressFrame = new GZIPOutputStream( new OutputStream() {
@Override
public void write(int value) throws IOException
{
PacketMEInventoryUpdate.this.data.writeByte( value );
}
} );
this.list = null;
}
public void appendItem(IAEItemStack is) throws IOException, BufferOverflowException
{ {
ByteBuf tmp = Unpooled.buffer( 2048 ); ByteBuf tmp = Unpooled.buffer( OPERATION_BYTE_LIMIT );
is.writeToPacket( tmp ); is.writeToPacket( tmp );
this.compressFrame.flush(); this.compressFrame.flush();
if ( this.writtenBytes + tmp.readableBytes() > 2 * 1024 * 1024 ) // 2mb! if( this.writtenBytes + tmp.readableBytes() > UNCOMPRESSED_PACKET_BYTE_LIMIT )
{
throw new BufferOverflowException(); throw new BufferOverflowException();
}
else else
{ {
this.writtenBytes += tmp.readableBytes(); this.writtenBytes += tmp.readableBytes();
@@ -191,5 +204,4 @@ public class PacketMEInventoryUpdate extends AppEngPacket
{ {
return this.empty; return this.empty;
} }
} }