Implement compression
This commit is contained in:
parent
fb98dd4864
commit
be44916461
@ -258,27 +258,26 @@ 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)
|
||||||
length = byte_size(data) |> Amethyst.Minecraft.Write.varint()
|
container = if Map.get(state, :compression) == nil do
|
||||||
to_write = case Map.get(state, :compression_threshold) do
|
# Packet ID is included in data
|
||||||
nil ->
|
Write.varint(byte_size(data)) <> data
|
||||||
# No compression
|
else
|
||||||
length <> data
|
threshold = Map.get(state, :compression, 0)
|
||||||
threshold when byte_size(length <> data) < threshold ->
|
data_length = byte_size(data)
|
||||||
# Compression unnecessary
|
if data_length >= threshold do
|
||||||
<<0>> <> length <> data
|
compressed = Write.varint(data_length) <> :zlib.zip(data)
|
||||||
threshold when byte_size(length <> data) >= threshold ->
|
Write.varint(byte_size(compressed)) <> compressed
|
||||||
# Compression
|
else
|
||||||
cdata = :zlib.compress(data)
|
compressed = Write.varint(0) <> data
|
||||||
outer_length = byte_size(length <> cdata)
|
Write.varint(byte_size(compressed)) <> compressed
|
||||||
Write.varint(outer_length) <> length <> cdata
|
end
|
||||||
|
end
|
||||||
|
encrypted = if Map.get(state, :encryption_state) == nil do
|
||||||
|
container
|
||||||
|
else
|
||||||
|
Map.get(state, :encryption_state) |> :crypto.crypto_update(container)
|
||||||
end
|
end
|
||||||
case Map.get(state, :encryption_state) do
|
|
||||||
nil ->
|
|
||||||
:gen_tcp.send(socket, to_write)
|
|
||||||
estate ->
|
|
||||||
encrypted = :crypto.crypto_update(estate, to_write)
|
|
||||||
:gen_tcp.send(socket, encrypted)
|
:gen_tcp.send(socket, encrypted)
|
||||||
end
|
|
||||||
rescue
|
rescue
|
||||||
e ->
|
e ->
|
||||||
Logger.error("Error sending packet #{inspect(packet)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
|
Logger.error("Error sending packet #{inspect(packet)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
|
||||||
|
@ -60,25 +60,20 @@ defmodule Amethyst.ConnectionReceiver do
|
|||||||
estate = if estate == nil do
|
estate = if estate == nil do
|
||||||
# Ask the handler if we have encryption now
|
# Ask the handler if we have encryption now
|
||||||
send(sender, {:get_encryption, self()})
|
send(sender, {:get_encryption, self()})
|
||||||
Logger.debug("Asking for news on encryption...")
|
|
||||||
receive do
|
receive do
|
||||||
nil -> nil
|
nil -> nil
|
||||||
some ->
|
some -> some
|
||||||
Logger.debug("Enabled decryption")
|
|
||||||
some
|
|
||||||
end
|
end
|
||||||
else nil end
|
else estate end
|
||||||
|
|
||||||
cstate = if cstate == nil do
|
cstate = if cstate == nil do
|
||||||
# Ask the handler if we have encryption now
|
# Ask the handler if we have encryption now
|
||||||
send(sender, {:get_compression, self()})
|
send(sender, {:get_compression, self()})
|
||||||
Logger.debug("Asking for news on compression...")
|
|
||||||
receive do
|
receive do
|
||||||
nil -> nil
|
nil -> nil
|
||||||
some ->
|
some -> some
|
||||||
Logger.debug("Enabled decompression")
|
|
||||||
some
|
|
||||||
end
|
end
|
||||||
else nil end
|
else cstate end
|
||||||
|
|
||||||
receive(socket, sender, estate, cstate)
|
receive(socket, sender, estate, cstate)
|
||||||
end
|
end
|
||||||
@ -109,7 +104,7 @@ defmodule Amethyst.ConnectionReceiver do
|
|||||||
rest
|
rest
|
||||||
else
|
else
|
||||||
# Compressed data
|
# Compressed data
|
||||||
rest |> :zlib.uncompress()
|
rest |> :zlib.unzip()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user