1
0
mirror of https://github.com/XeonSquared/OC-Copper.git synced 2024-11-27 03:38:05 +11:00

Fixes to RELib.

It should work now.
This commit is contained in:
gamemanj 2017-03-18 18:30:14 +00:00
parent b5013044f6
commit c5f174e6ed

View File

@ -14,7 +14,7 @@ return function (hostname, transmit, onRReceive, time)
-- The maximum amount of timers (used to cap memory usage) -- The maximum amount of timers (used to cap memory usage)
local tuningMaxTimers = 0x200 local tuningMaxTimers = 0x200
local tuningClearAntiduplicate = 120 local tuningClearAntiduplicate = 60
local tuningAttempts = 8 local tuningAttempts = 8
local tuningAttemptTime = 4 local tuningAttemptTime = 4
@ -31,7 +31,7 @@ return function (hostname, transmit, onRReceive, time)
local function addTimer(trig, expi) local function addTimer(trig, expi)
if #timers < tuningMaxTimers then if #timers < tuningMaxTimers then
local t = {trig, expi} local t = {trig, time() + expi}
table.insert(timers, t) table.insert(timers, t)
return t return t
end end
@ -56,23 +56,25 @@ return function (hostname, transmit, onRReceive, time)
end end
local onReceive = function (nfrom, nto, data) local onReceive = function (nfrom, nto, data)
if data:len() < 6 then return end if data:len() < 7 then return end
local port = data:byte(2) + (data:byte(1) * 256) local port = data:byte(2) + (data:byte(1) * 256)
local tp = data:byte(7) local tp = data:byte(7)
local globalId = data:sub(1, 5) local globalId = data:sub(1, 5)
if tp == 0x00 then if tp == 0x00 then
onRReceive(nfrom, nto, port, data, true) onRReceive(nfrom, nto, port, data:sub(8), true)
return return
end end
if nto ~= node.hostname then return end if nto ~= node.hostname then return end
if (tp == 0x01) and weAcked[nto .. globalId] then if (tp == 0x01) then
-- Only send one acknowledgement per packet, -- Only send one acknowledgement per packet,
-- so timers aren't generated by external packets -- but only receive the packet once.
-- Not a perfect system but, eh. -- (This is why timers are counted - to prevent the weAcked pool from getting too big.)
if not weAcked[nto .. globalId] then
onRReceive(nfrom, nto, port, data:sub(8), false)
end
weAcked[nto .. globalId] = addTimer(function () weAcked[nto .. globalId] = addTimer(function ()
weAcked[nto .. globalId] = nil weAcked[nto .. globalId] = nil
end, tuningClearAntiduplicate) end, tuningClearAntiduplicate)
onRReceive(nfrom, nto, port, data, false)
node.output(nto, nfrom, data:sub(1, 6) .. "\x02") node.output(nto, nfrom, data:sub(1, 6) .. "\x02")
end end
if (tp == 0x02) and needsAck[nfrom .. globalId] then if (tp == 0x02) and needsAck[nfrom .. globalId] then
@ -87,12 +89,19 @@ return function (hostname, transmit, onRReceive, time)
node = culib(hostname, transmit, onReceive, time) node = culib(hostname, transmit, onReceive, time)
local relib = {} local relib = {}
relib.setHostname = function (h)
node.hostname = h
end
relib.getHostname = function ()
return node.hostname
end
relib.refresh = function () relib.refresh = function ()
node.refresh() node.refresh()
local i = 1 local i = 1
local t = time() local t = time()
while i <= #timers do while i <= #timers do
if timers[i].expiry < t then if timers[i][2] <= t then
timers[i][1]()
table.remove(timers, i) table.remove(timers, i)
else else
i = i + 1 i = i + 1
@ -100,7 +109,10 @@ return function (hostname, transmit, onRReceive, time)
end end
end end
relib.input = node.input relib.input = node.input
-- can be reduced to output(nto, port, data) safely
relib.output = function (nto, port, data, unreliable, onSucceed, onFailure) relib.output = function (nto, port, data, unreliable, onSucceed, onFailure)
onSucceed = onSucceed or (function () end)
onFailure = onFailure or (function () end)
local gid = genGlobalId(port) local gid = genGlobalId(port)
if unreliable then if unreliable then
node.output(node.hostname, nto, gid .. "\x00\x00" .. data) node.output(node.hostname, nto, gid .. "\x00\x00" .. data)