mirror of
https://github.com/GTNewHorizons/CodeChickenCore.git
synced 2026-11-11 15:57:42 +00:00
Fixed packet compression and splitting on TCP connections.
This commit is contained in:
1 parent
7f42cd94e0
commit
4bd5aab59a
2 files changed
+41
-5
No files matched your search
@@ -3,18 +3,45 @@ package codechicken.lib.packet;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import codechicken.lib.asm.ObfMapping;
|
||||
|
||||
import net.minecraft.network.packet.NetHandler;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
|
||||
public class MetaPacket extends Packet
|
||||
/**
|
||||
* A class that facilitates sending of multiple packets one after the other.
|
||||
* It first sends an empty 250 packet and then it's payloads.
|
||||
*/
|
||||
public class MetaPacket extends Packet250CustomPayload
|
||||
{
|
||||
static
|
||||
{
|
||||
try
|
||||
{
|
||||
String fieldName = new ObfMapping("net/minecraft/network/packet/Packet", "packetClassToIdMap", "Ljava/util/Map;").toRuntime().s_name;
|
||||
Field field = Packet.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
Map<Class<? extends Packet>, Integer> packetClassToIdMap = (Map<Class<? extends Packet>, Integer>) field.get(null);
|
||||
packetClassToIdMap.put(MetaPacket.class, 250);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Packet> packets = new ArrayList<Packet>();
|
||||
|
||||
public MetaPacket(Packet... packets)
|
||||
{
|
||||
super("", null);
|
||||
|
||||
for(Packet p : packets)
|
||||
this.packets.add(p);
|
||||
}
|
||||
@@ -33,8 +60,14 @@ public class MetaPacket extends Packet
|
||||
@Override
|
||||
public void writePacketData(DataOutput dataout) throws IOException
|
||||
{
|
||||
//send a dummy 250
|
||||
super.writePacketData(dataout);
|
||||
|
||||
for(Packet p : packets)
|
||||
p.writePacketData(dataout);
|
||||
Packet.writePacket(p, dataout);
|
||||
|
||||
//adjust sent size for Packet.writePacket
|
||||
Packet.sentSize-=getPacketSize()-super.getPacketSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,7 +82,7 @@ public class MetaPacket extends Packet
|
||||
{
|
||||
int size = 0;
|
||||
for(Packet p : packets)
|
||||
size+=p.getPacketSize();
|
||||
size+=p.getPacketSize()+1;
|
||||
return size;
|
||||
}
|
||||
}
|
||||
@@ -501,15 +501,18 @@ public final class PacketCustom implements MCDataInput, MCDataOutput
|
||||
{
|
||||
deflater.setInput(data, 0, data.length);
|
||||
deflater.finish();
|
||||
byte[] cdata = new byte[data.length+4];
|
||||
int clen = deflater.deflate(cdata, 4, data.length);
|
||||
byte[] cbuf = new byte[data.length];
|
||||
int clen = deflater.deflate(cbuf, 0, data.length);
|
||||
if(clen == data.length || !deflater.finished())//not worth compressing, gets larger
|
||||
{
|
||||
type &= 0x7F;
|
||||
return data;
|
||||
}
|
||||
|
||||
byte[] cdata = new byte[clen+4];
|
||||
writeInt(cdata, 0, data.length);
|
||||
System.arraycopy(cbuf, 0, cdata, 4, clen);
|
||||
type|=0x80;
|
||||
return cdata;
|
||||
}
|
||||
catch(Exception e)
|
||||
|
||||
Reference in new issue
Block a user