Implement compression #3
@ -66,7 +66,7 @@ defmodule Amethyst.ConnectionHandler do
|
||||
loop(socket, connstate, version, state)
|
||||
{:set_compression, 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)
|
||||
{:set_position, position} ->
|
||||
prev_position = Map.get(state, :position)
|
||||
@ -118,7 +118,7 @@ defmodule Amethyst.ConnectionHandler do
|
||||
send(from, Map.get(state, :decryption_state))
|
||||
loop(socket, connstate, version, state)
|
||||
{:get_compression, from} ->
|
||||
send(from, Map.get(state, :compression_threshold))
|
||||
send(from, Map.get(state, :compression))
|
||||
loop(socket, connstate, version, state)
|
||||
{:packet, id, data} ->
|
||||
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
|
||||
try do
|
||||
data = connstate.serialize(packet, version)
|
||||
Logger.debug("#{inspect(Map.get(state, :compression))}")
|
||||
container = if Map.get(state, :compression) == nil do
|
||||
# Packet ID is included in data
|
||||
Write.varint(byte_size(data)) <> data
|
||||
@ -265,13 +266,14 @@ defmodule Amethyst.ConnectionHandler do
|
||||
threshold = Map.get(state, :compression, 0)
|
||||
data_length = byte_size(data)
|
||||
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
|
||||
else
|
||||
compressed = Write.varint(0) <> data
|
||||
Write.varint(byte_size(compressed)) <> compressed
|
||||
end
|
||||
end
|
||||
Logger.debug(inspect(container))
|
||||
encrypted = if Map.get(state, :encryption_state) == nil do
|
||||
container
|
||||
else
|
||||
|
@ -104,7 +104,7 @@ defmodule Amethyst.ConnectionReceiver do
|
||||
rest
|
||||
else
|
||||
# Compressed data
|
||||
rest |> :zlib.unzip()
|
||||
rest |> :zlib.uncompress()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -4,5 +4,5 @@ config :amethyst,
|
||||
port: 25599, # Bogus port for testing, avoids unexpected conflicts
|
||||
encryption: true, # Whether or not to request encryption from clients.
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user