1
0

SSCSM: Compress long messages

This commit is contained in:
luk3yx 2023-01-18 12:12:50 +13:00
parent 23844d8836
commit 22443daf2c
2 changed files with 23 additions and 5 deletions

View File

@ -307,17 +307,23 @@ core.register_on_receiving_chat_message(function(message)
if not callbacks then return true end if not callbacks then return true end
-- Handle split messages -- Handle split messages
local prefix = msg:sub(1, 1) local prefix = msg:byte(1)
if prefix == '\001' then if prefix == 1 then
msg = load_split_message(chan, msg) msg = load_split_message(chan, msg)
if not msg then if not msg then
return true return true
end end
prefix = msg:sub(1, 1) prefix = msg:byte(1)
end
-- Decompress messages
if prefix == 3 then
msg = minetest.decompress(minetest.decode_base64(msg:sub(2)))
prefix = msg:byte(1)
end end
-- Load the message -- Load the message
if prefix == '\002' then if prefix == 2 then
msg = msg:sub(2) msg = msg:sub(2)
else else
msg = core.parse_json(msg) msg = core.parse_json(msg)

View File

@ -20,6 +20,7 @@
-- --
sscsm = {minify=true} sscsm = {minify=true}
local format = string.format
local modpath = core.get_builtin_path() .. "game" .. DIR_DELIM .. local modpath = core.get_builtin_path() .. "game" .. DIR_DELIM ..
"sscsm" .. DIR_DELIM "sscsm" .. DIR_DELIM
@ -218,8 +219,19 @@ function sscsm.com_send(pname, channel, msg)
msg = assert(core.write_json(msg)) msg = assert(core.write_json(msg))
end end
-- Compress long messages
if #msg > 4096 then
-- Chat messages can't contain binary data so base64 is used
local compressed_msg = minetest.encode_base64(minetest.compress(msg))
-- Only use the compressed message if it's shorter
if #msg > #compressed_msg + 1 then
msg = "\003" .. compressed_msg
end
end
-- Short messages can be sent all at once -- Short messages can be sent all at once
local prefix = "\001SSCSM_COM\001" .. channel .. "\001" local prefix = format("\001SSCSM_COM\001%s\001", channel)
if #msg < 65300 then if #msg < 65300 then
core.chat_send_player(pname, prefix .. msg) core.chat_send_player(pname, prefix .. msg)
return return