Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove compression from terminal/monitor packets #1569

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
package dan200.computercraft.shared.computer.terminal;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
* A snapshot of a terminal's state.
Expand All @@ -31,20 +23,10 @@ public class TerminalState {
public final int width;
public final int height;

private final boolean compress;

@Nullable
private final ByteBuf buffer;

private @Nullable ByteBuf compressed;

public TerminalState(@Nullable NetworkedTerminal terminal) {
this(terminal, true);
}

public TerminalState(@Nullable NetworkedTerminal terminal, boolean compress) {
this.compress = compress;

if (terminal == null) {
colour = false;
width = height = 0;
Expand All @@ -61,14 +43,13 @@ public TerminalState(@Nullable NetworkedTerminal terminal, boolean compress) {

public TerminalState(FriendlyByteBuf buf) {
colour = buf.readBoolean();
compress = buf.readBoolean();

if (buf.readBoolean()) {
width = buf.readVarInt();
height = buf.readVarInt();

var length = buf.readVarInt();
buffer = readCompressed(buf, length, compress);
buffer = buf.readBytes(length);
} else {
width = height = 0;
buffer = null;
Expand All @@ -77,16 +58,13 @@ public TerminalState(FriendlyByteBuf buf) {

public void write(FriendlyByteBuf buf) {
buf.writeBoolean(colour);
buf.writeBoolean(compress);

buf.writeBoolean(buffer != null);
if (buffer != null) {
buf.writeVarInt(width);
buf.writeVarInt(height);

var sendBuffer = getCompressed();
buf.writeVarInt(sendBuffer.readableBytes());
buf.writeBytes(sendBuffer, sendBuffer.readerIndex(), sendBuffer.readableBytes());
buf.writeVarInt(buffer.readableBytes());
buf.writeBytes(buffer, buffer.readerIndex(), buffer.readableBytes());
}
}

Expand All @@ -110,40 +88,4 @@ public NetworkedTerminal create() {
terminal.read(new FriendlyByteBuf(buffer));
return terminal;
}

private ByteBuf getCompressed() {
if (buffer == null) throw new NullPointerException("buffer");
if (!compress) return buffer;
if (compressed != null) return compressed;

var compressed = Unpooled.buffer();
try (OutputStream stream = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
stream.write(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
} catch (IOException e) {
throw new UncheckedIOException(e);
}

return this.compressed = compressed;
}

private static ByteBuf readCompressed(ByteBuf buf, int length, boolean compress) {
if (compress) {
var buffer = Unpooled.buffer();
try (InputStream stream = new GZIPInputStream(new ByteBufInputStream(buf, length))) {
var swap = new byte[8192];
while (true) {
var bytes = stream.read(swap);
if (bytes == -1) break;
buffer.writeBytes(swap, 0, bytes);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return buffer;
} else {
var buffer = Unpooled.buffer(length);
buf.readBytes(buffer, length);
return buffer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,11 @@
*/
public class TerminalStateTest {
@RepeatedTest(5)
public void testCompressed() {
public void testRoundTrip() {
var terminal = randomTerminal();

var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
new TerminalState(terminal, true).write(buffer);

checkEqual(terminal, read(buffer));
assertEquals(0, buffer.readableBytes());
}

@RepeatedTest(5)
public void testUncompressed() {
var terminal = randomTerminal();

var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
new TerminalState(terminal, false).write(buffer);
new TerminalState(terminal).write(buffer);

checkEqual(terminal, read(buffer));
assertEquals(0, buffer.readableBytes());
Expand Down
Loading