On Fri, Jun 21, 2013 at 8:29 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
I do something like this in winapi, but it's finicky and I'm wondering under what conditions it can be guaranteed to be stable.
It's a daughter state that's meant to run in a separate OS thread.
Here is an example - I have a blocking command connection, and an async socket for notifications.
local winapi = require 'winapi'
local t,c2,err
local addr = arg[2] or 'localhost'
local c = socket.connect(addr,3333)
c:send ('yes\n')
c:receive()
local m = winapi.mutex()
t = winapi.thread(function()
c2,err = socket.connect(addr,3334)
if err then
c:close()
error('cannot connect to secondary socket '..err)
end
while true do
local res = c2:receive()
res = res:gsub('001円','\n')
m:lock()
io.write(res)
m:release()
end
end,'ok')
winapi.sleep(50)
Then there's a function which receives stuff from the console and passes it through the server:
function eval(line)
c:send(line..'\n')
m:lock()
local res = c:receive()
m:release()
return (res or '?'):gsub('001円','\n')
end
(The magic '001円' is just a convention for passing back strings containing newlines)
This version is now much more stable, and the key bit is that the listening socket must be created in the _new thread_....
I'd like any comments about this simple-minded design, since most of the time I just want to just do two things at once with LuaSocket ;)
steve d.