1
0
mirror of https://github.com/XeonSquared/OC-Copper.git synced 2024-11-14 22:08:05 +11:00

Make relib relatively backend-independent, increase the max timers to 0x400.

relib now relies on an additional parameter which should be
 require("culib"), but could instead be a Copper-like library.
This commit is contained in:
20kdc 2017-03-20 16:14:36 +00:00
parent 4a4cd485b1
commit 8e8b3f650d
2 changed files with 35 additions and 11 deletions

View File

@ -19,6 +19,7 @@ if package.loaded["occure"] then
error("Already installed") error("Already installed")
end end
local culib = require("culib")
local node = require("relib")(host, function (tgt, data) local node = require("relib")(host, function (tgt, data)
for _, v in ipairs(modems) do for _, v in ipairs(modems) do
if tgt then if tgt then
@ -29,15 +30,16 @@ local node = require("relib")(host, function (tgt, data)
end end
end, function (...) end, function (...)
computer.pushSignal("copper_packet", ...) computer.pushSignal("copper_packet", ...)
end, computer.uptime) end, computer.uptime, culib)
package.loaded["occure"] = node
for v, _ in component.list("modem") do for v, _ in component.list("modem") do
local m = component.proxy(v) local m = component.proxy(v)
table.insert(modems, m) table.insert(modems, m)
m.open(4957) m.open(4957)
end end
package.loaded["occure"] = node
event.listen("modem_message", function (et, adto, adfrom, port, dist, magic, data) event.listen("modem_message", function (et, adto, adfrom, port, dist, magic, data)
if et ~= "modem_message" then return end if et ~= "modem_message" then return end
if port == 4957 then if port == 4957 then

View File

@ -4,16 +4,34 @@
-- Copper Reliability Layer -- Copper Reliability Layer
-- Notably, this should be instantiated rather than the normal Copper instance. -- Notably, this should be instantiated rather than the normal Copper instance.
local culib = require("culib") -- The Copper node constructor (which could be replaced with something else,
-- should you decide to run the Reliability layer over another transport)
-- is passed as function (hostname, transmit, onReceive, time),
-- where hostname is the hostname given,
-- transmit is the (target/nil (for broadcast), data) function as given,
-- onReceive is the (nfrom. nto, ndata) function
-- internal to the Reliability Layer to translate packets for onRReceive
-- and acknowledge packets that need to be acknowledged,
-- and time is the function-that-returns-current-time-in-seconds
-- function as given.
-- The resulting Copper-like node should have the following fields:
-- node.hostname: Editable hostname.
-- node.refresh(): Do various timed cleanup tasks.
-- node.input(...): relib.input is set to this for convenience.
-- Ought to be function (opaqueFrom, data).
-- node.output(nFrom, nTo, data): The normal Copper send function.
-- onRReceive is now: (from, to, port, data, unreliablePacket) -- onRReceive is now: (from, to, port, data, unreliablePacket)
-- where to can be anything for unreliable packets, but otherwise is the current hostname. -- where to can be anything for unreliable packets,
return function (hostname, transmit, onRReceive, time) -- but otherwise is the current hostname.
return function (hostname, transmit, onRReceive, time, culib)
-- node.hostname should be used for hostname generally. -- node.hostname should be used for hostname generally.
local node local node
-- The maximum amount of timers (used to cap memory usage) -- The maximum amount of timers (used to cap memory usage).
local tuningMaxTimers = 0x200 -- Assuming 16 bytes per key, this should be 64k.
local tuningMaxTimers = 0x400
local tuningClearAntiduplicate = 60 local tuningClearAntiduplicate = 60
local tuningAttempts = 12 local tuningAttempts = 12
local tuningAttemptTime = 2.5 local tuningAttemptTime = 2.5
@ -78,15 +96,19 @@ return function (hostname, transmit, onRReceive, time)
if nto ~= node.hostname then return end if nto ~= node.hostname then return end
node.output(nto, nfrom, data:sub(1, 6) .. "\x02") node.output(nto, nfrom, data:sub(1, 6) .. "\x02")
end end
-- Ignore ACKs for our packets that aren't going our way.
-- (Just a fringe case of packet ID reuse)
if nto ~= node.hostname then
return
end
if (tp == 0x02) and needsAck[nfrom .. globalId] then if (tp == 0x02) and needsAck[nfrom .. globalId] then
needsAck[nfrom .. globalId][1](nfrom) needsAck[nfrom .. globalId][1](nfrom)
killTimer(needsAck[nfrom .. globalId][2]) killTimer(needsAck[nfrom .. globalId][2])
needsAck[nfrom .. globalId] = nil needsAck[nfrom .. globalId] = nil
end end
if nto ~= node.hostname then
return
end
end end
-- Instantiate the node or lookalike.
node = culib(hostname, transmit, onReceive, time) node = culib(hostname, transmit, onReceive, time)
local relib = {} local relib = {}