Skip to content

Commit

Permalink
Merge elemlist with join and room packets
Browse files Browse the repository at this point in the history
Also fix some wonky reconnection logic.
  • Loading branch information
LBPHacker committed Nov 14, 2024
1 parent af0ffb2 commit 8996340
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 48 deletions.
83 changes: 48 additions & 35 deletions tptmp/client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ function client_i:read_xy_12_()
return bit.rshift(d24, 12), bit.band(d24, 0xFFF)
end

function client_i:read_elemlist_()
local length = self:read_24be_()
local cstr = self:read_str24_()
return {
length = length,
cstr = cstr,
}
end

function client_i:handle_disconnect_reason_2_()
local reason = self:read_str8_()
self.should_not_reconnect_func_()
Expand Down Expand Up @@ -170,11 +179,14 @@ function client_i:handle_room_16_()
local id = self:read_bytes_(1)
local nick = self:read_str8_()
self:add_member_(id, nick)
local member = self.id_to_member[id]
self:parse_elemlist_(member)
end
self:rehash_supported_elements_()
self:reformat_nicks_()
self:push_names("Joined ")
self.window_:set_subtitle("room", self.room_name_)
self.localcmd_:reconnect_commit({
self.should_reconnect_func_({
room = self.room_name_,
host = self.host_,
port = self.port_,
Expand All @@ -184,17 +196,32 @@ function client_i:handle_room_16_()
end

function client_i:user_sync_()
self:send_elemlist(util.element_identifiers())
self.profile_:user_sync()
end

function client_i:parse_elemlist_(member)
local elemlist = self:read_elemlist_()
local str, _, err = bz2.decompress(elemlist.cstr, elemlist.length)
local identifiers = {}
if str then
for name in str:gmatch("[^ ]+") do
identifiers[name] = true
end
else
self.log_event_func_(colours.commonstr.error .. "Failed to parse supported element list from " .. member.formatted_nick .. colours.commonstr.error .. ": " .. err)
end
member.identifiers = identifiers
end

function client_i:handle_join_17_()
local id = self:read_bytes_(1)
local nick = self:read_str8_()
self:add_member_(id, nick)
self:reformat_nicks_()
self.window_:backlog_push_join(self.id_to_member[id].formatted_nick)
local member = self.id_to_member[id]
self:parse_elemlist_(member)
self:rehash_supported_elements_()
self.window_:backlog_push_join(member.formatted_nick)
self:user_sync_()
end

Expand Down Expand Up @@ -232,23 +259,6 @@ function client_i:handle_server_22_()
self.window_:backlog_push_server(msg)
end

function client_i:handle_elemlist_23_()
local member = self:member_prefix_()
local length = self:read_24be_()
local cstr = self:read_str24_()
local str, _, err = bz2.decompress(cstr, length)
local identifiers = {}
if str then
for name in str:gmatch("[^ ]+") do
identifiers[name] = true
end
else
self.log_event_func_(colours.commonstr.error .. "Failed to parse supported element list from " .. member.formatted_nick .. colours.commonstr.error .. ": " .. err)
end
member.identifiers = identifiers
self:rehash_supported_elements_()
end

function client_i:rehash_supported_elements_()
local unsupported = {}
for key in pairs(self.identifiers_) do
Expand Down Expand Up @@ -808,7 +818,19 @@ function client_i:handshake_()
conn_status = self:read_bytes_(1)
end
if conn_status == 1 then
self.should_reconnect_func_()
do
local arr = {}
for name in pairs(util.element_identifiers()) do
table.insert(arr, name)
end
local str = table.concat(arr, " ")
local cstr = bz2.compress(str)
self:write_elemlist_({
length = #str,
cstr = cstr,
})
self:write_flush_()
end
self.registered_ = true
self.nick_ = self:read_str8_()
self:reformat_nicks_()
Expand Down Expand Up @@ -848,19 +870,6 @@ function client_i:send_say3rd(str)
self:write_flush_()
end

function client_i:send_elemlist(identifiers)
self:write_("\23")
local arr = {}
for name in pairs(identifiers) do
table.insert(arr, name)
end
local str = table.concat(arr, " ")
local cstr = bz2.compress(str)
self:write_24be_(#str)
self:write_str24_(cstr)
self:write_flush_()
end

function client_i:send_mousepos(px, py)
self:write_("\32")
self:write_xy_12_(px, py)
Expand Down Expand Up @@ -1432,6 +1441,11 @@ function client_i:write_xy_12_(x, y)
self:write_24be_(bit.bor(bit.lshift(x, 12), y))
end

function client_i:write_elemlist_(elemlist)
self:write_24be_(elemlist.length)
self:write_str24_(elemlist.cstr)
end

function client_i:nick()
return self.nick_
end
Expand Down Expand Up @@ -1506,7 +1520,6 @@ local function new(params)
status_ = "ready",
window_ = params.window,
profile_ = params.profile,
localcmd_ = params.localcmd,
initial_room_ = params.initial_room,
set_id_func_ = params.set_id_func,
get_id_func_ = params.get_id_func,
Expand Down
12 changes: 10 additions & 2 deletions tptmp/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ local function run()
return prof:should_ignore_mouse()
end,
})
local cmd = localcmd.new({
local cmd
cmd = localcmd.new({
window_status_func = get_window_status,
window_set_floating_func = set_floating,
client_func = function()
return cli and cli:registered() and cli
end,
cancel_reconnect_func = function(params)
if should_reconnect_at then
should_reconnect_at = nil
return true
end
end,
new_client_func = function(params)
should_reconnect_at = nil
params.window = win
Expand All @@ -180,8 +187,9 @@ local function run()
params.get_qa_func = get_qa
params.log_event_func = log_event
params.handle_error_func = handle_error
params.should_reconnect_func = function()
params.should_reconnect_func = function(reconnect_info)
should_reconnect = true
cmd:reconnect_commit(reconnect_info)
end
params.should_not_reconnect_func = function()
should_reconnect = false
Expand Down
4 changes: 3 additions & 1 deletion tptmp/client/localcmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ local cmdp = command_parser.new({
port = port and tonumber(port:gsub("[^0-9]", ""):sub(1, 5)) or config.default_port,
secure = secure,
initial_room = words[2],
localcmd = localcmd,
})
new_cli:nick_colour_seed(localcmd.nick_colour_seed_)
new_cli:fps_sync(localcmd.fps_sync_)
Expand All @@ -188,6 +187,8 @@ local cmdp = command_parser.new({
local cli = localcmd.client_func_()
if cli then
localcmd.kill_client_func_()
elseif localcmd.cancel_reconnect_func_() then
localcmd.window_:backlog_push_error("Reconnection attempt cancelled")
else
localcmd.window_:backlog_push_error("Not connected, cannot disconnect")
end
Expand Down Expand Up @@ -355,6 +356,7 @@ local function new(params)
window_status_func_ = params.window_status_func,
window_set_floating_func_ = params.window_set_floating_func,
client_func_ = params.client_func,
cancel_reconnect_func_ = params.cancel_reconnect_func,
new_client_func_ = params.new_client_func,
kill_client_func_ = params.kill_client_func,
nick_colour_seed_ = manager.get("nickColourSeed", "0"),
Expand Down
2 changes: 1 addition & 1 deletion tptmp/common/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return {
-- ***********************************************************************

-- * Protocol version, between 0 and 254. 255 is reserved for future use.
version = 33,
version = 34,

-- * Client-to-server message size limit, between 0 and 255, the latter
-- limit being imposted by the protocol.
Expand Down
30 changes: 22 additions & 8 deletions tptmp/server/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ function client_i:read_nullstr_(max)
return table.concat(collect)
end

function client_i:read_elemlist_()
local length = self:read_(3)
local cstr = self:read_str24_()
return {
length = length,
cstr = cstr,
}
end

function client_i:send_handshake_failure_(message)
self:write_("\0")
self:write_nullstr_(message)
Expand Down Expand Up @@ -126,6 +135,7 @@ function client_i:send_room(id, name, items)
for i = 1, #items do
self:write_bytes_(items[i].id)
self:write_str8_(items[i].nick)
self:write_elemlist_(items[i].elemlist)
end
self:write_flush_()
end
Expand All @@ -134,10 +144,11 @@ function client_i:send_room_chunk(chunk)
self:write_flush_(chunk)
end

function client_i:send_join(id, nick)
function client_i:send_join(id, nick, elemlist)
self:write_("\17")
self:write_bytes_(id)
self:write_str8_(nick)
self:write_elemlist_(elemlist)
self:write_flush_()
end

Expand Down Expand Up @@ -269,13 +280,6 @@ local function header_24be(d24)
return string.char(hi, mi, lo)
end

function client_i:handle_elemlist_23_()
local length = self:read_(3)
local cstr = self:read_str24_()
self.room_:broadcast(self, "\23" .. self.room_id_str_ .. length .. header_24be(#cstr))
self.room_:broadcast(self, cstr)
end

local sync_30_size = 3
do
local location_size = 3
Expand Down Expand Up @@ -512,6 +516,7 @@ function client_i:handshake_()
self.inick_ = self.nick_:lower()
self.server_:register_client(self)
self:send_handshake_success_()
self.elemlist_ = self:read_elemlist_()
self.handshake_done_ = true
util.cqueues_wrap(cqueues.running(), function()
self:ping_()
Expand Down Expand Up @@ -852,6 +857,11 @@ function client_i:write_24be_(d24)
self:write_bytes_(hi, mi, lo)
end

function client_i:write_elemlist_(elemlist)
self:write_(elemlist.length)
self:write_str24_(elemlist.cstr)
end

function client_i:start()
assert(self.status_ == "ready", "not ready")
self.status_ = "running"
Expand All @@ -878,6 +888,10 @@ function client_i:nick()
return self.nick_
end

function client_i:elemlist()
return self.elemlist_
end

function client_i:register_time()
return self.register_time_
end
Expand Down
3 changes: 2 additions & 1 deletion tptmp/server/room.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ function room_i:join(client)
table.insert(others, {
id = other_id,
nick = other_client:nick(),
elemlist = other_client:elemlist(),
})
end
end
client:send_room(id, self.name_, others)
for other_client in self:clients() do
if other_client ~= client then
other_client:send_join(id, client:nick())
other_client:send_join(id, client:nick(), client:elemlist())
end
end
self:cleanup_dead_ids_()
Expand Down

0 comments on commit 8996340

Please sign in to comment.