Fix pipes in Linux with socat

This commit is contained in:
Empathic Qubit 2021-05-04 20:30:52 -04:00
parent a5b089520b
commit 123d7508d1
4 changed files with 45 additions and 35 deletions

View file

@ -7,7 +7,7 @@ See [YouTube](https://www.youtube.com/watch?v=Q69_wmEkp-k) for an example run.
## Requirements ## Requirements
* lsnes with **Lua 5.2** (do not try to build with 5.3, it does not work!) * 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) * A Donkey Kong Country 2 1.1 US ROM (matching hash b79c2bb86f6fc76e1fc61c62fc16d51c664c381e58bc2933be643bbc4d8b610c)
### Windows ### Windows

View file

@ -22,7 +22,7 @@ local speciesId = -1
local generationIndex = nil local generationIndex = nil
local inputPipeName = os.getenv("RUNNER_INPUT_PIPE") 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) print('Opening input pipe '..inputPipeName)
local inputPipe = util.openReadPipe(inputPipeName) local inputPipe = util.openReadPipe(inputPipeName)
@ -31,16 +31,13 @@ if inputPipe == nil then
end end
print('Opened input pipe '..inputPipeName) print('Opened input pipe '..inputPipeName)
print('Opening output file '..outputFilePath) print('Opening output file '..outputPipeName)
local outputFile = nil local outputPipe = util.openReadPipeWriter(outputPipeName)
while outputFile == nil do print('Opened output file '..outputPipeName)
outputFile = io.open(outputFilePath, 'w')
end
print('Opened output file '..outputFilePath)
local function writeResponse(object) local function writeResponse(object)
outputFile:write(serpent.dump(object).."\n") outputPipe:write(serpent.dump(object).."\n")
outputFile:flush() outputPipe:flush()
end end
local runner = Runner(Promise) local runner = Runner(Promise)

View file

@ -7,21 +7,6 @@ local Promise = nil
local util = nil local util = nil
local config = dofile(base.."/config.lua") local config = dofile(base.."/config.lua")
local serpent = dofile(base.."/serpent.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_".. local pipePrefix = "donk_runner_"..
string.hex(math.floor(random.integer(0, 0xffffffff))).. string.hex(math.floor(random.integer(0, 0xffffffff)))..
@ -77,23 +62,18 @@ local function launchChildren(_M, count)
input = nil, input = nil,
} }
local outputFileName = outputPrefix..i local outputPipeName = outputPrefix..i
local inputPipeName = inputPrefix..i local inputPipeName = inputPrefix..i
local inputFileName = inputPrefix..i
if util.isWin then
outputFileName = '\\\\.\\pipe\\'..outputFileName
inputFileName = '\\\\.\\pipe\\'..inputPipeName
end
local settingsDir = nil local settingsDir = nil
if util.isWin then if util.isWin then
settingsDir = tempDir.."/donk_runner_settings_"..i settingsDir = util.getTempDir().."/donk_runner_settings_"..i
util.mkdir(settingsDir) util.mkdir(settingsDir)
end end
local envs = { local envs = {
RUNNER_INPUT_PIPE = inputPipeName, RUNNER_INPUT_PIPE = inputPipeName,
RUNNER_OUTPUT_FILE = outputFileName, RUNNER_OUTPUT_PIPE = outputPipeName,
APPDATA = settingsDir, APPDATA = settingsDir,
} }
@ -104,7 +84,7 @@ local function launchChildren(_M, count)
local promise = util.promiseWrap(function() local promise = util.promiseWrap(function()
newOne.output:read("*l") newOne.output:read("*l")
while newOne.input == nil do while newOne.input == nil do
newOne.input = io.open(inputFileName, 'w') newOne.input = util.openReadPipeWriter(inputPipeName)
end end
end) end)
table.insert(promises, promise) table.insert(promises, promise)

View file

@ -14,6 +14,26 @@ function _M.promiseWrap(next)
return promise:next(next) return promise:next(next)
end 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 --- Echo a command, run it, and return the file handle
--- @param cmd string The command to execute --- @param cmd string The command to execute
--- @param workdir string The working directory --- @param workdir string The working directory
@ -60,10 +80,23 @@ function _M.openReadPipe(name)
print(cmd) print(cmd)
return io.popen(cmd, 'r') return io.popen(cmd, 'r')
else else
error('Not implemented') return io.popen("socat 'UNIX-LISTEN:".._M.getTempDir().."/"..name.."' -", 'r')
end end
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 --- Download a url
--- @param url string URI of resource to download --- @param url string URI of resource to download
--- @param dest string File to save resource to --- @param dest string File to save resource to