Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add netstream implementation for digiscreen #2977

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions lua/entities/gmod_wire_digitalscreen/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
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)

Expand All @@ -54,19 +54,21 @@
end

local pixelbits = {3, 1, 3, 4, 1}

net.Receive("wire_digitalscreen", function()

Check warning on line 58 in lua/entities/gmod_wire_digitalscreen/cl_init.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'double quoted strings' and 'single quoted strings'
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
Expand Down Expand Up @@ -227,7 +229,7 @@
return c, c, c
end

local floor = math.floor

Check warning on line 232 in lua/entities/gmod_wire_digitalscreen/cl_init.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: floor

function ENT:RedrawPixel(a)
if a >= self.ScreenWidth*self.ScreenHeight then return end
Expand Down
44 changes: 3 additions & 41 deletions lua/entities/gmod_wire_digitalscreen/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

Check warning on line 3 in lua/entities/gmod_wire_digitalscreen/init.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'single quoted strings' and 'double quoted strings'
DEFINE_BASECLASS( "base_wire_entity" )

ENT.WireDebugName = "DigitalScreen"
Expand Down Expand Up @@ -96,44 +96,10 @@
----------------------------------------------------
-- 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)
Expand Down Expand Up @@ -168,7 +134,6 @@
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

Expand Down Expand Up @@ -206,16 +171,13 @@
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
Expand Down Expand Up @@ -309,7 +271,7 @@

-- reset memory
self.Memory = mem
elseif Address == 1048575 then -- CLK

Check warning on line 274 in lua/entities/gmod_wire_digitalscreen/init.lua

View workflow job for this annotation

GitHub Actions / lint

"Empty block"

Empty elseif statement
-- not needed atm
end
end
Expand Down
Loading