diff --git a/inventoryManager.lua b/inventoryManager.lua index 24d9a10..9d9424f 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -383,6 +383,11 @@ local function main() -- Parallel tasks ----------------------------------------------- + -- Shared queue: capture task writes here, processor task reads. + -- This ensures modem_message events are never lost while the + -- processor yields for peripheral calls (pushItems, list, etc). + local networkQueue = {} + parallel.waitForAny( -- Task 1: Background inventory scanner resilient("Scanner", function() @@ -630,14 +635,36 @@ local function main() end end), - -- Task 13: Network order/command listener - resilient("Network-listener", function() + -- Task 13a: Network message capture (fast — never yields to peripheral calls) + -- This coroutine's filter is ALWAYS "modem_message", so it can never + -- miss events while other tasks yield for "task_complete" etc. + resilient("Network-capture", function() if not ctx.networkModem then while true do sleep(3600) end end while true do local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") if channel == cfg.ORDER_CHANNEL and type(message) == "table" then + table.insert(networkQueue, { replyChannel = replyChannel, message = message }) + os.queueEvent("network_queued") + end + end + end), + + -- Task 13b: Network message processor (drains queue — safe to yield) + resilient("Network-processor", function() + if not ctx.networkModem then + while true do sleep(3600) end + end + while true do + if #networkQueue == 0 then + os.pullEvent("network_queued") + end + while #networkQueue > 0 do + local entry = table.remove(networkQueue, 1) + local message = entry.message + local replyChannel = entry.replyChannel + if isCommandDuplicate(message.commandId) then log.debug("NET", "Duplicate command skipped: %s", tostring(message.commandId)) else @@ -888,7 +915,7 @@ local function main() log.error("NET", "Handler error: %s", tostring(handlerErr)) end end -- idempotency else - end + end -- queue loop end end) )