From 6b551ed4a5a9e961a5bebf4ba391aa6333d56e21 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Mon, 11 Dec 2023 18:05:33 -0500 Subject: [PATCH] Update new players with trivial name table Refactored flushing Increased update transfer size limit Decreased flush delay to 1 tick --- lua/wire/server/wirelib.lua | 4 +- lua/wire/sh_wirenet.lua | 90 ++++++++++++++++++++++--------------- lua/wire/wireshared.lua | 12 +++-- 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/lua/wire/server/wirelib.lua b/lua/wire/server/wirelib.lua index ab5e2e0f73..baceae055a 100644 --- a/lua/wire/server/wirelib.lua +++ b/lua/wire/server/wirelib.lua @@ -1426,8 +1426,8 @@ function WireLib.Notify(ply, msg, severity, color) net.WriteUInt(severity, 4) net.WriteBool(color ~= nil) if color ~= nil then net.WriteColor(color, false) end - local data = util.Compress(msg) - local datal = math.min(#data, 2048) + local data = util.Compress(string.sub(msg, 1, 2048)) + local datal = #data net.WriteUInt(datal, 11) net.WriteData(data, datal) net.Send(ply) diff --git a/lua/wire/sh_wirenet.lua b/lua/wire/sh_wirenet.lua index 010567ce57..ff44f82838 100644 --- a/lua/wire/sh_wirenet.lua +++ b/lua/wire/sh_wirenet.lua @@ -27,44 +27,55 @@ net.Receive("wirelib_net_message", msg_handler) if SERVER then util.AddNetworkString("wirelib_net_message") + local MAXLEN = 4096 + + local handler_names = {} local handler_queue = {} local queue_handler_firstidx, queue_handler_lastidx - - local function queue_handler_flush(ply) - local restart, lastidx = false, queue_handler_lastidx - if queue_handler_firstidx then - net.Start("wirelib_net_message") - - local data, len = "", 0 - for i, v in ipairs(handler_queue) do - len = len + #v + 1 - data = data .. v .. "\0" - if len > 2048 then - lastidx = i - restart = true - break + local function queue_handler_flush(ply, tbl, first, last) + local restart = false + tbl = tbl or handler_queue + first = first or queue_handler_firstidx + last = last or queue_handler_lastidx + + if first then + local name = "wirelib_net_flush" .. (ply and ply:UserID() or "") + + local function flush() + net.Start("wirelib_net_message") + local data, len, j = "", 0, last + for i, v in ipairs(tbl) do + len = len + #v + 1 + data = data .. v .. "\0" + if len > MAXLEN then + j = i + restart = true + break + end end - end - - net.WriteUInt(0, SIZE) - net.WriteUInt(queue_handler_firstidx, SIZE) - net.WriteUInt(lastidx, SIZE) - data = util.Compress(data) - local datalen = #data - net.WriteUInt(datalen, 16) - net.WriteData(data, datalen) + net.WriteUInt(0, SIZE) + net.WriteUInt(first, SIZE) + net.WriteUInt(j, SIZE) + + data = util.Compress(data) + local datalen = #data + net.WriteUInt(datalen, 16) + net.WriteData(data, datalen) + if ply then net.Send(ply) else net.Broadcast() end + + if restart and first < last then + first = j + 1 + timer.Create(name, 0, 2, flush) + return true + else + timer.Remove(name) + end + end - if ply then net.Send(ply) else net.Broadcast() end - end + flush() - if restart and queue_handler_firstidx < queue_handler_lastidx then - queue_handler_firstidx = lastidx + 1 - timer.Create("wirelib_net_flush", 0, 2, queue_handler_flush) - else - handler_queue = {} - queue_handler_firstidx, queue_handler_lastidx = nil, nil - timer.Remove("wirelib_net_flush") + if tbl == handler_queue then queue_handler_firstidx, queue_handler_lastidx, handler_queue = nil, nil, {} end end end local function queue_handler_update(idx, name) @@ -78,14 +89,14 @@ if SERVER then queue_handler_firstidx, queue_handler_lastidx = idx, idx handler_queue[1] = name - timer.Adjust("wirelib_net_flush", 1, 2) + timer.Adjust("wirelib_net_flush", 0, 2) end else queue_handler_firstidx, queue_handler_lastidx = idx, idx handler_queue[1] = name - timer.Create("wirelib_net_flush", 1, 2, queue_handler_flush) + queue_handler_flush() end end @@ -93,6 +104,7 @@ if SERVER then error("WireLib.Net trying to call unimplemented handler on server") end + ---@param name string update_handlers = function(name, callback) local handler_idx = registered_handlers[name] if not handler_idx then @@ -110,6 +122,7 @@ if SERVER then else registered_handlers[handler_idx] = callback end + handler_names[handler_idx] = name end registered_handlers[0] = function(_, ply) error(string.format("WireLib.Net received invalid message from %s (Player %d, Entity %d)", ply:SteamID(), ply:UserID(), ply:EntIndex())) @@ -139,6 +152,11 @@ if SERVER then update_handlers(name:lower(), callback) end + gameevent.Listen("player_activate") + + hook.Add("player_activate", "wirenet_ff_player", function(d) + queue_handler_flush(Player(d.userid), handler_names, 1, #handler_names) + end) else update_handlers = function(name, callback) local handler_idx = registered_handlers[name] @@ -189,6 +207,4 @@ function Net.Receive(name, callback) update_handlers(name:lower(), callback) end -function Net.Receivers() - return registered_handlers -end \ No newline at end of file +Net.Receivers = registered_handlers \ No newline at end of file diff --git a/lua/wire/wireshared.lua b/lua/wire/wireshared.lua index 57e7acc83f..3d46042738 100644 --- a/lua/wire/wireshared.lua +++ b/lua/wire/wireshared.lua @@ -1204,10 +1204,8 @@ end) -- Notify -- ----@enum WireLib.NotifySeverity -WireLib.NotifySeverity = { - None = 0, - Info = 1, - Warning = 2, - Error = 3 -} \ No newline at end of file +---@alias WireLib.NotifySeverity +---| 0 # None +---| 1 # Info +---| 2 # Warning +---| 3 # Error \ No newline at end of file