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,10 +51,11 @@ 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
{ {
@@ -105,16 +107,17 @@ public class AppEngPacketHandlerBase
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 )
{ {
@@ -123,18 +126,13 @@ public class AppEngPacketHandlerBase
{ {
} }
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
{
return (AppEngPacket) this.con.newInstance( in );
}
public static PacketTypes getPacket( int id ) public static PacketTypes getPacket( int id )
{ {
return ( values() )[id]; return ( values() )[id];
@@ -142,9 +140,12 @@ public class AppEngPacketHandlerBase
public static PacketTypes getID( Class<? extends AppEngPacket> c ) public static PacketTypes getID( Class<? extends AppEngPacket> c )
{ {
return AppEngPacketHandlerBase.REVERSE_LOOKUP.get( c ); return REVERSE_LOOKUP.get( c );
} }
public AppEngPacket parsePacket( ByteBuf in ) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
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,21 +82,20 @@ 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 );
@@ -104,6 +113,32 @@ public class PacketMEInventoryUpdate extends AppEngPacket
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 )
@@ -121,9 +156,9 @@ public class PacketMEInventoryUpdate extends AppEngPacket
if( gs instanceof GuiNetworkStatus ) if( gs instanceof GuiNetworkStatus )
( (GuiNetworkStatus) gs ).postUpdate( this.list ); ( (GuiNetworkStatus) gs ).postUpdate( this.list );
} }
@Nullable
@Override @Override
public FMLProxyPacket getProxy() public FMLProxyPacket getProxy()
{ {
@@ -138,42 +173,20 @@ public class PacketMEInventoryUpdate extends AppEngPacket
{ {
AELog.error( e ); AELog.error( e );
} }
return null; return null;
} }
// api
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 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;
} }
} }