From c255e537d8207fa1f3678e713830b281b7b6b7a9 Mon Sep 17 00:00:00 2001 From: bjcscat Date: Tue, 9 Jan 2024 19:51:26 -0600 Subject: [PATCH] Add netstream implementation for digiscreen --- .../gmod_wire_digitalscreen/cl_init.lua | 28 ++++++------ lua/entities/gmod_wire_digitalscreen/init.lua | 44 ++----------------- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/lua/entities/gmod_wire_digitalscreen/cl_init.lua b/lua/entities/gmod_wire_digitalscreen/cl_init.lua index 2295fdf215..7e7736eb10 100644 --- a/lua/entities/gmod_wire_digitalscreen/cl_init.lua +++ b/lua/entities/gmod_wire_digitalscreen/cl_init.lua @@ -21,15 +21,15 @@ function ENT:Initialize() self.RefreshRows[i] = i-1 end - //0..786431 - RGB data + -- 0..786431 - RGB data - //1048569 - Color mode (0: RGBXXX; 1: R G B) - //1048570 - Clear row - //1048571 - Clear column - //1048572 - Screen Height - //1048573 - Screen Width - //1048574 - Hardware Clear Screen - //1048575 - CLK + -- 1048569 - Color mode (0: RGBXXX; 1: R G B) + -- 1048570 - Clear row + -- 1048571 - Clear column + -- 1048572 - Screen Height + -- 1048573 - Screen Width + -- 1048574 - Hardware Clear Screen + -- 1048575 - CLK self.GPU = WireGPU(self) @@ -54,19 +54,21 @@ local function stringToNumber(index, str, bytes) end local pixelbits = {3, 1, 3, 4, 1} + net.Receive("wire_digitalscreen", function() local ent = Entity(net.ReadUInt(16)) if IsValid(ent) and ent.Memory1 and ent.Memory2 then local pixelbit = pixelbits[net.ReadUInt(5)] - local len = net.ReadUInt(32) - local datastr = util.Decompress(net.ReadData(len)) - if #datastr>0 then - ent:AddBuffer(datastr,pixelbit) - end + net.ReadStream(nil, function (datastr) + if #datastr>0 then + ent:AddBuffer(datastr,pixelbit) + end + end) end end) + function ENT:AddBuffer(datastr,pixelbit) self.buffer[#self.buffer+1] = {datastr=datastr,readIndex=1,pixelbit=pixelbit} end diff --git a/lua/entities/gmod_wire_digitalscreen/init.lua b/lua/entities/gmod_wire_digitalscreen/init.lua index 3e6ebe5716..169a84fe9f 100644 --- a/lua/entities/gmod_wire_digitalscreen/init.lua +++ b/lua/entities/gmod_wire_digitalscreen/init.lua @@ -96,44 +96,10 @@ end ---------------------------------------------------- -- Processing limiters and global bandwidth limiters local maxProcessingTime = engine.TickInterval() * 0.9 -local defaultMaxBandwidth = 10000 -- 10k per screen max limit - is arbitrary. needs to be smaller than the global limit. -local defaultMaxGlobalBandwidth = 20000 -- 20k is a good global limit in my testing. higher than that seems to cause issues -local maxBandwidth = defaultMaxBandwidth -local globalBandwidthLookup = {} -local function calcGlobalBW() - maxBandwidth = defaultMaxGlobalBandwidth - local n = 0 - - -- count the number of digi screens currently sending data - for digi in pairs(globalBandwidthLookup) do - if not IsValid(digi) then globalBandwidthLookup[digi] = nil end -- this most likely won't trigger due to OnRemove, but just in case - n = n + 1 - end - - -- player count also seems to affect lag somewhat - -- it seems logical that this would have something to do with the upload bandwidth of the server - -- but that seems unlikely since testing shows that the amount of data sent isn't very high - -- it's more likely that the net message library just isn't very efficient - -- the numbers here are picked somewhat arbitrarily, with a bit of guessing. - -- change in the future if necessary. - n = n + math.max(0,player.GetCount()-2) / 4 - - -- during testing, lag seems to increase somewhat faster as more net messages are sent at the same time - -- so we double this value to compensate - n = n * 2 - - maxBandwidth = math.max(100,math.Round(math.min(defaultMaxBandwidth,maxBandwidth / n),2)) -end -local function addGlobalBW(e) - globalBandwidthLookup[e] = true - calcGlobalBW() -end -local function removeGlobalBW(e) globalBandwidthLookup[e] = nil end ----------------------------------------------------- +local maxBandwidth = 10000 -- 10k per screen max limit - is arbitrary. needs to be smaller than the global limit. function ENT:OnRemove() BaseClass.OnRemove(self) - removeGlobalBW(self) end local function buildData(datastr, memory, pixelbit, range, bytesRemaining, sTime) @@ -168,7 +134,6 @@ util.AddNetworkString("wire_digitalscreen") local pixelbits = {3, 1, 3, 4, 1} --The compressed pixel formats are in bytes function ENT:FlushCache(ply) if not next(self.ChangedCellRanges) then - removeGlobalBW(self) return end @@ -206,16 +171,13 @@ function ENT:FlushCache(ply) end numberToString(datastr,0,3) - datastr = util.Compress(table.concat(datastr)) - local len = #datastr net.Start("wire_digitalscreen") net.WriteUInt(self:EntIndex(),16) net.WriteUInt(pixelformat, 5) - net.WriteUInt(len, 32) - net.WriteData(datastr,len) - addGlobalBW(self) + net.WriteStream(table.concat(datastr), nil, false) + self.UpdateRate = math.Round(math.max(len / maxBandwidth, 0.05),2) if ply then net.Send(ply) else net.Broadcast() end