From 123d7508d1d3534422d54935f339583c7ae10be1 Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Tue, 4 May 2021 20:30:52 -0400 Subject: [PATCH] Fix pipes in Linux with socat --- README.md | 2 +- runner-process.lua | 15 ++++++--------- runner-wrapper.lua | 28 ++++------------------------ util.lua | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 2ef766a..a00313e 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ See [YouTube](https://www.youtube.com/watch?v=Q69_wmEkp-k) for an example run. ## Requirements * lsnes with **Lua 5.2** (do not try to build with 5.3, it does not work!) -* inotifywait for Linux, or a fairly recent version of Windows that has PowerShell +* socat for Linux, or a fairly recent version of Windows that has PowerShell * A Donkey Kong Country 2 1.1 US ROM (matching hash b79c2bb86f6fc76e1fc61c62fc16d51c664c381e58bc2933be643bbc4d8b610c) ### Windows diff --git a/runner-process.lua b/runner-process.lua index c85fe70..4805595 100644 --- a/runner-process.lua +++ b/runner-process.lua @@ -22,7 +22,7 @@ local speciesId = -1 local generationIndex = nil local inputPipeName = os.getenv("RUNNER_INPUT_PIPE") -local outputFilePath = os.getenv("RUNNER_OUTPUT_FILE") +local outputPipeName = os.getenv("RUNNER_OUTPUT_PIPE") print('Opening input pipe '..inputPipeName) local inputPipe = util.openReadPipe(inputPipeName) @@ -31,16 +31,13 @@ if inputPipe == nil then end print('Opened input pipe '..inputPipeName) -print('Opening output file '..outputFilePath) -local outputFile = nil -while outputFile == nil do - outputFile = io.open(outputFilePath, 'w') -end -print('Opened output file '..outputFilePath) +print('Opening output file '..outputPipeName) +local outputPipe = util.openReadPipeWriter(outputPipeName) +print('Opened output file '..outputPipeName) local function writeResponse(object) - outputFile:write(serpent.dump(object).."\n") - outputFile:flush() + outputPipe:write(serpent.dump(object).."\n") + outputPipe:flush() end local runner = Runner(Promise) diff --git a/runner-wrapper.lua b/runner-wrapper.lua index e18e2ae..dc9fbfb 100644 --- a/runner-wrapper.lua +++ b/runner-wrapper.lua @@ -7,21 +7,6 @@ local Promise = nil local util = nil local config = dofile(base.."/config.lua") local serpent = dofile(base.."/serpent.lua") -local temps = { - os.getenv("TMPDIR"), - os.getenv("TEMP"), - os.getenv("TEMPDIR"), - os.getenv("TMP"), -} - -local tempDir = "/tmp" -for i=1,#temps,1 do - local temp = temps[i] - if temp ~= nil and temp ~= "" then - tempDir = temps[i] - break - end -end local pipePrefix = "donk_runner_".. string.hex(math.floor(random.integer(0, 0xffffffff))).. @@ -77,23 +62,18 @@ local function launchChildren(_M, count) input = nil, } - local outputFileName = outputPrefix..i + local outputPipeName = outputPrefix..i local inputPipeName = inputPrefix..i - local inputFileName = inputPrefix..i - if util.isWin then - outputFileName = '\\\\.\\pipe\\'..outputFileName - inputFileName = '\\\\.\\pipe\\'..inputPipeName - end local settingsDir = nil if util.isWin then - settingsDir = tempDir.."/donk_runner_settings_"..i + settingsDir = util.getTempDir().."/donk_runner_settings_"..i util.mkdir(settingsDir) end local envs = { RUNNER_INPUT_PIPE = inputPipeName, - RUNNER_OUTPUT_FILE = outputFileName, + RUNNER_OUTPUT_PIPE = outputPipeName, APPDATA = settingsDir, } @@ -104,7 +84,7 @@ local function launchChildren(_M, count) local promise = util.promiseWrap(function() newOne.output:read("*l") while newOne.input == nil do - newOne.input = io.open(inputFileName, 'w') + newOne.input = util.openReadPipeWriter(inputPipeName) end end) table.insert(promises, promise) diff --git a/util.lua b/util.lua index 89cd41b..19767b2 100644 --- a/util.lua +++ b/util.lua @@ -14,6 +14,26 @@ function _M.promiseWrap(next) return promise:next(next) end +function _M.getTempDir() + local temps = { + os.getenv("TMPDIR"), + os.getenv("TEMP"), + os.getenv("TEMPDIR"), + os.getenv("TMP"), + } + + local tempDir = "/tmp" + for i=1,#temps,1 do + local temp = temps[i] + if temp ~= nil and temp ~= "" then + tempDir = temps[i] + break + end + end + + return tempDir +end + --- Echo a command, run it, and return the file handle --- @param cmd string The command to execute --- @param workdir string The working directory @@ -60,10 +80,23 @@ function _M.openReadPipe(name) print(cmd) return io.popen(cmd, 'r') else - error('Not implemented') + return io.popen("socat 'UNIX-LISTEN:".._M.getTempDir().."/"..name.."' -", 'r') end end +function _M.openReadPipeWriter(name) + local writer = nil + while writer == nil do + if _M.isWin then + writer = io.open('\\\\.\\pipe\\'..name, 'w') + else + writer = io.popen("socat 'UNIX-CONNECT:".._M.getTempDir().."/"..name.."' -", 'w') + end + end + + return writer +end + --- Download a url --- @param url string URI of resource to download --- @param dest string File to save resource to