Actually implement compression
All checks were successful
Build & Test / nix-build (push) Successful in 1m29s
Build & Test / nix-build (pull_request) Successful in 1m24s

This commit is contained in:
Kodi Craft 2024-10-06 15:49:55 +02:00
parent be44916461
commit 8b09c78f02
Signed by: kodi
GPG Key ID: 69D9EED60B242822
3 changed files with 7 additions and 5 deletions

View File

@ -66,7 +66,7 @@ defmodule Amethyst.ConnectionHandler do
loop(socket, connstate, version, state) loop(socket, connstate, version, state)
{:set_compression, threshold} -> {:set_compression, threshold} ->
Logger.debug("Enabling comrpession with threshold #{threshold}") Logger.debug("Enabling comrpession with threshold #{threshold}")
state = Map.put(state, :compression_threshold, threshold) state = Map.put(state, :compression, threshold)
loop(socket, connstate, version, state) loop(socket, connstate, version, state)
{:set_position, position} -> {:set_position, position} ->
prev_position = Map.get(state, :position) prev_position = Map.get(state, :position)
@ -118,7 +118,7 @@ defmodule Amethyst.ConnectionHandler do
send(from, Map.get(state, :decryption_state)) send(from, Map.get(state, :decryption_state))
loop(socket, connstate, version, state) loop(socket, connstate, version, state)
{:get_compression, from} -> {:get_compression, from} ->
send(from, Map.get(state, :compression_threshold)) send(from, Map.get(state, :compression))
loop(socket, connstate, version, state) loop(socket, connstate, version, state)
{:packet, id, data} -> {:packet, id, data} ->
state = handle_packet(id, data, connstate, version, state) state = handle_packet(id, data, connstate, version, state)
@ -258,6 +258,7 @@ defmodule Amethyst.ConnectionHandler do
defp send_packet(socket, connstate, packet, version, state) do defp send_packet(socket, connstate, packet, version, state) do
try do try do
data = connstate.serialize(packet, version) data = connstate.serialize(packet, version)
Logger.debug("#{inspect(Map.get(state, :compression))}")
container = if Map.get(state, :compression) == nil do container = if Map.get(state, :compression) == nil do
# Packet ID is included in data # Packet ID is included in data
Write.varint(byte_size(data)) <> data Write.varint(byte_size(data)) <> data
@ -265,13 +266,14 @@ defmodule Amethyst.ConnectionHandler do
threshold = Map.get(state, :compression, 0) threshold = Map.get(state, :compression, 0)
data_length = byte_size(data) data_length = byte_size(data)
if data_length >= threshold do if data_length >= threshold do
compressed = Write.varint(data_length) <> :zlib.zip(data) compressed = Write.varint(data_length) <> :zlib.compress(data)
Write.varint(byte_size(compressed)) <> compressed Write.varint(byte_size(compressed)) <> compressed
else else
compressed = Write.varint(0) <> data compressed = Write.varint(0) <> data
Write.varint(byte_size(compressed)) <> compressed Write.varint(byte_size(compressed)) <> compressed
end end
end end
Logger.debug(inspect(container))
encrypted = if Map.get(state, :encryption_state) == nil do encrypted = if Map.get(state, :encryption_state) == nil do
container container
else else

View File

@ -104,7 +104,7 @@ defmodule Amethyst.ConnectionReceiver do
rest rest
else else
# Compressed data # Compressed data
rest |> :zlib.unzip() rest |> :zlib.uncompress()
end end
end end

View File

@ -4,5 +4,5 @@ config :amethyst,
port: 25599, # Bogus port for testing, avoids unexpected conflicts port: 25599, # Bogus port for testing, avoids unexpected conflicts
encryption: true, # Whether or not to request encryption from clients. encryption: true, # Whether or not to request encryption from clients.
auth: false, # Whether or not users should be authenticated with Mojang. auth: false, # Whether or not users should be authenticated with Mojang.
compression: nil, # Packets larger than this amount are sent compressed. Set to nil to disable compression. compression: 256, # Packets larger than this amount are sent compressed. Set to nil to disable compression.
default_game: Example.Game # Which game new players should be sent to default_game: Example.Game # Which game new players should be sent to