Skip to content

Commit 4855366

Browse files
committed
digi screen max global bandwidth
1 parent 7b5ac40 commit 4855366

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

lua/entities/gmod_wire_digitalscreen/cl_init.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ function ENT:Think()
108108
if self.buffer[1] ~= nil then
109109
local maxtime = SysTime() + RealFrameTime() * 0.05 -- do more depending on client FPS. Higher fps = more work
110110

111-
if not self.co or coroutine.status(self.co) == "dead" then
112-
self.co = coroutine.create( function()
113-
self:ProcessBuffer()
114-
end )
115-
end
111+
while SysTime() < maxtime and self.buffer[1] do
112+
if not self.co or coroutine.status(self.co) == "dead" then
113+
self.co = coroutine.create( function()
114+
self:ProcessBuffer()
115+
end )
116+
end
116117

117-
while SysTime() < maxtime and coroutine.status(self.co) ~= "dead" do
118118
coroutine.resume(self.co)
119119
end
120120
end

lua/entities/gmod_wire_digitalscreen/init.lua

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AddCSLuaFile( "cl_init.lua" )
22
AddCSLuaFile( "shared.lua" )
33
include('shared.lua')
4+
DEFINE_BASECLASS( "base_wire_entity" )
45

56
ENT.WireDebugName = "DigitalScreen"
67

@@ -92,7 +93,33 @@ local function numberToString(t, number, bytes)
9293
t[#t+1] = table.concat(str)
9394
end
9495

96+
----------------------------------------------------
97+
-- Processing limiters and global bandwidth limiters
9598
local maxProcessingTime = engine.TickInterval() * 0.9
99+
local defaultMaxBandwidth = 10000
100+
local defaultMaxGlobalBandwidth = 20000
101+
local maxBandwidth = defaultMaxBandwidth
102+
local globalBandwidthLookup = {}
103+
local function calcGlobalBW()
104+
maxBandwidth = defaultMaxGlobalBandwidth
105+
local n = 0
106+
for digi in pairs(globalBandwidthLookup) do
107+
if not IsValid(digi) then globalBandwidthLookup[digi] = nil end -- this most likely won't trigger due to OnRemove, but just in case
108+
n = n + 1
109+
end
110+
maxBandwidth = math.Round(math.min(defaultMaxBandwidth,maxBandwidth / n),2)
111+
end
112+
local function addGlobalBW(e)
113+
globalBandwidthLookup[e] = true
114+
calcGlobalBW()
115+
end
116+
local function removeGlobalBW(e) globalBandwidthLookup[e] = nil end
117+
----------------------------------------------------
118+
119+
function ENT:OnRemove()
120+
BaseClass.OnRemove(self)
121+
removeGlobalBW(self)
122+
end
96123

97124
local function buildData(datastr, memory, pixelbit, range, bytesRemaining, sTime)
98125
if bytesRemaining < 15 then return 0 end
@@ -122,9 +149,13 @@ end
122149

123150
util.AddNetworkString("wire_digitalscreen")
124151

152+
125153
local pixelbits = {3, 1, 3, 4, 1} --The compressed pixel formats are in bytes
126154
function ENT:FlushCache(ply)
127-
if not next(self.ChangedCellRanges) then return end
155+
if not next(self.ChangedCellRanges) then
156+
removeGlobalBW(self)
157+
return
158+
end
128159

129160
local pixelformat = (math.floor(self.Memory[1048569]) or 0) + 1
130161
if pixelformat < 1 or pixelformat > #pixelbits then pixelformat = 1 end
@@ -161,14 +192,16 @@ function ENT:FlushCache(ply)
161192

162193
numberToString(datastr,0,3)
163194
datastr = util.Compress(table.concat(datastr))
195+
local len = #datastr
164196

165197
net.Start("wire_digitalscreen")
166198
net.WriteUInt(self:EntIndex(),16)
167199
net.WriteUInt(pixelformat, 5)
168-
net.WriteUInt(#datastr, 32)
169-
net.WriteData(datastr,#datastr)
200+
net.WriteUInt(len, 32)
201+
net.WriteData(datastr,len)
170202

171-
self.UpdateRate = math.Round(math.max(#datastr / 10000, 0.05),2)
203+
addGlobalBW(self)
204+
self.UpdateRate = math.Round(math.max(len / maxBandwidth, 0.05),2)
172205

173206
if ply then net.Send(ply) else net.Broadcast() end
174207
end

0 commit comments

Comments
 (0)