Skip to content

Commit 07d3f0b

Browse files
committed
properly compress sent packets fixes #260
1 parent 7b888d1 commit 07d3f0b

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

src/main/java/systems/kinau/fishingbot/network/protocol/NetworkHandler.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.security.PublicKey;
2626
import java.util.zip.DataFormatException;
27+
import java.util.zip.Deflater;
2728
import java.util.zip.Inflater;
2829

2930
@Getter
@@ -111,27 +112,15 @@ public void sendPacket(Packet packet) {
111112
}
112113

113114
if (getThreshold() >= 0) {
114-
// Send packet (with 0 threshold, no compression)
115-
ByteArrayDataOutput send1 = ByteStreams.newDataOutput();
116-
Packet.writeVarInt(0, send1);
117-
send1.write(buf.toByteArray());
118-
ByteArrayDataOutput send2 = ByteStreams.newDataOutput();
119-
Packet.writeVarInt(send1.toByteArray().length, send2);
120-
send2.write(send1.toByteArray());
121115
try {
122-
out.write(send2.toByteArray());
123-
out.flush();
116+
sendCompressed(buf);
124117
} catch (IOException e) {
125118
FishingBot.getLog().severe("Error while trying to send: " + packet.getClass().getSimpleName());
119+
e.printStackTrace();
126120
}
127121
} else {
128-
// Send packet (without threshold)
129-
ByteArrayDataOutput send = ByteStreams.newDataOutput();
130-
Packet.writeVarInt(buf.toByteArray().length, send);
131-
send.write(buf.toByteArray());
132122
try {
133-
out.write(send.toByteArray());
134-
out.flush();
123+
sendUncompressed(buf);
135124
} catch (IOException e) {
136125
FishingBot.getLog().severe("Error while trying to send: " + packet.getClass().getSimpleName());
137126
e.printStackTrace();
@@ -141,6 +130,58 @@ public void sendPacket(Packet packet) {
141130
FishingBot.getLog().info("[" + getState().name().toUpperCase() + "] C >>> |S|: " + packet.getClass().getSimpleName());
142131
}
143132

133+
private void sendCompressed(ByteArrayDataOutput buf) throws IOException {
134+
int length = buf.toByteArray().length;
135+
136+
if (length < getThreshold()) {
137+
ByteArrayDataOutput compressedBuf = ByteStreams.newDataOutput();
138+
Packet.writeVarInt(0, compressedBuf);
139+
compressedBuf.write(buf.toByteArray());
140+
141+
ByteArrayDataOutput send = prependLength(compressedBuf);
142+
143+
out.write(send.toByteArray());
144+
} else {
145+
ByteArrayDataOutput compressedBuf = compress(buf, length);
146+
147+
ByteArrayDataOutput send = prependLength(compressedBuf);
148+
149+
out.write(send.toByteArray());
150+
}
151+
152+
out.flush();
153+
}
154+
155+
private ByteArrayDataOutput compress(ByteArrayDataOutput buf, int length) throws IOException {
156+
ByteArrayDataOutput compressedBuf = ByteStreams.newDataOutput();
157+
Packet.writeVarInt(length, compressedBuf);
158+
Deflater deflater = new Deflater();
159+
deflater.setInput(buf.toByteArray());
160+
deflater.finish();
161+
162+
byte[] encodeBuf = new byte[8192];
163+
while (!deflater.finished()) {
164+
int len = deflater.deflate(encodeBuf);
165+
compressedBuf.write(encodeBuf, 0, len);
166+
}
167+
168+
deflater.reset();
169+
return compressedBuf;
170+
}
171+
172+
private void sendUncompressed(ByteArrayDataOutput buf) throws IOException {
173+
ByteArrayDataOutput send = prependLength(buf);
174+
out.write(send.toByteArray());
175+
out.flush();
176+
}
177+
178+
private ByteArrayDataOutput prependLength(ByteArrayDataOutput buf) {
179+
ByteArrayDataOutput lengthPrepended = ByteStreams.newDataOutput();
180+
Packet.writeVarInt(buf.toByteArray().length, lengthPrepended);
181+
lengthPrepended.write(buf.toByteArray());
182+
return lengthPrepended;
183+
}
184+
144185
public void readData() throws IOException {
145186
if (getThreshold() >= 0) {
146187
int plen1 = Packet.readVarInt(in);

0 commit comments

Comments
 (0)