|
1 | 1 | AddCSLuaFile( "cl_init.lua" )
|
2 | 2 | AddCSLuaFile( "shared.lua" )
|
3 | 3 | include('shared.lua')
|
| 4 | +DEFINE_BASECLASS( "base_wire_entity" ) |
4 | 5 |
|
5 | 6 | ENT.WireDebugName = "DigitalScreen"
|
6 | 7 |
|
@@ -92,7 +93,33 @@ local function numberToString(t, number, bytes)
|
92 | 93 | t[#t+1] = table.concat(str)
|
93 | 94 | end
|
94 | 95 |
|
| 96 | +---------------------------------------------------- |
| 97 | +-- Processing limiters and global bandwidth limiters |
95 | 98 | 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 |
96 | 123 |
|
97 | 124 | local function buildData(datastr, memory, pixelbit, range, bytesRemaining, sTime)
|
98 | 125 | if bytesRemaining < 15 then return 0 end
|
|
122 | 149 |
|
123 | 150 | util.AddNetworkString("wire_digitalscreen")
|
124 | 151 |
|
| 152 | + |
125 | 153 | local pixelbits = {3, 1, 3, 4, 1} --The compressed pixel formats are in bytes
|
126 | 154 | 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 |
128 | 159 |
|
129 | 160 | local pixelformat = (math.floor(self.Memory[1048569]) or 0) + 1
|
130 | 161 | if pixelformat < 1 or pixelformat > #pixelbits then pixelformat = 1 end
|
@@ -161,14 +192,16 @@ function ENT:FlushCache(ply)
|
161 | 192 |
|
162 | 193 | numberToString(datastr,0,3)
|
163 | 194 | datastr = util.Compress(table.concat(datastr))
|
| 195 | + local len = #datastr |
164 | 196 |
|
165 | 197 | net.Start("wire_digitalscreen")
|
166 | 198 | net.WriteUInt(self:EntIndex(),16)
|
167 | 199 | net.WriteUInt(pixelformat, 5)
|
168 |
| - net.WriteUInt(#datastr, 32) |
169 |
| - net.WriteData(datastr,#datastr) |
| 200 | + net.WriteUInt(len, 32) |
| 201 | + net.WriteData(datastr,len) |
170 | 202 |
|
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) |
172 | 205 |
|
173 | 206 | if ply then net.Send(ply) else net.Broadcast() end
|
174 | 207 | end
|
|
0 commit comments