diff --git a/lua/wire/wireshared.lua b/lua/wire/wireshared.lua index aa2d078bff..7bd0a69223 100644 --- a/lua/wire/wireshared.lua +++ b/lua/wire/wireshared.lua @@ -314,6 +314,102 @@ elseif SERVER then end end +do +local max_items_per_flush = 1024 +local queue_limit = 65536 +local ack_timeout = 30 + +local function PlyQueue() + return { + __flushing = false, + __ack_timeout = 0, + add = function(self, item) + local n=#self+1 + if n>queue_limit then return end + self[n]=item + end + } +end + +local net_Send = SERVER and net.Send or net.SendToServer +WireLib.NetQueue = { + __index = { + add = SERVER and function(self, item, ply) + if ply==nil then + for _, ply in player.Iterator() do self.plyqueues[ply]:add(item) end + else + self.plyqueues[ply]:add(item) + end + self:notifyFlush() + end or function(self, item) + self.plyqueues[NULL]:add(item) + self:notifyFlush() + end, + cleanup = function(self, ply) + self.plyqueues[ply] = nil + end, + notifyFlush = function(self) + if self.flushing then return end + self.flushing = true + timer.Simple(0, function() self:flush() end) + end, + flush = function(self) + for ply, queue in pairs(self.plyqueues) do self:flushQueue(ply, queue) end + self.flushing = false + end, + flushQueue = function(self, ply, queue) + if queue[1]==nil then return end + local t = CurTime() + if queue.__flushing and t 0 then - local entry - - local amount = net.ReadUInt(8) - if net.ReadBit() ~= 0 then - -- outputs - entry = ents_with_outputs[eid] - if not entry then - entry = {} - ents_with_outputs[eid] = entry - end - else - -- inputs - entry = ents_with_inputs[eid] - if not entry then - entry = {} - ents_with_inputs[eid] = entry - end + end + elseif cmd == CMD_PORT then + if net.ReadUInt(1)==PORT_TYPE_INPUT then + local entry = {} + for i=1, net.ReadUInt(8) do + entry[i] = {net.ReadString(), net.ReadString(), net.ReadString(), net.ReadBool()} + -- print("Port",eid,entry[i][1],entry[i][2],entry[i][3],entry[i][4]) end - - local endindex = cmd+amount-1 - for i = cmd,endindex do + ents_with_inputs[eid]=entry + else + local entry = {} + for i=1, net.ReadUInt(8) do entry[i] = {net.ReadString(), net.ReadString(), net.ReadString()} + -- print("Port",eid,entry[i][1],entry[i][2],entry[i][3]) end + ents_with_outputs[eid]=entry end - end - for i=1, #connections do - local eid, num, state = unpack(connections[i]) - local entry = ents_with_inputs[eid] - if not entry then - entry = {} - ents_with_inputs[eid] = entry - elseif entry[num] then - entry[num][4] = state + elseif cmd == CMD_LINK then + for i=1, net.ReadUInt(8) do + local num, state = net.ReadUInt(8), net.ReadBool() + -- print("Link",eid,num, state) + local entry = ents_with_inputs[eid] + if entry and entry[num] then + entry[num][4] = state + end end end - end) + end function WireLib.GetPorts(ent) local eid = ent:EntIndex()